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 |
(...skipping 16 matching lines...) Expand all Loading... |
27 // and/or janky code paths. | 27 // and/or janky code paths. |
28 // | 28 // |
29 // Sample StackSamplingProfiler usage: | 29 // Sample StackSamplingProfiler usage: |
30 // | 30 // |
31 // // Create and customize params as desired. | 31 // // Create and customize params as desired. |
32 // base::StackStackSamplingProfiler::SamplingParams params; | 32 // base::StackStackSamplingProfiler::SamplingParams params; |
33 // // Any thread's ID may be passed as the target. | 33 // // Any thread's ID may be passed as the target. |
34 // base::StackSamplingProfiler profiler(base::PlatformThread::CurrentId()), | 34 // base::StackSamplingProfiler profiler(base::PlatformThread::CurrentId()), |
35 // params); | 35 // params); |
36 // | 36 // |
37 // // To process the call stack profiles within Chrome rather than via UMA, | 37 // // Or, to process the profiles within Chrome rather than via UMA, use a |
38 // // set a custom completed callback: | 38 // // custom completed callback: |
39 // base::StackStackSamplingProfiler::CompletedCallback | 39 // base::StackStackSamplingProfiler::CompletedCallback |
40 // thread_safe_callback = ...; | 40 // thread_safe_callback = ...; |
41 // profiler.SetCustomCompletedCallback(thread_safe_callback); | 41 // base::StackSamplingProfiler profiler(base::PlatformThread::CurrentId()), |
| 42 // params, thread_safe_callback); |
42 // | 43 // |
43 // profiler.Start(); | 44 // profiler.Start(); |
44 // // ... work being done on the target thread here ... | 45 // // ... work being done on the target thread here ... |
45 // profiler.Stop(); // optional, stops collection before complete per params | 46 // profiler.Stop(); // optional, stops collection before complete per params |
46 // | 47 // |
47 // The default SamplingParams causes stacks to be recorded in a single burst at | 48 // The default SamplingParams causes stacks to be recorded in a single burst at |
48 // a 10Hz interval for a total of 30 seconds. All of these parameters may be | 49 // a 10Hz interval for a total of 30 seconds. All of these parameters may be |
49 // altered as desired. | 50 // altered as desired. |
50 // | 51 // |
51 // When all call stack profiles are complete or the profiler is stopped, if the | 52 // When all call stack profiles are complete or the profiler is stopped, if the |
52 // custom completed callback was set it is called from the profiler thread with | 53 // custom completed callback was set it is called from a thread created by the |
53 // the completed profiles. A profile is considered complete if all requested | 54 // profiler with the completed profiles. A profile is considered complete if all |
54 // samples were recorded for the profile (i.e. it was not stopped | 55 // requested samples were recorded for the profile (i.e. it was not stopped |
55 // prematurely). If no callback was set, the completed profiles are stored | 56 // prematurely). If no callback was set, the default completed callback will be |
56 // internally and retrieved for UMA through GetPendingProfiles(). | 57 // called with the profiles. It is expected that the the default completed |
57 // GetPendingProfiles() should never be called by other code; to retrieve | 58 // callback is set by the metrics system to allow profiles to be provided via |
58 // profiles for in-process processing, set a completed callback. | 59 // UMA. |
59 // | 60 // |
60 // The results of the profiling are passed to the completed callback and consist | 61 // The results of the profiling are passed to the completed callback and consist |
61 // of a vector of CallStackProfiles. Each CallStackProfile corresponds to a | 62 // of a vector of CallStackProfiles. Each CallStackProfile corresponds to a |
62 // burst as specified in SamplingParams and contains a set of Samples and | 63 // burst as specified in SamplingParams and contains a set of Samples and |
63 // Modules. One Sample corresponds to a single recorded stack, and the Modules | 64 // Modules. One Sample corresponds to a single recorded stack, and the Modules |
64 // record those modules associated with the recorded stack frames. | 65 // record those modules associated with the recorded stack frames. |
65 class BASE_EXPORT StackSamplingProfiler { | 66 class BASE_EXPORT StackSamplingProfiler { |
66 public: | 67 public: |
67 // 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. |
68 struct BASE_EXPORT Module { | 69 struct BASE_EXPORT Module { |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
157 // The callback type used to collect completed profiles. | 158 // The callback type used to collect completed profiles. |
158 // | 159 // |
159 // IMPORTANT NOTE: the callback is invoked on a thread the profiler | 160 // IMPORTANT NOTE: the callback is invoked on a thread the profiler |
160 // constructs, rather than on the thread used to construct the profiler and | 161 // constructs, rather than on the thread used to construct the profiler and |
161 // set the callback, and thus the callback must be callable on any thread. For | 162 // set the callback, and thus the callback must be callable on any thread. For |
162 // threads with message loops that create StackSamplingProfilers, posting a | 163 // threads with message loops that create StackSamplingProfilers, posting a |
163 // task to the message loop with a copy of the profiles is the recommended | 164 // task to the message loop with a copy of the profiles is the recommended |
164 // thread-safe callback implementation. | 165 // thread-safe callback implementation. |
165 using CompletedCallback = Callback<void(const CallStackProfiles&)>; | 166 using CompletedCallback = Callback<void(const CallStackProfiles&)>; |
166 | 167 |
| 168 // Creates a profiler that sends completed profiles to the default completed |
| 169 // callback. |
167 StackSamplingProfiler(PlatformThreadId thread_id, | 170 StackSamplingProfiler(PlatformThreadId thread_id, |
168 const SamplingParams& params); | 171 const SamplingParams& params); |
| 172 // Creates a profiler that sends completed profiles to |completed_callback|. |
| 173 StackSamplingProfiler(PlatformThreadId thread_id, |
| 174 const SamplingParams& params, |
| 175 CompletedCallback callback); |
169 ~StackSamplingProfiler(); | 176 ~StackSamplingProfiler(); |
170 | 177 |
171 // Initializes the profiler and starts sampling. | 178 // Initializes the profiler and starts sampling. |
172 void Start(); | 179 void Start(); |
173 | 180 |
174 // Stops the profiler and any ongoing sampling. Calling this function is | 181 // Stops the profiler and any ongoing sampling. Calling this function is |
175 // optional; if not invoked profiling terminates when all the profiling bursts | 182 // optional; if not invoked profiling terminates when all the profiling bursts |
176 // specified in the SamplingParams are completed. | 183 // specified in the SamplingParams are completed. |
177 void Stop(); | 184 void Stop(); |
178 | 185 |
179 // Moves all pending call stack profiles from internal storage to | 186 // Sets a callback to process profiles collected by profiler instances without |
180 // |profiles|. This function is thread safe. | 187 // a completed callback. Profiles are queued internally until a non-null |
| 188 // callback is provided to this function, |
181 // | 189 // |
182 // ***This is intended for use only by UMA.*** Callers who want to process the | 190 // The callback is typically called on a thread created by the profiler. If |
183 // collected profiles should use SetCustomCompletedCallback. | 191 // completed profiles are queued when set, however, it will also be called |
184 static void GetPendingProfiles(CallStackProfiles* call_stack_profiles); | 192 // immediately on the calling thread. |
185 | 193 static void SetDefaultCompletedCallback(CompletedCallback callback); |
186 // By default, collected call stack profiles are stored internally and can be | |
187 // retrieved by GetPendingProfiles. If a callback is provided via this | |
188 // function, however, it is called with the collected profiles instead. | |
189 void set_custom_completed_callback(CompletedCallback callback) { | |
190 custom_completed_callback_ = callback; | |
191 } | |
192 | 194 |
193 private: | 195 private: |
194 // SamplingThread is a separate thread used to suspend and sample stacks from | 196 // SamplingThread is a separate thread used to suspend and sample stacks from |
195 // the target thread. | 197 // the target thread. |
196 class SamplingThread : public PlatformThread::Delegate { | 198 class SamplingThread : public PlatformThread::Delegate { |
197 public: | 199 public: |
198 // Samples stacks using |native_sampler|. When complete, invokes | 200 // Samples stacks using |native_sampler|. When complete, invokes |
199 // |completed_callback| with the collected call stack profiles. | 201 // |completed_callback| with the collected call stack profiles. |
200 // |completed_callback| must be callable on any thread. | 202 // |completed_callback| must be callable on any thread. |
201 SamplingThread(scoped_ptr<NativeStackSampler> native_sampler, | 203 SamplingThread(scoped_ptr<NativeStackSampler> native_sampler, |
(...skipping 17 matching lines...) Expand all Loading... |
219 // only full bursts. | 221 // only full bursts. |
220 void CollectProfiles(CallStackProfiles* profiles); | 222 void CollectProfiles(CallStackProfiles* profiles); |
221 | 223 |
222 scoped_ptr<NativeStackSampler> native_sampler_; | 224 scoped_ptr<NativeStackSampler> native_sampler_; |
223 const SamplingParams params_; | 225 const SamplingParams params_; |
224 | 226 |
225 // If Stop() is called, it signals this event to force the sampling to | 227 // If Stop() is called, it signals this event to force the sampling to |
226 // terminate before all the samples specified in |params_| are collected. | 228 // terminate before all the samples specified in |params_| are collected. |
227 WaitableEvent stop_event_; | 229 WaitableEvent stop_event_; |
228 | 230 |
229 CompletedCallback completed_callback_; | 231 const CompletedCallback completed_callback_; |
230 | 232 |
231 DISALLOW_COPY_AND_ASSIGN(SamplingThread); | 233 DISALLOW_COPY_AND_ASSIGN(SamplingThread); |
232 }; | 234 }; |
233 | 235 |
234 // The thread whose stack will be sampled. | 236 // The thread whose stack will be sampled. |
235 PlatformThreadId thread_id_; | 237 PlatformThreadId thread_id_; |
236 | 238 |
237 const SamplingParams params_; | 239 const SamplingParams params_; |
238 | 240 |
239 scoped_ptr<SamplingThread> sampling_thread_; | 241 scoped_ptr<SamplingThread> sampling_thread_; |
| 242 PlatformThreadHandle sampling_thread_handle_; |
240 | 243 |
241 CompletedCallback custom_completed_callback_; | 244 const CompletedCallback completed_callback_; |
242 | 245 |
243 DISALLOW_COPY_AND_ASSIGN(StackSamplingProfiler); | 246 DISALLOW_COPY_AND_ASSIGN(StackSamplingProfiler); |
244 }; | 247 }; |
245 | 248 |
246 // The metrics provider code wants to put Samples in a map and compare them, | 249 // The metrics provider code wants to put Samples in a map and compare them, |
247 // which requires us to define a few operators. | 250 // which requires us to define a few operators. |
248 BASE_EXPORT bool operator==(const StackSamplingProfiler::Frame& a, | 251 BASE_EXPORT bool operator==(const StackSamplingProfiler::Frame& a, |
249 const StackSamplingProfiler::Frame& b); | 252 const StackSamplingProfiler::Frame& b); |
250 BASE_EXPORT bool operator<(const StackSamplingProfiler::Frame& a, | 253 BASE_EXPORT bool operator<(const StackSamplingProfiler::Frame& a, |
251 const StackSamplingProfiler::Frame& b); | 254 const StackSamplingProfiler::Frame& b); |
252 | 255 |
253 } // namespace base | 256 } // namespace base |
254 | 257 |
255 #endif // BASE_PROFILER_STACK_SAMPLING_PROFILER_H_ | 258 #endif // BASE_PROFILER_STACK_SAMPLING_PROFILER_H_ |
OLD | NEW |