Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(170)

Side by Side Diff: base/profiler/stack_sampling_profiler.cc

Issue 2554123002: Support parallel captures from the StackSamplingProfiler. (Closed)
Patch Set: Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « base/profiler/stack_sampling_profiler.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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"
16 #include "base/profiler/native_stack_sampler.h" 20 #include "base/profiler/native_stack_sampler.h"
17 #include "base/synchronization/lock.h" 21 #include "base/synchronization/lock.h"
22 #include "base/threading/simple_thread.h"
18 #include "base/threading/thread_task_runner_handle.h" 23 #include "base/threading/thread_task_runner_handle.h"
19 #include "base/timer/elapsed_timer.h" 24 #include "base/timer/elapsed_timer.h"
20 25
21 namespace base { 26 namespace base {
22 27
23 namespace { 28 namespace {
24 29
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 ---------------------------------------------------------------- 30 // AsyncRunner ----------------------------------------------------------------
29 31
30 // Helper class to allow a profiler to be run completely asynchronously from the 32 // Helper class to allow a profiler to be run completely asynchronously from the
31 // initiator, without being concerned with the profiler's lifetime. 33 // initiator, without being concerned with the profiler's lifetime.
32 class AsyncRunner { 34 class AsyncRunner {
33 public: 35 public:
34 // Sets up a profiler and arranges for it to be deleted on its completed 36 // Sets up a profiler and arranges for it to be deleted on its completed
35 // callback. 37 // callback.
36 static void Run(PlatformThreadId thread_id, 38 static void Run(PlatformThreadId thread_id,
37 const StackSamplingProfiler::SamplingParams& params, 39 const StackSamplingProfiler::SamplingParams& params,
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after
153 StackSamplingProfiler::CallStackProfile 155 StackSamplingProfiler::CallStackProfile
154 StackSamplingProfiler::CallStackProfile::CopyForTesting() const { 156 StackSamplingProfiler::CallStackProfile::CopyForTesting() const {
155 return CallStackProfile(*this); 157 return CallStackProfile(*this);
156 } 158 }
157 159
158 StackSamplingProfiler::CallStackProfile::CallStackProfile( 160 StackSamplingProfiler::CallStackProfile::CallStackProfile(
159 const CallStackProfile& other) = default; 161 const CallStackProfile& other) = default;
160 162
161 // StackSamplingProfiler::SamplingThread -------------------------------------- 163 // StackSamplingProfiler::SamplingThread --------------------------------------
162 164
163 StackSamplingProfiler::SamplingThread::SamplingThread( 165 class StackSamplingProfiler::SamplingThread : public SimpleThread {
164 std::unique_ptr<NativeStackSampler> native_sampler, 166 public:
165 const SamplingParams& params, 167 struct ActiveCapture {
166 const CompletedCallback& completed_callback) 168 ActiveCapture(PlatformThreadId target,
167 : native_sampler_(std::move(native_sampler)), 169 const SamplingParams& params,
168 params_(params), 170 const CompletedCallback& callback,
169 stop_event_(WaitableEvent::ResetPolicy::AUTOMATIC, 171 std::unique_ptr<NativeStackSampler> sampler)
170 WaitableEvent::InitialState::NOT_SIGNALED), 172 : capture_id(subtle::NoBarrier_AtomicIncrement(&next_capture_id_, 1)),
171 completed_callback_(completed_callback) {} 173 target(target),
174 params(params),
175 callback(callback),
176 native_sampler(std::move(sampler)) {}
177 ~ActiveCapture() {}
178
179 // Updates the |next_sample_time| time based on configured parameters.
180 // This will keep a consistent average interval between samples but will
181 // result in constant series of acquisitions, thus nearly locking out the
182 // target thread, if the interval is smaller than the time it takes to
183 // actually acquire the sample. Anything sampling that quickly is going
184 // to be a problem anyway so don't worry about it.
185 bool UpdateNextSampleTime() {
186 if (++sample < params.samples_per_burst) {
187 next_sample_time += params.sampling_interval;
188 return true;
189 }
190
191 if (++burst < params.bursts) {
192 sample = 0;
193 next_sample_time += params.burst_interval;
194 return true;
195 }
196
197 return false;
198 }
199
200 // An identifier for this capture, used to uniquely identify it to outside
201 // interests.
202 const int capture_id;
203
204 Time next_sample_time;
205
206 PlatformThreadId target;
207 SamplingParams params;
208 CompletedCallback callback;
209
210 std::unique_ptr<NativeStackSampler> native_sampler;
211
212 // Counters that indicate the current position along the acquisition.
213 int burst = 0;
214 int sample = 0;
215
216 // The time that a profile was started, for calculating the total duration.
217 Time profile_start_time;
218
219 // The captured stack samples. The active profile is always at the back().
220 CallStackProfiles profiles;
221
222 private:
223 static subtle::AtomicWord next_capture_id_;
Mike Wittman 2016/12/06 21:04:58 We probably can avoid need for a thread-safe id by
bcwhite 2016/12/07 15:15:30 My concern with that is that addresses may be reus
Mike Wittman 2016/12/07 16:25:02 Yes, care would need to be taken to ensure the Sta
Mike Wittman 2016/12/07 17:20:42 Also, the current implementation can use base::Sta
bcwhite 2016/12/15 18:07:50 Done.
224 };
225
226 // Gets the single instance of this class.
227 static SamplingThread* GetInstance();
228
229 // Adds a new ActiveCapture to the thread. This can be called externally
230 // from any thread. This returns an ID that can later be used to stop
231 // the sampling.
232 int Start(std::unique_ptr<ActiveCapture> capture);
233
234 // Stops an active capture based on its ID, forcing it to run its callback
235 // if any data has been collected. This can be called externally from any
236 // thread.
237 void Stop(int id);
238
239 private:
240 SamplingThread();
241 ~SamplingThread() override;
242 friend struct DefaultSingletonTraits<SamplingThread>;
243
244 // These methods are called when a new capture is started, when it is
245 // finished, and for each individual sample, respectively.
246 void StartCapture(ActiveCapture* capture);
247 void FinishCapture(ActiveCapture* capture);
248 void PerformCapture(ActiveCapture* capture);
249
250 // SimpleThread:
251 void Run() override;
252
253 // Compares two active capture pointers for which is less. This is used to
254 // order the priority queue.
255 static bool ActiveCaptureOrder(std::unique_ptr<ActiveCapture>& lhs,
256 std::unique_ptr<ActiveCapture>& rhs) {
257 // Null pointers always have the greater priority so they can be cleared
258 // from the queue.
259 if (!lhs)
260 return false;
261 if (!rhs)
262 return true;
263
264 // Compare the next sample times. The one farthest out is the lesser.
265 return lhs->next_sample_time > rhs->next_sample_time;
266 }
267
268 // A vector of active captures. Entries are managed using push_heap() and
269 // pop_heap() to keep it as a priority queue. Because of the need to iterate
270 // over the entries without popping them, a std::priority_queue is
271 // insufficient.
272 std::vector<std::unique_ptr<ActiveCapture>> active_captures_;
273
274 // This signals that something has changed with the capture configuration.
275 // The capture thread will check for changes as soon as possible.
276 WaitableEvent capture_change_;
277 Lock capture_change_lock_;
278 std::vector<std::unique_ptr<ActiveCapture>> start_captures_;
279 std::vector<int> stop_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 : SimpleThread("Chrome_SamplingProfilerThread", SimpleThread::Options()),
289 capture_change_(WaitableEvent::ResetPolicy::AUTOMATIC,
290 WaitableEvent::InitialState::NOT_SIGNALED) {}
172 291
173 StackSamplingProfiler::SamplingThread::~SamplingThread() {} 292 StackSamplingProfiler::SamplingThread::~SamplingThread() {}
174 293
175 void StackSamplingProfiler::SamplingThread::ThreadMain() { 294 StackSamplingProfiler::SamplingThread*
176 PlatformThread::SetName("Chrome_SamplingProfilerThread"); 295 StackSamplingProfiler::SamplingThread::GetInstance() {
177 296 return Singleton<SamplingThread>::get();
178 // For now, just ignore any requests to profile while another profiler is 297 }
179 // working. 298
180 if (!concurrent_profiling_lock.Get().Try()) 299 int StackSamplingProfiler::SamplingThread::Start(
181 return; 300 std::unique_ptr<ActiveCapture> capture) {
182 301 int id = capture->capture_id;
183 CallStackProfiles profiles; 302
184 CollectProfiles(&profiles); 303 {
185 concurrent_profiling_lock.Get().Release(); 304 AutoLock lock(capture_change_lock_);
186 completed_callback_.Run(std::move(profiles)); 305 start_captures_.push_back(std::move(capture));
187 } 306 }
188 307
189 // Depending on how long the sampling takes and the length of the sampling 308 if (!HasBeenStarted())
190 // interval, a burst of samples could take arbitrarily longer than 309 SimpleThread::Start();
191 // samples_per_burst * sampling_interval. In this case, we (somewhat 310
192 // arbitrarily) honor the number of samples requested rather than strictly 311 return id;
193 // adhering to the sampling intervals. Once we have established users for the 312 }
194 // StackSamplingProfiler and the collected data to judge, we may go the other 313
195 // way or make this behavior configurable. 314 void StackSamplingProfiler::SamplingThread::Stop(int id) {
196 void StackSamplingProfiler::SamplingThread::CollectProfile( 315 AutoLock lock(capture_change_lock_);
197 CallStackProfile* profile, 316 stop_captures_.push_back(id);
198 TimeDelta* elapsed_time, 317 }
199 bool* was_stopped) { 318
200 ElapsedTimer profile_timer; 319 void StackSamplingProfiler::SamplingThread::StartCapture(
201 native_sampler_->ProfileRecordingStarting(&profile->modules); 320 ActiveCapture* capture) {
202 profile->sampling_period = params_.sampling_interval; 321 DCHECK(capture->native_sampler);
203 *was_stopped = false; 322 }
204 TimeDelta previous_elapsed_sample_time; 323
205 for (int i = 0; i < params_.samples_per_burst; ++i) { 324 void StackSamplingProfiler::SamplingThread::FinishCapture(
206 if (i != 0) { 325 ActiveCapture* capture) {
207 // Always wait, even if for 0 seconds, so we can observe a signal on 326 // If there is no duration for the final profile (because it was stopped),
208 // stop_event_. 327 // calculated it now.
209 if (stop_event_.TimedWait( 328 if (!capture->profiles.empty() &&
210 std::max(params_.sampling_interval - previous_elapsed_sample_time, 329 capture->profiles.back().profile_duration == TimeDelta()) {
211 TimeDelta()))) { 330 capture->profiles.back().profile_duration =
212 *was_stopped = true; 331 Time::Now() - capture->profile_start_time;
213 break; 332 }
214 } 333
215 } 334 // Run the associated callback, passing the captured profiles. It's okay to
216 ElapsedTimer sample_timer; 335 // move them because this capture is about to be deleted.
217 profile->samples.push_back(Sample()); 336 capture->callback.Run(std::move(capture->profiles));
218 native_sampler_->RecordStackSample(&profile->samples.back()); 337 }
219 previous_elapsed_sample_time = sample_timer.Elapsed(); 338
220 } 339 void StackSamplingProfiler::SamplingThread::PerformCapture(
221 340 ActiveCapture* capture) {
222 *elapsed_time = profile_timer.Elapsed(); 341 // If this is the first sample of a burst, a new Profile needs to be created
223 profile->profile_duration = *elapsed_time; 342 // and filled.
224 native_sampler_->ProfileRecordingStopped(); 343 if (capture->sample == 0) {
225 } 344 capture->profiles.push_back(CallStackProfile());
226 345 CallStackProfile& profile = capture->profiles.back();
227 // In an analogous manner to CollectProfile() and samples exceeding the expected 346 profile.sampling_period = capture->params.sampling_interval;
228 // total sampling time, bursts may also exceed the burst_interval. We adopt the 347 capture->profile_start_time = Time::Now();
229 // same wait-and-see approach here. 348 capture->native_sampler->ProfileRecordingStarting(&profile.modules);
Mike Wittman 2016/12/06 21:04:57 The matching call to ProfileRecordingStopped has b
bcwhite 2016/12/15 18:07:50 Done.
230 void StackSamplingProfiler::SamplingThread::CollectProfiles( 349 }
231 CallStackProfiles* profiles) { 350
232 if (stop_event_.TimedWait(params_.initial_delay)) 351 // The currently active profile being acptured.
233 return; 352 CallStackProfile& profile = capture->profiles.back();
234 353
235 TimeDelta previous_elapsed_profile_time; 354 // Capture a single sample.
236 for (int i = 0; i < params_.bursts; ++i) { 355 profile.samples.push_back(Sample());
237 if (i != 0) { 356 capture->native_sampler->RecordStackSample(&profile.samples.back());
238 // Always wait, even if for 0 seconds, so we can observe a signal on 357
239 // stop_event_. 358 // If this is the last sample of a burst, record the total time.
240 if (stop_event_.TimedWait( 359 if (capture->sample == capture->params.samples_per_burst - 1) {
241 std::max(params_.burst_interval - previous_elapsed_profile_time, 360 profile.profile_duration = Time::Now() - capture->profile_start_time;
242 TimeDelta()))) 361 }
243 return; 362 }
244 } 363
245 364 void StackSamplingProfiler::SamplingThread::Run() {
246 CallStackProfile profile; 365 while (true) {
247 bool was_stopped = false; 366 TimeDelta wait;
248 CollectProfile(&profile, &previous_elapsed_profile_time, &was_stopped); 367 ActiveCapture* capture = nullptr;
249 if (!profile.samples.empty()) 368 if (!active_captures_.empty()) {
250 profiles->push_back(std::move(profile)); 369 capture = active_captures_.front().get();
251 370 if (!capture) {
252 if (was_stopped) 371 // If the top is null, it must have been explicitly removed. Try next.
253 return; 372 std::pop_heap(active_captures_.begin(), active_captures_.end(),
254 } 373 &ActiveCaptureOrder);
255 } 374 active_captures_.pop_back();
256 375 continue;
257 void StackSamplingProfiler::SamplingThread::Stop() { 376 }
258 stop_event_.Signal(); 377 wait = active_captures_.front()->next_sample_time - Time::Now();
378 } else {
379 wait = TimeDelta::FromDays(365); // A long, long time.
Mike Wittman 2016/12/06 21:04:58 There's a general desire to have as few persistent
bcwhite 2016/12/15 18:07:50 To be done in a future CL.
380 }
381
382 if (wait > TimeDelta() && capture_change_.TimedWait(wait)) {
383 // Something has changed.
384 AutoLock lock(capture_change_lock_);
385
386 // Add any new captures to the priority queue and start it.
387 while (!start_captures_.empty()) {
388 std::unique_ptr<ActiveCapture> capture_ptr =
389 std::move(start_captures_.back());
390 start_captures_.pop_back();
391 capture->next_sample_time =
392 Time::Now() + capture_ptr->params.initial_delay;
393 StartCapture(capture_ptr.get());
394 active_captures_.push_back(std::move(capture_ptr));
395 std::push_heap(active_captures_.begin(), active_captures_.end(),
396 &ActiveCaptureOrder);
397 }
398
399 // Remove any captures that are to be stopped.
400 while (!stop_captures_.empty()) {
401 int stop_id = stop_captures_.back();
402 stop_captures_.pop_back();
403
404 // Loop through all active captures and finish the one with a matching
405 // ID. There are certainly more efficient ways to do this for large
406 // collections but given that there will generally be very few active
407 // captures and very few captures being stopped, it would likely be
408 // slower to do something more complicated.
409 for (auto& capture_ptr : active_captures_) {
410 if (capture_ptr->capture_id == stop_id) {
411 FinishCapture(capture_ptr.get());
412 capture_ptr.reset(); // Nullify the pointer inside the queue.
413 break;
414 }
415 }
416 }
417
418 // As the thread woke due to a "capture-change" event, start over waiting
419 // for the next capture time.
420 continue;
421 }
422
423 // If there were no active captures, check again.
424 if (!capture)
425 continue;
426
427 // Pop the capture from the head of the queue.
428 std::pop_heap(active_captures_.begin(), active_captures_.end(),
429 &ActiveCaptureOrder);
430 std::unique_ptr<ActiveCapture> capture_ptr =
431 std::move(active_captures_.back());
432 active_captures_.pop_back();
433 DCHECK_EQ(capture, capture_ptr.get());
434
435 // Do the collection of a single sample.
436 PerformCapture(capture);
437
438 // Update the time of the next capture.
439 if (capture->UpdateNextSampleTime()) {
440 // Place the updated entry back on the queue.
441 active_captures_.push_back(std::move(capture_ptr));
Mike Wittman 2016/12/06 21:04:57 push_heap?
bcwhite 2016/12/15 18:07:50 Acknowledged.
442 } else {
443 // All capturing has completed so finish the collection.
444 FinishCapture(capture);
445 }
446 }
259 } 447 }
260 448
261 // StackSamplingProfiler ------------------------------------------------------ 449 // StackSamplingProfiler ------------------------------------------------------
262 450
263 subtle::Atomic32 StackSamplingProfiler::process_phases_ = 0; 451 subtle::Atomic32 StackSamplingProfiler::process_phases_ = 0;
264 452
265 StackSamplingProfiler::SamplingParams::SamplingParams() 453 StackSamplingProfiler::SamplingParams::SamplingParams()
266 : initial_delay(TimeDelta::FromMilliseconds(0)), 454 : initial_delay(TimeDelta::FromMilliseconds(0)),
267 bursts(1), 455 bursts(1),
268 burst_interval(TimeDelta::FromMilliseconds(10000)), 456 burst_interval(TimeDelta::FromMilliseconds(10000)),
(...skipping 11 matching lines...) Expand all
280 PlatformThreadId thread_id, 468 PlatformThreadId thread_id,
281 const SamplingParams& params, 469 const SamplingParams& params,
282 const CompletedCallback& callback, 470 const CompletedCallback& callback,
283 NativeStackSamplerTestDelegate* test_delegate) 471 NativeStackSamplerTestDelegate* test_delegate)
284 : thread_id_(thread_id), params_(params), completed_callback_(callback), 472 : thread_id_(thread_id), params_(params), completed_callback_(callback),
285 test_delegate_(test_delegate) { 473 test_delegate_(test_delegate) {
286 } 474 }
287 475
288 StackSamplingProfiler::~StackSamplingProfiler() { 476 StackSamplingProfiler::~StackSamplingProfiler() {
289 Stop(); 477 Stop();
290 if (!sampling_thread_handle_.is_null())
291 PlatformThread::Join(sampling_thread_handle_);
292 } 478 }
293 479
294 // static 480 // static
295 void StackSamplingProfiler::StartAndRunAsync( 481 void StackSamplingProfiler::StartAndRunAsync(
296 PlatformThreadId thread_id, 482 PlatformThreadId thread_id,
297 const SamplingParams& params, 483 const SamplingParams& params,
298 const CompletedCallback& callback) { 484 const CompletedCallback& callback) {
299 CHECK(ThreadTaskRunnerHandle::Get()); 485 CHECK(ThreadTaskRunnerHandle::Get());
300 AsyncRunner::Run(thread_id, params, callback); 486 AsyncRunner::Run(thread_id, params, callback);
301 } 487 }
302 488
303 void StackSamplingProfiler::Start() { 489 void StackSamplingProfiler::Start() {
304 if (completed_callback_.is_null()) 490 if (completed_callback_.is_null())
305 return; 491 return;
306 492
307 std::unique_ptr<NativeStackSampler> native_sampler = 493 std::unique_ptr<NativeStackSampler> native_sampler =
308 NativeStackSampler::Create(thread_id_, &RecordAnnotations, 494 NativeStackSampler::Create(thread_id_, &RecordAnnotations,
Mike Wittman 2016/12/06 21:04:58 We'll need to refactor to use a single stack copy
bcwhite 2016/12/15 18:07:50 Future CL.
309 test_delegate_); 495 test_delegate_);
310 if (!native_sampler) 496 if (!native_sampler)
311 return; 497 return;
312 498
313 sampling_thread_.reset(new SamplingThread(std::move(native_sampler), params_, 499 capture_id_ = SamplingThread::GetInstance()->Start(
314 completed_callback_)); 500 MakeUnique<SamplingThread::ActiveCapture>(
315 if (!PlatformThread::Create(0, sampling_thread_.get(), 501 thread_id_, params_, completed_callback_, std::move(native_sampler)));
316 &sampling_thread_handle_))
317 sampling_thread_.reset();
318 } 502 }
319 503
320 void StackSamplingProfiler::Stop() { 504 void StackSamplingProfiler::Stop() {
321 if (sampling_thread_) 505 SamplingThread::GetInstance()->Stop(capture_id_);
322 sampling_thread_->Stop();
323 } 506 }
324 507
325 // static 508 // static
326 void StackSamplingProfiler::SetProcessPhase(int phase) { 509 void StackSamplingProfiler::SetProcessPhase(int phase) {
327 DCHECK_LE(0, phase); 510 DCHECK_LE(0, phase);
328 DCHECK_GT(static_cast<int>(sizeof(process_phases_) * 8), phase); 511 DCHECK_GT(static_cast<int>(sizeof(process_phases_) * 8), phase);
329 DCHECK_EQ(0, subtle::NoBarrier_Load(&process_phases_) & (1 << phase)); 512 DCHECK_EQ(0, subtle::NoBarrier_Load(&process_phases_) & (1 << phase));
330 ChangeAtomicFlags(&process_phases_, 1 << phase, 0); 513 ChangeAtomicFlags(&process_phases_, 1 << phase, 0);
331 } 514 }
332 515
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
378 } 561 }
379 562
380 bool operator<(const StackSamplingProfiler::Frame &a, 563 bool operator<(const StackSamplingProfiler::Frame &a,
381 const StackSamplingProfiler::Frame &b) { 564 const StackSamplingProfiler::Frame &b) {
382 return (a.module_index < b.module_index) || 565 return (a.module_index < b.module_index) ||
383 (a.module_index == b.module_index && 566 (a.module_index == b.module_index &&
384 a.instruction_pointer < b.instruction_pointer); 567 a.instruction_pointer < b.instruction_pointer);
385 } 568 }
386 569
387 } // namespace base 570 } // namespace base
OLDNEW
« no previous file with comments | « base/profiler/stack_sampling_profiler.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698