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/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" | |
| 21 #include "base/memory/weak_ptr.h" | |
| 16 #include "base/profiler/native_stack_sampler.h" | 22 #include "base/profiler/native_stack_sampler.h" |
| 17 #include "base/synchronization/lock.h" | 23 #include "base/synchronization/lock.h" |
| 24 #include "base/threading/thread.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. | |
| 26 LazyInstance<Lock>::Leaky concurrent_profiling_lock = LAZY_INSTANCE_INITIALIZER; | |
| 27 | |
| 28 // AsyncRunner ---------------------------------------------------------------- | 32 // AsyncRunner ---------------------------------------------------------------- |
| 29 | 33 |
| 30 // Helper class to allow a profiler to be run completely asynchronously from the | 34 // Helper class to allow a profiler to be run completely asynchronously from the |
| 31 // initiator, without being concerned with the profiler's lifetime. | 35 // initiator, without being concerned with the profiler's lifetime. |
| 32 class AsyncRunner { | 36 class AsyncRunner { |
| 33 public: | 37 public: |
| 34 // Sets up a profiler and arranges for it to be deleted on its completed | 38 // Sets up a profiler and arranges for it to be deleted on its completed |
| 35 // callback. | 39 // callback. |
| 36 static void Run(PlatformThreadId thread_id, | 40 static void Run(PlatformThreadId thread_id, |
| 37 const StackSamplingProfiler::SamplingParams& params, | 41 const StackSamplingProfiler::SamplingParams& params, |
| (...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 153 StackSamplingProfiler::CallStackProfile | 157 StackSamplingProfiler::CallStackProfile |
| 154 StackSamplingProfiler::CallStackProfile::CopyForTesting() const { | 158 StackSamplingProfiler::CallStackProfile::CopyForTesting() const { |
| 155 return CallStackProfile(*this); | 159 return CallStackProfile(*this); |
| 156 } | 160 } |
| 157 | 161 |
| 158 StackSamplingProfiler::CallStackProfile::CallStackProfile( | 162 StackSamplingProfiler::CallStackProfile::CallStackProfile( |
| 159 const CallStackProfile& other) = default; | 163 const CallStackProfile& other) = default; |
| 160 | 164 |
| 161 // StackSamplingProfiler::SamplingThread -------------------------------------- | 165 // StackSamplingProfiler::SamplingThread -------------------------------------- |
| 162 | 166 |
| 163 StackSamplingProfiler::SamplingThread::SamplingThread( | 167 class StackSamplingProfiler::SamplingThread : public Thread { |
| 164 std::unique_ptr<NativeStackSampler> native_sampler, | 168 public: |
| 165 const SamplingParams& params, | 169 struct ActiveCapture { |
| 166 const CompletedCallback& completed_callback) | 170 ActiveCapture(PlatformThreadId target, |
| 167 : native_sampler_(std::move(native_sampler)), | 171 const SamplingParams& params, |
| 168 params_(params), | 172 const CompletedCallback& callback, |
| 169 stop_event_(WaitableEvent::ResetPolicy::AUTOMATIC, | 173 std::unique_ptr<NativeStackSampler> sampler) |
| 170 WaitableEvent::InitialState::NOT_SIGNALED), | 174 : capture_id(next_capture_id_.GetNext()), |
| 171 completed_callback_(completed_callback) {} | 175 target(target), |
| 172 | 176 params(params), |
| 173 StackSamplingProfiler::SamplingThread::~SamplingThread() {} | 177 callback(callback), |
| 174 | 178 native_sampler(std::move(sampler)), |
| 175 void StackSamplingProfiler::SamplingThread::ThreadMain() { | 179 weak_ptr_factory_(this) {} |
| 176 PlatformThread::SetName("Chrome_SamplingProfilerThread"); | 180 ~ActiveCapture() {} |
| 177 | 181 |
| 178 // For now, just ignore any requests to profile while another profiler is | 182 // Updates the |next_sample_time| time based on configured parameters. |
| 179 // working. | 183 // This will keep a consistent average interval between samples but will |
| 180 if (!concurrent_profiling_lock.Get().Try()) | 184 // result in constant series of acquisitions, thus nearly locking out the |
| 185 // target thread, if the interval is smaller than the time it takes to | |
| 186 // actually acquire the sample. Anything sampling that quickly is going | |
| 187 // to be a problem anyway so don't worry about it. | |
| 188 bool UpdateNextSampleTime() { | |
|
bcwhite
2016/12/22 16:12:10
Really? I've seen it many times and Alexei has in
Mike Wittman
2016/12/22 17:38:22
The style guide says structs should not have any f
bcwhite
2017/01/05 16:35:58
Done.
| |
| 189 if (stopped) | |
| 190 return false; | |
| 191 | |
| 192 if (++sample < params.samples_per_burst) { | |
| 193 next_sample_time += params.sampling_interval; | |
| 194 return true; | |
| 195 } | |
| 196 | |
| 197 if (++burst < params.bursts) { | |
| 198 sample = 0; | |
| 199 next_sample_time += params.burst_interval; | |
| 200 return true; | |
| 201 } | |
| 202 | |
| 203 return false; | |
| 204 } | |
| 205 | |
| 206 WeakPtr<ActiveCapture> GetWeakPtr() { | |
| 207 return weak_ptr_factory_.GetWeakPtr(); | |
| 208 } | |
| 209 | |
| 210 // An identifier for this capture, used to uniquely identify it to outside | |
| 211 // interests. | |
| 212 const int capture_id; | |
| 213 | |
| 214 Time next_sample_time; | |
| 215 | |
| 216 PlatformThreadId target; | |
| 217 SamplingParams params; | |
| 218 CompletedCallback callback; | |
| 219 | |
| 220 std::unique_ptr<NativeStackSampler> native_sampler; | |
| 221 | |
| 222 // Counters that indicate the current position along the acquisition. | |
| 223 int burst = 0; | |
| 224 int sample = 0; | |
| 225 | |
| 226 // Indicates if the capture has been stopped (and reported). | |
| 227 bool stopped = false; | |
| 228 | |
| 229 // The time that a profile was started, for calculating the total duration. | |
| 230 Time profile_start_time; | |
| 231 | |
| 232 // The captured stack samples. The active profile is always at the back(). | |
| 233 CallStackProfiles profiles; | |
| 234 | |
| 235 private: | |
| 236 static StaticAtomicSequenceNumber next_capture_id_; | |
| 237 | |
| 238 WeakPtrFactory<ActiveCapture> weak_ptr_factory_; | |
| 239 }; | |
| 240 | |
| 241 // Gets the single instance of this class. | |
| 242 static SamplingThread* GetInstance(); | |
| 243 | |
| 244 // Starts the thread. | |
| 245 void Start(); | |
| 246 | |
| 247 // Adds a new ActiveCapture to the thread. This can be called externally | |
| 248 // from any thread. This returns an ID that can later be used to stop | |
| 249 // the sampling. | |
| 250 int Add(std::unique_ptr<ActiveCapture> capture); | |
| 251 | |
| 252 // Stops an active capture based on its ID, forcing it to run its callback | |
| 253 // if any data has been collected. This can be called externally from any | |
| 254 // thread. | |
| 255 void Stop(int id); | |
| 256 | |
| 257 private: | |
| 258 SamplingThread(); | |
| 259 ~SamplingThread() override; | |
| 260 friend struct DefaultSingletonTraits<SamplingThread>; | |
| 261 | |
| 262 // These methods are called when a new capture begins, when it is | |
| 263 // finished, and for each individual sample, respectively. | |
| 264 void BeginRecording(ActiveCapture* capture); | |
| 265 void FinishRecording(ActiveCapture* capture); | |
| 266 void PerformRecording(ActiveCapture* capture); | |
| 267 | |
| 268 // These methods are tasks that get posted to the internal message queue. | |
| 269 void StartCaptureTask(std::unique_ptr<ActiveCapture> capture); | |
| 270 void StopCaptureTask(int id); | |
| 271 void PerformCaptureTask(std::unique_ptr<ActiveCapture> capture); | |
| 272 | |
| 273 // Thread: | |
| 274 void CleanUp() override; | |
| 275 | |
| 276 static constexpr int kMinimumThreadRunTimeSeconds = 60; | |
| 277 | |
| 278 // The task-runner for the sampling thread. This is saved so that it can | |
| 279 // be freely used by any calling thread. | |
| 280 scoped_refptr<SingleThreadTaskRunner> task_runner_; | |
| 281 Lock task_runner_lock_; | |
| 282 | |
| 283 // A map of IDs to active captures. This is a weak-pointer because it's | |
| 284 // possible that objects are deleted because their collection is complete. | |
| 285 std::map<int, WeakPtr<ActiveCapture>> active_captures_; | |
|
Mike Wittman
2016/12/15 20:37:53
I'm not seeing the benefit of using WeakPtr<Active
bcwhite
2016/12/21 16:39:10
The ownership was with the posted tasks so other p
Mike Wittman
2016/12/21 19:38:41
Can we pass the id rather than the raw pointer? Pa
bcwhite
2016/12/22 16:12:10
Done.
| |
| 286 | |
| 287 DISALLOW_COPY_AND_ASSIGN(SamplingThread); | |
| 288 }; | |
| 289 | |
| 290 StaticAtomicSequenceNumber | |
| 291 StackSamplingProfiler::SamplingThread::ActiveCapture::next_capture_id_; | |
| 292 | |
| 293 StackSamplingProfiler::SamplingThread::SamplingThread() | |
| 294 : Thread("Chrome_SamplingProfilerThread") {} | |
| 295 | |
| 296 StackSamplingProfiler::SamplingThread::~SamplingThread() { | |
| 297 Thread::Stop(); | |
| 298 } | |
| 299 | |
| 300 StackSamplingProfiler::SamplingThread* | |
| 301 StackSamplingProfiler::SamplingThread::GetInstance() { | |
| 302 return Singleton<SamplingThread>::get(); | |
| 303 } | |
| 304 | |
| 305 void StackSamplingProfiler::SamplingThread::Start() { | |
| 306 Thread::Options options; | |
| 307 // Use a higher priority for a more accurate sampling interval. | |
| 308 options.priority = ThreadPriority::DISPLAY; | |
| 309 Thread::StartWithOptions(options); | |
| 310 } | |
| 311 | |
| 312 int StackSamplingProfiler::SamplingThread::Add( | |
| 313 std::unique_ptr<ActiveCapture> capture) { | |
| 314 int id = capture->capture_id; | |
| 315 | |
| 316 AutoLock lock(task_runner_lock_); | |
| 317 if (!task_runner_) { | |
| 318 // The thread is not running. Start it and get associated runner. | |
| 319 Start(); | |
| 320 task_runner_ = task_runner(); | |
| 321 DCHECK(task_runner_); | |
|
Mike Wittman
2016/12/15 20:37:53
Remove this DCHECK? Seems like it's just verifying
bcwhite
2016/12/21 16:39:10
Done.
| |
| 322 } | |
| 323 | |
| 324 bool success = task_runner_->PostTask( | |
| 325 FROM_HERE, Bind(&SamplingThread::StartCaptureTask, Unretained(this), | |
| 326 Passed(&capture))); | |
| 327 DCHECK(success); | |
|
Mike Wittman
2016/12/15 20:37:53
Remove this one as well? I don't think this will e
bcwhite
2016/12/21 16:39:10
Done.
| |
| 328 | |
| 329 return id; | |
| 330 } | |
| 331 | |
| 332 void StackSamplingProfiler::SamplingThread::Stop(int id) { | |
| 333 AutoLock lock(task_runner_lock_); | |
| 334 if (!task_runner_) | |
| 335 return; // Everything has already stopped. | |
| 336 | |
| 337 // This can fail if the thread were to exit between acquisition of the task | |
| 338 // runner above and the call below. In that case, however, everything has | |
| 339 // stopped so there's no need to try to stop it. | |
| 340 task_runner_->PostTask( | |
| 341 FROM_HERE, Bind(&SamplingThread::StopCaptureTask, Unretained(this), id)); | |
| 342 } | |
| 343 | |
| 344 void StackSamplingProfiler::SamplingThread::BeginRecording( | |
| 345 ActiveCapture* capture) { | |
| 346 DCHECK(capture->native_sampler); | |
| 347 } | |
| 348 | |
| 349 void StackSamplingProfiler::SamplingThread::FinishRecording( | |
| 350 ActiveCapture* capture) { | |
| 351 DCHECK(!capture->stopped); | |
| 352 capture->stopped = true; | |
| 353 | |
| 354 // If there is no duration for the final profile (because it was stopped), | |
| 355 // calculated it now. | |
| 356 if (!capture->profiles.empty() && | |
| 357 capture->profiles.back().profile_duration == TimeDelta()) { | |
| 358 capture->profiles.back().profile_duration = | |
| 359 Time::Now() - capture->profile_start_time; | |
| 360 } | |
| 361 | |
| 362 // Run the associated callback, passing the captured profiles. It's okay to | |
| 363 // move them because this capture is about to be deleted. | |
| 364 capture->callback.Run(std::move(capture->profiles)); | |
| 365 } | |
|
Mike Wittman
2016/12/15 20:37:53
Why not erase the capture from active_captures_ at
bcwhite
2016/12/21 16:39:10
Done.
| |
| 366 | |
| 367 void StackSamplingProfiler::SamplingThread::PerformRecording( | |
| 368 ActiveCapture* capture) { | |
| 369 DCHECK(!capture->stopped); | |
| 370 | |
| 371 // If this is the first sample of a burst, a new Profile needs to be created | |
| 372 // and filled. | |
| 373 if (capture->sample == 0) { | |
| 374 capture->profiles.push_back(CallStackProfile()); | |
| 375 CallStackProfile& profile = capture->profiles.back(); | |
| 376 profile.sampling_period = capture->params.sampling_interval; | |
| 377 capture->profile_start_time = Time::Now(); | |
| 378 capture->native_sampler->ProfileRecordingStarting(&profile.modules); | |
| 379 } | |
| 380 | |
| 381 // The currently active profile being acptured. | |
| 382 CallStackProfile& profile = capture->profiles.back(); | |
| 383 | |
| 384 // Capture a single sample. | |
| 385 profile.samples.push_back(Sample()); | |
| 386 capture->native_sampler->RecordStackSample(&profile.samples.back()); | |
| 387 | |
| 388 // If this is the last sample of a burst, record the total time. | |
| 389 if (capture->sample == capture->params.samples_per_burst - 1) { | |
| 390 profile.profile_duration = Time::Now() - capture->profile_start_time; | |
| 391 capture->native_sampler->ProfileRecordingStopped(); | |
| 392 } | |
| 393 } | |
| 394 | |
| 395 void StackSamplingProfiler::SamplingThread::StartCaptureTask( | |
| 396 std::unique_ptr<ActiveCapture> capture) { | |
| 397 active_captures_.insert( | |
| 398 std::make_pair(capture->capture_id, capture->GetWeakPtr())); | |
| 399 BeginRecording(capture.get()); | |
| 400 bool success = task_runner()->PostDelayedTask( | |
|
Mike Wittman
2016/12/15 20:37:53
While the task_runner() accesses on the profiler t
bcwhite
2016/12/21 16:39:10
Such a method would have to fetch the current thre
Mike Wittman
2016/12/21 19:38:41
Fetching the thread id on Windows is cheap: it's s
bcwhite
2016/12/22 16:12:10
Linux does a direct syscall():
https://cs.chromium
Mike Wittman
2016/12/22 17:38:22
Ah, missed that the Linux implementation doesn't g
bcwhite
2017/01/05 16:35:58
I started down this path but it ends up requiring
Mike Wittman
2017/01/05 21:08:39
Yeah, it's probably not worth going to the extent
bcwhite
2017/01/05 22:04:22
Trying this but there are issues.
GetOrCreate isn
| |
| 401 FROM_HERE, Bind(&SamplingThread::PerformCaptureTask, Unretained(this), | |
| 402 Passed(&capture)), | |
| 403 capture->params.initial_delay); | |
| 404 DCHECK(success); | |
|
Mike Wittman
2016/12/15 20:37:53
I think this can be removed for the same reasons a
bcwhite
2016/12/21 16:39:10
Done.
| |
| 405 } | |
| 406 | |
| 407 void StackSamplingProfiler::SamplingThread::StopCaptureTask(int id) { | |
| 408 auto found = active_captures_.find(id); | |
| 409 if (found == active_captures_.end()) | |
| 410 return; // Gone and forgotten. | |
| 411 | |
| 412 ActiveCapture* capture = found->second.get(); | |
| 413 if (!capture) | |
| 414 return; // Gone but not forgotten. | |
| 415 | |
| 416 if (capture->stopped) | |
| 181 return; | 417 return; |
| 182 | 418 |
| 183 CallStackProfiles profiles; | 419 FinishRecording(capture); |
| 184 CollectProfiles(&profiles); | 420 } |
| 185 concurrent_profiling_lock.Get().Release(); | 421 |
| 186 completed_callback_.Run(std::move(profiles)); | 422 void StackSamplingProfiler::SamplingThread::PerformCaptureTask( |
| 187 } | 423 std::unique_ptr<ActiveCapture> capture) { |
| 188 | 424 DCHECK(capture); |
| 189 // Depending on how long the sampling takes and the length of the sampling | 425 // Don't do anything if the capture has already stopped. |
| 190 // interval, a burst of samples could take arbitrarily longer than | 426 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; | 427 return; |
| 234 | 428 |
| 235 TimeDelta previous_elapsed_profile_time; | 429 // Handle first-run with no "next time". |
| 236 for (int i = 0; i < params_.bursts; ++i) { | 430 if (capture->next_sample_time == Time()) |
| 237 if (i != 0) { | 431 capture->next_sample_time = Time::Now(); |
| 238 // Always wait, even if for 0 seconds, so we can observe a signal on | 432 |
| 239 // stop_event_. | 433 // Do the collection of a single sample. |
| 240 if (stop_event_.TimedWait( | 434 PerformRecording(capture.get()); |
| 241 std::max(params_.burst_interval - previous_elapsed_profile_time, | 435 |
| 242 TimeDelta()))) | 436 // Update the time of the next capture. |
| 243 return; | 437 if (capture->UpdateNextSampleTime()) { |
| 244 } | 438 // Place the updated entry back on the queue. |
| 245 | 439 bool success = task_runner()->PostDelayedTask( |
| 246 CallStackProfile profile; | 440 FROM_HERE, Bind(&SamplingThread::PerformCaptureTask, Unretained(this), |
| 247 bool was_stopped = false; | 441 Passed(&capture)), |
| 248 CollectProfile(&profile, &previous_elapsed_profile_time, &was_stopped); | 442 std::max(capture->next_sample_time - Time::Now(), TimeDelta())); |
| 249 if (!profile.samples.empty()) | 443 DCHECK(success); |
| 250 profiles->push_back(std::move(profile)); | 444 } else { |
| 251 | 445 // All capturing has completed so finish the collection. Let object expire. |
| 252 if (was_stopped) | 446 FinishRecording(capture.get()); |
| 253 return; | 447 } |
| 254 } | 448 } |
| 255 } | 449 |
| 256 | 450 void StackSamplingProfiler::SamplingThread::CleanUp() { |
| 257 void StackSamplingProfiler::SamplingThread::Stop() { | 451 // Clear out the list of active captures. The sampling thread is exited |
| 258 stop_event_.Signal(); | 452 // so they must all have finished. |
| 453 active_captures_.clear(); | |
| 454 | |
| 455 // Let the parent clean up. | |
| 456 Thread::CleanUp(); | |
| 259 } | 457 } |
| 260 | 458 |
| 261 // StackSamplingProfiler ------------------------------------------------------ | 459 // StackSamplingProfiler ------------------------------------------------------ |
| 262 | 460 |
| 263 subtle::Atomic32 StackSamplingProfiler::process_phases_ = 0; | 461 subtle::Atomic32 StackSamplingProfiler::process_phases_ = 0; |
| 264 | 462 |
| 265 StackSamplingProfiler::SamplingParams::SamplingParams() | 463 StackSamplingProfiler::SamplingParams::SamplingParams() |
| 266 : initial_delay(TimeDelta::FromMilliseconds(0)), | 464 : initial_delay(TimeDelta::FromMilliseconds(0)), |
| 267 bursts(1), | 465 bursts(1), |
| 268 burst_interval(TimeDelta::FromMilliseconds(10000)), | 466 burst_interval(TimeDelta::FromMilliseconds(10000)), |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 280 PlatformThreadId thread_id, | 478 PlatformThreadId thread_id, |
| 281 const SamplingParams& params, | 479 const SamplingParams& params, |
| 282 const CompletedCallback& callback, | 480 const CompletedCallback& callback, |
| 283 NativeStackSamplerTestDelegate* test_delegate) | 481 NativeStackSamplerTestDelegate* test_delegate) |
| 284 : thread_id_(thread_id), params_(params), completed_callback_(callback), | 482 : thread_id_(thread_id), params_(params), completed_callback_(callback), |
| 285 test_delegate_(test_delegate) { | 483 test_delegate_(test_delegate) { |
| 286 } | 484 } |
| 287 | 485 |
| 288 StackSamplingProfiler::~StackSamplingProfiler() { | 486 StackSamplingProfiler::~StackSamplingProfiler() { |
| 289 Stop(); | 487 Stop(); |
| 290 if (!sampling_thread_handle_.is_null()) | |
| 291 PlatformThread::Join(sampling_thread_handle_); | |
| 292 } | 488 } |
| 293 | 489 |
| 294 // static | 490 // static |
| 295 void StackSamplingProfiler::StartAndRunAsync( | 491 void StackSamplingProfiler::StartAndRunAsync( |
| 296 PlatformThreadId thread_id, | 492 PlatformThreadId thread_id, |
| 297 const SamplingParams& params, | 493 const SamplingParams& params, |
| 298 const CompletedCallback& callback) { | 494 const CompletedCallback& callback) { |
| 299 CHECK(ThreadTaskRunnerHandle::Get()); | 495 CHECK(ThreadTaskRunnerHandle::Get()); |
| 300 AsyncRunner::Run(thread_id, params, callback); | 496 AsyncRunner::Run(thread_id, params, callback); |
| 301 } | 497 } |
| 302 | 498 |
| 303 void StackSamplingProfiler::Start() { | 499 void StackSamplingProfiler::Start() { |
| 304 if (completed_callback_.is_null()) | 500 if (completed_callback_.is_null()) |
| 305 return; | 501 return; |
| 306 | 502 |
| 307 std::unique_ptr<NativeStackSampler> native_sampler = | 503 std::unique_ptr<NativeStackSampler> native_sampler = |
| 308 NativeStackSampler::Create(thread_id_, &RecordAnnotations, | 504 NativeStackSampler::Create(thread_id_, &RecordAnnotations, |
| 309 test_delegate_); | 505 test_delegate_); |
| 310 if (!native_sampler) | 506 if (!native_sampler) |
| 311 return; | 507 return; |
| 312 | 508 |
| 313 sampling_thread_.reset(new SamplingThread(std::move(native_sampler), params_, | 509 capture_id_ = SamplingThread::GetInstance()->Add( |
| 314 completed_callback_)); | 510 MakeUnique<SamplingThread::ActiveCapture>( |
| 315 if (!PlatformThread::Create(0, sampling_thread_.get(), | 511 thread_id_, params_, completed_callback_, std::move(native_sampler))); |
| 316 &sampling_thread_handle_)) | |
| 317 sampling_thread_.reset(); | |
| 318 } | 512 } |
| 319 | 513 |
| 320 void StackSamplingProfiler::Stop() { | 514 void StackSamplingProfiler::Stop() { |
| 321 if (sampling_thread_) | 515 SamplingThread::GetInstance()->Stop(capture_id_); |
| 322 sampling_thread_->Stop(); | |
| 323 } | 516 } |
| 324 | 517 |
| 325 // static | 518 // static |
| 326 void StackSamplingProfiler::SetProcessPhase(int phase) { | 519 void StackSamplingProfiler::SetProcessPhase(int phase) { |
| 327 DCHECK_LE(0, phase); | 520 DCHECK_LE(0, phase); |
| 328 DCHECK_GT(static_cast<int>(sizeof(process_phases_) * 8), phase); | 521 DCHECK_GT(static_cast<int>(sizeof(process_phases_) * 8), phase); |
| 329 DCHECK_EQ(0, subtle::NoBarrier_Load(&process_phases_) & (1 << phase)); | 522 DCHECK_EQ(0, subtle::NoBarrier_Load(&process_phases_) & (1 << phase)); |
| 330 ChangeAtomicFlags(&process_phases_, 1 << phase, 0); | 523 ChangeAtomicFlags(&process_phases_, 1 << phase, 0); |
| 331 } | 524 } |
| 332 | 525 |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 378 } | 571 } |
| 379 | 572 |
| 380 bool operator<(const StackSamplingProfiler::Frame &a, | 573 bool operator<(const StackSamplingProfiler::Frame &a, |
| 381 const StackSamplingProfiler::Frame &b) { | 574 const StackSamplingProfiler::Frame &b) { |
| 382 return (a.module_index < b.module_index) || | 575 return (a.module_index < b.module_index) || |
| 383 (a.module_index == b.module_index && | 576 (a.module_index == b.module_index && |
| 384 a.instruction_pointer < b.instruction_pointer); | 577 a.instruction_pointer < b.instruction_pointer); |
| 385 } | 578 } |
| 386 | 579 |
| 387 } // namespace base | 580 } // namespace base |
| OLD | NEW |