Chromium Code Reviews| 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 // Disables inherent idle-shutdown behavior. | |
| 119 static void DisableIdleShutdown(); | |
| 120 | |
| 121 // Begins an idle shutdown as if the idle-timer had expired. | |
| 122 static void ShutdownIfIdle(); | |
| 123 }; | |
| 124 | |
| 125 struct CollectionContext { | |
| 126 CollectionContext(PlatformThreadId target, | |
| 127 const SamplingParams& params, | |
| 128 const CompletedCallback& callback, | |
| 129 WaitableEvent* finished, | |
| 130 std::unique_ptr<NativeStackSampler> sampler) | |
| 131 : collection_id(next_collection_id_.GetNext()), | |
| 132 target(target), | |
| 133 params(params), | |
| 134 callback(callback), | |
| 135 finished(finished), | |
| 136 native_sampler(std::move(sampler)) {} | |
| 137 ~CollectionContext() {} | |
| 138 | |
| 139 // An identifier for this collection, used to uniquely identify it to | |
| 140 // outside interests. | |
| 141 const int collection_id; | |
| 142 | |
| 143 const PlatformThreadId target; // ID of The thread being sampled. | |
| 144 const SamplingParams params; // Information about how to sample. | |
| 145 const CompletedCallback callback; // Callback made when sampling complete. | |
| 146 WaitableEvent* const finished; // Signaled when all sampling complete. | |
| 147 | |
| 148 // Platform-specific module that does the actual sampling. | |
| 149 std::unique_ptr<NativeStackSampler> native_sampler; | |
| 150 | |
| 151 // The absolute time for the next sample. | |
| 152 Time next_sample_time; | |
| 153 | |
| 154 // The time that a profile was started, for calculating the total duration. | |
| 155 Time profile_start_time; | |
| 156 | |
| 157 // Counters that indicate the current position along the acquisition. | |
| 158 int burst = 0; | |
| 159 int sample = 0; | |
| 160 | |
| 161 // The collected stack samples. The active profile is always at the back(). | |
| 162 CallStackProfiles profiles; | |
| 163 | |
| 164 private: | |
| 165 static StaticAtomicSequenceNumber next_collection_id_; | |
| 166 }; | |
| 167 | |
| 168 // Gets the single instance of this class. | |
| 169 static SamplingThread* GetInstance(); | |
| 170 | |
| 171 // Starts the thread. | |
| 172 void Start(); | |
| 173 | |
| 174 // Adds a new CollectionContext to the thread. This can be called externally | |
| 175 // from any thread. This returns an ID that can later be used to stop | |
| 176 // the sampling. | |
| 177 int Add(std::unique_ptr<CollectionContext> collection); | |
| 178 | |
| 179 // Removes an active collection based on its ID, forcing it to run its | |
| 180 // callback if any data has been collected. This can be called externally | |
| 181 // from any thread. | |
| 182 void Remove(int id); | |
| 183 | |
| 184 private: | |
| 185 friend class TestAPI; | |
| 186 friend struct DefaultSingletonTraits<SamplingThread>; | |
| 187 | |
| 188 // The different states in which the sampling-thread can be. | |
| 189 enum ThreadExecutionState { | |
| 190 // The thread is not running because it has never been started. It will be | |
| 191 // started when a sampling request is received. | |
| 192 NOT_STARTED, | |
| 193 | |
| 194 // The thread is running and processing tasks. This is the state when any | |
| 195 // sampling requests are active and during the "idle" period afterward | |
| 196 // before the thread is stopped. | |
| 197 RUNNING, | |
| 198 | |
| 199 // Once all sampling requests have finished and the "idle" period has | |
| 200 // expired, the thread will be set to this state and its shutdown | |
| 201 // initiated. A call to Stop() must be made to ensure the previous thread | |
| 202 // has completely exited before calling Start() and moving back to the | |
| 203 // RUNNING state. | |
| 204 EXITING, | |
| 205 }; | |
| 206 | |
| 207 SamplingThread(); | |
| 208 ~SamplingThread() override; | |
| 209 | |
| 210 // Get task runner that is usable from the outside. | |
| 211 scoped_refptr<SingleThreadTaskRunner> GetOrCreateTaskRunnerForAdd(); | |
| 212 scoped_refptr<SingleThreadTaskRunner> GetTaskRunner( | |
| 213 ThreadExecutionState* out_state); | |
| 214 | |
| 215 // Get task runner that is usable from the sampling thread itself. | |
| 216 scoped_refptr<SingleThreadTaskRunner> GetTaskRunnerOnSamplingThread(); | |
| 217 | |
| 218 // Finishes a collection and reports collected data via callback. | |
| 219 void FinishCollection(CollectionContext* collection); | |
| 220 | |
| 221 // Records a single sample of a collection. | |
| 222 void RecordSample(CollectionContext* collection); | |
| 223 | |
| 224 // Check if the sampling thread is idle and begin a shutdown if so. | |
| 225 void ScheduleShutdownIfIdle(); | |
| 226 | |
| 227 // These methods are tasks that get posted to the internal message queue. | |
| 228 void AddCollectionTask(std::unique_ptr<CollectionContext> collection); | |
| 229 void RemoveCollectionTask(int id); | |
| 230 void PerformCollectionTask(int id); | |
| 231 void ShutdownTask(int add_events); | |
| 232 | |
| 233 // Updates the |next_sample_time| time based on configured parameters. | |
| 234 bool UpdateNextSampleTime(CollectionContext* collection); | |
| 235 | |
| 236 // Thread: | |
| 237 void CleanUp() override; | |
| 238 | |
| 239 // The task-runner for the sampling thread and some information about it. | |
| 240 // This must always be accessed while holding the lock. The saved task-runner | |
| 241 // can be freely used by any calling thread. | |
| 242 Lock task_runner_lock_; // Protects all task_runner_* fields. | |
| 243 scoped_refptr<SingleThreadTaskRunner> task_runner_; | |
| 244 ThreadExecutionState task_runner_thread_state_ = NOT_STARTED; | |
| 245 bool task_runner_disable_idle_shutdown_for_testing_ = false; | |
| 246 | |
| 247 // A counter that notes adds of new collection requests. It is incremented | |
| 248 // when changes occur so that delayed shutdown tasks are able to detect if | |
| 249 // samething new has happened while it was waiting. Like all |task_runner_*| | |
| 250 // vars, this must be accessed while holding |task_runner_lock_|. | |
| 251 int task_runner_add_events_ = 0; | |
| 252 | |
| 253 // A map of IDs to collection contexts. Because this class is a singleton | |
| 254 // that is never destroyed, context objects will never be destructed except | |
| 255 // by explicit action. Thus, it's acceptable to pass unretained pointers | |
| 256 // to these objects when posting tasks. | |
| 257 std::map<int, std::unique_ptr<CollectionContext>> active_collections_; | |
| 258 | |
| 259 DISALLOW_COPY_AND_ASSIGN(SamplingThread); | |
| 260 }; | |
| 261 | |
| 262 void StackSamplingProfiler::SamplingThread::TestAPI::DisableIdleShutdown() { | |
| 263 SamplingThread* sampler = SamplingThread::GetInstance(); | |
| 264 DCHECK(sampler); | |
| 265 | |
| 266 { | |
| 267 AutoLock lock(sampler->task_runner_lock_); | |
| 268 sampler->task_runner_disable_idle_shutdown_for_testing_ = true; | |
| 269 } | |
| 270 } | |
| 271 | |
| 272 void StackSamplingProfiler::SamplingThread::TestAPI::ShutdownIfIdle() { | |
| 273 SamplingThread* sampler = SamplingThread::GetInstance(); | |
| 274 DCHECK(sampler); | |
| 275 | |
| 276 ThreadExecutionState state; | |
| 277 scoped_refptr<SingleThreadTaskRunner> task_runner = | |
| 278 sampler->GetTaskRunner(&state); | |
| 279 DCHECK_EQ(RUNNING, state); | |
| 280 DCHECK(task_runner); | |
| 281 | |
| 282 int add_events; | |
| 283 { | |
| 284 AutoLock lock(sampler->task_runner_lock_); | |
| 285 add_events = sampler->task_runner_add_events_; | |
| 286 } | |
| 287 | |
| 288 // ShutdownTask will check if the thread is idle and skip the shutdown if not. | |
| 289 task_runner->PostTask(FROM_HERE, Bind(&SamplingThread::ShutdownTask, | |
| 290 Unretained(sampler), add_events)); | |
| 291 } | |
| 292 | |
| 293 StaticAtomicSequenceNumber StackSamplingProfiler::SamplingThread:: | |
| 294 CollectionContext::next_collection_id_; | |
| 295 | |
| 296 StackSamplingProfiler::SamplingThread::SamplingThread() | |
| 297 : Thread("Chrome_SamplingProfilerThread") {} | |
| 298 | |
| 299 StackSamplingProfiler::SamplingThread::~SamplingThread() { | |
| 300 Stop(); | |
| 301 } | |
| 302 | |
| 303 StackSamplingProfiler::SamplingThread* | |
| 304 StackSamplingProfiler::SamplingThread::GetInstance() { | |
| 305 return Singleton<SamplingThread, LeakySingletonTraits<SamplingThread>>::get(); | |
| 306 } | |
| 307 | |
| 308 void StackSamplingProfiler::SamplingThread::Start() { | |
| 309 Thread::Options options; | |
| 310 // Use a higher priority for a more accurate sampling interval. | |
| 311 options.priority = ThreadPriority::DISPLAY; | |
| 312 Thread::StartWithOptions(options); | |
| 313 } | |
| 314 | |
| 315 int StackSamplingProfiler::SamplingThread::Add( | |
| 316 std::unique_ptr<CollectionContext> collection) { | |
| 317 int id = collection->collection_id; | |
| 318 scoped_refptr<SingleThreadTaskRunner> task_runner = | |
| 319 GetOrCreateTaskRunnerForAdd(); | |
| 320 | |
| 321 task_runner->PostTask(FROM_HERE, Bind(&SamplingThread::AddCollectionTask, | |
| 322 Unretained(this), Passed(&collection))); | |
| 323 | |
| 324 return id; | |
| 325 } | |
| 326 | |
| 327 void StackSamplingProfiler::SamplingThread::Remove(int id) { | |
| 328 ThreadExecutionState state; | |
| 329 scoped_refptr<SingleThreadTaskRunner> task_runner = GetTaskRunner(&state); | |
| 330 DCHECK_NE(NOT_STARTED, state); | |
| 331 if (state != RUNNING) | |
| 332 return; | |
| 333 DCHECK(task_runner); | |
| 334 | |
| 335 // This can fail if the thread were to exit between acquisition of the task | |
| 336 // runner above and the call below. In that case, however, everything has | |
| 337 // stopped so there's no need to try to stop it. | |
| 338 task_runner->PostTask(FROM_HERE, Bind(&SamplingThread::RemoveCollectionTask, | |
| 339 Unretained(this), id)); | |
| 340 } | |
| 341 | |
| 342 scoped_refptr<SingleThreadTaskRunner> | |
| 343 StackSamplingProfiler::SamplingThread::GetOrCreateTaskRunnerForAdd() { | |
| 344 AutoLock lock(task_runner_lock_); | |
| 345 | |
| 346 // The increment of the "add events" count is why this method is to be only | |
| 347 // called from "add". | |
| 348 ++task_runner_add_events_; | |
| 349 | |
| 350 if (task_runner_thread_state_ == RUNNING) { | |
| 351 DCHECK(task_runner_); | |
| 352 // This shouldn't be called from the sampling thread as it's inefficient. | |
| 353 // Use GetTaskRunnerOnSamplingThread() instead. | |
| 354 DCHECK_NE(GetThreadId(), PlatformThread::CurrentId()); | |
| 355 return task_runner_; | |
| 356 } | |
| 357 | |
| 358 if (task_runner_thread_state_ == EXITING) { | |
| 359 // The previous instance has only been partially cleaned up. It is necessary | |
| 360 // to call Stop() before Start(). | |
| 361 Stop(); | |
| 362 } | |
| 363 | |
| 364 // The thread is not running. Start it and get associated runner. The task- | |
| 365 // runner has to be saved for future use because though it can be used from | |
| 366 // any thread, it can be acquired via task_runner() only on the created | |
| 367 // thread and the thread that creates it (i.e. this thread). | |
| 368 Start(); | |
| 369 task_runner_thread_state_ = RUNNING; | |
| 370 task_runner_ = Thread::task_runner(); | |
| 371 | |
| 372 // Detach the sampling thread from the "sequence" (i.e. thread) that | |
| 373 // started it so that it can be self-managed or stopped by another thread. | |
| 374 DetachFromSequence(); | |
| 375 | |
| 376 return task_runner_; | |
| 377 } | |
| 378 | |
| 379 scoped_refptr<SingleThreadTaskRunner> | |
| 380 StackSamplingProfiler::SamplingThread::GetTaskRunner( | |
| 381 ThreadExecutionState* out_state) { | |
| 382 AutoLock lock(task_runner_lock_); | |
| 383 if (out_state) | |
| 384 *out_state = task_runner_thread_state_; | |
| 385 if (task_runner_thread_state_ == RUNNING) { | |
| 386 // This shouldn't be called from the sampling thread as it's inefficient. | |
| 387 // Use GetTaskRunnerOnSamplingThread() instead. | |
| 388 DCHECK_NE(GetThreadId(), PlatformThread::CurrentId()); | |
| 389 DCHECK(task_runner_); | |
| 390 } else { | |
| 391 DCHECK(!task_runner_); | |
| 392 } | |
| 393 | |
| 394 return task_runner_; | |
| 395 } | |
| 396 | |
| 397 scoped_refptr<SingleThreadTaskRunner> | |
| 398 StackSamplingProfiler::SamplingThread::GetTaskRunnerOnSamplingThread() { | |
| 399 // This should be called only from the sampling thread as it has limited | |
| 400 // accessibility. | |
| 401 DCHECK_EQ(GetThreadId(), PlatformThread::CurrentId()); | |
| 402 | |
| 403 return Thread::task_runner(); | |
| 404 } | |
| 405 | |
| 406 void StackSamplingProfiler::SamplingThread::FinishCollection( | |
| 407 CollectionContext* collection) { | |
| 408 // If there is no duration for the final profile (because it was stopped), | |
| 409 // calculate it now. | |
| 410 if (!collection->profiles.empty() && | |
| 411 collection->profiles.back().profile_duration == TimeDelta()) { | |
| 412 collection->profiles.back().profile_duration = | |
| 413 Time::Now() - collection->profile_start_time; | |
| 414 } | |
| 415 | |
| 416 // Run the associated callback, passing the collected profiles. It's okay to | |
| 417 // move them because this collection is about to be deleted. | |
| 418 collection->callback.Run(std::move(collection->profiles)); | |
| 419 | |
| 420 // Signal that this collection is finished. | |
| 421 collection->finished->Signal(); | |
| 422 | |
| 423 // Remove this collection from the map of known ones. This must be done | |
| 424 // last as the |collection| parameter is invalid after this point. | |
| 425 size_t count = active_collections_.erase(collection->collection_id); | |
| 426 DCHECK_EQ(1U, count); | |
| 427 } | |
| 428 | |
| 429 void StackSamplingProfiler::SamplingThread::RecordSample( | |
| 430 CollectionContext* collection) { | |
| 431 DCHECK(collection->native_sampler); | |
| 432 | |
| 433 // If this is the first sample of a burst, a new Profile needs to be created | |
| 434 // and filled. | |
| 435 if (collection->sample == 0) { | |
| 436 collection->profiles.push_back(CallStackProfile()); | |
| 437 CallStackProfile& profile = collection->profiles.back(); | |
| 438 profile.sampling_period = collection->params.sampling_interval; | |
| 439 collection->profile_start_time = Time::Now(); | |
| 440 collection->native_sampler->ProfileRecordingStarting(&profile.modules); | |
| 441 } | |
| 442 | |
| 443 // The currently active profile being captured. | |
| 444 CallStackProfile& profile = collection->profiles.back(); | |
| 445 | |
| 446 // Record a single sample. | |
| 447 profile.samples.push_back(Sample()); | |
| 448 collection->native_sampler->RecordStackSample(&profile.samples.back()); | |
| 449 | |
| 450 // If this is the last sample of a burst, record the total time. | |
| 451 if (collection->sample == collection->params.samples_per_burst - 1) { | |
| 452 profile.profile_duration = Time::Now() - collection->profile_start_time; | |
| 453 collection->native_sampler->ProfileRecordingStopped(); | |
| 454 } | |
| 455 } | |
| 456 | |
| 457 void StackSamplingProfiler::SamplingThread::ScheduleShutdownIfIdle() { | |
| 458 if (!active_collections_.empty()) | |
| 459 return; | |
| 460 | |
| 461 int add_events; | |
| 462 { | |
| 463 AutoLock lock(task_runner_lock_); | |
| 464 if (task_runner_disable_idle_shutdown_for_testing_) | |
| 465 return; | |
| 466 add_events = task_runner_add_events_; | |
| 467 } | |
| 468 | |
| 469 GetTaskRunnerOnSamplingThread()->PostDelayedTask( | |
| 470 FROM_HERE, | |
| 471 Bind(&SamplingThread::ShutdownTask, Unretained(this), add_events), | |
| 472 TimeDelta::FromSeconds(60)); | |
| 473 } | |
| 474 | |
| 475 void StackSamplingProfiler::SamplingThread::AddCollectionTask( | |
| 476 std::unique_ptr<CollectionContext> collection) { | |
| 477 const int collection_id = collection->collection_id; | |
| 478 const TimeDelta initial_delay = collection->params.initial_delay; | |
| 479 | |
| 480 active_collections_.insert( | |
| 481 std::make_pair(collection_id, std::move(collection))); | |
| 482 | |
| 483 GetTaskRunnerOnSamplingThread()->PostDelayedTask( | |
| 484 FROM_HERE, | |
| 485 Bind(&SamplingThread::PerformCollectionTask, Unretained(this), | |
| 486 collection_id), | |
| 487 initial_delay); | |
| 488 | |
| 489 // Another increment of "add events" serves to invalidate any pending | |
| 490 // shutdown tasks that may have been initiated between the Add() and this | |
| 491 // task running. | |
| 492 { | |
| 493 AutoLock lock(task_runner_lock_); | |
| 494 ++task_runner_add_events_; | |
| 495 } | |
| 496 } | |
| 497 | |
| 498 void StackSamplingProfiler::SamplingThread::RemoveCollectionTask(int id) { | |
| 499 auto found = active_collections_.find(id); | |
| 500 if (found == active_collections_.end()) | |
| 501 return; | |
| 502 | |
| 503 FinishCollection(found->second.get()); | |
| 504 ScheduleShutdownIfIdle(); | |
| 505 } | |
| 506 | |
| 507 void StackSamplingProfiler::SamplingThread::PerformCollectionTask(int id) { | |
| 508 auto found = active_collections_.find(id); | |
| 509 | |
| 510 // The task won't be found if it has been stopped. | |
| 511 if (found == active_collections_.end()) | |
| 512 return; | |
| 513 | |
| 514 CollectionContext* collection = found->second.get(); | |
| 515 | |
| 516 // Handle first-run with no "next time". | |
| 517 if (collection->next_sample_time == Time()) | |
| 518 collection->next_sample_time = Time::Now(); | |
| 519 | |
| 520 // Do the collection of a single sample. | |
| 521 RecordSample(collection); | |
| 522 | |
| 523 // Update the time of the next sample recording. | |
| 524 if (UpdateNextSampleTime(collection)) { | |
| 525 bool success = GetTaskRunnerOnSamplingThread()->PostDelayedTask( | |
| 526 FROM_HERE, | |
| 527 Bind(&SamplingThread::PerformCollectionTask, Unretained(this), id), | |
| 528 std::max(collection->next_sample_time - Time::Now(), TimeDelta())); | |
| 529 DCHECK(success); | |
| 530 } else { | |
| 531 // All capturing has completed so finish the collection. Let object expire. | |
| 532 // The |collection| variable will be invalid after this call. | |
| 533 FinishCollection(collection); | |
| 534 ScheduleShutdownIfIdle(); | |
| 535 } | |
| 536 } | |
| 537 | |
| 538 void StackSamplingProfiler::SamplingThread::ShutdownTask(int add_events) { | |
| 539 // Stop here if not idle. This can be scheduled via the TestAPI which does | |
| 540 // not have access to pre-check if the thread is idle meaning that it has to | |
| 541 // be done here. | |
| 542 if (!active_collections_.empty()) | |
| 543 return; | |
|
Mike Wittman
2017/03/18 01:38:41
I don't think this conditional is needed.
Alterna
Mike Wittman
2017/03/20 14:59:40
I think we'll also need:
5. Invoke the callback a
bcwhite
2017/03/20 21:50:51
That puts a lot of burden and complexity on the te
Mike Wittman
2017/03/21 16:50:38
Why do we need this conditional at all at this poi
bcwhite
2017/03/22 17:48:54
Done.
| |
| 544 | |
| 545 // Holding this lock ensures that any attempt to start another job will | |
| 546 // get postponed until task_runner_ is cleared, thus eliminating the race. | |
| 547 AutoLock lock(task_runner_lock_); | |
| 548 | |
| 549 // If the current count of creation requests doesn't match the passed count | |
| 550 // then other tasks have been created since this was posted. Abort shutdown. | |
| 551 if (task_runner_add_events_ != add_events) | |
| 552 return; | |
| 553 | |
| 554 // There can be no new AddCollectionTasks at this point because creating | |
| 555 // those always increments "add events". There may be other requests, like | |
| 556 // Remove, but it's okay to schedule the thread to stop once they've been | |
| 557 // executed (i.e. "soon"). | |
| 558 StopSoon(); | |
| 559 | |
| 560 // StopSoon will have set the owning sequence (again) so it must be detached | |
| 561 // (again) in order for Stop/Start to be called (again) should more work | |
| 562 // come in. Holding the |task_runner_lock_| ensures the necessary happens- | |
| 563 // after with regard to this detach and future Thread API calls. | |
| 564 DetachFromSequence(); | |
| 565 | |
| 566 // Set the thread_state variable so the thread will be restarted when new | |
| 567 // work comes in. Remove the task_runner_ to avoid confusion. | |
| 568 task_runner_thread_state_ = EXITING; | |
| 569 task_runner_ = nullptr; | |
| 570 } | |
| 571 | |
| 572 bool StackSamplingProfiler::SamplingThread::UpdateNextSampleTime( | |
| 573 CollectionContext* collection) { | |
| 574 // This will keep a consistent average interval between samples but will | |
| 575 // result in constant series of acquisitions, thus nearly locking out the | |
| 576 // target thread, if the interval is smaller than the time it takes to | |
| 577 // actually acquire the sample. Anything sampling that quickly is going | |
| 578 // to be a problem anyway so don't worry about it. | |
| 579 if (++collection->sample < collection->params.samples_per_burst) { | |
| 580 collection->next_sample_time += collection->params.sampling_interval; | |
| 581 return true; | |
| 582 } | |
| 583 | |
| 584 if (++collection->burst < collection->params.bursts) { | |
| 585 collection->sample = 0; | |
| 586 collection->next_sample_time += collection->params.burst_interval; | |
| 587 return true; | |
| 588 } | |
| 589 | |
| 590 return false; | |
| 591 } | |
| 592 | |
| 593 void StackSamplingProfiler::SamplingThread::CleanUp() { | |
| 594 // There should be no collections remaining when the thread stops. | |
| 595 DCHECK(active_collections_.empty()); | |
| 596 | |
| 597 // Let the parent clean up. | |
| 598 Thread::CleanUp(); | |
| 599 } | |
| 600 | |
| 601 // StackSamplingProfiler ------------------------------------------------------ | |
| 602 | |
| 603 // static | |
| 604 bool StackSamplingProfiler::TestAPI::IsSamplingThreadRunning() { | |
| 605 return SamplingThread::GetInstance()->IsRunning(); | |
| 606 } | |
| 607 | |
| 608 // static | |
| 609 void StackSamplingProfiler::TestAPI::DisableIdleShutdown() { | |
| 610 SamplingThread::TestAPI::DisableIdleShutdown(); | |
| 611 } | |
| 612 | |
| 613 // static | |
| 614 void StackSamplingProfiler::TestAPI::InitiateSamplingThreadIdleShutdown() { | |
| 615 SamplingThread::TestAPI::ShutdownIfIdle(); | |
| 616 } | |
| 617 | |
| 618 subtle::Atomic32 StackSamplingProfiler::process_milestones_ = 0; | |
| 619 | |
| 620 StackSamplingProfiler::StackSamplingProfiler( | |
| 165 const SamplingParams& params, | 621 const SamplingParams& params, |
| 166 const CompletedCallback& completed_callback) | 622 const CompletedCallback& callback, |
| 167 : native_sampler_(std::move(native_sampler)), | 623 NativeStackSamplerTestDelegate* test_delegate) |
| 168 params_(params), | 624 : StackSamplingProfiler(base::PlatformThread::CurrentId(), |
| 169 stop_event_(WaitableEvent::ResetPolicy::AUTOMATIC, | 625 params, |
| 170 WaitableEvent::InitialState::NOT_SIGNALED), | 626 callback, |
| 171 completed_callback_(completed_callback) {} | 627 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 | 628 |
| 271 StackSamplingProfiler::StackSamplingProfiler( | 629 StackSamplingProfiler::StackSamplingProfiler( |
| 272 PlatformThreadId thread_id, | 630 PlatformThreadId thread_id, |
| 273 const SamplingParams& params, | 631 const SamplingParams& params, |
| 274 const CompletedCallback& callback, | 632 const CompletedCallback& callback, |
| 275 NativeStackSamplerTestDelegate* test_delegate) | 633 NativeStackSamplerTestDelegate* test_delegate) |
| 276 : thread_id_(thread_id), params_(params), completed_callback_(callback), | 634 : thread_id_(thread_id), |
| 277 test_delegate_(test_delegate) { | 635 params_(params), |
| 278 } | 636 completed_callback_(callback), |
| 637 // The event starts "signaled" so code knows it's safe to start thread. | |
| 638 profiling_inactive_(WaitableEvent::ResetPolicy::MANUAL, | |
| 639 WaitableEvent::InitialState::SIGNALED), | |
| 640 collection_id_(NULL_COLLECTION_ID), | |
| 641 test_delegate_(test_delegate) {} | |
| 279 | 642 |
| 280 StackSamplingProfiler::~StackSamplingProfiler() { | 643 StackSamplingProfiler::~StackSamplingProfiler() { |
| 644 // Stop is immediate but asynchronous. There is a non-zero probability that | |
| 645 // one more sample will be taken after this call returns. | |
| 281 Stop(); | 646 Stop(); |
| 282 if (!sampling_thread_handle_.is_null()) | 647 |
| 283 PlatformThread::Join(sampling_thread_handle_); | 648 // The behavior of sampling a thread that has exited is undefined and could |
| 284 } | 649 // cause Bad Things(tm) to occur. The safety model provided by this class is |
| 285 | 650 // that an instance of this object is expected to live at least as long as |
| 286 // static | 651 // the thread it is sampling. However, because the sampling is performed |
| 287 void StackSamplingProfiler::StartAndRunAsync( | 652 // asynchronously by the SamplingThread, there is no way to guarantee this |
| 288 PlatformThreadId thread_id, | 653 // is true without waiting for it to signal that it has finished. |
| 289 const SamplingParams& params, | 654 // |
| 290 const CompletedCallback& callback) { | 655 // The wait time should, at most, be only as long as it takes to collect one |
| 291 CHECK(ThreadTaskRunnerHandle::Get()); | 656 // sample (~200us) or none at all if sampling has already completed. |
| 292 AsyncRunner::Run(thread_id, params, callback); | 657 ThreadRestrictions::ScopedAllowWait allow_wait; |
| 658 profiling_inactive_.Wait(); | |
| 293 } | 659 } |
| 294 | 660 |
| 295 void StackSamplingProfiler::Start() { | 661 void StackSamplingProfiler::Start() { |
| 296 if (completed_callback_.is_null()) | 662 if (completed_callback_.is_null()) |
| 297 return; | 663 return; |
| 298 | 664 |
| 299 std::unique_ptr<NativeStackSampler> native_sampler = | 665 std::unique_ptr<NativeStackSampler> native_sampler = |
| 300 NativeStackSampler::Create(thread_id_, &RecordAnnotations, | 666 NativeStackSampler::Create(thread_id_, &RecordAnnotations, |
| 301 test_delegate_); | 667 test_delegate_); |
| 668 | |
| 302 if (!native_sampler) | 669 if (!native_sampler) |
| 303 return; | 670 return; |
| 304 | 671 |
| 305 sampling_thread_.reset(new SamplingThread(std::move(native_sampler), params_, | 672 // Wait for profiling to be "inactive", then reset it for the upcoming run. |
| 306 completed_callback_)); | 673 profiling_inactive_.Wait(); |
| 307 if (!PlatformThread::Create(0, sampling_thread_.get(), | 674 profiling_inactive_.Reset(); |
| 308 &sampling_thread_handle_)) | 675 |
| 309 sampling_thread_.reset(); | 676 DCHECK_EQ(NULL_COLLECTION_ID, collection_id_); |
| 677 collection_id_ = SamplingThread::GetInstance()->Add( | |
| 678 MakeUnique<SamplingThread::CollectionContext>( | |
| 679 thread_id_, params_, completed_callback_, &profiling_inactive_, | |
| 680 std::move(native_sampler))); | |
| 681 DCHECK_NE(NULL_COLLECTION_ID, collection_id_); | |
| 310 } | 682 } |
| 311 | 683 |
| 312 void StackSamplingProfiler::Stop() { | 684 void StackSamplingProfiler::Stop() { |
| 313 if (sampling_thread_) | 685 SamplingThread::GetInstance()->Remove(collection_id_); |
| 314 sampling_thread_->Stop(); | 686 collection_id_ = NULL_COLLECTION_ID; |
| 315 } | 687 } |
| 316 | 688 |
| 317 // static | 689 // static |
| 318 void StackSamplingProfiler::SetProcessMilestone(int milestone) { | 690 void StackSamplingProfiler::SetProcessMilestone(int milestone) { |
| 319 DCHECK_LE(0, milestone); | 691 DCHECK_LE(0, milestone); |
| 320 DCHECK_GT(static_cast<int>(sizeof(process_milestones_) * 8), milestone); | 692 DCHECK_GT(static_cast<int>(sizeof(process_milestones_) * 8), milestone); |
| 321 DCHECK_EQ(0, subtle::NoBarrier_Load(&process_milestones_) & (1 << milestone)); | 693 DCHECK_EQ(0, subtle::NoBarrier_Load(&process_milestones_) & (1 << milestone)); |
| 322 ChangeAtomicFlags(&process_milestones_, 1 << milestone, 0); | 694 ChangeAtomicFlags(&process_milestones_, 1 << milestone, 0); |
| 323 } | 695 } |
| 324 | 696 |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 370 } | 742 } |
| 371 | 743 |
| 372 bool operator<(const StackSamplingProfiler::Frame &a, | 744 bool operator<(const StackSamplingProfiler::Frame &a, |
| 373 const StackSamplingProfiler::Frame &b) { | 745 const StackSamplingProfiler::Frame &b) { |
| 374 return (a.module_index < b.module_index) || | 746 return (a.module_index < b.module_index) || |
| 375 (a.module_index == b.module_index && | 747 (a.module_index == b.module_index && |
| 376 a.instruction_pointer < b.instruction_pointer); | 748 a.instruction_pointer < b.instruction_pointer); |
| 377 } | 749 } |
| 378 | 750 |
| 379 } // namespace base | 751 } // namespace base |
| OLD | NEW |