OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 #include "base/memory/singleton.h" | 5 #include "base/memory/singleton.h" |
6 #include "base/third_party/dynamic_annotations/dynamic_annotations.h" | 6 #include "base/third_party/dynamic_annotations/dynamic_annotations.h" |
7 #include "base/trace_event/trace_event_synthetic_delay.h" | 7 #include "base/trace_event/trace_event_synthetic_delay.h" |
8 | 8 |
9 namespace { | 9 namespace { |
10 const int kMaxSyntheticDelays = 32; | 10 const int kMaxSyntheticDelays = 32; |
11 } // namespace | 11 } // namespace |
12 | 12 |
13 namespace base { | 13 namespace base { |
14 namespace trace_event { | 14 namespace trace_event { |
15 | 15 |
16 TraceEventSyntheticDelayClock::TraceEventSyntheticDelayClock() {} | 16 TraceEventSyntheticDelayClock::TraceEventSyntheticDelayClock() {} |
17 TraceEventSyntheticDelayClock::~TraceEventSyntheticDelayClock() {} | 17 TraceEventSyntheticDelayClock::~TraceEventSyntheticDelayClock() {} |
18 | 18 |
19 class TraceEventSyntheticDelayRegistry : public TraceEventSyntheticDelayClock { | 19 class TraceEventSyntheticDelayRegistry : public TraceEventSyntheticDelayClock { |
20 public: | 20 public: |
21 static TraceEventSyntheticDelayRegistry* GetInstance(); | 21 static TraceEventSyntheticDelayRegistry* GetInstance(); |
22 | 22 |
23 TraceEventSyntheticDelay* GetOrCreateDelay(const char* name); | 23 TraceEventSyntheticDelay* GetOrCreateDelay(const char* name); |
24 void ResetAllDelays(); | 24 void ResetAllDelays(); |
25 | 25 |
26 // TraceEventSyntheticDelayClock implementation. | 26 // TraceEventSyntheticDelayClock implementation. |
27 base::TimeTicks Now() override; | 27 TimeTicks Now() override; |
28 | 28 |
29 private: | 29 private: |
30 TraceEventSyntheticDelayRegistry(); | 30 TraceEventSyntheticDelayRegistry(); |
31 | 31 |
32 friend struct DefaultSingletonTraits<TraceEventSyntheticDelayRegistry>; | 32 friend struct DefaultSingletonTraits<TraceEventSyntheticDelayRegistry>; |
33 | 33 |
34 Lock lock_; | 34 Lock lock_; |
35 TraceEventSyntheticDelay delays_[kMaxSyntheticDelays]; | 35 TraceEventSyntheticDelay delays_[kMaxSyntheticDelays]; |
36 TraceEventSyntheticDelay dummy_delay_; | 36 TraceEventSyntheticDelay dummy_delay_; |
37 base::subtle::Atomic32 delay_count_; | 37 subtle::Atomic32 delay_count_; |
38 | 38 |
39 DISALLOW_COPY_AND_ASSIGN(TraceEventSyntheticDelayRegistry); | 39 DISALLOW_COPY_AND_ASSIGN(TraceEventSyntheticDelayRegistry); |
40 }; | 40 }; |
41 | 41 |
42 TraceEventSyntheticDelay::TraceEventSyntheticDelay() | 42 TraceEventSyntheticDelay::TraceEventSyntheticDelay() |
43 : mode_(STATIC), begin_count_(0), trigger_count_(0), clock_(NULL) {} | 43 : mode_(STATIC), begin_count_(0), trigger_count_(0), clock_(NULL) {} |
44 | 44 |
45 TraceEventSyntheticDelay::~TraceEventSyntheticDelay() {} | 45 TraceEventSyntheticDelay::~TraceEventSyntheticDelay() {} |
46 | 46 |
47 TraceEventSyntheticDelay* TraceEventSyntheticDelay::Lookup( | 47 TraceEventSyntheticDelay* TraceEventSyntheticDelay::Lookup( |
48 const std::string& name) { | 48 const std::string& name) { |
49 return TraceEventSyntheticDelayRegistry::GetInstance()->GetOrCreateDelay( | 49 return TraceEventSyntheticDelayRegistry::GetInstance()->GetOrCreateDelay( |
50 name.c_str()); | 50 name.c_str()); |
51 } | 51 } |
52 | 52 |
53 void TraceEventSyntheticDelay::Initialize( | 53 void TraceEventSyntheticDelay::Initialize( |
54 const std::string& name, | 54 const std::string& name, |
55 TraceEventSyntheticDelayClock* clock) { | 55 TraceEventSyntheticDelayClock* clock) { |
56 name_ = name; | 56 name_ = name; |
57 clock_ = clock; | 57 clock_ = clock; |
58 } | 58 } |
59 | 59 |
60 void TraceEventSyntheticDelay::SetTargetDuration( | 60 void TraceEventSyntheticDelay::SetTargetDuration(TimeDelta target_duration) { |
61 base::TimeDelta target_duration) { | |
62 AutoLock lock(lock_); | 61 AutoLock lock(lock_); |
63 target_duration_ = target_duration; | 62 target_duration_ = target_duration; |
64 trigger_count_ = 0; | 63 trigger_count_ = 0; |
65 begin_count_ = 0; | 64 begin_count_ = 0; |
66 } | 65 } |
67 | 66 |
68 void TraceEventSyntheticDelay::SetMode(Mode mode) { | 67 void TraceEventSyntheticDelay::SetMode(Mode mode) { |
69 AutoLock lock(lock_); | 68 AutoLock lock(lock_); |
70 mode_ = mode; | 69 mode_ = mode; |
71 } | 70 } |
72 | 71 |
73 void TraceEventSyntheticDelay::SetClock(TraceEventSyntheticDelayClock* clock) { | 72 void TraceEventSyntheticDelay::SetClock(TraceEventSyntheticDelayClock* clock) { |
74 AutoLock lock(lock_); | 73 AutoLock lock(lock_); |
75 clock_ = clock; | 74 clock_ = clock; |
76 } | 75 } |
77 | 76 |
78 void TraceEventSyntheticDelay::Begin() { | 77 void TraceEventSyntheticDelay::Begin() { |
79 // Note that we check for a non-zero target duration without locking to keep | 78 // Note that we check for a non-zero target duration without locking to keep |
80 // things quick for the common case when delays are disabled. Since the delay | 79 // things quick for the common case when delays are disabled. Since the delay |
81 // calculation is done with a lock held, it will always be correct. The only | 80 // calculation is done with a lock held, it will always be correct. The only |
82 // downside of this is that we may fail to apply some delays when the target | 81 // downside of this is that we may fail to apply some delays when the target |
83 // duration changes. | 82 // duration changes. |
84 ANNOTATE_BENIGN_RACE(&target_duration_, "Synthetic delay duration"); | 83 ANNOTATE_BENIGN_RACE(&target_duration_, "Synthetic delay duration"); |
85 if (!target_duration_.ToInternalValue()) | 84 if (!target_duration_.ToInternalValue()) |
86 return; | 85 return; |
87 | 86 |
88 base::TimeTicks start_time = clock_->Now(); | 87 TimeTicks start_time = clock_->Now(); |
89 { | 88 { |
90 AutoLock lock(lock_); | 89 AutoLock lock(lock_); |
91 if (++begin_count_ != 1) | 90 if (++begin_count_ != 1) |
92 return; | 91 return; |
93 end_time_ = CalculateEndTimeLocked(start_time); | 92 end_time_ = CalculateEndTimeLocked(start_time); |
94 } | 93 } |
95 } | 94 } |
96 | 95 |
97 void TraceEventSyntheticDelay::BeginParallel(base::TimeTicks* out_end_time) { | 96 void TraceEventSyntheticDelay::BeginParallel(TimeTicks* out_end_time) { |
98 // See note in Begin(). | 97 // See note in Begin(). |
99 ANNOTATE_BENIGN_RACE(&target_duration_, "Synthetic delay duration"); | 98 ANNOTATE_BENIGN_RACE(&target_duration_, "Synthetic delay duration"); |
100 if (!target_duration_.ToInternalValue()) { | 99 if (!target_duration_.ToInternalValue()) { |
101 *out_end_time = base::TimeTicks(); | 100 *out_end_time = TimeTicks(); |
102 return; | 101 return; |
103 } | 102 } |
104 | 103 |
105 base::TimeTicks start_time = clock_->Now(); | 104 TimeTicks start_time = clock_->Now(); |
106 { | 105 { |
107 AutoLock lock(lock_); | 106 AutoLock lock(lock_); |
108 *out_end_time = CalculateEndTimeLocked(start_time); | 107 *out_end_time = CalculateEndTimeLocked(start_time); |
109 } | 108 } |
110 } | 109 } |
111 | 110 |
112 void TraceEventSyntheticDelay::End() { | 111 void TraceEventSyntheticDelay::End() { |
113 // See note in Begin(). | 112 // See note in Begin(). |
114 ANNOTATE_BENIGN_RACE(&target_duration_, "Synthetic delay duration"); | 113 ANNOTATE_BENIGN_RACE(&target_duration_, "Synthetic delay duration"); |
115 if (!target_duration_.ToInternalValue()) | 114 if (!target_duration_.ToInternalValue()) |
116 return; | 115 return; |
117 | 116 |
118 base::TimeTicks end_time; | 117 TimeTicks end_time; |
119 { | 118 { |
120 AutoLock lock(lock_); | 119 AutoLock lock(lock_); |
121 if (!begin_count_ || --begin_count_ != 0) | 120 if (!begin_count_ || --begin_count_ != 0) |
122 return; | 121 return; |
123 end_time = end_time_; | 122 end_time = end_time_; |
124 } | 123 } |
125 if (!end_time.is_null()) | 124 if (!end_time.is_null()) |
126 ApplyDelay(end_time); | 125 ApplyDelay(end_time); |
127 } | 126 } |
128 | 127 |
129 void TraceEventSyntheticDelay::EndParallel(base::TimeTicks end_time) { | 128 void TraceEventSyntheticDelay::EndParallel(TimeTicks end_time) { |
130 if (!end_time.is_null()) | 129 if (!end_time.is_null()) |
131 ApplyDelay(end_time); | 130 ApplyDelay(end_time); |
132 } | 131 } |
133 | 132 |
134 base::TimeTicks TraceEventSyntheticDelay::CalculateEndTimeLocked( | 133 TimeTicks TraceEventSyntheticDelay::CalculateEndTimeLocked( |
135 base::TimeTicks start_time) { | 134 TimeTicks start_time) { |
136 if (mode_ == ONE_SHOT && trigger_count_++) | 135 if (mode_ == ONE_SHOT && trigger_count_++) |
137 return base::TimeTicks(); | 136 return TimeTicks(); |
138 else if (mode_ == ALTERNATING && trigger_count_++ % 2) | 137 else if (mode_ == ALTERNATING && trigger_count_++ % 2) |
139 return base::TimeTicks(); | 138 return TimeTicks(); |
140 return start_time + target_duration_; | 139 return start_time + target_duration_; |
141 } | 140 } |
142 | 141 |
143 void TraceEventSyntheticDelay::ApplyDelay(base::TimeTicks end_time) { | 142 void TraceEventSyntheticDelay::ApplyDelay(TimeTicks end_time) { |
144 TRACE_EVENT0("synthetic_delay", name_.c_str()); | 143 TRACE_EVENT0("synthetic_delay", name_.c_str()); |
145 while (clock_->Now() < end_time) { | 144 while (clock_->Now() < end_time) { |
146 // Busy loop. | 145 // Busy loop. |
147 } | 146 } |
148 } | 147 } |
149 | 148 |
150 TraceEventSyntheticDelayRegistry* | 149 TraceEventSyntheticDelayRegistry* |
151 TraceEventSyntheticDelayRegistry::GetInstance() { | 150 TraceEventSyntheticDelayRegistry::GetInstance() { |
152 return Singleton< | 151 return Singleton< |
153 TraceEventSyntheticDelayRegistry, | 152 TraceEventSyntheticDelayRegistry, |
154 LeakySingletonTraits<TraceEventSyntheticDelayRegistry> >::get(); | 153 LeakySingletonTraits<TraceEventSyntheticDelayRegistry> >::get(); |
155 } | 154 } |
156 | 155 |
157 TraceEventSyntheticDelayRegistry::TraceEventSyntheticDelayRegistry() | 156 TraceEventSyntheticDelayRegistry::TraceEventSyntheticDelayRegistry() |
158 : delay_count_(0) {} | 157 : delay_count_(0) {} |
159 | 158 |
160 TraceEventSyntheticDelay* TraceEventSyntheticDelayRegistry::GetOrCreateDelay( | 159 TraceEventSyntheticDelay* TraceEventSyntheticDelayRegistry::GetOrCreateDelay( |
161 const char* name) { | 160 const char* name) { |
162 // Try to find an existing delay first without locking to make the common case | 161 // Try to find an existing delay first without locking to make the common case |
163 // fast. | 162 // fast. |
164 int delay_count = base::subtle::Acquire_Load(&delay_count_); | 163 int delay_count = subtle::Acquire_Load(&delay_count_); |
165 for (int i = 0; i < delay_count; ++i) { | 164 for (int i = 0; i < delay_count; ++i) { |
166 if (!strcmp(name, delays_[i].name_.c_str())) | 165 if (!strcmp(name, delays_[i].name_.c_str())) |
167 return &delays_[i]; | 166 return &delays_[i]; |
168 } | 167 } |
169 | 168 |
170 AutoLock lock(lock_); | 169 AutoLock lock(lock_); |
171 delay_count = base::subtle::Acquire_Load(&delay_count_); | 170 delay_count = subtle::Acquire_Load(&delay_count_); |
172 for (int i = 0; i < delay_count; ++i) { | 171 for (int i = 0; i < delay_count; ++i) { |
173 if (!strcmp(name, delays_[i].name_.c_str())) | 172 if (!strcmp(name, delays_[i].name_.c_str())) |
174 return &delays_[i]; | 173 return &delays_[i]; |
175 } | 174 } |
176 | 175 |
177 DCHECK(delay_count < kMaxSyntheticDelays) | 176 DCHECK(delay_count < kMaxSyntheticDelays) |
178 << "must increase kMaxSyntheticDelays"; | 177 << "must increase kMaxSyntheticDelays"; |
179 if (delay_count >= kMaxSyntheticDelays) | 178 if (delay_count >= kMaxSyntheticDelays) |
180 return &dummy_delay_; | 179 return &dummy_delay_; |
181 | 180 |
182 delays_[delay_count].Initialize(std::string(name), this); | 181 delays_[delay_count].Initialize(std::string(name), this); |
183 base::subtle::Release_Store(&delay_count_, delay_count + 1); | 182 subtle::Release_Store(&delay_count_, delay_count + 1); |
184 return &delays_[delay_count]; | 183 return &delays_[delay_count]; |
185 } | 184 } |
186 | 185 |
187 base::TimeTicks TraceEventSyntheticDelayRegistry::Now() { | 186 TimeTicks TraceEventSyntheticDelayRegistry::Now() { |
188 return base::TimeTicks::Now(); | 187 return TimeTicks::Now(); |
189 } | 188 } |
190 | 189 |
191 void TraceEventSyntheticDelayRegistry::ResetAllDelays() { | 190 void TraceEventSyntheticDelayRegistry::ResetAllDelays() { |
192 AutoLock lock(lock_); | 191 AutoLock lock(lock_); |
193 int delay_count = base::subtle::Acquire_Load(&delay_count_); | 192 int delay_count = subtle::Acquire_Load(&delay_count_); |
194 for (int i = 0; i < delay_count; ++i) { | 193 for (int i = 0; i < delay_count; ++i) { |
195 delays_[i].SetTargetDuration(base::TimeDelta()); | 194 delays_[i].SetTargetDuration(TimeDelta()); |
196 delays_[i].SetClock(this); | 195 delays_[i].SetClock(this); |
197 } | 196 } |
198 } | 197 } |
199 | 198 |
200 void ResetTraceEventSyntheticDelays() { | 199 void ResetTraceEventSyntheticDelays() { |
201 TraceEventSyntheticDelayRegistry::GetInstance()->ResetAllDelays(); | 200 TraceEventSyntheticDelayRegistry::GetInstance()->ResetAllDelays(); |
202 } | 201 } |
203 | 202 |
204 } // namespace trace_event | 203 } // namespace trace_event |
205 } // namespace base | 204 } // namespace base |
(...skipping 20 matching lines...) Expand all Loading... |
226 delay_impl = | 225 delay_impl = |
227 base::trace_event::TraceEventSyntheticDelayRegistry::GetInstance() | 226 base::trace_event::TraceEventSyntheticDelayRegistry::GetInstance() |
228 ->GetOrCreateDelay(name); | 227 ->GetOrCreateDelay(name); |
229 base::subtle::Release_Store( | 228 base::subtle::Release_Store( |
230 impl_ptr, reinterpret_cast<base::subtle::AtomicWord>(delay_impl)); | 229 impl_ptr, reinterpret_cast<base::subtle::AtomicWord>(delay_impl)); |
231 } | 230 } |
232 return delay_impl; | 231 return delay_impl; |
233 } | 232 } |
234 | 233 |
235 } // namespace trace_event_internal | 234 } // namespace trace_event_internal |
OLD | NEW |