Compute can_use_lcd_text using property trees.
[chromium-blink-merge.git] / base / profiler / stack_sampling_profiler.h
blob9d52f27a7efc84645ec681756b3a92b194ec26d6
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, if the
53 // custom completed callback was set it is called from a thread created by the
54 // profiler with the completed profiles. A profile is considered complete if all
55 // requested samples were recorded for the profile (i.e. it was not stopped
56 // prematurely). If no callback was set, the default completed callback will be
57 // called with the profiles. It is expected that the the default completed
58 // callback is set by the metrics system to allow profiles to be provided via
59 // UMA.
61 // The results of the profiling are passed to the completed callback and consist
62 // of a vector of CallStackProfiles. Each CallStackProfile corresponds to a
63 // burst as specified in SamplingParams and contains a set of Samples and
64 // Modules. One Sample corresponds to a single recorded stack, and the Modules
65 // record those modules associated with the recorded stack frames.
66 class BASE_EXPORT StackSamplingProfiler {
67 public:
68 // Module represents the module (DLL or exe) corresponding to a stack frame.
69 struct BASE_EXPORT Module {
70 Module();
71 Module(const void* base_address, const std::string& id,
72 const FilePath& filename);
73 ~Module();
75 // Points to the base address of the module.
76 const void* base_address;
78 // An opaque binary string that uniquely identifies a particular program
79 // version with high probability. This is parsed from headers of the loaded
80 // module.
81 // For binaries generated by GNU tools:
82 // Contents of the .note.gnu.build-id field.
83 // On Windows:
84 // GUID + AGE in the debug image headers of a module.
85 std::string id;
87 // The filename of the module.
88 FilePath filename;
91 // Frame represents an individual sampled stack frame with module information.
92 struct BASE_EXPORT Frame {
93 // Identifies an unknown module.
94 static const size_t kUnknownModuleIndex = static_cast<size_t>(-1);
96 Frame(const void* instruction_pointer, size_t module_index);
97 ~Frame();
99 // The sampled instruction pointer within the function.
100 const void* instruction_pointer;
102 // Index of the module in CallStackProfile::modules. We don't represent
103 // module state directly here to save space.
104 size_t module_index;
107 // Sample represents a set of stack frames.
108 using Sample = std::vector<Frame>;
110 // CallStackProfile represents a set of samples.
111 struct BASE_EXPORT CallStackProfile {
112 CallStackProfile();
113 ~CallStackProfile();
115 std::vector<Module> modules;
116 std::vector<Sample> samples;
118 // Duration of this profile.
119 TimeDelta profile_duration;
121 // Time between samples.
122 TimeDelta sampling_period;
124 // True if sample ordering is important and should be preserved if and when
125 // this profile is compressed and processed.
126 bool preserve_sample_ordering;
128 // User data associated with this profile.
129 uintptr_t user_data;
132 using CallStackProfiles = std::vector<CallStackProfile>;
134 // Represents parameters that configure the sampling.
135 struct BASE_EXPORT SamplingParams {
136 SamplingParams();
138 // Time to delay before first samples are taken. Defaults to 0.
139 TimeDelta initial_delay;
141 // Number of sampling bursts to perform. Defaults to 1.
142 int bursts;
144 // Interval between sampling bursts. This is the desired duration from the
145 // start of one burst to the start of the next burst. Defaults to 10s.
146 TimeDelta burst_interval;
148 // Number of samples to record per burst. Defaults to 300.
149 int samples_per_burst;
151 // Interval between samples during a sampling burst. This is the desired
152 // duration from the start of one sample to the start of the next
153 // sample. Defaults to 100ms.
154 TimeDelta sampling_interval;
156 // True if sample ordering is important and should be preserved if and when
157 // this profile is compressed and processed. Defaults to false.
158 bool preserve_sample_ordering;
160 // User data associated with this profile.
161 uintptr_t user_data;
164 // The callback type used to collect completed profiles.
166 // IMPORTANT NOTE: the callback is invoked on a thread the profiler
167 // constructs, rather than on the thread used to construct the profiler and
168 // set the callback, and thus the callback must be callable on any thread. For
169 // threads with message loops that create StackSamplingProfilers, posting a
170 // task to the message loop with a copy of the profiles is the recommended
171 // thread-safe callback implementation.
172 using CompletedCallback = Callback<void(const CallStackProfiles&)>;
174 // Creates a profiler that sends completed profiles to the default completed
175 // callback.
176 StackSamplingProfiler(PlatformThreadId thread_id,
177 const SamplingParams& params);
178 // Creates a profiler that sends completed profiles to |completed_callback|.
179 StackSamplingProfiler(PlatformThreadId thread_id,
180 const SamplingParams& params,
181 CompletedCallback callback);
182 ~StackSamplingProfiler();
184 // Initializes the profiler and starts sampling.
185 void Start();
187 // Stops the profiler and any ongoing sampling. Calling this function is
188 // optional; if not invoked profiling terminates when all the profiling bursts
189 // specified in the SamplingParams are completed.
190 void Stop();
192 // Sets a callback to process profiles collected by profiler instances without
193 // a completed callback. Profiles are queued internally until a non-null
194 // callback is provided to this function,
196 // The callback is typically called on a thread created by the profiler. If
197 // completed profiles are queued when set, however, it will also be called
198 // immediately on the calling thread.
199 static void SetDefaultCompletedCallback(CompletedCallback callback);
201 private:
202 // SamplingThread is a separate thread used to suspend and sample stacks from
203 // the target thread.
204 class SamplingThread : public PlatformThread::Delegate {
205 public:
206 // Samples stacks using |native_sampler|. When complete, invokes
207 // |completed_callback| with the collected call stack profiles.
208 // |completed_callback| must be callable on any thread.
209 SamplingThread(scoped_ptr<NativeStackSampler> native_sampler,
210 const SamplingParams& params,
211 CompletedCallback completed_callback);
212 ~SamplingThread() override;
214 // PlatformThread::Delegate:
215 void ThreadMain() override;
217 void Stop();
219 private:
220 // Collects a call stack profile from a single burst. Returns true if the
221 // profile was collected, or false if collection was stopped before it
222 // completed.
223 bool CollectProfile(CallStackProfile* profile, TimeDelta* elapsed_time);
225 // Collects call stack profiles from all bursts, or until the sampling is
226 // stopped. If stopped before complete, |call_stack_profiles| will contain
227 // only full bursts.
228 void CollectProfiles(CallStackProfiles* profiles);
230 scoped_ptr<NativeStackSampler> native_sampler_;
231 const SamplingParams params_;
233 // If Stop() is called, it signals this event to force the sampling to
234 // terminate before all the samples specified in |params_| are collected.
235 WaitableEvent stop_event_;
237 const CompletedCallback completed_callback_;
239 DISALLOW_COPY_AND_ASSIGN(SamplingThread);
242 // The thread whose stack will be sampled.
243 PlatformThreadId thread_id_;
245 const SamplingParams params_;
247 scoped_ptr<SamplingThread> sampling_thread_;
248 PlatformThreadHandle sampling_thread_handle_;
250 const CompletedCallback completed_callback_;
252 DISALLOW_COPY_AND_ASSIGN(StackSamplingProfiler);
255 // The metrics provider code wants to put Samples in a map and compare them,
256 // which requires us to define a few operators.
257 BASE_EXPORT bool operator==(const StackSamplingProfiler::Frame& a,
258 const StackSamplingProfiler::Frame& b);
259 BASE_EXPORT bool operator<(const StackSamplingProfiler::Frame& a,
260 const StackSamplingProfiler::Frame& b);
262 } // namespace base
264 #endif // BASE_PROFILER_STACK_SAMPLING_PROFILER_H_