Revert "Fix broken channel icon in chrome://help on CrOS" and try again
[chromium-blink-merge.git] / base / profiler / stack_sampling_profiler.h
blob5a6c3d0514d00a337d462f06e79c0e89ced5a249
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
3 // found in the LICENSE file.
5 #ifndef BASE_PROFILER_STACK_SAMPLING_PROFILER_H_
6 #define BASE_PROFILER_STACK_SAMPLING_PROFILER_H_
8 #include <string>
9 #include <vector>
11 #include "base/base_export.h"
12 #include "base/callback.h"
13 #include "base/files/file_path.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/strings/string16.h"
16 #include "base/synchronization/waitable_event.h"
17 #include "base/threading/platform_thread.h"
18 #include "base/time/time.h"
20 namespace base {
22 class NativeStackSampler;
24 // StackSamplingProfiler periodically stops a thread to sample its stack, for
25 // the purpose of collecting information about which code paths are
26 // executing. This information is used in aggregate by UMA to identify hot
27 // and/or janky code paths.
29 // Sample StackSamplingProfiler usage:
31 // // Create and customize params as desired.
32 // base::StackStackSamplingProfiler::SamplingParams params;
33 // // Any thread's ID may be passed as the target.
34 // base::StackSamplingProfiler profiler(base::PlatformThread::CurrentId()),
35 // params);
37 // // Or, to process the profiles within Chrome rather than via UMA, use a
38 // // custom completed callback:
39 // base::StackStackSamplingProfiler::CompletedCallback
40 // thread_safe_callback = ...;
41 // base::StackSamplingProfiler profiler(base::PlatformThread::CurrentId()),
42 // params, thread_safe_callback);
44 // profiler.Start();
45 // // ... work being done on the target thread here ...
46 // profiler.Stop(); // optional, stops collection before complete per params
48 // The default SamplingParams causes stacks to be recorded in a single burst at
49 // a 10Hz interval for a total of 30 seconds. All of these parameters may be
50 // altered as desired.
52 // When all call stack profiles are complete, or the profiler is stopped, the
53 // completed callback is called from a thread created by the profiler with the
54 // completed profiles. A profile is considered complete if all requested samples
55 // were recorded for the profile (i.e. it was not stopped prematurely).
57 // The results of the profiling are passed to the completed callback and consist
58 // of a vector of CallStackProfiles. Each CallStackProfile corresponds to a
59 // burst as specified in SamplingParams and contains a set of Samples and
60 // Modules. One Sample corresponds to a single recorded stack, and the Modules
61 // record those modules associated with the recorded stack frames.
62 class BASE_EXPORT StackSamplingProfiler {
63 public:
64 // Module represents the module (DLL or exe) corresponding to a stack frame.
65 struct BASE_EXPORT Module {
66 Module();
67 Module(const void* base_address, const std::string& id,
68 const FilePath& filename);
69 ~Module();
71 // Points to the base address of the module.
72 const void* base_address;
74 // An opaque binary string that uniquely identifies a particular program
75 // version with high probability. This is parsed from headers of the loaded
76 // module.
77 // For binaries generated by GNU tools:
78 // Contents of the .note.gnu.build-id field.
79 // On Windows:
80 // GUID + AGE in the debug image headers of a module.
81 std::string id;
83 // The filename of the module.
84 FilePath filename;
87 // Frame represents an individual sampled stack frame with module information.
88 struct BASE_EXPORT Frame {
89 // Identifies an unknown module.
90 static const size_t kUnknownModuleIndex = static_cast<size_t>(-1);
92 Frame(const void* instruction_pointer, size_t module_index);
93 ~Frame();
95 // The sampled instruction pointer within the function.
96 const void* instruction_pointer;
98 // Index of the module in CallStackProfile::modules. We don't represent
99 // module state directly here to save space.
100 size_t module_index;
103 // Sample represents a set of stack frames.
104 using Sample = std::vector<Frame>;
106 // CallStackProfile represents a set of samples.
107 struct BASE_EXPORT CallStackProfile {
108 CallStackProfile();
109 ~CallStackProfile();
111 std::vector<Module> modules;
112 std::vector<Sample> samples;
114 // Duration of this profile.
115 TimeDelta profile_duration;
117 // Time between samples.
118 TimeDelta sampling_period;
121 using CallStackProfiles = std::vector<CallStackProfile>;
123 // Represents parameters that configure the sampling.
124 struct BASE_EXPORT SamplingParams {
125 SamplingParams();
127 // Time to delay before first samples are taken. Defaults to 0.
128 TimeDelta initial_delay;
130 // Number of sampling bursts to perform. Defaults to 1.
131 int bursts;
133 // Interval between sampling bursts. This is the desired duration from the
134 // start of one burst to the start of the next burst. Defaults to 10s.
135 TimeDelta burst_interval;
137 // Number of samples to record per burst. Defaults to 300.
138 int samples_per_burst;
140 // Interval between samples during a sampling burst. This is the desired
141 // duration from the start of one sample to the start of the next
142 // sample. Defaults to 100ms.
143 TimeDelta sampling_interval;
146 // The callback type used to collect completed profiles.
148 // IMPORTANT NOTE: the callback is invoked on a thread the profiler
149 // constructs, rather than on the thread used to construct the profiler and
150 // set the callback, and thus the callback must be callable on any thread. For
151 // threads with message loops that create StackSamplingProfilers, posting a
152 // task to the message loop with a copy of the profiles is the recommended
153 // thread-safe callback implementation.
154 using CompletedCallback = Callback<void(const CallStackProfiles&)>;
156 // Creates a profiler that sends completed profiles to |callback|.
157 StackSamplingProfiler(PlatformThreadId thread_id,
158 const SamplingParams& params,
159 const CompletedCallback& callback);
160 // Stops any profiling currently taking place before destroying the profiler.
161 ~StackSamplingProfiler();
163 // The fire-and-forget interface: starts a profiler and allows it to complete
164 // without the caller needing to manage the profiler lifetime. May be invoked
165 // from any thread, but requires that the calling thread has a message loop.
166 static void StartAndRunAsync(PlatformThreadId thread_id,
167 const SamplingParams& params,
168 const CompletedCallback& callback);
170 // Initializes the profiler and starts sampling.
171 void Start();
173 // Stops the profiler and any ongoing sampling. Calling this function is
174 // optional; if not invoked profiling terminates when all the profiling bursts
175 // specified in the SamplingParams are completed or the profiler is destroyed,
176 // whichever occurs first.
177 void Stop();
179 private:
180 // SamplingThread is a separate thread used to suspend and sample stacks from
181 // the target thread.
182 class SamplingThread : public PlatformThread::Delegate {
183 public:
184 // Samples stacks using |native_sampler|. When complete, invokes
185 // |completed_callback| with the collected call stack profiles.
186 // |completed_callback| must be callable on any thread.
187 SamplingThread(scoped_ptr<NativeStackSampler> native_sampler,
188 const SamplingParams& params,
189 const CompletedCallback& completed_callback);
190 ~SamplingThread() override;
192 // PlatformThread::Delegate:
193 void ThreadMain() override;
195 void Stop();
197 private:
198 // Collects a call stack profile from a single burst. Returns true if the
199 // profile was collected, or false if collection was stopped before it
200 // completed.
201 bool CollectProfile(CallStackProfile* profile, TimeDelta* elapsed_time);
203 // Collects call stack profiles from all bursts, or until the sampling is
204 // stopped. If stopped before complete, |call_stack_profiles| will contain
205 // only full bursts.
206 void CollectProfiles(CallStackProfiles* profiles);
208 scoped_ptr<NativeStackSampler> native_sampler_;
209 const SamplingParams params_;
211 // If Stop() is called, it signals this event to force the sampling to
212 // terminate before all the samples specified in |params_| are collected.
213 WaitableEvent stop_event_;
215 const CompletedCallback completed_callback_;
217 DISALLOW_COPY_AND_ASSIGN(SamplingThread);
220 // The thread whose stack will be sampled.
221 PlatformThreadId thread_id_;
223 const SamplingParams params_;
225 scoped_ptr<SamplingThread> sampling_thread_;
226 PlatformThreadHandle sampling_thread_handle_;
228 const CompletedCallback completed_callback_;
230 DISALLOW_COPY_AND_ASSIGN(StackSamplingProfiler);
233 // The metrics provider code wants to put Samples in a map and compare them,
234 // which requires us to define a few operators.
235 BASE_EXPORT bool operator==(const StackSamplingProfiler::Frame& a,
236 const StackSamplingProfiler::Frame& b);
237 BASE_EXPORT bool operator<(const StackSamplingProfiler::Frame& a,
238 const StackSamplingProfiler::Frame& b);
240 } // namespace base
242 #endif // BASE_PROFILER_STACK_SAMPLING_PROFILER_H_