OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/profiler/stack_sampling_profiler.h" | 5 #include "base/profiler/stack_sampling_profiler.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <map> | |
8 #include <utility> | 9 #include <utility> |
9 | 10 |
11 #include "base/atomic_sequence_num.h" | |
12 #include "base/atomicops.h" | |
10 #include "base/bind.h" | 13 #include "base/bind.h" |
11 #include "base/bind_helpers.h" | 14 #include "base/bind_helpers.h" |
12 #include "base/callback.h" | 15 #include "base/callback.h" |
13 #include "base/lazy_instance.h" | 16 #include "base/lazy_instance.h" |
14 #include "base/location.h" | 17 #include "base/location.h" |
15 #include "base/macros.h" | 18 #include "base/macros.h" |
19 #include "base/memory/ptr_util.h" | |
20 #include "base/memory/singleton.h" | |
16 #include "base/profiler/native_stack_sampler.h" | 21 #include "base/profiler/native_stack_sampler.h" |
17 #include "base/synchronization/lock.h" | 22 #include "base/synchronization/lock.h" |
23 #include "base/threading/thread.h" | |
24 #include "base/threading/thread_restrictions.h" | |
18 #include "base/threading/thread_task_runner_handle.h" | 25 #include "base/threading/thread_task_runner_handle.h" |
19 #include "base/timer/elapsed_timer.h" | 26 #include "base/timer/elapsed_timer.h" |
20 | 27 |
21 namespace base { | 28 namespace base { |
22 | 29 |
23 namespace { | 30 namespace { |
24 | 31 |
25 // Used to ensure only one profiler is running at a time. | 32 // This value is used when there is no collection in progress and thus no ID |
26 LazyInstance<Lock>::Leaky concurrent_profiling_lock = LAZY_INSTANCE_INITIALIZER; | 33 // for referencing the active collection to the SamplingThread. |
27 | 34 const int NULL_COLLECTION_ID = -1; |
28 // AsyncRunner ---------------------------------------------------------------- | |
29 | |
30 // Helper class to allow a profiler to be run completely asynchronously from the | |
31 // initiator, without being concerned with the profiler's lifetime. | |
32 class AsyncRunner { | |
33 public: | |
34 // Sets up a profiler and arranges for it to be deleted on its completed | |
35 // callback. | |
36 static void Run(PlatformThreadId thread_id, | |
37 const StackSamplingProfiler::SamplingParams& params, | |
38 const StackSamplingProfiler::CompletedCallback& callback); | |
39 | |
40 private: | |
41 AsyncRunner(); | |
42 | |
43 // Runs the callback and deletes the AsyncRunner instance. |profiles| is not | |
44 // const& because it must be passed with std::move. | |
45 static void RunCallbackAndDeleteInstance( | |
46 std::unique_ptr<AsyncRunner> object_to_be_deleted, | |
47 const StackSamplingProfiler::CompletedCallback& callback, | |
48 scoped_refptr<SingleThreadTaskRunner> task_runner, | |
49 StackSamplingProfiler::CallStackProfiles profiles); | |
50 | |
51 std::unique_ptr<StackSamplingProfiler> profiler_; | |
52 | |
53 DISALLOW_COPY_AND_ASSIGN(AsyncRunner); | |
54 }; | |
55 | |
56 // static | |
57 void AsyncRunner::Run( | |
58 PlatformThreadId thread_id, | |
59 const StackSamplingProfiler::SamplingParams& params, | |
60 const StackSamplingProfiler::CompletedCallback &callback) { | |
61 std::unique_ptr<AsyncRunner> runner(new AsyncRunner); | |
62 AsyncRunner* temp_ptr = runner.get(); | |
63 temp_ptr->profiler_.reset( | |
64 new StackSamplingProfiler(thread_id, params, | |
65 Bind(&AsyncRunner::RunCallbackAndDeleteInstance, | |
66 Passed(&runner), callback, | |
67 ThreadTaskRunnerHandle::Get()))); | |
68 // The callback won't be called until after Start(), so temp_ptr will still | |
69 // be valid here. | |
70 temp_ptr->profiler_->Start(); | |
71 } | |
72 | |
73 AsyncRunner::AsyncRunner() {} | |
74 | |
75 void AsyncRunner::RunCallbackAndDeleteInstance( | |
76 std::unique_ptr<AsyncRunner> object_to_be_deleted, | |
77 const StackSamplingProfiler::CompletedCallback& callback, | |
78 scoped_refptr<SingleThreadTaskRunner> task_runner, | |
79 StackSamplingProfiler::CallStackProfiles profiles) { | |
80 callback.Run(std::move(profiles)); | |
81 // Delete the instance on the original calling thread. | |
82 task_runner->DeleteSoon(FROM_HERE, object_to_be_deleted.release()); | |
83 } | |
84 | 35 |
85 void ChangeAtomicFlags(subtle::Atomic32* flags, | 36 void ChangeAtomicFlags(subtle::Atomic32* flags, |
86 subtle::Atomic32 set, | 37 subtle::Atomic32 set, |
87 subtle::Atomic32 clear) { | 38 subtle::Atomic32 clear) { |
88 DCHECK(set != 0 || clear != 0); | 39 DCHECK(set != 0 || clear != 0); |
89 DCHECK_EQ(0, set & clear); | 40 DCHECK_EQ(0, set & clear); |
90 | 41 |
91 subtle::Atomic32 bits = subtle::NoBarrier_Load(flags); | 42 subtle::Atomic32 bits = subtle::NoBarrier_Load(flags); |
92 while (true) { | 43 while (true) { |
93 subtle::Atomic32 existing = | 44 subtle::Atomic32 existing = |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
153 StackSamplingProfiler::CallStackProfile | 104 StackSamplingProfiler::CallStackProfile |
154 StackSamplingProfiler::CallStackProfile::CopyForTesting() const { | 105 StackSamplingProfiler::CallStackProfile::CopyForTesting() const { |
155 return CallStackProfile(*this); | 106 return CallStackProfile(*this); |
156 } | 107 } |
157 | 108 |
158 StackSamplingProfiler::CallStackProfile::CallStackProfile( | 109 StackSamplingProfiler::CallStackProfile::CallStackProfile( |
159 const CallStackProfile& other) = default; | 110 const CallStackProfile& other) = default; |
160 | 111 |
161 // StackSamplingProfiler::SamplingThread -------------------------------------- | 112 // StackSamplingProfiler::SamplingThread -------------------------------------- |
162 | 113 |
163 StackSamplingProfiler::SamplingThread::SamplingThread( | 114 class StackSamplingProfiler::SamplingThread : public Thread { |
164 std::unique_ptr<NativeStackSampler> native_sampler, | 115 public: |
116 class TestAPI { | |
117 public: | |
118 // Reset the existing sampler. This will unfortunately create the object | |
119 // unnecessarily if it doesn't already exist but there's no way around that. | |
120 static void Reset(); | |
121 | |
122 // Disables inherent idle-shutdown behavior. | |
123 static void DisableIdleShutdown(); | |
124 | |
125 // Begins an idle shutdown as if the idle-timer had expired and wait for | |
126 // it to execute. Since the timer would have only been started at a time | |
127 // when the sampling thread actually was idle, this must be called only | |
128 // when it is known that there are no active sampling threads. If | |
129 // |simulate_intervening_add| is true then, when executed, the shutdown | |
130 // task will believe that a new collection has been added since it was | |
131 // posted. | |
132 static void ShutdownAssumingIdle(bool simulate_intervening_add); | |
133 | |
134 private: | |
135 // Calls the sampling threads ShutdownTask and then signals an event. | |
136 static void ShutdownTaskAndSignalEvent(SamplingThread* sampler, | |
137 int add_events, | |
138 WaitableEvent* event); | |
139 }; | |
140 | |
141 struct CollectionContext { | |
142 CollectionContext(PlatformThreadId target, | |
143 const SamplingParams& params, | |
144 const CompletedCallback& callback, | |
145 WaitableEvent* finished, | |
146 std::unique_ptr<NativeStackSampler> sampler) | |
147 : collection_id(next_collection_id_.GetNext()), | |
148 target(target), | |
149 params(params), | |
150 callback(callback), | |
151 finished(finished), | |
152 native_sampler(std::move(sampler)) {} | |
153 ~CollectionContext() {} | |
154 | |
155 // An identifier for this collection, used to uniquely identify it to | |
156 // outside interests. | |
157 const int collection_id; | |
158 | |
159 const PlatformThreadId target; // ID of The thread being sampled. | |
160 const SamplingParams params; // Information about how to sample. | |
161 const CompletedCallback callback; // Callback made when sampling complete. | |
162 WaitableEvent* const finished; // Signaled when all sampling complete. | |
163 | |
164 // Platform-specific module that does the actual sampling. | |
165 std::unique_ptr<NativeStackSampler> native_sampler; | |
166 | |
167 // The absolute time for the next sample. | |
168 Time next_sample_time; | |
169 | |
170 // The time that a profile was started, for calculating the total duration. | |
171 Time profile_start_time; | |
172 | |
173 // Counters that indicate the current position along the acquisition. | |
174 int burst = 0; | |
175 int sample = 0; | |
176 | |
177 // The collected stack samples. The active profile is always at the back(). | |
178 CallStackProfiles profiles; | |
179 | |
180 private: | |
181 static StaticAtomicSequenceNumber next_collection_id_; | |
182 }; | |
183 | |
184 // Gets the single instance of this class. | |
185 static SamplingThread* GetInstance(); | |
186 | |
187 // Starts the thread. | |
188 void Start(); | |
189 | |
190 // Adds a new CollectionContext to the thread. This can be called externally | |
191 // from any thread. This returns an ID that can later be used to stop | |
192 // the sampling. | |
193 int Add(std::unique_ptr<CollectionContext> collection); | |
194 | |
195 // Removes an active collection based on its ID, forcing it to run its | |
196 // callback if any data has been collected. This can be called externally | |
197 // from any thread. | |
198 void Remove(int id); | |
199 | |
200 private: | |
201 friend class TestAPI; | |
202 friend struct DefaultSingletonTraits<SamplingThread>; | |
203 | |
204 // The different states in which the sampling-thread can be. | |
205 enum ThreadExecutionState { | |
206 // The thread is not running because it has never been started. It will be | |
207 // started when a sampling request is received. | |
208 NOT_STARTED, | |
209 | |
210 // The thread is running and processing tasks. This is the state when any | |
211 // sampling requests are active and during the "idle" period afterward | |
212 // before the thread is stopped. | |
213 RUNNING, | |
214 | |
215 // Once all sampling requests have finished and the "idle" period has | |
216 // expired, the thread will be set to this state and its shutdown | |
217 // initiated. A call to Stop() must be made to ensure the previous thread | |
218 // has completely exited before calling Start() and moving back to the | |
219 // RUNNING state. | |
220 EXITING, | |
221 }; | |
222 | |
223 SamplingThread(); | |
224 ~SamplingThread() override; | |
225 | |
226 // Get task runner that is usable from the outside. | |
227 scoped_refptr<SingleThreadTaskRunner> GetOrCreateTaskRunnerForAdd(); | |
228 scoped_refptr<SingleThreadTaskRunner> GetTaskRunner( | |
229 ThreadExecutionState* out_state); | |
230 | |
231 // Get task runner that is usable from the sampling thread itself. | |
232 scoped_refptr<SingleThreadTaskRunner> GetTaskRunnerOnSamplingThread(); | |
233 | |
234 // Finishes a collection and reports collected data via callback. | |
235 void FinishCollection(CollectionContext* collection); | |
236 | |
237 // Records a single sample of a collection. | |
238 void RecordSample(CollectionContext* collection); | |
239 | |
240 // Check if the sampling thread is idle and begin a shutdown if so. | |
241 void ScheduleShutdownIfIdle(); | |
242 | |
243 // These methods are tasks that get posted to the internal message queue. | |
244 void AddCollectionTask(std::unique_ptr<CollectionContext> collection); | |
245 void RemoveCollectionTask(int id); | |
246 void PerformCollectionTask(int id); | |
247 void ShutdownTask(int add_events); | |
248 | |
249 // Updates the |next_sample_time| time based on configured parameters. | |
Mike Wittman
2017/03/31 18:12:33
add comment on the meaning of the return value
bcwhite
2017/04/03 20:18:13
Done.
| |
250 bool UpdateNextSampleTime(CollectionContext* collection); | |
251 | |
252 // Thread: | |
253 void CleanUp() override; | |
254 | |
255 // The task-runner for the sampling thread and some information about it. | |
256 // This must always be accessed while holding the lock. The saved task-runner | |
257 // can be freely used by any calling thread. This lock is also used to | |
258 // order calls to the Thread API (Start, Stop, StopSoon, & DetachFromSequence) | |
259 // so that multiple threads may make those calls. | |
Mike Wittman
2017/03/31 18:12:33
broaden comment to discuss the general thread exec
bcwhite
2017/04/03 20:18:13
Done.
| |
260 Lock thread_execution_state_lock_; // Protects all thread_execution_state_* | |
261 ThreadExecutionState thread_execution_state_ = NOT_STARTED; | |
262 scoped_refptr<SingleThreadTaskRunner> thread_execution_state_task_runner_; | |
263 bool thread_execution_state_disable_idle_shutdown_for_testing_ = false; | |
264 | |
265 // A counter that notes adds of new collection requests. It is incremented | |
266 // when changes occur so that delayed shutdown tasks are able to detect if | |
267 // samething new has happened while it was waiting. Like all "execution_state" | |
268 // vars, this must be accessed while holding |thread_execution_state_lock_|. | |
269 int thread_execution_state_add_events_ = 0; | |
270 | |
271 // A map of IDs to collection contexts. Because this class is a singleton | |
272 // that is never destroyed, context objects will never be destructed except | |
273 // by explicit action. Thus, it's acceptable to pass unretained pointers | |
274 // to these objects when posting tasks. | |
275 std::map<int, std::unique_ptr<CollectionContext>> active_collections_; | |
Mike Wittman
2017/03/31 18:12:33
nit: move this declaration above the lock to make
bcwhite
2017/04/03 20:18:13
Done.
| |
276 | |
277 DISALLOW_COPY_AND_ASSIGN(SamplingThread); | |
278 }; | |
279 | |
280 // static | |
281 void StackSamplingProfiler::SamplingThread::TestAPI::Reset() { | |
282 SamplingThread* sampler = SamplingThread::GetInstance(); | |
283 | |
284 ThreadExecutionState state; | |
285 { | |
286 AutoLock lock(sampler->thread_execution_state_lock_); | |
287 state = sampler->thread_execution_state_; | |
288 CHECK(sampler->active_collections_.empty()); | |
289 } | |
290 | |
291 // Stop the thread and wait for it to exit. This has to be done through by | |
292 // the thread itself because it has taken ownership of its own lifetime. | |
293 if (state == RUNNING) { | |
294 ShutdownAssumingIdle(false); | |
295 state = EXITING; | |
296 } | |
297 // Make sure thread is cleaned up since state will be reset to NOT_STARTED. | |
298 if (state == EXITING) | |
299 sampler->Stop(); | |
300 | |
301 // Reset internal variables to the just-initialized state. | |
302 { | |
303 AutoLock lock(sampler->thread_execution_state_lock_); | |
304 sampler->thread_execution_state_ = NOT_STARTED; | |
305 sampler->thread_execution_state_task_runner_ = nullptr; | |
306 sampler->thread_execution_state_disable_idle_shutdown_for_testing_ = false; | |
307 sampler->thread_execution_state_add_events_ = 0; | |
308 } | |
309 } | |
310 | |
311 // static | |
312 void StackSamplingProfiler::SamplingThread::TestAPI::DisableIdleShutdown() { | |
313 SamplingThread* sampler = SamplingThread::GetInstance(); | |
314 | |
315 { | |
316 AutoLock lock(sampler->thread_execution_state_lock_); | |
317 sampler->thread_execution_state_disable_idle_shutdown_for_testing_ = true; | |
318 } | |
319 } | |
320 | |
321 // static | |
322 void StackSamplingProfiler::SamplingThread::TestAPI::ShutdownAssumingIdle( | |
323 bool simulate_intervening_add) { | |
324 SamplingThread* sampler = SamplingThread::GetInstance(); | |
325 | |
326 ThreadExecutionState state; | |
327 scoped_refptr<SingleThreadTaskRunner> task_runner = | |
328 sampler->GetTaskRunner(&state); | |
329 CHECK_EQ(RUNNING, state); | |
330 CHECK(task_runner); | |
331 | |
332 int add_events; | |
333 { | |
334 AutoLock lock(sampler->thread_execution_state_lock_); | |
335 add_events = sampler->thread_execution_state_add_events_; | |
336 if (simulate_intervening_add) | |
337 ++sampler->thread_execution_state_add_events_; | |
338 } | |
339 | |
340 WaitableEvent executed(WaitableEvent::ResetPolicy::MANUAL, | |
341 WaitableEvent::InitialState::NOT_SIGNALED); | |
342 // PostTaskAndReply won't work because thread and associated message-loop may | |
343 // be shut down. | |
344 task_runner->PostTask(FROM_HERE, | |
345 Bind(&ShutdownTaskAndSignalEvent, Unretained(sampler), | |
346 add_events, Unretained(&executed))); | |
347 executed.Wait(); | |
348 } | |
349 | |
350 // static | |
351 void StackSamplingProfiler::SamplingThread::TestAPI::ShutdownTaskAndSignalEvent( | |
352 SamplingThread* sampler, | |
353 int add_events, | |
354 WaitableEvent* event) { | |
355 sampler->ShutdownTask(add_events); | |
356 event->Signal(); | |
357 } | |
358 | |
359 StaticAtomicSequenceNumber StackSamplingProfiler::SamplingThread:: | |
360 CollectionContext::next_collection_id_; | |
361 | |
362 StackSamplingProfiler::SamplingThread::SamplingThread() | |
363 : Thread("Chrome_SamplingProfilerThread") {} | |
364 | |
365 StackSamplingProfiler::SamplingThread::~SamplingThread() { | |
366 Stop(); | |
367 } | |
368 | |
369 StackSamplingProfiler::SamplingThread* | |
370 StackSamplingProfiler::SamplingThread::GetInstance() { | |
371 return Singleton<SamplingThread, LeakySingletonTraits<SamplingThread>>::get(); | |
372 } | |
373 | |
374 void StackSamplingProfiler::SamplingThread::Start() { | |
375 Thread::Options options; | |
376 // Use a higher priority for a more accurate sampling interval. | |
377 options.priority = ThreadPriority::DISPLAY; | |
378 Thread::StartWithOptions(options); | |
379 } | |
380 | |
381 int StackSamplingProfiler::SamplingThread::Add( | |
382 std::unique_ptr<CollectionContext> collection) { | |
383 int id = collection->collection_id; | |
384 scoped_refptr<SingleThreadTaskRunner> task_runner = | |
385 GetOrCreateTaskRunnerForAdd(); | |
386 | |
387 task_runner->PostTask(FROM_HERE, Bind(&SamplingThread::AddCollectionTask, | |
388 Unretained(this), Passed(&collection))); | |
389 | |
390 return id; | |
391 } | |
392 | |
393 void StackSamplingProfiler::SamplingThread::Remove(int id) { | |
394 ThreadExecutionState state; | |
395 scoped_refptr<SingleThreadTaskRunner> task_runner = GetTaskRunner(&state); | |
396 if (state != RUNNING) | |
397 return; | |
398 DCHECK(task_runner); | |
399 | |
400 // This can fail if the thread were to exit between acquisition of the task | |
401 // runner above and the call below. In that case, however, everything has | |
402 // stopped so there's no need to try to stop it. | |
403 task_runner->PostTask(FROM_HERE, Bind(&SamplingThread::RemoveCollectionTask, | |
404 Unretained(this), id)); | |
405 } | |
406 | |
407 scoped_refptr<SingleThreadTaskRunner> | |
408 StackSamplingProfiler::SamplingThread::GetOrCreateTaskRunnerForAdd() { | |
409 AutoLock lock(thread_execution_state_lock_); | |
410 | |
411 // The increment of the "add events" count is why this method is to be only | |
412 // called from "add". | |
413 ++thread_execution_state_add_events_; | |
414 | |
415 if (thread_execution_state_ == RUNNING) { | |
416 DCHECK(thread_execution_state_task_runner_); | |
417 // This shouldn't be called from the sampling thread as it's inefficient. | |
418 // Use GetTaskRunnerOnSamplingThread() instead. | |
419 DCHECK_NE(GetThreadId(), PlatformThread::CurrentId()); | |
420 return thread_execution_state_task_runner_; | |
421 } | |
422 | |
423 if (thread_execution_state_ == EXITING) { | |
424 // The previous instance has only been partially cleaned up. It is necessary | |
425 // to call Stop() before Start(). | |
426 Stop(); | |
427 } | |
428 | |
429 // The thread is not running. Start it and get associated runner. The task- | |
430 // runner has to be saved for future use because though it can be used from | |
431 // any thread, it can be acquired via task_runner() only on the created | |
432 // thread and the thread that creates it (i.e. this thread). | |
433 Start(); | |
434 thread_execution_state_ = RUNNING; | |
435 thread_execution_state_task_runner_ = Thread::task_runner(); | |
436 | |
437 // Detach the sampling thread from the "sequence" (i.e. thread) that | |
438 // started it so that it can be self-managed or stopped by another thread. | |
439 DetachFromSequence(); | |
440 | |
441 return thread_execution_state_task_runner_; | |
442 } | |
443 | |
444 scoped_refptr<SingleThreadTaskRunner> | |
445 StackSamplingProfiler::SamplingThread::GetTaskRunner( | |
446 ThreadExecutionState* out_state) { | |
447 AutoLock lock(thread_execution_state_lock_); | |
448 if (out_state) | |
449 *out_state = thread_execution_state_; | |
450 if (thread_execution_state_ == RUNNING) { | |
451 // This shouldn't be called from the sampling thread as it's inefficient. | |
452 // Use GetTaskRunnerOnSamplingThread() instead. | |
453 DCHECK_NE(GetThreadId(), PlatformThread::CurrentId()); | |
454 DCHECK(thread_execution_state_task_runner_); | |
455 } else { | |
456 DCHECK(!thread_execution_state_task_runner_); | |
457 } | |
458 | |
459 return thread_execution_state_task_runner_; | |
460 } | |
461 | |
462 scoped_refptr<SingleThreadTaskRunner> | |
463 StackSamplingProfiler::SamplingThread::GetTaskRunnerOnSamplingThread() { | |
464 // This should be called only from the sampling thread as it has limited | |
465 // accessibility. | |
466 DCHECK_EQ(GetThreadId(), PlatformThread::CurrentId()); | |
467 | |
468 return Thread::task_runner(); | |
469 } | |
470 | |
471 void StackSamplingProfiler::SamplingThread::FinishCollection( | |
472 CollectionContext* collection) { | |
473 // If there is no duration for the final profile (because it was stopped), | |
474 // calculate it now. | |
475 if (!collection->profiles.empty() && | |
476 collection->profiles.back().profile_duration == TimeDelta()) { | |
477 collection->profiles.back().profile_duration = | |
478 Time::Now() - collection->profile_start_time; | |
479 } | |
480 | |
481 // Extract some information so callback and event-signalling can still be | |
482 // done after the collection has been removed from the list of "active" ones. | |
483 // This allows the the controlling object (and tests using it) to be confident | |
484 // that collection is fully finished when those things occur. | |
485 const CompletedCallback callback = collection->callback; | |
486 CallStackProfiles profiles = std::move(collection->profiles); | |
487 WaitableEvent* finished = collection->finished; | |
488 | |
489 // Remove this collection from the map of known ones. The |collection| | |
490 // parameter is invalid after this point. | |
491 size_t count = active_collections_.erase(collection->collection_id); | |
492 DCHECK_EQ(1U, count); | |
493 | |
494 // Run the associated callback, passing the collected profiles. | |
495 callback.Run(std::move(profiles)); | |
496 | |
497 // Signal that this collection is finished. | |
498 finished->Signal(); | |
499 } | |
500 | |
501 void StackSamplingProfiler::SamplingThread::RecordSample( | |
502 CollectionContext* collection) { | |
503 DCHECK(collection->native_sampler); | |
504 | |
505 // If this is the first sample of a burst, a new Profile needs to be created | |
506 // and filled. | |
507 if (collection->sample == 0) { | |
508 collection->profiles.push_back(CallStackProfile()); | |
509 CallStackProfile& profile = collection->profiles.back(); | |
510 profile.sampling_period = collection->params.sampling_interval; | |
511 collection->profile_start_time = Time::Now(); | |
512 collection->native_sampler->ProfileRecordingStarting(&profile.modules); | |
513 } | |
514 | |
515 // The currently active profile being captured. | |
516 CallStackProfile& profile = collection->profiles.back(); | |
517 | |
518 // Record a single sample. | |
519 profile.samples.push_back(Sample()); | |
520 collection->native_sampler->RecordStackSample(&profile.samples.back()); | |
521 | |
522 // If this is the last sample of a burst, record the total time. | |
523 if (collection->sample == collection->params.samples_per_burst - 1) { | |
524 profile.profile_duration = Time::Now() - collection->profile_start_time; | |
525 collection->native_sampler->ProfileRecordingStopped(); | |
526 } | |
527 } | |
528 | |
529 void StackSamplingProfiler::SamplingThread::ScheduleShutdownIfIdle() { | |
530 if (!active_collections_.empty()) | |
531 return; | |
532 | |
533 int add_events; | |
534 { | |
535 AutoLock lock(thread_execution_state_lock_); | |
536 if (thread_execution_state_disable_idle_shutdown_for_testing_) | |
537 return; | |
538 add_events = thread_execution_state_add_events_; | |
539 } | |
540 | |
541 GetTaskRunnerOnSamplingThread()->PostDelayedTask( | |
542 FROM_HERE, | |
543 Bind(&SamplingThread::ShutdownTask, Unretained(this), add_events), | |
544 TimeDelta::FromSeconds(60)); | |
545 } | |
546 | |
547 void StackSamplingProfiler::SamplingThread::AddCollectionTask( | |
548 std::unique_ptr<CollectionContext> collection) { | |
549 const int collection_id = collection->collection_id; | |
550 const TimeDelta initial_delay = collection->params.initial_delay; | |
551 | |
552 active_collections_.insert( | |
553 std::make_pair(collection_id, std::move(collection))); | |
554 | |
555 GetTaskRunnerOnSamplingThread()->PostDelayedTask( | |
556 FROM_HERE, | |
557 Bind(&SamplingThread::PerformCollectionTask, Unretained(this), | |
558 collection_id), | |
559 initial_delay); | |
560 | |
561 // Another increment of "add events" serves to invalidate any pending | |
562 // shutdown tasks that may have been initiated between the Add() and this | |
563 // task running. | |
564 { | |
565 AutoLock lock(thread_execution_state_lock_); | |
566 ++thread_execution_state_add_events_; | |
567 } | |
568 } | |
569 | |
570 void StackSamplingProfiler::SamplingThread::RemoveCollectionTask(int id) { | |
571 auto found = active_collections_.find(id); | |
572 if (found == active_collections_.end()) | |
573 return; | |
574 | |
575 FinishCollection(found->second.get()); | |
576 ScheduleShutdownIfIdle(); | |
577 } | |
578 | |
579 void StackSamplingProfiler::SamplingThread::PerformCollectionTask(int id) { | |
580 auto found = active_collections_.find(id); | |
581 | |
582 // The task won't be found if it has been stopped. | |
583 if (found == active_collections_.end()) | |
584 return; | |
585 | |
586 CollectionContext* collection = found->second.get(); | |
587 | |
588 // Handle first-run with no "next time". | |
589 if (collection->next_sample_time == Time()) | |
590 collection->next_sample_time = Time::Now(); | |
591 | |
592 // Do the collection of a single sample. | |
593 RecordSample(collection); | |
594 | |
595 // Update the time of the next sample recording. | |
596 if (UpdateNextSampleTime(collection)) { | |
597 bool success = GetTaskRunnerOnSamplingThread()->PostDelayedTask( | |
598 FROM_HERE, | |
599 Bind(&SamplingThread::PerformCollectionTask, Unretained(this), id), | |
600 std::max(collection->next_sample_time - Time::Now(), TimeDelta())); | |
601 DCHECK(success); | |
602 } else { | |
603 // All capturing has completed so finish the collection. Let object expire. | |
Mike Wittman
2017/03/31 18:12:33
nit: clarify the meaning of "Let object expire."
bcwhite
2017/04/03 20:18:13
Done.
| |
604 // The |collection| variable will be invalid after this call. | |
605 FinishCollection(collection); | |
606 ScheduleShutdownIfIdle(); | |
607 } | |
608 } | |
609 | |
610 void StackSamplingProfiler::SamplingThread::ShutdownTask(int add_events) { | |
611 // Holding this lock ensures that any attempt to start another job will | |
612 // get postponed until thread_execution_state_task_runner_ is cleared, thus | |
613 // eliminating the race. | |
Mike Wittman
2017/03/31 18:12:33
mention what the race is
bcwhite
2017/04/03 20:18:13
Done.
| |
614 AutoLock lock(thread_execution_state_lock_); | |
615 | |
616 // If the current count of creation requests doesn't match the passed count | |
617 // then other tasks have been created since this was posted. Abort shutdown. | |
618 if (thread_execution_state_add_events_ != add_events) | |
619 return; | |
620 | |
621 // There can be no new AddCollectionTasks at this point because creating | |
622 // those always increments "add events". There may be other requests, like | |
623 // Remove, but it's okay to schedule the thread to stop once they've been | |
624 // executed (i.e. "soon"). | |
625 DCHECK(active_collections_.empty()); | |
626 StopSoon(); | |
627 | |
628 // StopSoon will have set the owning sequence (again) so it must be detached | |
629 // (again) in order for Stop/Start to be called (again) should more work | |
630 // come in. Holding the |thread_execution_state_lock_| ensures the necessary | |
631 // happens-after with regard to this detach and future Thread API calls. | |
632 DetachFromSequence(); | |
633 | |
634 // Set the thread_state variable so the thread will be restarted when new | |
635 // work comes in. Remove the thread_execution_state_task_runner_ to avoid | |
636 // confusion. | |
637 thread_execution_state_ = EXITING; | |
638 thread_execution_state_task_runner_ = nullptr; | |
639 } | |
640 | |
641 bool StackSamplingProfiler::SamplingThread::UpdateNextSampleTime( | |
642 CollectionContext* collection) { | |
643 // This will keep a consistent average interval between samples but will | |
644 // result in constant series of acquisitions, thus nearly locking out the | |
645 // target thread, if the interval is smaller than the time it takes to | |
646 // actually acquire the sample. Anything sampling that quickly is going | |
647 // to be a problem anyway so don't worry about it. | |
648 if (++collection->sample < collection->params.samples_per_burst) { | |
649 collection->next_sample_time += collection->params.sampling_interval; | |
650 return true; | |
651 } | |
652 | |
653 if (++collection->burst < collection->params.bursts) { | |
654 collection->sample = 0; | |
655 collection->next_sample_time += collection->params.burst_interval; | |
656 return true; | |
657 } | |
658 | |
659 return false; | |
660 } | |
661 | |
662 void StackSamplingProfiler::SamplingThread::CleanUp() { | |
663 // There should be no collections remaining when the thread stops. | |
664 DCHECK(active_collections_.empty()); | |
665 | |
666 // Let the parent clean up. | |
667 Thread::CleanUp(); | |
668 } | |
669 | |
670 // StackSamplingProfiler ------------------------------------------------------ | |
671 | |
672 // static | |
673 void StackSamplingProfiler::TestAPI::Reset() { | |
674 SamplingThread::TestAPI::Reset(); | |
675 ResetAnnotations(); | |
676 } | |
677 | |
678 // static | |
679 void StackSamplingProfiler::TestAPI::ResetAnnotations() { | |
680 subtle::NoBarrier_Store(&process_milestones_, 0u); | |
681 } | |
682 | |
683 // static | |
684 bool StackSamplingProfiler::TestAPI::IsSamplingThreadRunning() { | |
685 return SamplingThread::GetInstance()->IsRunning(); | |
686 } | |
687 | |
688 // static | |
689 void StackSamplingProfiler::TestAPI::DisableIdleShutdown() { | |
690 SamplingThread::TestAPI::DisableIdleShutdown(); | |
691 } | |
692 | |
693 // static | |
694 void StackSamplingProfiler::TestAPI::PerformSamplingThreadIdleShutdown( | |
695 bool simulate_intervening_start) { | |
696 SamplingThread::TestAPI::ShutdownAssumingIdle(simulate_intervening_start); | |
697 } | |
698 | |
699 subtle::Atomic32 StackSamplingProfiler::process_milestones_ = 0; | |
700 | |
701 StackSamplingProfiler::StackSamplingProfiler( | |
165 const SamplingParams& params, | 702 const SamplingParams& params, |
166 const CompletedCallback& completed_callback) | 703 const CompletedCallback& callback, |
167 : native_sampler_(std::move(native_sampler)), | 704 NativeStackSamplerTestDelegate* test_delegate) |
168 params_(params), | 705 : StackSamplingProfiler(base::PlatformThread::CurrentId(), |
169 stop_event_(WaitableEvent::ResetPolicy::AUTOMATIC, | 706 params, |
170 WaitableEvent::InitialState::NOT_SIGNALED), | 707 callback, |
171 completed_callback_(completed_callback) {} | 708 test_delegate) {} |
172 | |
173 StackSamplingProfiler::SamplingThread::~SamplingThread() {} | |
174 | |
175 void StackSamplingProfiler::SamplingThread::ThreadMain() { | |
176 PlatformThread::SetName("Chrome_SamplingProfilerThread"); | |
177 | |
178 // For now, just ignore any requests to profile while another profiler is | |
179 // working. | |
180 if (!concurrent_profiling_lock.Get().Try()) | |
181 return; | |
182 | |
183 CallStackProfiles profiles; | |
184 CollectProfiles(&profiles); | |
185 concurrent_profiling_lock.Get().Release(); | |
186 completed_callback_.Run(std::move(profiles)); | |
187 } | |
188 | |
189 // Depending on how long the sampling takes and the length of the sampling | |
190 // interval, a burst of samples could take arbitrarily longer than | |
191 // samples_per_burst * sampling_interval. In this case, we (somewhat | |
192 // arbitrarily) honor the number of samples requested rather than strictly | |
193 // adhering to the sampling intervals. Once we have established users for the | |
194 // StackSamplingProfiler and the collected data to judge, we may go the other | |
195 // way or make this behavior configurable. | |
196 void StackSamplingProfiler::SamplingThread::CollectProfile( | |
197 CallStackProfile* profile, | |
198 TimeDelta* elapsed_time, | |
199 bool* was_stopped) { | |
200 ElapsedTimer profile_timer; | |
201 native_sampler_->ProfileRecordingStarting(&profile->modules); | |
202 profile->sampling_period = params_.sampling_interval; | |
203 *was_stopped = false; | |
204 TimeDelta previous_elapsed_sample_time; | |
205 for (int i = 0; i < params_.samples_per_burst; ++i) { | |
206 if (i != 0) { | |
207 // Always wait, even if for 0 seconds, so we can observe a signal on | |
208 // stop_event_. | |
209 if (stop_event_.TimedWait( | |
210 std::max(params_.sampling_interval - previous_elapsed_sample_time, | |
211 TimeDelta()))) { | |
212 *was_stopped = true; | |
213 break; | |
214 } | |
215 } | |
216 ElapsedTimer sample_timer; | |
217 profile->samples.push_back(Sample()); | |
218 native_sampler_->RecordStackSample(&profile->samples.back()); | |
219 previous_elapsed_sample_time = sample_timer.Elapsed(); | |
220 } | |
221 | |
222 *elapsed_time = profile_timer.Elapsed(); | |
223 profile->profile_duration = *elapsed_time; | |
224 native_sampler_->ProfileRecordingStopped(); | |
225 } | |
226 | |
227 // In an analogous manner to CollectProfile() and samples exceeding the expected | |
228 // total sampling time, bursts may also exceed the burst_interval. We adopt the | |
229 // same wait-and-see approach here. | |
230 void StackSamplingProfiler::SamplingThread::CollectProfiles( | |
231 CallStackProfiles* profiles) { | |
232 if (stop_event_.TimedWait(params_.initial_delay)) | |
233 return; | |
234 | |
235 TimeDelta previous_elapsed_profile_time; | |
236 for (int i = 0; i < params_.bursts; ++i) { | |
237 if (i != 0) { | |
238 // Always wait, even if for 0 seconds, so we can observe a signal on | |
239 // stop_event_. | |
240 if (stop_event_.TimedWait( | |
241 std::max(params_.burst_interval - previous_elapsed_profile_time, | |
242 TimeDelta()))) | |
243 return; | |
244 } | |
245 | |
246 CallStackProfile profile; | |
247 bool was_stopped = false; | |
248 CollectProfile(&profile, &previous_elapsed_profile_time, &was_stopped); | |
249 if (!profile.samples.empty()) | |
250 profiles->push_back(std::move(profile)); | |
251 | |
252 if (was_stopped) | |
253 return; | |
254 } | |
255 } | |
256 | |
257 void StackSamplingProfiler::SamplingThread::Stop() { | |
258 stop_event_.Signal(); | |
259 } | |
260 | |
261 // StackSamplingProfiler ------------------------------------------------------ | |
262 | |
263 subtle::Atomic32 StackSamplingProfiler::process_milestones_ = 0; | |
264 | |
265 StackSamplingProfiler::StackSamplingProfiler( | |
266 PlatformThreadId thread_id, | |
267 const SamplingParams& params, | |
268 const CompletedCallback& callback) | |
269 : StackSamplingProfiler(thread_id, params, callback, nullptr) {} | |
270 | 709 |
271 StackSamplingProfiler::StackSamplingProfiler( | 710 StackSamplingProfiler::StackSamplingProfiler( |
272 PlatformThreadId thread_id, | 711 PlatformThreadId thread_id, |
273 const SamplingParams& params, | 712 const SamplingParams& params, |
274 const CompletedCallback& callback, | 713 const CompletedCallback& callback, |
275 NativeStackSamplerTestDelegate* test_delegate) | 714 NativeStackSamplerTestDelegate* test_delegate) |
276 : thread_id_(thread_id), params_(params), completed_callback_(callback), | 715 : thread_id_(thread_id), |
277 test_delegate_(test_delegate) { | 716 params_(params), |
278 } | 717 completed_callback_(callback), |
718 // The event starts "signaled" so code knows it's safe to start thread. | |
719 profiling_inactive_(WaitableEvent::ResetPolicy::MANUAL, | |
720 WaitableEvent::InitialState::SIGNALED), | |
721 collection_id_(NULL_COLLECTION_ID), | |
722 test_delegate_(test_delegate) {} | |
279 | 723 |
280 StackSamplingProfiler::~StackSamplingProfiler() { | 724 StackSamplingProfiler::~StackSamplingProfiler() { |
725 // Stop is immediate but asynchronous. There is a non-zero probability that | |
726 // one more sample will be taken after this call returns. | |
281 Stop(); | 727 Stop(); |
282 if (!sampling_thread_handle_.is_null()) | 728 |
283 PlatformThread::Join(sampling_thread_handle_); | 729 // The behavior of sampling a thread that has exited is undefined and could |
284 } | 730 // cause Bad Things(tm) to occur. The safety model provided by this class is |
285 | 731 // that an instance of this object is expected to live at least as long as |
286 // static | 732 // the thread it is sampling. However, because the sampling is performed |
287 void StackSamplingProfiler::StartAndRunAsync( | 733 // asynchronously by the SamplingThread, there is no way to guarantee this |
288 PlatformThreadId thread_id, | 734 // is true without waiting for it to signal that it has finished. |
289 const SamplingParams& params, | 735 // |
290 const CompletedCallback& callback) { | 736 // The wait time should, at most, be only as long as it takes to collect one |
291 CHECK(ThreadTaskRunnerHandle::Get()); | 737 // sample (~200us) or none at all if sampling has already completed. |
292 AsyncRunner::Run(thread_id, params, callback); | 738 ThreadRestrictions::ScopedAllowWait allow_wait; |
739 profiling_inactive_.Wait(); | |
293 } | 740 } |
294 | 741 |
295 void StackSamplingProfiler::Start() { | 742 void StackSamplingProfiler::Start() { |
296 if (completed_callback_.is_null()) | 743 if (completed_callback_.is_null()) |
297 return; | 744 return; |
298 | 745 |
299 std::unique_ptr<NativeStackSampler> native_sampler = | 746 std::unique_ptr<NativeStackSampler> native_sampler = |
300 NativeStackSampler::Create(thread_id_, &RecordAnnotations, | 747 NativeStackSampler::Create(thread_id_, &RecordAnnotations, |
301 test_delegate_); | 748 test_delegate_); |
749 | |
302 if (!native_sampler) | 750 if (!native_sampler) |
303 return; | 751 return; |
304 | 752 |
305 sampling_thread_.reset(new SamplingThread(std::move(native_sampler), params_, | 753 // Wait for profiling to be "inactive", then reset it for the upcoming run. |
306 completed_callback_)); | 754 profiling_inactive_.Wait(); |
307 if (!PlatformThread::Create(0, sampling_thread_.get(), | 755 profiling_inactive_.Reset(); |
308 &sampling_thread_handle_)) | 756 |
309 sampling_thread_.reset(); | 757 DCHECK_EQ(NULL_COLLECTION_ID, collection_id_); |
758 collection_id_ = SamplingThread::GetInstance()->Add( | |
759 MakeUnique<SamplingThread::CollectionContext>( | |
760 thread_id_, params_, completed_callback_, &profiling_inactive_, | |
761 std::move(native_sampler))); | |
762 DCHECK_NE(NULL_COLLECTION_ID, collection_id_); | |
310 } | 763 } |
311 | 764 |
312 void StackSamplingProfiler::Stop() { | 765 void StackSamplingProfiler::Stop() { |
313 if (sampling_thread_) | 766 SamplingThread::GetInstance()->Remove(collection_id_); |
314 sampling_thread_->Stop(); | 767 collection_id_ = NULL_COLLECTION_ID; |
315 } | 768 } |
316 | 769 |
317 // static | 770 // static |
318 void StackSamplingProfiler::SetProcessMilestone(int milestone) { | 771 void StackSamplingProfiler::SetProcessMilestone(int milestone) { |
319 DCHECK_LE(0, milestone); | 772 DCHECK_LE(0, milestone); |
320 DCHECK_GT(static_cast<int>(sizeof(process_milestones_) * 8), milestone); | 773 DCHECK_GT(static_cast<int>(sizeof(process_milestones_) * 8), milestone); |
321 DCHECK_EQ(0, subtle::NoBarrier_Load(&process_milestones_) & (1 << milestone)); | 774 DCHECK_EQ(0, subtle::NoBarrier_Load(&process_milestones_) & (1 << milestone)); |
322 ChangeAtomicFlags(&process_milestones_, 1 << milestone, 0); | 775 ChangeAtomicFlags(&process_milestones_, 1 << milestone, 0); |
323 } | 776 } |
324 | 777 |
325 // static | 778 // static |
326 void StackSamplingProfiler::ResetAnnotationsForTesting() { | |
327 subtle::NoBarrier_Store(&process_milestones_, 0u); | |
328 } | |
329 | |
330 // static | |
331 void StackSamplingProfiler::RecordAnnotations(Sample* sample) { | 779 void StackSamplingProfiler::RecordAnnotations(Sample* sample) { |
332 // The code inside this method must not do anything that could acquire a | 780 // The code inside this method must not do anything that could acquire a |
333 // mutex, including allocating memory (which includes LOG messages) because | 781 // mutex, including allocating memory (which includes LOG messages) because |
334 // that mutex could be held by a stopped thread, thus resulting in deadlock. | 782 // that mutex could be held by a stopped thread, thus resulting in deadlock. |
335 sample->process_milestones = subtle::NoBarrier_Load(&process_milestones_); | 783 sample->process_milestones = subtle::NoBarrier_Load(&process_milestones_); |
336 } | 784 } |
337 | 785 |
338 // StackSamplingProfiler::Frame global functions ------------------------------ | 786 // StackSamplingProfiler::Frame global functions ------------------------------ |
339 | 787 |
340 bool operator==(const StackSamplingProfiler::Module& a, | 788 bool operator==(const StackSamplingProfiler::Module& a, |
(...skipping 29 matching lines...) Expand all Loading... | |
370 } | 818 } |
371 | 819 |
372 bool operator<(const StackSamplingProfiler::Frame &a, | 820 bool operator<(const StackSamplingProfiler::Frame &a, |
373 const StackSamplingProfiler::Frame &b) { | 821 const StackSamplingProfiler::Frame &b) { |
374 return (a.module_index < b.module_index) || | 822 return (a.module_index < b.module_index) || |
375 (a.module_index == b.module_index && | 823 (a.module_index == b.module_index && |
376 a.instruction_pointer < b.instruction_pointer); | 824 a.instruction_pointer < b.instruction_pointer); |
377 } | 825 } |
378 | 826 |
379 } // namespace base | 827 } // namespace base |
OLD | NEW |