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 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
154 bool preserve_sample_ordering; | 155 bool preserve_sample_ordering; |
155 }; | 156 }; |
156 | 157 |
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. | 162 // set the callback, and thus the callback must be callable on any thread. |
162 using CompletedCallback = Callback<void(const CallStackProfiles&)>; | 163 using CompletedCallback = Callback<void(const CallStackProfiles&)>; |
163 | 164 |
| 165 // Creates a profiler that sends completed profiles to the default completed |
| 166 // callback. |
164 StackSamplingProfiler(PlatformThreadId thread_id, | 167 StackSamplingProfiler(PlatformThreadId thread_id, |
165 const SamplingParams& params); | 168 const SamplingParams& params); |
| 169 // Creates a profiler that sends completed profiles to |completed_callback|. |
| 170 StackSamplingProfiler(PlatformThreadId thread_id, |
| 171 const SamplingParams& params, |
| 172 CompletedCallback callback); |
166 ~StackSamplingProfiler(); | 173 ~StackSamplingProfiler(); |
167 | 174 |
168 // Initializes the profiler and starts sampling. | 175 // Initializes the profiler and starts sampling. |
169 void Start(); | 176 void Start(); |
170 | 177 |
171 // Stops the profiler and any ongoing sampling. Calling this function is | 178 // Stops the profiler and any ongoing sampling. Calling this function is |
172 // optional; if not invoked profiling terminates when all the profiling bursts | 179 // optional; if not invoked profiling terminates when all the profiling bursts |
173 // specified in the SamplingParams are completed. | 180 // specified in the SamplingParams are completed. |
174 void Stop(); | 181 void Stop(); |
175 | 182 |
176 // Moves all pending call stack profiles from internal storage to | 183 // Sets a callback to process profiles collected by profiler instances without |
177 // |profiles|. This function is thread safe. | 184 // a completed callback. Profiles are queued internally until a non-null |
| 185 // callback is provided to this function, |
178 // | 186 // |
179 // ***This is intended for use only by UMA.*** Callers who want to process the | 187 // The callback is typically called on a thread created by the profiler. If |
180 // collected profiles should use SetCustomCompletedCallback. | 188 // completed profiles are queued when set, however, it will also be called |
181 static void GetPendingProfiles(CallStackProfiles* call_stack_profiles); | 189 // immediately on the calling thread. |
182 | 190 static void SetDefaultCompletedCallback(CompletedCallback callback); |
183 // By default, collected call stack profiles are stored internally and can be | |
184 // retrieved by GetPendingProfiles. If a callback is provided via this | |
185 // function, however, it is called with the collected profiles instead. | |
186 void set_custom_completed_callback(CompletedCallback callback) { | |
187 custom_completed_callback_ = callback; | |
188 } | |
189 | 191 |
190 private: | 192 private: |
191 // SamplingThread is a separate thread used to suspend and sample stacks from | 193 // SamplingThread is a separate thread used to suspend and sample stacks from |
192 // the target thread. | 194 // the target thread. |
193 class SamplingThread : public PlatformThread::Delegate { | 195 class SamplingThread : public PlatformThread::Delegate { |
194 public: | 196 public: |
195 // Samples stacks using |native_sampler|. When complete, invokes | 197 // Samples stacks using |native_sampler|. When complete, invokes |
196 // |completed_callback| with the collected call stack profiles. | 198 // |completed_callback| with the collected call stack profiles. |
197 // |completed_callback| must be callable on any thread. | 199 // |completed_callback| must be callable on any thread. |
198 SamplingThread(scoped_ptr<NativeStackSampler> native_sampler, | 200 SamplingThread(scoped_ptr<NativeStackSampler> native_sampler, |
(...skipping 17 matching lines...) Expand all Loading... |
216 // only full bursts. | 218 // only full bursts. |
217 void CollectProfiles(CallStackProfiles* profiles); | 219 void CollectProfiles(CallStackProfiles* profiles); |
218 | 220 |
219 scoped_ptr<NativeStackSampler> native_sampler_; | 221 scoped_ptr<NativeStackSampler> native_sampler_; |
220 const SamplingParams params_; | 222 const SamplingParams params_; |
221 | 223 |
222 // If Stop() is called, it signals this event to force the sampling to | 224 // If Stop() is called, it signals this event to force the sampling to |
223 // terminate before all the samples specified in |params_| are collected. | 225 // terminate before all the samples specified in |params_| are collected. |
224 WaitableEvent stop_event_; | 226 WaitableEvent stop_event_; |
225 | 227 |
226 CompletedCallback completed_callback_; | 228 const CompletedCallback completed_callback_; |
227 | 229 |
228 DISALLOW_COPY_AND_ASSIGN(SamplingThread); | 230 DISALLOW_COPY_AND_ASSIGN(SamplingThread); |
229 }; | 231 }; |
230 | 232 |
231 // The thread whose stack will be sampled. | 233 // The thread whose stack will be sampled. |
232 PlatformThreadId thread_id_; | 234 PlatformThreadId thread_id_; |
233 | 235 |
234 const SamplingParams params_; | 236 const SamplingParams params_; |
235 | 237 |
236 scoped_ptr<SamplingThread> sampling_thread_; | 238 scoped_ptr<SamplingThread> sampling_thread_; |
| 239 PlatformThreadHandle sampling_thread_handle_; |
237 | 240 |
238 CompletedCallback custom_completed_callback_; | 241 const CompletedCallback completed_callback_; |
239 | 242 |
240 DISALLOW_COPY_AND_ASSIGN(StackSamplingProfiler); | 243 DISALLOW_COPY_AND_ASSIGN(StackSamplingProfiler); |
241 }; | 244 }; |
242 | 245 |
243 // The metrics provider code wants to put Samples in a map and compare them, | 246 // The metrics provider code wants to put Samples in a map and compare them, |
244 // which requires us to define a few operators. | 247 // which requires us to define a few operators. |
245 BASE_EXPORT bool operator==(const StackSamplingProfiler::Frame& a, | 248 BASE_EXPORT bool operator==(const StackSamplingProfiler::Frame& a, |
246 const StackSamplingProfiler::Frame& b); | 249 const StackSamplingProfiler::Frame& b); |
247 BASE_EXPORT bool operator<(const StackSamplingProfiler::Frame& a, | 250 BASE_EXPORT bool operator<(const StackSamplingProfiler::Frame& a, |
248 const StackSamplingProfiler::Frame& b); | 251 const StackSamplingProfiler::Frame& b); |
249 | 252 |
250 } // namespace base | 253 } // namespace base |
251 | 254 |
252 #endif // BASE_PROFILER_STACK_SAMPLING_PROFILER_H_ | 255 #endif // BASE_PROFILER_STACK_SAMPLING_PROFILER_H_ |
OLD | NEW |