| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // The synthetic delay framework makes it possible to dynamically inject | 5 // The synthetic delay framework makes it possible to dynamically inject |
| 6 // arbitrary delays into into different parts of the codebase. This can be used, | 6 // arbitrary delays into into different parts of the codebase. This can be used, |
| 7 // for instance, for testing various task scheduling algorithms. | 7 // for instance, for testing various task scheduling algorithms. |
| 8 // | 8 // |
| 9 // The delays are specified in terms of a target duration for a given block of | 9 // The delays are specified in terms of a target duration for a given block of |
| 10 // code. If the code executes faster than the duration, the thread is made to | 10 // code. If the code executes faster than the duration, the thread is made to |
| 11 // sleep until the deadline is met. | 11 // sleep until the deadline is met. |
| 12 // | 12 // |
| 13 // Code can be instrumented for delays with two sets of macros. First, for | 13 // Code can be instrumented for delays with two sets of macros. First, for |
| 14 // delays that should apply within a scope, use the following macro: | 14 // delays that should apply within a scope, use the following macro: |
| 15 // | 15 // |
| 16 // TRACE_EVENT_SYNTHETIC_DELAY("cc.LayerTreeHost.DrawAndSwap"); | 16 // TRACE_EVENT_SYNTHETIC_DELAY("cc.LayerTreeHost.DrawAndSwap"); |
| 17 // | 17 // |
| 18 // For delaying operations that span multiple scopes, use: | 18 // For delaying operations that span multiple scopes, use: |
| 19 // | 19 // |
| 20 // TRACE_EVENT_SYNTHETIC_DELAY_ACTIVATE("cc.Scheduler.BeginMainFrame"); | 20 // TRACE_EVENT_SYNTHETIC_DELAY_BEGIN("cc.Scheduler.BeginMainFrame"); |
| 21 // ... | 21 // ... |
| 22 // TRACE_EVENT_SYNTHETIC_DELAY_APPLY("cc.Scheduler.BeginMainFrame"); | 22 // TRACE_EVENT_SYNTHETIC_DELAY_END("cc.Scheduler.BeginMainFrame"); |
| 23 // | 23 // |
| 24 // Here ACTIVATE establishes the start time for the delay and APPLY executes the | 24 // Here BEGIN establishes the start time for the delay and END executes the |
| 25 // delay based on the remaining time. ACTIVATE may be called one or multiple | 25 // delay based on the remaining time. If BEGIN is called multiple times in a |
| 26 // times before APPLY. Only the first call will have an effect. If ACTIVATE | 26 // row, END should be called a corresponding number of times. Only the last |
| 27 // hasn't been called since the last call to APPLY, APPLY will be a no-op. | 27 // call to END will have an effect. |
| 28 // | 28 // |
| 29 // Note that while the same delay can be applied in several threads | 29 // Note that a single delay may begin on one thread and end on another. This |
| 30 // simultaneously, a single delay operation cannot begin on one thread and end | 30 // implies that a single delay cannot not be applied in several threads at once. |
| 31 // on another. | |
| 32 | 31 |
| 33 #ifndef BASE_DEBUG_TRACE_EVENT_SYNTHETIC_DELAY_H_ | 32 #ifndef BASE_DEBUG_TRACE_EVENT_SYNTHETIC_DELAY_H_ |
| 34 #define BASE_DEBUG_TRACE_EVENT_SYNTHETIC_DELAY_H_ | 33 #define BASE_DEBUG_TRACE_EVENT_SYNTHETIC_DELAY_H_ |
| 35 | 34 |
| 36 #include "base/atomicops.h" | 35 #include "base/atomicops.h" |
| 37 #include "base/debug/trace_event.h" | 36 #include "base/debug/trace_event.h" |
| 38 #include "base/synchronization/lock.h" | 37 #include "base/synchronization/lock.h" |
| 39 #include "base/time/time.h" | 38 #include "base/time/time.h" |
| 40 | 39 |
| 41 // Apply a named delay in the current scope. | 40 // Apply a named delay in the current scope. |
| 42 #define TRACE_EVENT_SYNTHETIC_DELAY(name) \ | 41 #define TRACE_EVENT_SYNTHETIC_DELAY(name) \ |
| 43 static base::subtle::AtomicWord INTERNAL_TRACE_EVENT_UID(impl_ptr) = 0; \ | 42 static base::subtle::AtomicWord INTERNAL_TRACE_EVENT_UID(impl_ptr) = 0; \ |
| 44 trace_event_internal::ScopedSyntheticDelay INTERNAL_TRACE_EVENT_UID(delay)( \ | 43 trace_event_internal::ScopedSyntheticDelay INTERNAL_TRACE_EVENT_UID(delay)( \ |
| 45 name, &INTERNAL_TRACE_EVENT_UID(impl_ptr)); | 44 name, &INTERNAL_TRACE_EVENT_UID(impl_ptr)); |
| 46 | 45 |
| 47 // Activate a named delay, establishing its timing start point. May be called | 46 // Begin a named delay, establishing its timing start point. May be called |
| 48 // multiple times, but only the first call will have an effect. | 47 // multiple times as long as the calls to TRACE_EVENT_SYNTHETIC_DELAY_END are |
| 49 #define TRACE_EVENT_SYNTHETIC_DELAY_ACTIVATE(name) \ | 48 // balanced. Only the first call records the timing start point. |
| 49 #define TRACE_EVENT_SYNTHETIC_DELAY_BEGIN(name) \ |
| 50 do { \ | 50 do { \ |
| 51 static base::subtle::AtomicWord impl_ptr = 0; \ | 51 static base::subtle::AtomicWord impl_ptr = 0; \ |
| 52 trace_event_internal::GetOrCreateDelay(name, &impl_ptr)->Activate(); \ | 52 trace_event_internal::GetOrCreateDelay(name, &impl_ptr)->Begin(); \ |
| 53 } while (false) | 53 } while (false) |
| 54 | 54 |
| 55 // Apply a named delay. If TRACE_EVENT_SYNTHETIC_DELAY_ACTIVATE was called for | 55 // End a named delay. The delay is applied only if this call matches the |
| 56 // the same delay, that point in time is used as the delay start point. If not, | 56 // first corresponding call to TRACE_EVENT_SYNTHETIC_DELAY_BEGIN with the |
| 57 // this call will be a no-op. | 57 // same delay. |
| 58 #define TRACE_EVENT_SYNTHETIC_DELAY_APPLY(name) \ | 58 #define TRACE_EVENT_SYNTHETIC_DELAY_END(name) \ |
| 59 do { \ | 59 do { \ |
| 60 static base::subtle::AtomicWord impl_ptr = 0; \ | 60 static base::subtle::AtomicWord impl_ptr = 0; \ |
| 61 trace_event_internal::GetOrCreateDelay(name, &impl_ptr)->Apply(); \ | 61 trace_event_internal::GetOrCreateDelay(name, &impl_ptr)->End(); \ |
| 62 } while (false) | 62 } while (false) |
| 63 | 63 |
| 64 template <typename Type> | 64 template <typename Type> |
| 65 struct DefaultSingletonTraits; | 65 struct DefaultSingletonTraits; |
| 66 | 66 |
| 67 namespace base { | 67 namespace base { |
| 68 namespace debug { | 68 namespace debug { |
| 69 | 69 |
| 70 // Time source for computing delay durations. Used for testing. | 70 // Time source for computing delay durations. Used for testing. |
| 71 class TRACE_EVENT_API_CLASS_EXPORT TraceEventSyntheticDelayClock { | 71 class TRACE_EVENT_API_CLASS_EXPORT TraceEventSyntheticDelayClock { |
| (...skipping 15 matching lines...) Expand all Loading... |
| 87 ALTERNATING // Apply the configured delay every other time. | 87 ALTERNATING // Apply the configured delay every other time. |
| 88 }; | 88 }; |
| 89 | 89 |
| 90 // Returns an existing named delay instance or creates a new one with |name|. | 90 // Returns an existing named delay instance or creates a new one with |name|. |
| 91 static TraceEventSyntheticDelay* Lookup(const std::string& name); | 91 static TraceEventSyntheticDelay* Lookup(const std::string& name); |
| 92 | 92 |
| 93 void SetTargetDuration(TimeDelta target_duration); | 93 void SetTargetDuration(TimeDelta target_duration); |
| 94 void SetMode(Mode mode); | 94 void SetMode(Mode mode); |
| 95 void SetClock(TraceEventSyntheticDelayClock* clock); | 95 void SetClock(TraceEventSyntheticDelayClock* clock); |
| 96 | 96 |
| 97 // Establish the timing start point for the delay. No-op if the start point | 97 // Begin the delay, establishing its timing start point. May be called |
| 98 // was already set. | 98 // multiple times as long as the calls to End() are balanced. Only the first |
| 99 void Activate(); | 99 // call records the timing start point. |
| 100 void Begin(); |
| 100 | 101 |
| 101 // Execute the delay based on the current time and how long ago the start | 102 // End the delay. The delay is applied only if this call matches the first |
| 102 // point was established. If Activate wasn't called, this call will be a | 103 // corresponding call to Begin() with the same delay. |
| 103 // no-op. | 104 void End(); |
| 104 void Apply(); | 105 |
| 106 // Reset the number of calls to Begin() for this delay and act as if Begin() |
| 107 // was then called |begin_count| times. Useful for queuing several pieces of |
| 108 // work that will get executed on different worker threads. |
| 109 void ResetAndBeginMultiple(int begin_count); |
| 105 | 110 |
| 106 private: | 111 private: |
| 107 TraceEventSyntheticDelay(); | 112 TraceEventSyntheticDelay(); |
| 108 ~TraceEventSyntheticDelay(); | 113 ~TraceEventSyntheticDelay(); |
| 109 friend class TraceEventSyntheticDelayRegistry; | 114 friend class TraceEventSyntheticDelayRegistry; |
| 110 | 115 |
| 116 void BeginInternal(int begin_delta, bool reset); |
| 111 void Initialize(const std::string& name, | 117 void Initialize(const std::string& name, |
| 112 TraceEventSyntheticDelayClock* clock, | 118 TraceEventSyntheticDelayClock* clock); |
| 113 int thread_state_index); | |
| 114 void ApplyDelay(base::TimeTicks end_time); | 119 void ApplyDelay(base::TimeTicks end_time); |
| 115 | 120 |
| 116 Lock lock_; | 121 Lock lock_; |
| 117 Mode mode_; | 122 Mode mode_; |
| 118 std::string name_; | 123 std::string name_; |
| 119 int generation_; | 124 int begin_count_; |
| 125 int trigger_count_; |
| 126 base::TimeTicks start_time_; |
| 120 base::TimeDelta target_duration_; | 127 base::TimeDelta target_duration_; |
| 121 int thread_state_index_; | |
| 122 TraceEventSyntheticDelayClock* clock_; | 128 TraceEventSyntheticDelayClock* clock_; |
| 123 | 129 |
| 124 DISALLOW_COPY_AND_ASSIGN(TraceEventSyntheticDelay); | 130 DISALLOW_COPY_AND_ASSIGN(TraceEventSyntheticDelay); |
| 125 }; | 131 }; |
| 126 | 132 |
| 127 // Set the target durations of all registered synthetic delay points to zero. | 133 // Set the target durations of all registered synthetic delay points to zero. |
| 128 TRACE_EVENT_API_CLASS_EXPORT void ResetTraceEventSyntheticDelays(); | 134 TRACE_EVENT_API_CLASS_EXPORT void ResetTraceEventSyntheticDelays(); |
| 129 | 135 |
| 130 } // namespace debug | 136 } // namespace debug |
| 131 } // namespace base | 137 } // namespace base |
| (...skipping 13 matching lines...) Expand all Loading... |
| 145 DISALLOW_COPY_AND_ASSIGN(ScopedSyntheticDelay); | 151 DISALLOW_COPY_AND_ASSIGN(ScopedSyntheticDelay); |
| 146 }; | 152 }; |
| 147 | 153 |
| 148 // Helper for registering delays. Do not use directly. | 154 // Helper for registering delays. Do not use directly. |
| 149 TRACE_EVENT_API_CLASS_EXPORT base::debug::TraceEventSyntheticDelay* | 155 TRACE_EVENT_API_CLASS_EXPORT base::debug::TraceEventSyntheticDelay* |
| 150 GetOrCreateDelay(const char* name, base::subtle::AtomicWord* impl_ptr); | 156 GetOrCreateDelay(const char* name, base::subtle::AtomicWord* impl_ptr); |
| 151 | 157 |
| 152 } // namespace trace_event_internal | 158 } // namespace trace_event_internal |
| 153 | 159 |
| 154 #endif /* BASE_DEBUG_TRACE_EVENT_SYNTHETIC_DELAY_H_ */ | 160 #endif /* BASE_DEBUG_TRACE_EVENT_SYNTHETIC_DELAY_H_ */ |
| OLD | NEW |