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> | |
| 9 #include <queue> | |
| 8 #include <utility> | 10 #include <utility> | 
| 9 | 11 | 
| 12 #include "base/atomic_sequence_num.h" | |
| 13 #include "base/atomicops.h" | |
| 10 #include "base/bind.h" | 14 #include "base/bind.h" | 
| 11 #include "base/bind_helpers.h" | 15 #include "base/bind_helpers.h" | 
| 12 #include "base/callback.h" | 16 #include "base/callback.h" | 
| 13 #include "base/lazy_instance.h" | 17 #include "base/lazy_instance.h" | 
| 14 #include "base/location.h" | 18 #include "base/location.h" | 
| 15 #include "base/macros.h" | 19 #include "base/macros.h" | 
| 20 #include "base/memory/ptr_util.h" | |
| 21 #include "base/memory/singleton.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 CollectionContext { | 
| 166 const CompletedCallback& completed_callback) | 170 CollectionContext(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 : collection_id(next_collection_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 ~CollectionContext() {} | 
| 176 PlatformThread::SetName("Chrome_SamplingProfilerThread"); | 180 | 
| 177 | 181 // An identifier for this collection, used to uniquely identify it to | 
| 178 // For now, just ignore any requests to profile while another profiler is | 182 // outside interests. | 
| 179 // working. | 183 const int collection_id; | 
| 180 if (!concurrent_profiling_lock.Get().Try()) | 184 | 
| 185 Time next_sample_time; | |
| 186 | |
| 187 PlatformThreadId target; | |
| 188 SamplingParams params; | |
| 189 CompletedCallback callback; | |
| 190 | |
| 191 std::unique_ptr<NativeStackSampler> native_sampler; | |
| 192 | |
| 193 // Counters that indicate the current position along the acquisition. | |
| 194 int burst = 0; | |
| 195 int sample = 0; | |
| 196 | |
| 197 // The time that a profile was started, for calculating the total duration. | |
| 198 Time profile_start_time; | |
| 199 | |
| 200 // The collected stack samples. The active profile is always at the back(). | |
| 201 CallStackProfiles profiles; | |
| 202 | |
| 203 private: | |
| 204 static StaticAtomicSequenceNumber next_collection_id_; | |
| 205 }; | |
| 206 | |
| 207 // Gets the single instance of this class. | |
| 208 static SamplingThread* GetInstance(); | |
| 209 | |
| 210 // Starts the thread. | |
| 211 void Start(); | |
| 212 | |
| 213 // Adds a new CollectionContext to the thread. This can be called externally | |
| 214 // from any thread. This returns an ID that can later be used to stop | |
| 215 // the sampling. | |
| 216 int Add(std::unique_ptr<CollectionContext> collection); | |
| 217 | |
| 218 // Stops an active collection based on its ID, forcing it to run its callback | |
| 219 // if any data has been collected. This can be called externally from any | |
| 220 // thread. | |
| 221 void Stop(int id); | |
| 222 | |
| 223 private: | |
| 224 SamplingThread(); | |
| 225 ~SamplingThread() override; | |
| 226 friend struct DefaultSingletonTraits<SamplingThread>; | |
| 227 | |
| 228 // Get task runner that is usable from the outside. | |
| 229 scoped_refptr<SingleThreadTaskRunner> GetOrCreateTaskRunner(); | |
| 230 scoped_refptr<SingleThreadTaskRunner> GetTaskRunner(); | |
| 231 | |
| 232 // Get tas krunner that is usable from the sampling thread itself. | |
| 
 
Mike Wittman
2017/01/06 16:02:40
nit: task runner
 
bcwhite
2017/01/06 16:43:52
Done.
 
 | |
| 233 scoped_refptr<SingleThreadTaskRunner> GetTaskRunnerFromSamplingThread(); | |
| 234 | |
| 235 // Finishes a collection and reports collected data via callback. | |
| 236 void FinishCollection(CollectionContext* collection); | |
| 237 | |
| 238 // Records a single sample of a collection. | |
| 239 void RecordSample(CollectionContext* collection); | |
| 240 | |
| 241 // These methods are tasks that get posted to the internal message queue. | |
| 242 void StartCollectionTask(std::unique_ptr<CollectionContext> collection_ptr); | |
| 243 void StopCollectionTask(int id); | |
| 244 void PerformCollectionTask(int id); | |
| 245 | |
| 246 // Updates the |next_sample_time| time based on configured parameters. | |
| 247 bool UpdateNextSampleTime(CollectionContext* collection); | |
| 248 | |
| 249 // Thread: | |
| 250 void CleanUp() override; | |
| 251 | |
| 252 // The task-runner for the sampling thread. This is saved so that it can | |
| 253 // be freely used by any calling thread. | |
| 254 scoped_refptr<SingleThreadTaskRunner> task_runner_; | |
| 255 Lock task_runner_lock_; | |
| 256 | |
| 257 // A map of IDs to collection contexts. Because this class is a singleton | |
| 258 // that is never destroyed, context objects will never be destructed except | |
| 259 // by explicit action. Thus, it's acceptable to pass unretained pointers | |
| 260 // to these objects when posting tasks. | |
| 261 std::map<int, std::unique_ptr<CollectionContext>> active_collections_; | |
| 262 | |
| 263 DISALLOW_COPY_AND_ASSIGN(SamplingThread); | |
| 264 }; | |
| 265 | |
| 266 StaticAtomicSequenceNumber StackSamplingProfiler::SamplingThread:: | |
| 267 CollectionContext::next_collection_id_; | |
| 268 | |
| 269 StackSamplingProfiler::SamplingThread::SamplingThread() | |
| 270 : Thread("Chrome_SamplingProfilerThread") {} | |
| 271 | |
| 272 StackSamplingProfiler::SamplingThread::~SamplingThread() { | |
| 273 Thread::Stop(); | |
| 274 } | |
| 275 | |
| 276 StackSamplingProfiler::SamplingThread* | |
| 277 StackSamplingProfiler::SamplingThread::GetInstance() { | |
| 278 return Singleton<SamplingThread, LeakySingletonTraits<SamplingThread>>::get(); | |
| 279 } | |
| 280 | |
| 281 void StackSamplingProfiler::SamplingThread::Start() { | |
| 282 Thread::Options options; | |
| 283 // Use a higher priority for a more accurate sampling interval. | |
| 284 options.priority = ThreadPriority::DISPLAY; | |
| 285 Thread::StartWithOptions(options); | |
| 286 } | |
| 287 | |
| 288 int StackSamplingProfiler::SamplingThread::Add( | |
| 289 std::unique_ptr<CollectionContext> collection) { | |
| 290 int id = collection->collection_id; | |
| 291 GetOrCreateTaskRunner()->PostTask( | |
| 292 FROM_HERE, Bind(&SamplingThread::StartCollectionTask, Unretained(this), | |
| 293 Passed(&collection))); | |
| 294 return id; | |
| 295 } | |
| 296 | |
| 297 void StackSamplingProfiler::SamplingThread::Stop(int id) { | |
| 298 scoped_refptr<SingleThreadTaskRunner> task_runner = GetTaskRunner(); | |
| 299 if (!task_runner) | |
| 300 return; // Everything has already stopped. | |
| 301 | |
| 302 // This can fail if the thread were to exit between acquisition of the task | |
| 303 // runner above and the call below. In that case, however, everything has | |
| 304 // stopped so there's no need to try to stop it. | |
| 305 task_runner->PostTask(FROM_HERE, Bind(&SamplingThread::StopCollectionTask, | |
| 306 Unretained(this), id)); | |
| 307 } | |
| 308 | |
| 309 scoped_refptr<SingleThreadTaskRunner> | |
| 310 StackSamplingProfiler::SamplingThread::GetOrCreateTaskRunner() { | |
| 311 AutoLock lock(task_runner_lock_); | |
| 312 if (!task_runner_) { | |
| 313 // The thread is not running. Start it and get associated runner. The task- | |
| 314 // runner has to be saved for future use because though it can be used from | |
| 315 // any thread, it can be acquired via task_runner() only on the created | |
| 316 // thread and the thread that creates it (i.e. this thread). | |
| 317 Start(); | |
| 318 task_runner_ = task_runner(); | |
| 
 
Mike Wittman
2017/01/06 16:02:40
nit: Thread::task_runner() to be explicitly clear
 
bcwhite
2017/01/06 16:43:52
Done.
 
 | |
| 319 } else { | |
| 320 // This shouldn't be called from the sampling thread as it's inefficient. | |
| 321 // Use GetTaskRunnerFromSamplingThread() instead. | |
| 322 DCHECK_NE(GetThreadId(), PlatformThread::CurrentId()); | |
| 323 } | |
| 324 | |
| 325 return task_runner_; | |
| 326 } | |
| 327 | |
| 328 scoped_refptr<SingleThreadTaskRunner> | |
| 329 StackSamplingProfiler::SamplingThread::GetTaskRunner() { | |
| 330 // This shouldn't be called from the sampling thread as it's inefficient. Use | |
| 331 // GetTaskRunnerFromSamplingThread() instead. | |
| 332 DCHECK_NE(GetThreadId(), PlatformThread::CurrentId()); | |
| 333 | |
| 334 AutoLock lock(task_runner_lock_); | |
| 335 return task_runner_; | |
| 336 } | |
| 337 | |
| 338 scoped_refptr<SingleThreadTaskRunner> | |
| 339 StackSamplingProfiler::SamplingThread::GetTaskRunnerFromSamplingThread() { | |
| 
 
Mike Wittman
2017/01/06 16:02:40
How about GetTaskRunnerOnSamplingThread? GetTaskRu
 
bcwhite
2017/01/06 16:43:52
Done.
 
 | |
| 340 // This should be called only from the sampling thread as it has limited | |
| 341 // accessibility. | |
| 342 DCHECK_EQ(GetThreadId(), PlatformThread::CurrentId()); | |
| 343 | |
| 344 return task_runner(); | |
| 
 
Mike Wittman
2017/01/06 16:02:40
nit: Thread::task_runner() here also
 
bcwhite
2017/01/06 16:43:52
Done.
 
 | |
| 345 } | |
| 346 | |
| 347 void StackSamplingProfiler::SamplingThread::FinishCollection( | |
| 348 CollectionContext* collection) { | |
| 349 // If there is no duration for the final profile (because it was stopped), | |
| 350 // calculated it now. | |
| 351 if (!collection->profiles.empty() && | |
| 352 collection->profiles.back().profile_duration == TimeDelta()) { | |
| 353 collection->profiles.back().profile_duration = | |
| 354 Time::Now() - collection->profile_start_time; | |
| 355 } | |
| 356 | |
| 357 // Run the associated callback, passing the collected profiles. It's okay to | |
| 358 // move them because this collection is about to be deleted. | |
| 359 collection->callback.Run(std::move(collection->profiles)); | |
| 360 | |
| 361 // Remove this collection from the map of known ones. This must be done | |
| 362 // last as the |collection| parameter is invalid after this point. | |
| 363 size_t count = active_collections_.erase(collection->collection_id); | |
| 364 DCHECK_EQ(1U, count); | |
| 365 } | |
| 366 | |
| 367 void StackSamplingProfiler::SamplingThread::RecordSample( | |
| 368 CollectionContext* collection) { | |
| 369 DCHECK(collection->native_sampler); | |
| 370 | |
| 371 // If this is the first sample of a burst, a new Profile needs to be created | |
| 372 // and filled. | |
| 373 if (collection->sample == 0) { | |
| 374 collection->profiles.push_back(CallStackProfile()); | |
| 375 CallStackProfile& profile = collection->profiles.back(); | |
| 376 profile.sampling_period = collection->params.sampling_interval; | |
| 377 collection->profile_start_time = Time::Now(); | |
| 378 collection->native_sampler->ProfileRecordingStarting(&profile.modules); | |
| 379 } | |
| 380 | |
| 381 // The currently active profile being acptured. | |
| 382 CallStackProfile& profile = collection->profiles.back(); | |
| 383 | |
| 384 // Record a single sample. | |
| 385 profile.samples.push_back(Sample()); | |
| 386 collection->native_sampler->RecordStackSample(&profile.samples.back()); | |
| 387 | |
| 388 // If this is the last sample of a burst, record the total time. | |
| 389 if (collection->sample == collection->params.samples_per_burst - 1) { | |
| 390 profile.profile_duration = Time::Now() - collection->profile_start_time; | |
| 391 collection->native_sampler->ProfileRecordingStopped(); | |
| 392 } | |
| 393 } | |
| 394 | |
| 395 void StackSamplingProfiler::SamplingThread::StartCollectionTask( | |
| 396 std::unique_ptr<CollectionContext> collection_ptr) { | |
| 397 // Ownership of the collection is going to be given to a map but a pointer | |
| 398 // to it will be needed later. | |
| 399 CollectionContext* collection = collection_ptr.get(); | |
| 400 active_collections_.insert( | |
| 401 std::make_pair(collection->collection_id, std::move(collection_ptr))); | |
| 402 | |
| 403 GetTaskRunnerFromSamplingThread()->PostDelayedTask( | |
| 404 FROM_HERE, Bind(&SamplingThread::PerformCollectionTask, Unretained(this), | |
| 405 collection->collection_id), | |
| 406 collection->params.initial_delay); | |
| 407 } | |
| 408 | |
| 409 void StackSamplingProfiler::SamplingThread::StopCollectionTask(int id) { | |
| 410 auto found = active_collections_.find(id); | |
| 411 if (found == active_collections_.end()) | |
| 181 return; | 412 return; | 
| 182 | 413 | 
| 183 CallStackProfiles profiles; | 414 FinishCollection(found->second.get()); | 
| 184 CollectProfiles(&profiles); | 415 } | 
| 185 concurrent_profiling_lock.Get().Release(); | 416 | 
| 186 completed_callback_.Run(std::move(profiles)); | 417 void StackSamplingProfiler::SamplingThread::PerformCollectionTask(int id) { | 
| 187 } | 418 auto found = active_collections_.find(id); | 
| 188 | 419 | 
| 189 // Depending on how long the sampling takes and the length of the sampling | 420 // The task won't be found if it has been stopped. | 
| 190 // interval, a burst of samples could take arbitrarily longer than | 421 if (found == active_collections_.end()) | 
| 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; | 422 return; | 
| 234 | 423 | 
| 235 TimeDelta previous_elapsed_profile_time; | 424 CollectionContext* collection = found->second.get(); | 
| 236 for (int i = 0; i < params_.bursts; ++i) { | 425 | 
| 237 if (i != 0) { | 426 // Handle first-run with no "next time". | 
| 238 // Always wait, even if for 0 seconds, so we can observe a signal on | 427 if (collection->next_sample_time == Time()) | 
| 239 // stop_event_. | 428 collection->next_sample_time = Time::Now(); | 
| 240 if (stop_event_.TimedWait( | 429 | 
| 241 std::max(params_.burst_interval - previous_elapsed_profile_time, | 430 // Do the collection of a single sample. | 
| 242 TimeDelta()))) | 431 RecordSample(collection); | 
| 243 return; | 432 | 
| 244 } | 433 // Update the time of the next sample recording. | 
| 245 | 434 if (UpdateNextSampleTime(collection)) { | 
| 246 CallStackProfile profile; | 435 bool success = GetTaskRunnerFromSamplingThread()->PostDelayedTask( | 
| 247 bool was_stopped = false; | 436 FROM_HERE, | 
| 248 CollectProfile(&profile, &previous_elapsed_profile_time, &was_stopped); | 437 Bind(&SamplingThread::PerformCollectionTask, Unretained(this), id), | 
| 249 if (!profile.samples.empty()) | 438 std::max(collection->next_sample_time - Time::Now(), TimeDelta())); | 
| 250 profiles->push_back(std::move(profile)); | 439 DCHECK(success); | 
| 251 | 440 } else { | 
| 252 if (was_stopped) | 441 // All capturing has completed so finish the collection. Let object expire. | 
| 253 return; | 442 // The |collection| variable will be invalid after this call. | 
| 254 } | 443 FinishCollection(collection); | 
| 255 } | 444 } | 
| 256 | 445 } | 
| 257 void StackSamplingProfiler::SamplingThread::Stop() { | 446 | 
| 258 stop_event_.Signal(); | 447 bool StackSamplingProfiler::SamplingThread::UpdateNextSampleTime( | 
| 448 CollectionContext* collection) { | |
| 449 if (++collection->sample < collection->params.samples_per_burst) { | |
| 450 collection->next_sample_time += collection->params.sampling_interval; | |
| 451 return true; | |
| 452 } | |
| 453 | |
| 454 // This will keep a consistent average interval between samples but will | |
| 455 // result in constant series of acquisitions, thus nearly locking out the | |
| 456 // target thread, if the interval is smaller than the time it takes to | |
| 457 // actually acquire the sample. Anything sampling that quickly is going | |
| 458 // to be a problem anyway so don't worry about it. | |
| 459 if (++collection->burst < collection->params.bursts) { | |
| 460 collection->sample = 0; | |
| 461 collection->next_sample_time += collection->params.burst_interval; | |
| 462 return true; | |
| 463 } | |
| 464 | |
| 465 return false; | |
| 466 } | |
| 467 | |
| 468 void StackSamplingProfiler::SamplingThread::CleanUp() { | |
| 469 // There should be no collections remaining when the thread stops. | |
| 470 DCHECK(active_collections_.empty()); | |
| 471 | |
| 472 // Let the parent clean up. | |
| 473 Thread::CleanUp(); | |
| 259 } | 474 } | 
| 260 | 475 | 
| 261 // StackSamplingProfiler ------------------------------------------------------ | 476 // StackSamplingProfiler ------------------------------------------------------ | 
| 262 | 477 | 
| 263 subtle::Atomic32 StackSamplingProfiler::process_phases_ = 0; | 478 subtle::Atomic32 StackSamplingProfiler::process_phases_ = 0; | 
| 264 | 479 | 
| 265 StackSamplingProfiler::SamplingParams::SamplingParams() | 480 StackSamplingProfiler::SamplingParams::SamplingParams() | 
| 266 : initial_delay(TimeDelta::FromMilliseconds(0)), | 481 : initial_delay(TimeDelta::FromMilliseconds(0)), | 
| 267 bursts(1), | 482 bursts(1), | 
| 268 burst_interval(TimeDelta::FromMilliseconds(10000)), | 483 burst_interval(TimeDelta::FromMilliseconds(10000)), | 
| (...skipping 11 matching lines...) Expand all Loading... | |
| 280 PlatformThreadId thread_id, | 495 PlatformThreadId thread_id, | 
| 281 const SamplingParams& params, | 496 const SamplingParams& params, | 
| 282 const CompletedCallback& callback, | 497 const CompletedCallback& callback, | 
| 283 NativeStackSamplerTestDelegate* test_delegate) | 498 NativeStackSamplerTestDelegate* test_delegate) | 
| 284 : thread_id_(thread_id), params_(params), completed_callback_(callback), | 499 : thread_id_(thread_id), params_(params), completed_callback_(callback), | 
| 285 test_delegate_(test_delegate) { | 500 test_delegate_(test_delegate) { | 
| 286 } | 501 } | 
| 287 | 502 | 
| 288 StackSamplingProfiler::~StackSamplingProfiler() { | 503 StackSamplingProfiler::~StackSamplingProfiler() { | 
| 289 Stop(); | 504 Stop(); | 
| 290 if (!sampling_thread_handle_.is_null()) | |
| 291 PlatformThread::Join(sampling_thread_handle_); | |
| 292 } | 505 } | 
| 293 | 506 | 
| 294 // static | 507 // static | 
| 295 void StackSamplingProfiler::StartAndRunAsync( | 508 void StackSamplingProfiler::StartAndRunAsync( | 
| 296 PlatformThreadId thread_id, | 509 PlatformThreadId thread_id, | 
| 297 const SamplingParams& params, | 510 const SamplingParams& params, | 
| 298 const CompletedCallback& callback) { | 511 const CompletedCallback& callback) { | 
| 299 CHECK(ThreadTaskRunnerHandle::Get()); | 512 CHECK(ThreadTaskRunnerHandle::Get()); | 
| 300 AsyncRunner::Run(thread_id, params, callback); | 513 AsyncRunner::Run(thread_id, params, callback); | 
| 301 } | 514 } | 
| 302 | 515 | 
| 303 void StackSamplingProfiler::Start() { | 516 void StackSamplingProfiler::Start() { | 
| 304 if (completed_callback_.is_null()) | 517 if (completed_callback_.is_null()) | 
| 305 return; | 518 return; | 
| 306 | 519 | 
| 307 std::unique_ptr<NativeStackSampler> native_sampler = | 520 std::unique_ptr<NativeStackSampler> native_sampler = | 
| 308 NativeStackSampler::Create(thread_id_, &RecordAnnotations, | 521 NativeStackSampler::Create(thread_id_, &RecordAnnotations, | 
| 309 test_delegate_); | 522 test_delegate_); | 
| 310 if (!native_sampler) | 523 if (!native_sampler) | 
| 311 return; | 524 return; | 
| 312 | 525 | 
| 313 sampling_thread_.reset(new SamplingThread(std::move(native_sampler), params_, | 526 collection_id_ = SamplingThread::GetInstance()->Add( | 
| 314 completed_callback_)); | 527 MakeUnique<SamplingThread::CollectionContext>( | 
| 315 if (!PlatformThread::Create(0, sampling_thread_.get(), | 528 thread_id_, params_, completed_callback_, std::move(native_sampler))); | 
| 316 &sampling_thread_handle_)) | |
| 317 sampling_thread_.reset(); | |
| 318 } | 529 } | 
| 319 | 530 | 
| 320 void StackSamplingProfiler::Stop() { | 531 void StackSamplingProfiler::Stop() { | 
| 321 if (sampling_thread_) | 532 SamplingThread::GetInstance()->Stop(collection_id_); | 
| 322 sampling_thread_->Stop(); | |
| 323 } | 533 } | 
| 324 | 534 | 
| 325 // static | 535 // static | 
| 326 void StackSamplingProfiler::SetProcessPhase(int phase) { | 536 void StackSamplingProfiler::SetProcessPhase(int phase) { | 
| 327 DCHECK_LE(0, phase); | 537 DCHECK_LE(0, phase); | 
| 328 DCHECK_GT(static_cast<int>(sizeof(process_phases_) * 8), phase); | 538 DCHECK_GT(static_cast<int>(sizeof(process_phases_) * 8), phase); | 
| 329 DCHECK_EQ(0, subtle::NoBarrier_Load(&process_phases_) & (1 << phase)); | 539 DCHECK_EQ(0, subtle::NoBarrier_Load(&process_phases_) & (1 << phase)); | 
| 330 ChangeAtomicFlags(&process_phases_, 1 << phase, 0); | 540 ChangeAtomicFlags(&process_phases_, 1 << phase, 0); | 
| 331 } | 541 } | 
| 332 | 542 | 
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 378 } | 588 } | 
| 379 | 589 | 
| 380 bool operator<(const StackSamplingProfiler::Frame &a, | 590 bool operator<(const StackSamplingProfiler::Frame &a, | 
| 381 const StackSamplingProfiler::Frame &b) { | 591 const StackSamplingProfiler::Frame &b) { | 
| 382 return (a.module_index < b.module_index) || | 592 return (a.module_index < b.module_index) || | 
| 383 (a.module_index == b.module_index && | 593 (a.module_index == b.module_index && | 
| 384 a.instruction_pointer < b.instruction_pointer); | 594 a.instruction_pointer < b.instruction_pointer); | 
| 385 } | 595 } | 
| 386 | 596 | 
| 387 } // namespace base | 597 } // namespace base | 
| OLD | NEW |