OLD | NEW |
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 <string> | 8 #include <string> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/base_export.h" | 11 #include "base/base_export.h" |
12 #include "base/callback.h" | 12 #include "base/callback.h" |
13 #include "base/files/file_path.h" | 13 #include "base/files/file_path.h" |
14 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
15 #include "base/strings/string16.h" | 15 #include "base/strings/string16.h" |
| 16 #include "base/synchronization/waitable_event.h" |
16 #include "base/threading/platform_thread.h" | 17 #include "base/threading/platform_thread.h" |
17 #include "base/time/time.h" | 18 #include "base/time/time.h" |
18 | 19 |
19 namespace base { | 20 namespace base { |
20 | 21 |
| 22 class NativeStackSampler; |
| 23 |
21 // StackSamplingProfiler periodically stops a thread to sample its stack, for | 24 // StackSamplingProfiler periodically stops a thread to sample its stack, for |
22 // the purpose of collecting information about which code paths are | 25 // the purpose of collecting information about which code paths are |
23 // executing. This information is used in aggregate by UMA to identify hot | 26 // executing. This information is used in aggregate by UMA to identify hot |
24 // and/or janky code paths. | 27 // and/or janky code paths. |
25 // | 28 // |
26 // Sample StackStackSamplingProfiler usage: | 29 // Sample StackSamplingProfiler usage: |
27 // | 30 // |
28 // // Create and customize params as desired. | 31 // // Create and customize params as desired. |
29 // base::StackStackSamplingProfiler::SamplingParams params; | 32 // base::StackStackSamplingProfiler::SamplingParams params; |
30 // // Any thread's ID may be passed as the target. | 33 // // Any thread's ID may be passed as the target. |
31 // base::StackSamplingProfiler profiler(base::PlatformThread::CurrentId()), | 34 // base::StackSamplingProfiler profiler(base::PlatformThread::CurrentId()), |
32 // params); | 35 // params); |
33 // | 36 // |
34 // // To process the profiles within Chrome rather than via UMA, set a custom | 37 // // Or, to process the profiles within Chrome rather than via UMA, use a |
35 // // completed callback: | 38 // // custom completed callback: |
36 // base::Callback<void(const std::vector<Profile>&)> | 39 // base::StackStackSamplingProfiler::CompletedCallback |
37 // thread_safe_callback = ...; | 40 // thread_safe_callback = ...; |
38 // profiler.SetCustomCompletedCallback(thread_safe_callback); | 41 // base::StackSamplingProfiler profiler(base::PlatformThread::CurrentId()), |
| 42 // params, thread_safe_callback); |
39 // | 43 // |
40 // profiler.Start(); | 44 // profiler.Start(); |
41 // // ... work being done on the target thread here ... | 45 // // ... work being done on the target thread here ... |
42 // profiler.Stop(); // optional, stops collection before complete per params | 46 // profiler.Stop(); // optional, stops collection before complete per params |
43 // | 47 // |
44 // When all profiles are complete or the profiler is stopped, if the custom | 48 // The default SamplingParams causes stacks to be recorded in a single burst at |
45 // completed callback was set it will be called from the profiler thread with | 49 // a 10Hz interval for a total of 30 seconds. All of these parameters may be |
46 // the completed profiles. If no callback was set, the profiles are stored | 50 // altered as desired. |
47 // internally and retrieved for UMA through | 51 // |
48 // GetPendingProfiles(). GetPendingProfiles() should never be called by other | 52 // When all call stack profiles are complete or the profiler is stopped, if the |
49 // code; to retrieve profiles for in-process processing, set a completed | 53 // custom completed callback was set it is called from a thread created by the |
50 // callback. | 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. |
| 60 // |
| 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. |
51 class BASE_EXPORT StackSamplingProfiler { | 66 class BASE_EXPORT StackSamplingProfiler { |
52 public: | 67 public: |
53 // Module represents the module (DLL or exe) corresponding to a stack frame. | 68 // Module represents the module (DLL or exe) corresponding to a stack frame. |
54 struct BASE_EXPORT Module { | 69 struct BASE_EXPORT Module { |
55 Module(); | 70 Module(); |
| 71 Module(const void* base_address, const std::string& id, |
| 72 const FilePath& filename); |
56 ~Module(); | 73 ~Module(); |
57 | 74 |
58 // Points to the base address of the module. | 75 // Points to the base address of the module. |
59 const void* base_address; | 76 const void* base_address; |
| 77 |
60 // An opaque binary string that uniquely identifies a particular program | 78 // An opaque binary string that uniquely identifies a particular program |
61 // version with high probability. This is parsed from headers of the loaded | 79 // version with high probability. This is parsed from headers of the loaded |
62 // module. | 80 // module. |
63 // For binaries generated by GNU tools: | 81 // For binaries generated by GNU tools: |
64 // Contents of the .note.gnu.build-id field. | 82 // Contents of the .note.gnu.build-id field. |
65 // On Windows: | 83 // On Windows: |
66 // GUID + AGE in the debug image headers of a module. | 84 // GUID + AGE in the debug image headers of a module. |
67 std::string id; | 85 std::string id; |
| 86 |
68 // The filename of the module. | 87 // The filename of the module. |
69 FilePath filename; | 88 FilePath filename; |
70 }; | 89 }; |
71 | 90 |
72 // Frame represents an individual sampled stack frame with module information. | 91 // Frame represents an individual sampled stack frame with module information. |
73 struct BASE_EXPORT Frame { | 92 struct BASE_EXPORT Frame { |
74 Frame(); | 93 // Identifies an unknown module. |
| 94 static const size_t kUnknownModuleIndex = static_cast<size_t>(-1); |
| 95 |
| 96 Frame(const void* instruction_pointer, size_t module_index); |
75 ~Frame(); | 97 ~Frame(); |
76 | 98 |
77 // The sampled instruction pointer within the function. | 99 // The sampled instruction pointer within the function. |
78 const void* instruction_pointer; | 100 const void* instruction_pointer; |
79 // Index of the module in the array of modules. We don't represent module | 101 |
80 // state directly here to save space. | 102 // Index of the module in CallStackProfile::modules. We don't represent |
81 int module_index; | 103 // module state directly here to save space. |
| 104 size_t module_index; |
82 }; | 105 }; |
83 | 106 |
84 // Sample represents a set of stack frames. | 107 // Sample represents a set of stack frames. |
85 using Sample = std::vector<Frame>; | 108 using Sample = std::vector<Frame>; |
86 | 109 |
87 // Profile represents a set of samples. | 110 // CallStackProfile represents a set of samples. |
88 struct BASE_EXPORT Profile { | 111 struct BASE_EXPORT CallStackProfile { |
89 Profile(); | 112 CallStackProfile(); |
90 ~Profile(); | 113 ~CallStackProfile(); |
91 | 114 |
92 std::vector<Module> modules; | 115 std::vector<Module> modules; |
93 std::vector<Sample> samples; | 116 std::vector<Sample> samples; |
| 117 |
94 // Duration of this profile. | 118 // Duration of this profile. |
95 TimeDelta profile_duration; | 119 TimeDelta profile_duration; |
| 120 |
96 // Time between samples. | 121 // Time between samples. |
97 TimeDelta sampling_period; | 122 TimeDelta sampling_period; |
| 123 |
98 // True if sample ordering is important and should be preserved if and when | 124 // True if sample ordering is important and should be preserved if and when |
99 // this profile is compressed and processed. | 125 // this profile is compressed and processed. |
100 bool preserve_sample_ordering; | 126 bool preserve_sample_ordering; |
| 127 |
| 128 // User data associated with this profile. |
| 129 uintptr_t user_data; |
101 }; | 130 }; |
102 | 131 |
103 // NativeStackSampler abstracts the native implementation required to record a | 132 using CallStackProfiles = std::vector<CallStackProfile>; |
104 // stack sample for a given thread. | |
105 class NativeStackSampler { | |
106 public: | |
107 virtual ~NativeStackSampler(); | |
108 | |
109 // Create a stack sampler that records samples for |thread_handle|. Returns | |
110 // null if this platform does not support stack sampling. | |
111 static scoped_ptr<NativeStackSampler> Create(PlatformThreadId thread_id); | |
112 | |
113 // Notify the sampler that we're starting to record a new profile. This | |
114 // function is called on the SamplingThread. | |
115 virtual void ProfileRecordingStarting(Profile* profile) = 0; | |
116 | |
117 // Record a stack sample. This function is called on the SamplingThread. | |
118 virtual void RecordStackSample(Sample* sample) = 0; | |
119 | |
120 // Notify the sampler that we've stopped recording the current profile. This | |
121 // function is called on the SamplingThread. | |
122 virtual void ProfileRecordingStopped() = 0; | |
123 | |
124 protected: | |
125 NativeStackSampler(); | |
126 | |
127 private: | |
128 DISALLOW_COPY_AND_ASSIGN(NativeStackSampler); | |
129 }; | |
130 | 133 |
131 // Represents parameters that configure the sampling. | 134 // Represents parameters that configure the sampling. |
132 struct BASE_EXPORT SamplingParams { | 135 struct BASE_EXPORT SamplingParams { |
133 SamplingParams(); | 136 SamplingParams(); |
134 | 137 |
135 // Time to delay before first samples are taken. Defaults to 0. | 138 // Time to delay before first samples are taken. Defaults to 0. |
136 TimeDelta initial_delay; | 139 TimeDelta initial_delay; |
| 140 |
137 // Number of sampling bursts to perform. Defaults to 1. | 141 // Number of sampling bursts to perform. Defaults to 1. |
138 int bursts; | 142 int bursts; |
| 143 |
139 // Interval between sampling bursts. This is the desired duration from the | 144 // Interval between sampling bursts. This is the desired duration from the |
140 // start of one burst to the start of the next burst. Defaults to 10s. | 145 // start of one burst to the start of the next burst. Defaults to 10s. |
141 TimeDelta burst_interval; | 146 TimeDelta burst_interval; |
| 147 |
142 // Number of samples to record per burst. Defaults to 300. | 148 // Number of samples to record per burst. Defaults to 300. |
143 int samples_per_burst; | 149 int samples_per_burst; |
| 150 |
144 // Interval between samples during a sampling burst. This is the desired | 151 // Interval between samples during a sampling burst. This is the desired |
145 // duration from the start of one burst to the start of the next | 152 // duration from the start of one sample to the start of the next |
146 // burst. Defaults to 100ms. | 153 // sample. Defaults to 100ms. |
147 TimeDelta sampling_interval; | 154 TimeDelta sampling_interval; |
| 155 |
148 // True if sample ordering is important and should be preserved if and when | 156 // True if sample ordering is important and should be preserved if and when |
149 // this profile is compressed and processed. Defaults to false. | 157 // this profile is compressed and processed. Defaults to false. |
150 bool preserve_sample_ordering; | 158 bool preserve_sample_ordering; |
| 159 |
| 160 // User data associated with this profile. |
| 161 uintptr_t user_data; |
151 }; | 162 }; |
152 | 163 |
| 164 // The callback type used to collect completed profiles. |
| 165 // |
| 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&)>; |
| 173 |
| 174 // Creates a profiler that sends completed profiles to the default completed |
| 175 // callback. |
153 StackSamplingProfiler(PlatformThreadId thread_id, | 176 StackSamplingProfiler(PlatformThreadId thread_id, |
154 const SamplingParams& params); | 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); |
155 ~StackSamplingProfiler(); | 182 ~StackSamplingProfiler(); |
156 | 183 |
157 // Initializes the profiler and starts sampling. | 184 // Initializes the profiler and starts sampling. |
158 void Start(); | 185 void Start(); |
| 186 |
159 // Stops the profiler and any ongoing sampling. Calling this function is | 187 // Stops the profiler and any ongoing sampling. Calling this function is |
160 // optional; if not invoked profiling will terminate when all the profiling | 188 // optional; if not invoked profiling terminates when all the profiling bursts |
161 // bursts specified in the SamplingParams are completed. | 189 // specified in the SamplingParams are completed. |
162 void Stop(); | 190 void Stop(); |
163 | 191 |
164 // Gets the pending profiles into *|profiles| and clears the internal | 192 // Sets a callback to process profiles collected by profiler instances without |
165 // storage. This function is thread safe. | 193 // a completed callback. Profiles are queued internally until a non-null |
| 194 // callback is provided to this function, |
166 // | 195 // |
167 // ***This is intended for use only by UMA.*** Callers who want to process the | 196 // The callback is typically called on a thread created by the profiler. If |
168 // collected profiles should use SetCustomCompletedCallback. | 197 // completed profiles are queued when set, however, it will also be called |
169 static void GetPendingProfiles(std::vector<Profile>* profiles); | 198 // immediately on the calling thread. |
170 | 199 static void SetDefaultCompletedCallback(CompletedCallback callback); |
171 // By default, collected profiles are stored internally and can be retrieved | |
172 // by GetPendingProfiles. If a callback is provided via this function, | |
173 // however, it will be called with the collected profiles instead. Note that | |
174 // this call to the callback occurs *on the profiler thread*. | |
175 void SetCustomCompletedCallback( | |
176 Callback<void(const std::vector<Profile>&)> callback); | |
177 | 200 |
178 private: | 201 private: |
179 class SamplingThread; | 202 // SamplingThread is a separate thread used to suspend and sample stacks from |
180 struct SamplingThreadDeleter { | 203 // the target thread. |
181 void operator() (SamplingThread* thread) const; | 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; |
| 213 |
| 214 // PlatformThread::Delegate: |
| 215 void ThreadMain() override; |
| 216 |
| 217 void Stop(); |
| 218 |
| 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); |
| 224 |
| 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); |
| 229 |
| 230 scoped_ptr<NativeStackSampler> native_sampler_; |
| 231 const SamplingParams params_; |
| 232 |
| 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_; |
| 236 |
| 237 const CompletedCallback completed_callback_; |
| 238 |
| 239 DISALLOW_COPY_AND_ASSIGN(SamplingThread); |
182 }; | 240 }; |
183 | 241 |
184 // The thread whose stack will be sampled. | 242 // The thread whose stack will be sampled. |
185 PlatformThreadId thread_id_; | 243 PlatformThreadId thread_id_; |
186 | 244 |
187 const SamplingParams params_; | 245 const SamplingParams params_; |
188 | 246 |
189 scoped_ptr<SamplingThread, SamplingThreadDeleter> sampling_thread_; | 247 scoped_ptr<SamplingThread> sampling_thread_; |
190 scoped_ptr<NativeStackSampler> native_sampler_; | 248 PlatformThreadHandle sampling_thread_handle_; |
191 | 249 |
192 Callback<void(const std::vector<Profile>&)> custom_completed_callback_; | 250 const CompletedCallback completed_callback_; |
193 | 251 |
194 DISALLOW_COPY_AND_ASSIGN(StackSamplingProfiler); | 252 DISALLOW_COPY_AND_ASSIGN(StackSamplingProfiler); |
195 }; | 253 }; |
196 | 254 |
197 // Defined to allow equality check of Samples. | 255 // The metrics provider code wants to put Samples in a map and compare them, |
| 256 // which requires us to define a few operators. |
198 BASE_EXPORT bool operator==(const StackSamplingProfiler::Frame& a, | 257 BASE_EXPORT bool operator==(const StackSamplingProfiler::Frame& a, |
199 const StackSamplingProfiler::Frame& b); | 258 const StackSamplingProfiler::Frame& b); |
200 // Defined to allow ordering of Samples. | |
201 BASE_EXPORT bool operator<(const StackSamplingProfiler::Frame& a, | 259 BASE_EXPORT bool operator<(const StackSamplingProfiler::Frame& a, |
202 const StackSamplingProfiler::Frame& b); | 260 const StackSamplingProfiler::Frame& b); |
203 | 261 |
204 } // namespace base | 262 } // namespace base |
205 | 263 |
206 #endif // BASE_PROFILER_STACK_SAMPLING_PROFILER_H_ | 264 #endif // BASE_PROFILER_STACK_SAMPLING_PROFILER_H_ |
OLD | NEW |