/Users/deen/code/yugabyte-db/src/yb/util/debug/trace_event_synthetic_delay.h
Line | Count | Source |
1 | | // Copyright 2014 The Chromium Authors. All rights reserved. |
2 | | // Use of this source code is governed by a BSD-style license that can be |
3 | | // found in the LICENSE file. |
4 | | // |
5 | | // The following only applies to changes made to this file as part of YugaByte development. |
6 | | // |
7 | | // Portions Copyright (c) YugaByte, Inc. |
8 | | // |
9 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
10 | | // in compliance with the License. You may obtain a copy of the License at |
11 | | // |
12 | | // http://www.apache.org/licenses/LICENSE-2.0 |
13 | | // |
14 | | // Unless required by applicable law or agreed to in writing, software distributed under the License |
15 | | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
16 | | // or implied. See the License for the specific language governing permissions and limitations |
17 | | // under the License. |
18 | | // |
19 | | |
20 | | // The synthetic delay framework makes it possible to dynamically inject |
21 | | // arbitrary delays into into different parts of the codebase. This can be used, |
22 | | // for instance, for testing various task scheduling algorithms. |
23 | | // |
24 | | // The delays are specified in terms of a target duration for a given block of |
25 | | // code. If the code executes faster than the duration, the thread is made to |
26 | | // sleep until the deadline is met. |
27 | | // |
28 | | // Code can be instrumented for delays with two sets of macros. First, for |
29 | | // delays that should apply within a scope, use the following macro: |
30 | | // |
31 | | // TRACE_EVENT_SYNTHETIC_DELAY("cc.LayerTreeHost.DrawAndSwap"); |
32 | | // |
33 | | // For delaying operations that span multiple scopes, use: |
34 | | // |
35 | | // TRACE_EVENT_SYNTHETIC_DELAY_BEGIN("cc.Scheduler.BeginMainFrame"); |
36 | | // ... |
37 | | // TRACE_EVENT_SYNTHETIC_DELAY_END("cc.Scheduler.BeginMainFrame"); |
38 | | // |
39 | | // Here BEGIN establishes the start time for the delay and END executes the |
40 | | // delay based on the remaining time. If BEGIN is called multiple times in a |
41 | | // row, END should be called a corresponding number of times. Only the last |
42 | | // call to END will have an effect. |
43 | | // |
44 | | // Note that a single delay may begin on one thread and end on another. This |
45 | | // implies that a single delay cannot not be applied in several threads at once. |
46 | | |
47 | | #ifndef YB_UTIL_DEBUG_TRACE_EVENT_SYNTHETIC_DELAY_H_ |
48 | | #define YB_UTIL_DEBUG_TRACE_EVENT_SYNTHETIC_DELAY_H_ |
49 | | |
50 | | #include "yb/gutil/atomicops.h" |
51 | | #include "yb/util/debug/trace_event.h" |
52 | | #include "yb/util/monotime.h" |
53 | | |
54 | | // Apply a named delay in the current scope. |
55 | | #define TRACE_EVENT_SYNTHETIC_DELAY(name) \ |
56 | 9 | static AtomicWord INTERNAL_TRACE_EVENT_UID(impl_ptr) = 0; \ |
57 | 9 | trace_event_internal::ScopedSyntheticDelay INTERNAL_TRACE_EVENT_UID(delay)( \ |
58 | 9 | name, &INTERNAL_TRACE_EVENT_UID(impl_ptr)); |
59 | | |
60 | | // Begin a named delay, establishing its timing start point. May be called |
61 | | // multiple times as long as the calls to TRACE_EVENT_SYNTHETIC_DELAY_END are |
62 | | // balanced. Only the first call records the timing start point. |
63 | | #define TRACE_EVENT_SYNTHETIC_DELAY_BEGIN(name) \ |
64 | 6 | do { \ |
65 | 6 | static AtomicWord impl_ptr = 0; \ |
66 | 6 | trace_event_internal::GetOrCreateDelay(name, &impl_ptr)->Begin(); \ |
67 | 6 | } while (false) |
68 | | |
69 | | // End a named delay. The delay is applied only if this call matches the |
70 | | // first corresponding call to TRACE_EVENT_SYNTHETIC_DELAY_BEGIN with the |
71 | | // same delay. |
72 | | #define TRACE_EVENT_SYNTHETIC_DELAY_END(name) \ |
73 | 8 | do { \ |
74 | 8 | static AtomicWord impl_ptr = 0; \ |
75 | 8 | trace_event_internal::GetOrCreateDelay(name, &impl_ptr)->End(); \ |
76 | 8 | } while (false) |
77 | | |
78 | | namespace yb { |
79 | | namespace debug { |
80 | | |
81 | | // Time source for computing delay durations. Used for testing. |
82 | | class TRACE_EVENT_API_CLASS_EXPORT TraceEventSyntheticDelayClock { |
83 | | public: |
84 | | TraceEventSyntheticDelayClock(); |
85 | | virtual ~TraceEventSyntheticDelayClock(); |
86 | | virtual MonoTime Now() = 0; |
87 | | |
88 | | private: |
89 | | DISALLOW_COPY_AND_ASSIGN(TraceEventSyntheticDelayClock); |
90 | | }; |
91 | | |
92 | | // Single delay point instance. |
93 | | class TRACE_EVENT_API_CLASS_EXPORT TraceEventSyntheticDelay { |
94 | | public: |
95 | | enum Mode { |
96 | | STATIC, // Apply the configured delay every time. |
97 | | ONE_SHOT, // Apply the configured delay just once. |
98 | | ALTERNATING // Apply the configured delay every other time. |
99 | | }; |
100 | | |
101 | | // Returns an existing named delay instance or creates a new one with |name|. |
102 | | static TraceEventSyntheticDelay* Lookup(const std::string& name); |
103 | | |
104 | | void SetTargetDuration(const MonoDelta& target_duration); |
105 | | void SetMode(Mode mode); |
106 | | void SetClock(TraceEventSyntheticDelayClock* clock); |
107 | | |
108 | | // Begin the delay, establishing its timing start point. May be called |
109 | | // multiple times as long as the calls to End() are balanced. Only the first |
110 | | // call records the timing start point. |
111 | | void Begin(); |
112 | | |
113 | | // End the delay. The delay is applied only if this call matches the first |
114 | | // corresponding call to Begin() with the same delay. |
115 | | void End(); |
116 | | |
117 | | // Begin a parallel instance of the delay. Several parallel instances may be |
118 | | // active simultaneously and will complete independently. The computed end |
119 | | // time for the delay is stored in |out_end_time|, which should later be |
120 | | // passed to EndParallel(). |
121 | | void BeginParallel(MonoTime* out_end_time); |
122 | | |
123 | | // End a previously started parallel delay. |end_time| is the delay end point |
124 | | // computed by BeginParallel(). |
125 | | void EndParallel(const MonoTime& end_time); |
126 | | |
127 | | private: |
128 | | TraceEventSyntheticDelay(); |
129 | | ~TraceEventSyntheticDelay(); |
130 | | friend class TraceEventSyntheticDelayRegistry; |
131 | | |
132 | | void Initialize(const std::string& name, |
133 | | TraceEventSyntheticDelayClock* clock); |
134 | | MonoTime CalculateEndTimeLocked(const MonoTime& start_time); |
135 | | void ApplyDelay(const MonoTime& end_time); |
136 | | |
137 | | Mutex lock_; |
138 | | Mode mode_; |
139 | | std::string name_; |
140 | | int begin_count_; |
141 | | int trigger_count_; |
142 | | MonoTime end_time_; |
143 | | MonoDelta target_duration_; |
144 | | TraceEventSyntheticDelayClock* clock_; |
145 | | |
146 | | DISALLOW_COPY_AND_ASSIGN(TraceEventSyntheticDelay); |
147 | | }; |
148 | | |
149 | | // Set the target durations of all registered synthetic delay points to zero. |
150 | | TRACE_EVENT_API_CLASS_EXPORT void ResetTraceEventSyntheticDelays(); |
151 | | |
152 | | } // namespace debug |
153 | | } // namespace yb |
154 | | |
155 | | namespace trace_event_internal { |
156 | | |
157 | | // Helper class for scoped delays. Do not use directly. |
158 | | class TRACE_EVENT_API_CLASS_EXPORT ScopedSyntheticDelay { |
159 | | public: |
160 | | explicit ScopedSyntheticDelay(const char* name, |
161 | | AtomicWord* impl_ptr); |
162 | | ~ScopedSyntheticDelay(); |
163 | | |
164 | | private: |
165 | | yb::debug::TraceEventSyntheticDelay* delay_impl_; |
166 | | yb::MonoTime end_time_; |
167 | | |
168 | | DISALLOW_COPY_AND_ASSIGN(ScopedSyntheticDelay); |
169 | | }; |
170 | | |
171 | | // Helper for registering delays. Do not use directly. |
172 | | TRACE_EVENT_API_CLASS_EXPORT yb::debug::TraceEventSyntheticDelay* |
173 | | GetOrCreateDelay(const char* name, AtomicWord* impl_ptr); |
174 | | |
175 | | } // namespace trace_event_internal |
176 | | |
177 | | #endif /* YB_UTIL_DEBUG_TRACE_EVENT_SYNTHETIC_DELAY_H_ */ |