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 <queue> | |
| 8 #include <utility> | 9 #include <utility> |
| 9 | 10 |
| 11 #include "base/atomicops.h" | |
| 10 #include "base/bind.h" | 12 #include "base/bind.h" |
| 11 #include "base/bind_helpers.h" | 13 #include "base/bind_helpers.h" |
| 12 #include "base/callback.h" | 14 #include "base/callback.h" |
| 13 #include "base/lazy_instance.h" | 15 #include "base/lazy_instance.h" |
| 14 #include "base/location.h" | 16 #include "base/location.h" |
| 15 #include "base/macros.h" | 17 #include "base/macros.h" |
| 18 #include "base/memory/ptr_util.h" | |
| 19 #include "base/memory/singleton.h" | |
| 20 #include "base/memory/weak_ptr.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" | |
| 18 #include "base/threading/thread_task_runner_handle.h" | 24 #include "base/threading/thread_task_runner_handle.h" |
| 19 #include "base/timer/elapsed_timer.h" | 25 #include "base/timer/elapsed_timer.h" |
| 20 | 26 |
| 21 namespace base { | 27 namespace base { |
| 22 | 28 |
| 23 namespace { | 29 namespace { |
| 24 | 30 |
| 25 // Used to ensure only one profiler is running at a time. | |
| 26 LazyInstance<Lock>::Leaky concurrent_profiling_lock = LAZY_INSTANCE_INITIALIZER; | |
| 27 | |
| 28 // AsyncRunner ---------------------------------------------------------------- | 31 // AsyncRunner ---------------------------------------------------------------- |
| 29 | 32 |
| 30 // Helper class to allow a profiler to be run completely asynchronously from the | 33 // Helper class to allow a profiler to be run completely asynchronously from the |
| 31 // initiator, without being concerned with the profiler's lifetime. | 34 // initiator, without being concerned with the profiler's lifetime. |
| 32 class AsyncRunner { | 35 class AsyncRunner { |
| 33 public: | 36 public: |
| 34 // Sets up a profiler and arranges for it to be deleted on its completed | 37 // Sets up a profiler and arranges for it to be deleted on its completed |
| 35 // callback. | 38 // callback. |
| 36 static void Run(PlatformThreadId thread_id, | 39 static void Run(PlatformThreadId thread_id, |
| 37 const StackSamplingProfiler::SamplingParams& params, | 40 const StackSamplingProfiler::SamplingParams& params, |
| (...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 153 StackSamplingProfiler::CallStackProfile | 156 StackSamplingProfiler::CallStackProfile |
| 154 StackSamplingProfiler::CallStackProfile::CopyForTesting() const { | 157 StackSamplingProfiler::CallStackProfile::CopyForTesting() const { |
| 155 return CallStackProfile(*this); | 158 return CallStackProfile(*this); |
| 156 } | 159 } |
| 157 | 160 |
| 158 StackSamplingProfiler::CallStackProfile::CallStackProfile( | 161 StackSamplingProfiler::CallStackProfile::CallStackProfile( |
| 159 const CallStackProfile& other) = default; | 162 const CallStackProfile& other) = default; |
| 160 | 163 |
| 161 // StackSamplingProfiler::SamplingThread -------------------------------------- | 164 // StackSamplingProfiler::SamplingThread -------------------------------------- |
| 162 | 165 |
| 163 StackSamplingProfiler::SamplingThread::SamplingThread( | 166 class StackSamplingProfiler::SamplingThread : public Thread { |
| 164 std::unique_ptr<NativeStackSampler> native_sampler, | 167 public: |
| 165 const SamplingParams& params, | 168 struct ActiveCapture { |
| 166 const CompletedCallback& completed_callback) | 169 ActiveCapture(PlatformThreadId target, |
| 167 : native_sampler_(std::move(native_sampler)), | 170 const SamplingParams& params, |
| 168 params_(params), | 171 const CompletedCallback& callback, |
| 169 stop_event_(WaitableEvent::ResetPolicy::AUTOMATIC, | 172 std::unique_ptr<NativeStackSampler> sampler) |
| 170 WaitableEvent::InitialState::NOT_SIGNALED), | 173 : capture_id(subtle::NoBarrier_AtomicIncrement(&next_capture_id_, 1)), |
| 171 completed_callback_(completed_callback) {} | 174 target(target), |
| 172 | 175 params(params), |
| 173 StackSamplingProfiler::SamplingThread::~SamplingThread() {} | 176 callback(callback), |
| 174 | 177 native_sampler(std::move(sampler)), |
| 175 void StackSamplingProfiler::SamplingThread::ThreadMain() { | 178 weak_ptr_factory_(this) {} |
| 176 PlatformThread::SetName("Chrome_SamplingProfilerThread"); | 179 ~ActiveCapture() {} |
| 177 | 180 |
| 178 // For now, just ignore any requests to profile while another profiler is | 181 // Updates the |next_sample_time| time based on configured parameters. |
| 179 // working. | 182 // This will keep a consistent average interval between samples but will |
| 180 if (!concurrent_profiling_lock.Get().Try()) | 183 // result in constant series of acquisitions, thus nearly locking out the |
| 184 // target thread, if the interval is smaller than the time it takes to | |
| 185 // actually acquire the sample. Anything sampling that quickly is going | |
| 186 // to be a problem anyway so don't worry about it. | |
| 187 bool UpdateNextSampleTime() { | |
| 188 if (stopped) | |
| 189 return false; | |
| 190 | |
| 191 if (++sample < params.samples_per_burst) { | |
| 192 next_sample_time += params.sampling_interval; | |
| 193 return true; | |
| 194 } | |
| 195 | |
| 196 if (++burst < params.bursts) { | |
| 197 sample = 0; | |
| 198 next_sample_time += params.burst_interval; | |
| 199 return true; | |
| 200 } | |
| 201 | |
| 202 return false; | |
| 203 } | |
| 204 | |
| 205 WeakPtr<ActiveCapture> GetWeakPtr() { | |
| 206 return weak_ptr_factory_.GetWeakPtr(); | |
| 207 } | |
| 208 | |
| 209 // An identifier for this capture, used to uniquely identify it to outside | |
| 210 // interests. | |
| 211 const int capture_id; | |
| 212 | |
| 213 Time next_sample_time; | |
| 214 | |
| 215 PlatformThreadId target; | |
| 216 SamplingParams params; | |
| 217 CompletedCallback callback; | |
| 218 | |
| 219 std::unique_ptr<NativeStackSampler> native_sampler; | |
| 220 | |
| 221 // Counters that indicate the current position along the acquisition. | |
| 222 int burst = 0; | |
| 223 int sample = 0; | |
| 224 | |
| 225 // Indicates if the capture has been stopped (and reported). | |
| 226 bool stopped = false; | |
| 227 | |
| 228 // The time that a profile was started, for calculating the total duration. | |
| 229 Time profile_start_time; | |
| 230 | |
| 231 // The captured stack samples. The active profile is always at the back(). | |
| 232 CallStackProfiles profiles; | |
| 233 | |
| 234 private: | |
| 235 static subtle::AtomicWord next_capture_id_; | |
| 236 | |
| 237 WeakPtrFactory<ActiveCapture> weak_ptr_factory_; | |
| 238 }; | |
| 239 | |
| 240 // Gets the single instance of this class. | |
| 241 static SamplingThread* GetInstance(); | |
| 242 | |
| 243 // Starts the thread. | |
| 244 void Start(); | |
| 245 | |
| 246 // Adds a new ActiveCapture to the thread. This can be called externally | |
| 247 // from any thread. This returns an ID that can later be used to stop | |
| 248 // the sampling. | |
| 249 int Add(std::unique_ptr<ActiveCapture> capture); | |
| 250 | |
| 251 // Stops an active capture based on its ID, forcing it to run its callback | |
| 252 // if any data has been collected. This can be called externally from any | |
| 253 // thread. | |
| 254 void Stop(int id); | |
| 255 | |
| 256 private: | |
| 257 SamplingThread(); | |
| 258 ~SamplingThread() override; | |
| 259 friend struct DefaultSingletonTraits<SamplingThread>; | |
| 260 | |
| 261 // These methods are called when a new capture begins, when it is | |
| 262 // finished, and for each individual sample, respectively. | |
| 263 void BeginCapture(ActiveCapture* capture); | |
| 264 void FinishCapture(ActiveCapture* capture); | |
| 265 void PerformCapture(ActiveCapture* capture); | |
|
Mike Wittman
2016/12/09 21:45:02
The term "capture" is overloaded in the method nam
bcwhite
2016/12/15 18:07:50
Done, though shortened to just Begin/End/PerformRe
Mike Wittman
2016/12/15 20:37:53
This still has the same issue: "recording" is used
bcwhite
2016/12/21 16:39:10
Done.
| |
| 266 | |
| 267 // These methods are tasks that get posted to the internal message queue. | |
| 268 void StartCaptureTask(std::unique_ptr<ActiveCapture> capture); | |
| 269 void StopCaptureTask(int id); | |
| 270 void PerformCaptureTask(std::unique_ptr<ActiveCapture> capture); | |
| 271 | |
| 272 // Thread: | |
| 273 void Init() override; | |
| 274 | |
| 275 static constexpr int kMinimumThreadRunTimeSeconds = 60; | |
| 276 | |
| 277 // A map of IDs to active captures. This is a weak-pointer because it's | |
| 278 // possible that objects are deleted because their collection is complete. | |
| 279 std::map<int, WeakPtr<ActiveCapture>> active_captures_; | |
| 280 | |
| 281 DISALLOW_COPY_AND_ASSIGN(SamplingThread); | |
| 282 }; | |
| 283 | |
| 284 subtle::AtomicWord | |
| 285 StackSamplingProfiler::SamplingThread::ActiveCapture::next_capture_id_ = 0; | |
| 286 | |
| 287 StackSamplingProfiler::SamplingThread::SamplingThread() | |
| 288 : Thread("Chrome_SamplingProfilerThread") {} | |
| 289 | |
| 290 StackSamplingProfiler::SamplingThread::~SamplingThread() { | |
| 291 Thread::Stop(); | |
| 292 } | |
| 293 | |
| 294 StackSamplingProfiler::SamplingThread* | |
| 295 StackSamplingProfiler::SamplingThread::GetInstance() { | |
| 296 return Singleton<SamplingThread>::get(); | |
| 297 } | |
| 298 | |
| 299 void StackSamplingProfiler::SamplingThread::Start() { | |
| 300 Thread::Options options; | |
| 301 // Use a higher priority for a more accurate sampling interval. | |
| 302 options.priority = ThreadPriority::DISPLAY; | |
| 303 | |
| 304 Thread::StartWithOptions(options); | |
| 305 } | |
| 306 | |
| 307 int StackSamplingProfiler::SamplingThread::Add( | |
| 308 std::unique_ptr<ActiveCapture> capture) { | |
| 309 int id = capture->capture_id; | |
| 310 | |
| 311 scoped_refptr<SingleThreadTaskRunner> runner = task_runner(); | |
|
Mike Wittman
2016/12/09 21:45:01
According to the Thread documentation, task_runner
bcwhite
2016/12/09 23:38:30
The comment for Thread::task_runner() says:
// I
Mike Wittman
2016/12/10 00:24:23
Right, but Add() will never be called on the threa
bcwhite
2016/12/13 16:08:11
Ah, I understand. So if this is called from other
Mike Wittman
2016/12/13 18:16:41
It may be possible to call task_runner() on the St
bcwhite
2016/12/14 15:37:59
Wasn't there an issue with a task-runner not being
Mike Wittman
2016/12/14 18:00:47
The UI thread does not have a task runner when it
bcwhite
2016/12/14 19:39:39
I must be missing something. The SamplingThread's
Mike Wittman
2016/12/14 20:47:50
I don't think this is correct. Thread::task_runner
bcwhite
2016/12/15 11:42:15
Ah! So _fetching_ the task-runner must be done on
bcwhite
2016/12/15 15:01:16
It's going to still be necessary to have a lock, t
Mike Wittman
2016/12/15 17:22:46
Yeah, I'm not surprised we can't completely avoid
| |
| 312 if (!runner) { | |
| 313 // The thread is not running. Start it and try again. | |
| 314 Start(); | |
| 315 runner = task_runner(); | |
| 316 DCHECK(runner); | |
| 317 } | |
| 318 | |
| 319 // Having the task-runner doesn't prevent the thread from exiting between | |
| 320 // when it was acquired and the PostTask below. If that were to happen, the | |
| 321 // PostTask would fail and no capture would be done. A retry isn't possible | |
| 322 // because the |capture| object was passed and thus no longer exists locally. | |
| 323 // To prevent this, first create a dummy task that is delayed long enough | |
| 324 // that the real task is, for all practical purposes, guaranteed to get | |
| 325 // queued. | |
| 326 if (!runner->PostDelayedTask( | |
| 327 FROM_HERE, Bind(&DoNothing), | |
| 328 TimeDelta::FromSeconds(kMinimumThreadRunTimeSeconds))) { | |
| 329 // The thread must have exited. Restart it and get the new task runner. | |
| 330 Start(); | |
| 331 runner = task_runner(); | |
| 332 DCHECK(runner); | |
| 333 } | |
| 334 | |
| 335 bool success = | |
| 336 runner->PostTask(FROM_HERE, Bind(&SamplingThread::StartCaptureTask, | |
|
Mike Wittman
2016/12/09 21:45:02
It's common practice to implement thread hopping u
bcwhite
2016/12/09 23:38:30
Add() and Stop() are always coming from a differen
Mike Wittman
2016/12/10 00:24:23
I think it's worth doing this for Stop() at least.
bcwhite
2016/12/13 16:08:11
I can see that. The downside is that there are th
| |
| 337 Unretained(this), Passed(&capture))); | |
| 338 DCHECK(success); | |
| 339 | |
| 340 return id; | |
| 341 } | |
| 342 | |
| 343 void StackSamplingProfiler::SamplingThread::Stop(int id) { | |
| 344 scoped_refptr<SingleThreadTaskRunner> runner = task_runner(); | |
|
Mike Wittman
2016/12/09 21:45:01
Same issue with task_runner() here.
bcwhite
2016/12/15 18:07:50
Done.
| |
| 345 if (!runner) | |
| 346 return; // Everything has already stopped. | |
| 347 | |
| 348 // This can fail if the thread were to exit between acquisition of the task | |
| 349 // runner above and the call below. In that case, however, everything has | |
| 350 // stopped so there's no need to try to stop it. | |
| 351 runner->PostTask( | |
| 352 FROM_HERE, Bind(&SamplingThread::StopCaptureTask, Unretained(this), id)); | |
|
Mike Wittman
2016/12/09 21:45:02
Same comment here about thread hopping.
| |
| 353 } | |
| 354 | |
| 355 void StackSamplingProfiler::SamplingThread::BeginCapture( | |
| 356 ActiveCapture* capture) { | |
| 357 DCHECK(capture->native_sampler); | |
| 358 } | |
| 359 | |
| 360 void StackSamplingProfiler::SamplingThread::FinishCapture( | |
| 361 ActiveCapture* capture) { | |
| 362 DCHECK(!capture->stopped); | |
| 363 capture->stopped = true; | |
| 364 | |
| 365 // If there is no duration for the final profile (because it was stopped), | |
| 366 // calculated it now. | |
| 367 if (!capture->profiles.empty() && | |
| 368 capture->profiles.back().profile_duration == TimeDelta()) { | |
| 369 capture->profiles.back().profile_duration = | |
| 370 Time::Now() - capture->profile_start_time; | |
| 371 } | |
| 372 | |
| 373 // Run the associated callback, passing the captured profiles. It's okay to | |
| 374 // move them because this capture is about to be deleted. | |
| 375 capture->callback.Run(std::move(capture->profiles)); | |
| 376 } | |
| 377 | |
| 378 void StackSamplingProfiler::SamplingThread::PerformCapture( | |
| 379 ActiveCapture* capture) { | |
| 380 DCHECK(!capture->stopped); | |
| 381 | |
| 382 // If this is the first sample of a burst, a new Profile needs to be created | |
| 383 // and filled. | |
| 384 if (capture->sample == 0) { | |
| 385 capture->profiles.push_back(CallStackProfile()); | |
| 386 CallStackProfile& profile = capture->profiles.back(); | |
| 387 profile.sampling_period = capture->params.sampling_interval; | |
| 388 capture->profile_start_time = Time::Now(); | |
| 389 capture->native_sampler->ProfileRecordingStarting(&profile.modules); | |
| 390 } | |
| 391 | |
| 392 // The currently active profile being acptured. | |
| 393 CallStackProfile& profile = capture->profiles.back(); | |
| 394 | |
| 395 // Capture a single sample. | |
| 396 profile.samples.push_back(Sample()); | |
| 397 capture->native_sampler->RecordStackSample(&profile.samples.back()); | |
| 398 | |
| 399 // If this is the last sample of a burst, record the total time. | |
| 400 if (capture->sample == capture->params.samples_per_burst - 1) { | |
| 401 profile.profile_duration = Time::Now() - capture->profile_start_time; | |
| 402 capture->native_sampler->ProfileRecordingStopped(); | |
| 403 } | |
| 404 } | |
| 405 | |
| 406 void StackSamplingProfiler::SamplingThread::StartCaptureTask( | |
| 407 std::unique_ptr<ActiveCapture> capture) { | |
| 408 active_captures_.insert( | |
| 409 std::make_pair(capture->capture_id, capture->GetWeakPtr())); | |
| 410 BeginCapture(capture.get()); | |
| 411 bool success = task_runner()->PostDelayedTask( | |
| 412 FROM_HERE, Bind(&SamplingThread::PerformCaptureTask, Unretained(this), | |
| 413 Passed(&capture)), | |
| 414 capture->params.initial_delay); | |
| 415 DCHECK(success); | |
| 416 } | |
| 417 | |
| 418 void StackSamplingProfiler::SamplingThread::StopCaptureTask(int id) { | |
| 419 auto found = active_captures_.find(id); | |
| 420 if (found == active_captures_.end()) | |
| 421 return; // Gone and forgotten. | |
| 422 | |
| 423 ActiveCapture* capture = found->second.get(); | |
| 424 if (!capture) | |
| 425 return; // Gone but not forgotten. | |
| 426 | |
| 427 if (capture->stopped) | |
| 181 return; | 428 return; |
| 182 | 429 |
|
Mike Wittman
2016/12/09 21:45:01
where does the capture get erased from active_capt
bcwhite
2016/12/09 23:38:30
In ::Cleanup()
... which I realized after uploadin
| |
| 183 CallStackProfiles profiles; | 430 FinishCapture(capture); |
| 184 CollectProfiles(&profiles); | 431 } |
| 185 concurrent_profiling_lock.Get().Release(); | 432 |
| 186 completed_callback_.Run(std::move(profiles)); | 433 void StackSamplingProfiler::SamplingThread::PerformCaptureTask( |
| 187 } | 434 std::unique_ptr<ActiveCapture> capture) { |
| 188 | 435 DCHECK(capture); |
| 189 // Depending on how long the sampling takes and the length of the sampling | 436 // Don't do anything if the capture has already stopped. |
| 190 // interval, a burst of samples could take arbitrarily longer than | 437 if (capture->stopped) |
| 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; | 438 return; |
| 234 | 439 |
| 235 TimeDelta previous_elapsed_profile_time; | 440 // Handle first-run with no "next time". |
| 236 for (int i = 0; i < params_.bursts; ++i) { | 441 if (capture->next_sample_time == Time()) |
| 237 if (i != 0) { | 442 capture->next_sample_time = Time::Now(); |
| 238 // Always wait, even if for 0 seconds, so we can observe a signal on | 443 |
| 239 // stop_event_. | 444 // Do the collection of a single sample. |
| 240 if (stop_event_.TimedWait( | 445 PerformCapture(capture.get()); |
| 241 std::max(params_.burst_interval - previous_elapsed_profile_time, | 446 |
| 242 TimeDelta()))) | 447 // Update the time of the next capture. |
| 243 return; | 448 if (capture->UpdateNextSampleTime()) { |
| 244 } | 449 // Place the updated entry back on the queue. |
| 245 | 450 bool success = task_runner()->PostDelayedTask( |
| 246 CallStackProfile profile; | 451 FROM_HERE, Bind(&SamplingThread::PerformCaptureTask, Unretained(this), |
| 247 bool was_stopped = false; | 452 Passed(&capture)), |
| 248 CollectProfile(&profile, &previous_elapsed_profile_time, &was_stopped); | 453 std::max(capture->next_sample_time - Time::Now(), TimeDelta())); |
| 249 if (!profile.samples.empty()) | 454 DCHECK(success); |
| 250 profiles->push_back(std::move(profile)); | 455 } else { |
| 251 | 456 // All capturing has completed so finish the collection. Let object expire. |
| 252 if (was_stopped) | 457 FinishCapture(capture.get()); |
| 253 return; | 458 } |
| 254 } | 459 } |
| 255 } | 460 |
| 256 | 461 void StackSamplingProfiler::SamplingThread::Init() { |
| 257 void StackSamplingProfiler::SamplingThread::Stop() { | 462 // Let the parent initialize. |
| 258 stop_event_.Signal(); | 463 Thread::Init(); |
| 464 | |
| 465 // Create a dummy task so that the thread won't exit for at least some | |
| 466 // minimum amount of time. Otherwise, the thread could exit just after | |
| 467 // starting and before a caller has time to start the real work. | |
| 468 DCHECK(task_runner()); | |
| 469 bool success = task_runner()->PostDelayedTask( | |
| 470 FROM_HERE, Bind(&DoNothing), | |
| 471 TimeDelta::FromSeconds(kMinimumThreadRunTimeSeconds)); | |
|
Mike Wittman
2016/12/09 21:45:01
My understanding is that the message loop just wai
bcwhite
2016/12/09 23:38:30
Correct. My idea is to add the ability for it to
Mike Wittman
2016/12/10 00:24:23
I think that will be confusing to readers since it
bcwhite
2016/12/13 16:08:11
Message looks are already RunForever or RunUntilId
Mike Wittman
2016/12/13 18:16:41
RunForever is pretty much the only mode that's use
bcwhite
2016/12/15 18:07:50
Acknowledged.
| |
| 472 DCHECK(success); | |
| 259 } | 473 } |
| 260 | 474 |
| 261 // StackSamplingProfiler ------------------------------------------------------ | 475 // StackSamplingProfiler ------------------------------------------------------ |
| 262 | 476 |
| 263 subtle::Atomic32 StackSamplingProfiler::process_phases_ = 0; | 477 subtle::Atomic32 StackSamplingProfiler::process_phases_ = 0; |
| 264 | 478 |
| 265 StackSamplingProfiler::SamplingParams::SamplingParams() | 479 StackSamplingProfiler::SamplingParams::SamplingParams() |
| 266 : initial_delay(TimeDelta::FromMilliseconds(0)), | 480 : initial_delay(TimeDelta::FromMilliseconds(0)), |
| 267 bursts(1), | 481 bursts(1), |
| 268 burst_interval(TimeDelta::FromMilliseconds(10000)), | 482 burst_interval(TimeDelta::FromMilliseconds(10000)), |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 280 PlatformThreadId thread_id, | 494 PlatformThreadId thread_id, |
| 281 const SamplingParams& params, | 495 const SamplingParams& params, |
| 282 const CompletedCallback& callback, | 496 const CompletedCallback& callback, |
| 283 NativeStackSamplerTestDelegate* test_delegate) | 497 NativeStackSamplerTestDelegate* test_delegate) |
| 284 : thread_id_(thread_id), params_(params), completed_callback_(callback), | 498 : thread_id_(thread_id), params_(params), completed_callback_(callback), |
| 285 test_delegate_(test_delegate) { | 499 test_delegate_(test_delegate) { |
| 286 } | 500 } |
| 287 | 501 |
| 288 StackSamplingProfiler::~StackSamplingProfiler() { | 502 StackSamplingProfiler::~StackSamplingProfiler() { |
| 289 Stop(); | 503 Stop(); |
| 290 if (!sampling_thread_handle_.is_null()) | |
| 291 PlatformThread::Join(sampling_thread_handle_); | |
| 292 } | 504 } |
| 293 | 505 |
| 294 // static | 506 // static |
| 295 void StackSamplingProfiler::StartAndRunAsync( | 507 void StackSamplingProfiler::StartAndRunAsync( |
| 296 PlatformThreadId thread_id, | 508 PlatformThreadId thread_id, |
| 297 const SamplingParams& params, | 509 const SamplingParams& params, |
| 298 const CompletedCallback& callback) { | 510 const CompletedCallback& callback) { |
| 299 CHECK(ThreadTaskRunnerHandle::Get()); | 511 CHECK(ThreadTaskRunnerHandle::Get()); |
| 300 AsyncRunner::Run(thread_id, params, callback); | 512 AsyncRunner::Run(thread_id, params, callback); |
| 301 } | 513 } |
| 302 | 514 |
| 303 void StackSamplingProfiler::Start() { | 515 void StackSamplingProfiler::Start() { |
| 304 if (completed_callback_.is_null()) | 516 if (completed_callback_.is_null()) |
| 305 return; | 517 return; |
| 306 | 518 |
| 307 std::unique_ptr<NativeStackSampler> native_sampler = | 519 std::unique_ptr<NativeStackSampler> native_sampler = |
| 308 NativeStackSampler::Create(thread_id_, &RecordAnnotations, | 520 NativeStackSampler::Create(thread_id_, &RecordAnnotations, |
| 309 test_delegate_); | 521 test_delegate_); |
| 310 if (!native_sampler) | 522 if (!native_sampler) |
| 311 return; | 523 return; |
| 312 | 524 |
| 313 sampling_thread_.reset(new SamplingThread(std::move(native_sampler), params_, | 525 capture_id_ = SamplingThread::GetInstance()->Add( |
| 314 completed_callback_)); | 526 MakeUnique<SamplingThread::ActiveCapture>( |
| 315 if (!PlatformThread::Create(0, sampling_thread_.get(), | 527 thread_id_, params_, completed_callback_, std::move(native_sampler))); |
| 316 &sampling_thread_handle_)) | |
| 317 sampling_thread_.reset(); | |
| 318 } | 528 } |
| 319 | 529 |
| 320 void StackSamplingProfiler::Stop() { | 530 void StackSamplingProfiler::Stop() { |
| 321 if (sampling_thread_) | 531 SamplingThread::GetInstance()->Stop(capture_id_); |
| 322 sampling_thread_->Stop(); | |
| 323 } | 532 } |
| 324 | 533 |
| 325 // static | 534 // static |
| 326 void StackSamplingProfiler::SetProcessPhase(int phase) { | 535 void StackSamplingProfiler::SetProcessPhase(int phase) { |
| 327 DCHECK_LE(0, phase); | 536 DCHECK_LE(0, phase); |
| 328 DCHECK_GT(static_cast<int>(sizeof(process_phases_) * 8), phase); | 537 DCHECK_GT(static_cast<int>(sizeof(process_phases_) * 8), phase); |
| 329 DCHECK_EQ(0, subtle::NoBarrier_Load(&process_phases_) & (1 << phase)); | 538 DCHECK_EQ(0, subtle::NoBarrier_Load(&process_phases_) & (1 << phase)); |
| 330 ChangeAtomicFlags(&process_phases_, 1 << phase, 0); | 539 ChangeAtomicFlags(&process_phases_, 1 << phase, 0); |
| 331 } | 540 } |
| 332 | 541 |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 378 } | 587 } |
| 379 | 588 |
| 380 bool operator<(const StackSamplingProfiler::Frame &a, | 589 bool operator<(const StackSamplingProfiler::Frame &a, |
| 381 const StackSamplingProfiler::Frame &b) { | 590 const StackSamplingProfiler::Frame &b) { |
| 382 return (a.module_index < b.module_index) || | 591 return (a.module_index < b.module_index) || |
| 383 (a.module_index == b.module_index && | 592 (a.module_index == b.module_index && |
| 384 a.instruction_pointer < b.instruction_pointer); | 593 a.instruction_pointer < b.instruction_pointer); |
| 385 } | 594 } |
| 386 | 595 |
| 387 } // namespace base | 596 } // namespace base |
| OLD | NEW |