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

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, 10 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;
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 137 matching lines...) Expand 10 before | Expand all | Expand 10 after
175 176
176 // Number of samples to record per burst. Defaults to 300. 177 // Number of samples to record per burst. Defaults to 300.
177 int samples_per_burst; 178 int samples_per_burst;
178 179
179 // Interval between samples during a sampling burst. This is the desired 180 // Interval between samples during a sampling burst. This is the desired
180 // duration from the start of one sample to the start of the next 181 // duration from the start of one sample to the start of the next
181 // sample. Defaults to 100ms. 182 // sample. Defaults to 100ms.
182 TimeDelta sampling_interval; 183 TimeDelta sampling_interval;
183 }; 184 };
184 185
186 // Testing support.
187 class BASE_EXPORT TestAPI {
188 public:
189 // Returns whether the sampling thread is currently running or not.
190 static bool IsSamplingThreadRunning();
191
192 // Sets the auto-shutdown delay time for the sampling thread, in ms. Set
193 // this to zero to disable it.
194 static void SetSamplingThreadIdleShutdownTime(int shutdown_ms);
Mike Wittman 2017/02/22 20:32:18 This function is only ever used to disable the the
bcwhite 2017/02/24 20:39:24 Done.
195
196 // Initiates an idle shutdown task, as though the idle timer had expired,
197 // causing the thread to exit if there are no more sampling tasks pending.
198 // This returns immediately. Watch IsSamplingThreadRunningForTesting() to
199 // know when the thread has exited, though it will never do so if it is
200 // not idle.
201 static void InitiateSamplingThreadIdleShutdown();
202 };
203
185 // The callback type used to collect completed profiles. The passed |profiles| 204 // The callback type used to collect completed profiles. The passed |profiles|
186 // are move-only. 205 // are move-only. Other threads, including the UI thread, may block on
206 // callback completion so this should run as quickly as possible.
187 // 207 //
188 // IMPORTANT NOTE: the callback is invoked on a thread the profiler 208 // IMPORTANT NOTE: The callback is invoked on a thread the profiler
189 // constructs, rather than on the thread used to construct the profiler and 209 // constructs, rather than on the thread used to construct the profiler and
190 // set the callback, and thus the callback must be callable on any thread. For 210 // set the callback, and thus the callback must be callable on any thread. For
191 // threads with message loops that create StackSamplingProfilers, posting a 211 // threads with message loops that create StackSamplingProfilers, posting a
192 // task to the message loop with a copy of the profiles is the recommended 212 // task to the message loop with the moved (i.e. std::move) profiles is the
193 // thread-safe callback implementation. 213 // thread-safe callback implementation.
194 using CompletedCallback = Callback<void(CallStackProfiles)>; 214 using CompletedCallback = Callback<void(CallStackProfiles)>;
195 215
196 // Creates a profiler that sends completed profiles to |callback|. The second 216 // Creates a profiler for the CURRENT thread that sends completed profiles
197 // constructor is for test purposes. 217 // to |callback|. An optional |test_delegate| can be supplied by tests.
198 StackSamplingProfiler(PlatformThreadId thread_id, 218 // The caller must ensure that this object gets destroyed before the current
199 const SamplingParams& params, 219 // thread exits.
200 const CompletedCallback& callback); 220 StackSamplingProfiler(
201 StackSamplingProfiler(PlatformThreadId thread_id, 221 const SamplingParams& params,
202 const SamplingParams& params, 222 const CompletedCallback& callback,
203 const CompletedCallback& callback, 223 NativeStackSamplerTestDelegate* test_delegate = nullptr);
204 NativeStackSamplerTestDelegate* test_delegate); 224
225 // Creates a profiler for ANOTHER thread that sends completed profiles to
226 // |callback|. An optional |test_delegate| can be supplied by tests.
227 //
228 // IMPORTANT: The caller must ensure that the thread being sampled does not
229 // exit before this object gets destructed or Bad Things(tm) may occur.
230 StackSamplingProfiler(
231 PlatformThreadId thread_id,
232 const SamplingParams& params,
233 const CompletedCallback& callback,
234 NativeStackSamplerTestDelegate* test_delegate = nullptr);
235
205 // Stops any profiling currently taking place before destroying the profiler. 236 // Stops any profiling currently taking place before destroying the profiler.
237 // This will block until the callback has been run.
206 ~StackSamplingProfiler(); 238 ~StackSamplingProfiler();
207 239
208 // The fire-and-forget interface: starts a profiler and allows it to complete
209 // without the caller needing to manage the profiler lifetime. May be invoked
210 // from any thread, but requires that the calling thread has a message loop.
211 static void StartAndRunAsync(PlatformThreadId thread_id,
212 const SamplingParams& params,
213 const CompletedCallback& callback);
214
215 // Initializes the profiler and starts sampling. 240 // Initializes the profiler and starts sampling.
216 void Start(); 241 void Start();
217 242
218 // Stops the profiler and any ongoing sampling. Calling this function is 243 // Stops the profiler and any ongoing sampling. This method will return
219 // optional; if not invoked profiling terminates when all the profiling bursts 244 // immediately with the callback being run asynchronously. At most one
220 // specified in the SamplingParams are completed or the profiler is destroyed, 245 // more stack sample will be taken after this method returns. Calling this
221 // whichever occurs first. 246 // function is optional; if not invoked profiling terminates when all the
247 // profiling bursts specified in the SamplingParams are completed or the
248 // profiler object is destroyed, whichever occurs first.
222 void Stop(); 249 void Stop();
223 250
224 // Set the current system state that is recorded with each captured stack 251 // Set the current system state that is recorded with each captured stack
225 // frame. This is thread-safe so can be called from anywhere. The parameter 252 // frame. This is thread-safe so can be called from anywhere. The parameter
226 // value should be from an enumeration of the appropriate type with values 253 // value should be from an enumeration of the appropriate type with values
227 // ranging from 0 to 31, inclusive. This sets bits within Sample field of 254 // ranging from 0 to 31, inclusive. This sets bits within Sample field of
228 // |process_milestones|. The actual meanings of these bits are defined 255 // |process_milestones|. The actual meanings of these bits are defined
229 // (globally) by the caller(s). 256 // (globally) by the caller(s).
230 static void SetProcessMilestone(int milestone); 257 static void SetProcessMilestone(int milestone);
231 static void ResetAnnotationsForTesting(); 258 static void ResetAnnotationsForTesting();
232 259
233 private: 260 private:
261 friend class TestAPI;
262
234 // SamplingThread is a separate thread used to suspend and sample stacks from 263 // SamplingThread is a separate thread used to suspend and sample stacks from
235 // the target thread. 264 // the target thread.
236 class SamplingThread : public PlatformThread::Delegate { 265 class SamplingThread;
237 public:
238 // Samples stacks using |native_sampler|. When complete, invokes
239 // |completed_callback| with the collected call stack profiles.
240 // |completed_callback| must be callable on any thread.
241 SamplingThread(std::unique_ptr<NativeStackSampler> native_sampler,
242 const SamplingParams& params,
243 const CompletedCallback& completed_callback);
244 ~SamplingThread() override;
245
246 // PlatformThread::Delegate:
247 void ThreadMain() override;
248
249 void Stop();
250
251 private:
252 // Collects |profile| from a single burst. If the profiler was stopped
253 // during collection, sets |was_stopped| and provides the set of samples
254 // collected up to that point.
255 void CollectProfile(CallStackProfile* profile, TimeDelta* elapsed_time,
256 bool* was_stopped);
257
258 // Collects call stack profiles from all bursts, or until the sampling is
259 // stopped. If stopped before complete, the last profile in
260 // |call_stack_profiles| may contain a partial burst.
261 void CollectProfiles(CallStackProfiles* profiles);
262
263 std::unique_ptr<NativeStackSampler> native_sampler_;
264 const SamplingParams params_;
265
266 // If Stop() is called, it signals this event to force the sampling to
267 // terminate before all the samples specified in |params_| are collected.
268 WaitableEvent stop_event_;
269
270 const CompletedCallback completed_callback_;
271
272 DISALLOW_COPY_AND_ASSIGN(SamplingThread);
273 };
274 266
275 // Adds annotations to a Sample. 267 // Adds annotations to a Sample.
276 static void RecordAnnotations(Sample* sample); 268 static void RecordAnnotations(Sample* sample);
277 269
278 // This global variables holds the current system state and is recorded with 270 // This global variables holds the current system state and is recorded with
279 // every captured sample, done on a separate thread which is why updates to 271 // every captured sample, done on a separate thread which is why updates to
280 // this must be atomic. A PostTask to move the the updates to that thread 272 // this must be atomic. A PostTask to move the the updates to that thread
281 // would skew the timing and a lock could result in deadlock if the thread 273 // would skew the timing and a lock could result in deadlock if the thread
282 // making a change was also being profiled and got stopped. 274 // making a change was also being profiled and got stopped.
283 static subtle::Atomic32 process_milestones_; 275 static subtle::Atomic32 process_milestones_;
284 276
285 // The thread whose stack will be sampled. 277 // The thread whose stack will be sampled.
286 PlatformThreadId thread_id_; 278 PlatformThreadId thread_id_;
287 279
288 const SamplingParams params_; 280 const SamplingParams params_;
289 281
290 std::unique_ptr<SamplingThread> sampling_thread_; 282 const CompletedCallback completed_callback_;
291 PlatformThreadHandle sampling_thread_handle_;
292 283
293 const CompletedCallback completed_callback_; 284 // An event signaled when all sampling is complete and the callback done.
285 WaitableEvent finished_event_;
286
287 // Object that does the native sampling. This is created during construction
288 // and later passed to the sampling thread when profiling is started.
289 std::unique_ptr<NativeStackSampler> native_sampler_;
290
291 // An ID uniquely identifying this collection to the sampling thread.
Mike Wittman 2017/02/22 20:32:18 Also mention the conditions under which this will
bcwhite 2017/02/24 20:39:24 Done.
292 int collection_id_;
294 293
295 // Stored until it can be passed to the NativeStackSampler created in Start(). 294 // Stored until it can be passed to the NativeStackSampler created in Start().
296 NativeStackSamplerTestDelegate* const test_delegate_; 295 NativeStackSamplerTestDelegate* const test_delegate_;
297 296
298 DISALLOW_COPY_AND_ASSIGN(StackSamplingProfiler); 297 DISALLOW_COPY_AND_ASSIGN(StackSamplingProfiler);
299 }; 298 };
300 299
301 // These operators permit types to be compared and used in a map of Samples, as 300 // These operators permit types to be compared and used in a map of Samples, as
302 // done in tests and by the metrics provider code. 301 // done in tests and by the metrics provider code.
303 BASE_EXPORT bool operator==(const StackSamplingProfiler::Module& a, 302 BASE_EXPORT bool operator==(const StackSamplingProfiler::Module& a,
304 const StackSamplingProfiler::Module& b); 303 const StackSamplingProfiler::Module& b);
305 BASE_EXPORT bool operator==(const StackSamplingProfiler::Sample& a, 304 BASE_EXPORT bool operator==(const StackSamplingProfiler::Sample& a,
306 const StackSamplingProfiler::Sample& b); 305 const StackSamplingProfiler::Sample& b);
307 BASE_EXPORT bool operator!=(const StackSamplingProfiler::Sample& a, 306 BASE_EXPORT bool operator!=(const StackSamplingProfiler::Sample& a,
308 const StackSamplingProfiler::Sample& b); 307 const StackSamplingProfiler::Sample& b);
309 BASE_EXPORT bool operator<(const StackSamplingProfiler::Sample& a, 308 BASE_EXPORT bool operator<(const StackSamplingProfiler::Sample& a,
310 const StackSamplingProfiler::Sample& b); 309 const StackSamplingProfiler::Sample& b);
311 BASE_EXPORT bool operator==(const StackSamplingProfiler::Frame& a, 310 BASE_EXPORT bool operator==(const StackSamplingProfiler::Frame& a,
312 const StackSamplingProfiler::Frame& b); 311 const StackSamplingProfiler::Frame& b);
313 BASE_EXPORT bool operator<(const StackSamplingProfiler::Frame& a, 312 BASE_EXPORT bool operator<(const StackSamplingProfiler::Frame& a,
314 const StackSamplingProfiler::Frame& b); 313 const StackSamplingProfiler::Frame& b);
315 314
316 } // namespace base 315 } // namespace base
317 316
318 #endif // BASE_PROFILER_STACK_SAMPLING_PROFILER_H_ 317 #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