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

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

Issue 2554123002: Support parallel captures from the StackSamplingProfiler. (Closed)
Patch Set: addressed review comments by wittman Created 3 years, 8 months 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
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 #ifndef BASE_PROFILER_STACK_SAMPLING_PROFILER_H_ 5 #ifndef BASE_PROFILER_STACK_SAMPLING_PROFILER_H_
6 #define BASE_PROFILER_STACK_SAMPLING_PROFILER_H_ 6 #define BASE_PROFILER_STACK_SAMPLING_PROFILER_H_
7 7
8 #include <stddef.h> 8 #include <stddef.h>
9 9
10 #include <memory> 10 #include <memory>
11 #include <string> 11 #include <string>
12 #include <vector> 12 #include <vector>
13 13
14 #include "base/atomicops.h" 14 #include "base/atomicops.h"
15 #include "base/base_export.h" 15 #include "base/base_export.h"
16 #include "base/callback.h" 16 #include "base/callback.h"
17 #include "base/files/file_path.h" 17 #include "base/files/file_path.h"
18 #include "base/macros.h" 18 #include "base/macros.h"
19 #include "base/strings/string16.h" 19 #include "base/strings/string16.h"
20 #include "base/synchronization/waitable_event.h" 20 #include "base/synchronization/waitable_event.h"
21 #include "base/threading/platform_thread.h" 21 #include "base/threading/platform_thread.h"
22 #include "base/time/time.h" 22 #include "base/time/time.h"
23 23
24 namespace base { 24 namespace base {
25 25
26 class NativeStackSampler; 26 class NativeStackSampler;
27 class NativeStackSamplerTestDelegate; 27 class NativeStackSamplerTestDelegate;
28 class WaitableEvent;
Mike Wittman 2017/03/31 18:12:33 This can be removed since the header is already in
bcwhite 2017/04/03 20:18:13 Done.
28 29
29 // StackSamplingProfiler periodically stops a thread to sample its stack, for 30 // StackSamplingProfiler periodically stops a thread to sample its stack, for
30 // the purpose of collecting information about which code paths are 31 // the purpose of collecting information about which code paths are
31 // executing. This information is used in aggregate by UMA to identify hot 32 // executing. This information is used in aggregate by UMA to identify hot
32 // and/or janky code paths. 33 // and/or janky code paths.
33 // 34 //
34 // Sample StackSamplingProfiler usage: 35 // Sample StackSamplingProfiler usage:
35 // 36 //
36 // // Create and customize params as desired. 37 // // Create and customize params as desired.
37 // base::StackStackSamplingProfiler::SamplingParams params; 38 // base::StackStackSamplingProfiler::SamplingParams params;
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after
172 TimeDelta burst_interval = TimeDelta::FromSeconds(10); 173 TimeDelta burst_interval = TimeDelta::FromSeconds(10);
173 174
174 // Number of samples to record per burst. 175 // Number of samples to record per burst.
175 int samples_per_burst = 300; 176 int samples_per_burst = 300;
176 177
177 // Interval between samples during a sampling burst. This is the desired 178 // Interval between samples during a sampling burst. This is the desired
178 // duration from the start of one sample to the start of the next sample. 179 // duration from the start of one sample to the start of the next sample.
179 TimeDelta sampling_interval = TimeDelta::FromMilliseconds(100); 180 TimeDelta sampling_interval = TimeDelta::FromMilliseconds(100);
180 }; 181 };
181 182
183 // Testing support.
Mike Wittman 2017/03/31 18:12:33 Add comment: The functions on this API are static
bcwhite 2017/04/03 20:18:13 Done.
184 class BASE_EXPORT TestAPI {
185 public:
186 // Resets the internal state to that of a fresh start. This is necessary
187 // so that tests don't inherit state from previous tests.
188 static void Reset();
189
190 // Resets internal annotations (like process phase) to initial values.
191 static void ResetAnnotations();
192
193 // Returns whether the sampling thread is currently running or not.
194 static bool IsSamplingThreadRunning();
195
196 // Disables inherent idle-shutdown behavior.
197 static void DisableIdleShutdown();
198
199 // Initiates an idle shutdown task, as though the idle timer had expired,
200 // causing the thread to exit. There is no "idle" check so this must be
201 // called only when all sampling tasks have completed. This blocks until
202 // the task has been executed, though the actual stopping of the thread
203 // still happens asynchronously. Watch IsSamplingThreadRunning() to know
204 // when the thread has exited. If |simulate_intervening_start| is true then
205 // this method will make it appear to the shutdown task that a new profiler
206 // was started between when the idle-shutdown was initiated and when it
207 // runs.
208 static void PerformSamplingThreadIdleShutdown(
209 bool simulate_intervening_start);
210 };
211
182 // The callback type used to collect completed profiles. The passed |profiles| 212 // The callback type used to collect completed profiles. The passed |profiles|
183 // are move-only. 213 // are move-only. Other threads, including the UI thread, may block on
214 // callback completion so this should run as quickly as possible.
184 // 215 //
185 // IMPORTANT NOTE: the callback is invoked on a thread the profiler 216 // IMPORTANT NOTE: The callback is invoked on a thread the profiler
186 // constructs, rather than on the thread used to construct the profiler and 217 // constructs, rather than on the thread used to construct the profiler and
187 // set the callback, and thus the callback must be callable on any thread. For 218 // set the callback, and thus the callback must be callable on any thread. For
188 // threads with message loops that create StackSamplingProfilers, posting a 219 // threads with message loops that create StackSamplingProfilers, posting a
189 // task to the message loop with a copy of the profiles is the recommended 220 // task to the message loop with the moved (i.e. std::move) profiles is the
190 // thread-safe callback implementation. 221 // thread-safe callback implementation.
191 using CompletedCallback = Callback<void(CallStackProfiles)>; 222 using CompletedCallback = Callback<void(CallStackProfiles)>;
192 223
193 // Creates a profiler that sends completed profiles to |callback|. The second 224 // Creates a profiler for the CURRENT thread that sends completed profiles
194 // constructor is for test purposes. 225 // to |callback|. An optional |test_delegate| can be supplied by tests.
195 StackSamplingProfiler(PlatformThreadId thread_id, 226 // The caller must ensure that this object gets destroyed before the current
196 const SamplingParams& params, 227 // thread exits.
197 const CompletedCallback& callback); 228 StackSamplingProfiler(
198 StackSamplingProfiler(PlatformThreadId thread_id, 229 const SamplingParams& params,
199 const SamplingParams& params, 230 const CompletedCallback& callback,
200 const CompletedCallback& callback, 231 NativeStackSamplerTestDelegate* test_delegate = nullptr);
201 NativeStackSamplerTestDelegate* test_delegate); 232
233 // Creates a profiler for ANOTHER thread that sends completed profiles to
234 // |callback|. An optional |test_delegate| can be supplied by tests.
235 //
236 // IMPORTANT: The caller must ensure that the thread being sampled does not
237 // exit before this object gets destructed or Bad Things(tm) may occur.
238 StackSamplingProfiler(
239 PlatformThreadId thread_id,
240 const SamplingParams& params,
241 const CompletedCallback& callback,
242 NativeStackSamplerTestDelegate* test_delegate = nullptr);
243
202 // Stops any profiling currently taking place before destroying the profiler. 244 // Stops any profiling currently taking place before destroying the profiler.
245 // This will block until the callback has been run.
Mike Wittman 2017/03/31 18:12:33 update comment: This will block until the callback
bcwhite 2017/04/03 20:18:13 Done.
203 ~StackSamplingProfiler(); 246 ~StackSamplingProfiler();
204 247
205 // The fire-and-forget interface: starts a profiler and allows it to complete
206 // without the caller needing to manage the profiler lifetime. May be invoked
207 // from any thread, but requires that the calling thread has a message loop.
208 static void StartAndRunAsync(PlatformThreadId thread_id,
209 const SamplingParams& params,
210 const CompletedCallback& callback);
211
212 // Initializes the profiler and starts sampling. 248 // Initializes the profiler and starts sampling.
213 void Start(); 249 void Start();
214 250
215 // Stops the profiler and any ongoing sampling. Calling this function is 251 // Stops the profiler and any ongoing sampling. This method will return
216 // optional; if not invoked profiling terminates when all the profiling bursts 252 // immediately with the callback being run asynchronously. At most one
217 // specified in the SamplingParams are completed or the profiler is destroyed, 253 // more stack sample will be taken after this method returns. Calling this
218 // whichever occurs first. 254 // function is optional; if not invoked profiling terminates when all the
255 // profiling bursts specified in the SamplingParams are completed or the
256 // profiler object is destroyed, whichever occurs first.
219 void Stop(); 257 void Stop();
220 258
221 // Set the current system state that is recorded with each captured stack 259 // Set the current system state that is recorded with each captured stack
222 // frame. This is thread-safe so can be called from anywhere. The parameter 260 // frame. This is thread-safe so can be called from anywhere. The parameter
223 // value should be from an enumeration of the appropriate type with values 261 // value should be from an enumeration of the appropriate type with values
224 // ranging from 0 to 31, inclusive. This sets bits within Sample field of 262 // ranging from 0 to 31, inclusive. This sets bits within Sample field of
225 // |process_milestones|. The actual meanings of these bits are defined 263 // |process_milestones|. The actual meanings of these bits are defined
226 // (globally) by the caller(s). 264 // (globally) by the caller(s).
227 static void SetProcessMilestone(int milestone); 265 static void SetProcessMilestone(int milestone);
228 static void ResetAnnotationsForTesting();
229 266
230 private: 267 private:
268 friend class TestAPI;
269
231 // SamplingThread is a separate thread used to suspend and sample stacks from 270 // SamplingThread is a separate thread used to suspend and sample stacks from
232 // the target thread. 271 // the target thread.
233 class SamplingThread : public PlatformThread::Delegate { 272 class SamplingThread;
234 public:
235 // Samples stacks using |native_sampler|. When complete, invokes
236 // |completed_callback| with the collected call stack profiles.
237 // |completed_callback| must be callable on any thread.
238 SamplingThread(std::unique_ptr<NativeStackSampler> native_sampler,
239 const SamplingParams& params,
240 const CompletedCallback& completed_callback);
241 ~SamplingThread() override;
242
243 // PlatformThread::Delegate:
244 void ThreadMain() override;
245
246 void Stop();
247
248 private:
249 // Collects |profile| from a single burst. If the profiler was stopped
250 // during collection, sets |was_stopped| and provides the set of samples
251 // collected up to that point.
252 void CollectProfile(CallStackProfile* profile, TimeDelta* elapsed_time,
253 bool* was_stopped);
254
255 // Collects call stack profiles from all bursts, or until the sampling is
256 // stopped. If stopped before complete, the last profile in
257 // |call_stack_profiles| may contain a partial burst.
258 void CollectProfiles(CallStackProfiles* profiles);
259
260 std::unique_ptr<NativeStackSampler> native_sampler_;
261 const SamplingParams params_;
262
263 // If Stop() is called, it signals this event to force the sampling to
264 // terminate before all the samples specified in |params_| are collected.
265 WaitableEvent stop_event_;
266
267 const CompletedCallback completed_callback_;
268
269 DISALLOW_COPY_AND_ASSIGN(SamplingThread);
270 };
271 273
272 // Adds annotations to a Sample. 274 // Adds annotations to a Sample.
273 static void RecordAnnotations(Sample* sample); 275 static void RecordAnnotations(Sample* sample);
274 276
275 // This global variables holds the current system state and is recorded with 277 // This global variables holds the current system state and is recorded with
276 // every captured sample, done on a separate thread which is why updates to 278 // every captured sample, done on a separate thread which is why updates to
277 // this must be atomic. A PostTask to move the the updates to that thread 279 // this must be atomic. A PostTask to move the the updates to that thread
278 // would skew the timing and a lock could result in deadlock if the thread 280 // would skew the timing and a lock could result in deadlock if the thread
279 // making a change was also being profiled and got stopped. 281 // making a change was also being profiled and got stopped.
280 static subtle::Atomic32 process_milestones_; 282 static subtle::Atomic32 process_milestones_;
281 283
282 // The thread whose stack will be sampled. 284 // The thread whose stack will be sampled.
283 PlatformThreadId thread_id_; 285 PlatformThreadId thread_id_;
284 286
285 const SamplingParams params_; 287 const SamplingParams params_;
286 288
287 std::unique_ptr<SamplingThread> sampling_thread_; 289 const CompletedCallback completed_callback_;
288 PlatformThreadHandle sampling_thread_handle_;
289 290
290 const CompletedCallback completed_callback_; 291 // This starts "signaled", is reset when sampling begins, and is signaled
292 // when that sampling is complete and the callback done.
293 WaitableEvent profiling_inactive_;
294
295 // Object that does the native sampling. This is created during construction
296 // and later passed to the sampling thread when profiling is started.
297 std::unique_ptr<NativeStackSampler> native_sampler_;
298
299 // An ID uniquely identifying this collection to the sampling thread. This
300 // will be an internal "null" value when no collection has been started.
301 int collection_id_;
291 302
292 // Stored until it can be passed to the NativeStackSampler created in Start(). 303 // Stored until it can be passed to the NativeStackSampler created in Start().
293 NativeStackSamplerTestDelegate* const test_delegate_; 304 NativeStackSamplerTestDelegate* const test_delegate_;
294 305
295 DISALLOW_COPY_AND_ASSIGN(StackSamplingProfiler); 306 DISALLOW_COPY_AND_ASSIGN(StackSamplingProfiler);
296 }; 307 };
297 308
298 // These operators permit types to be compared and used in a map of Samples, as 309 // These operators permit types to be compared and used in a map of Samples, as
299 // done in tests and by the metrics provider code. 310 // done in tests and by the metrics provider code.
300 BASE_EXPORT bool operator==(const StackSamplingProfiler::Module& a, 311 BASE_EXPORT bool operator==(const StackSamplingProfiler::Module& a,
301 const StackSamplingProfiler::Module& b); 312 const StackSamplingProfiler::Module& b);
302 BASE_EXPORT bool operator==(const StackSamplingProfiler::Sample& a, 313 BASE_EXPORT bool operator==(const StackSamplingProfiler::Sample& a,
303 const StackSamplingProfiler::Sample& b); 314 const StackSamplingProfiler::Sample& b);
304 BASE_EXPORT bool operator!=(const StackSamplingProfiler::Sample& a, 315 BASE_EXPORT bool operator!=(const StackSamplingProfiler::Sample& a,
305 const StackSamplingProfiler::Sample& b); 316 const StackSamplingProfiler::Sample& b);
306 BASE_EXPORT bool operator<(const StackSamplingProfiler::Sample& a, 317 BASE_EXPORT bool operator<(const StackSamplingProfiler::Sample& a,
307 const StackSamplingProfiler::Sample& b); 318 const StackSamplingProfiler::Sample& b);
308 BASE_EXPORT bool operator==(const StackSamplingProfiler::Frame& a, 319 BASE_EXPORT bool operator==(const StackSamplingProfiler::Frame& a,
309 const StackSamplingProfiler::Frame& b); 320 const StackSamplingProfiler::Frame& b);
310 BASE_EXPORT bool operator<(const StackSamplingProfiler::Frame& a, 321 BASE_EXPORT bool operator<(const StackSamplingProfiler::Frame& a,
311 const StackSamplingProfiler::Frame& b); 322 const StackSamplingProfiler::Frame& b);
312 323
313 } // namespace base 324 } // namespace base
314 325
315 #endif // BASE_PROFILER_STACK_SAMPLING_PROFILER_H_ 326 #endif // BASE_PROFILER_STACK_SAMPLING_PROFILER_H_
OLDNEW
« no previous file with comments | « no previous file | base/profiler/stack_sampling_profiler.cc » ('j') | base/profiler/stack_sampling_profiler.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698