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 <stddef.h> | 8 #include <stddef.h> |
9 | 9 |
10 #include <memory> | 10 #include <memory> |
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
172 TimeDelta burst_interval = TimeDelta::FromSeconds(10); | 172 TimeDelta burst_interval = TimeDelta::FromSeconds(10); |
173 | 173 |
174 // Number of samples to record per burst. | 174 // Number of samples to record per burst. |
175 int samples_per_burst = 300; | 175 int samples_per_burst = 300; |
176 | 176 |
177 // Interval between samples during a sampling burst. This is the desired | 177 // Interval between samples during a sampling burst. This is the desired |
178 // duration from the start of one sample to the start of the next sample. | 178 // duration from the start of one sample to the start of the next sample. |
179 TimeDelta sampling_interval = TimeDelta::FromMilliseconds(100); | 179 TimeDelta sampling_interval = TimeDelta::FromMilliseconds(100); |
180 }; | 180 }; |
181 | 181 |
182 // Testing support. These methods are static beause they interact with the | |
183 // sampling thread, a singleton used by all StackSamplingProfiler objects. | |
184 class BASE_EXPORT TestAPI { | |
185 public: | |
186 // Resets the internal state to that of a fresh start. This is necessary | |
187 // so that tests don't inherit state from previous tests. | |
188 static void Reset(); | |
189 | |
190 // Resets internal annotations (like process phase) to initial values. | |
191 static void ResetAnnotations(); | |
192 | |
193 // Returns whether the sampling thread is currently running or not. | |
194 static bool IsSamplingThreadRunning(); | |
gab
2017/04/05 20:38:43
Thread::IsRunning() isn't thread-safe (though the
bcwhite
2017/04/06 18:40:18
I see. Would the best solution be to have CleanUp
gab
2017/04/06 19:31:28
Hmmm if the comment above is respected and IsRunni
| |
195 | |
196 // Disables inherent idle-shutdown behavior. | |
197 static void DisableIdleShutdown(); | |
gab
2017/04/05 20:38:42
Since you should support having multiple pending d
bcwhite
2017/04/06 18:40:18
They can and it's handled.
gab
2017/04/06 19:31:28
Hmm okay, but you also by doing so don't test call
Mike Wittman
2017/04/06 21:54:30
Yes, the tests don't explicitly generate multiple
| |
198 | |
199 // Initiates an idle shutdown task, as though the idle timer had expired, | |
200 // causing the thread to exit. There is no "idle" check so this must be | |
201 // called only when all sampling tasks have completed. This blocks until | |
202 // the task has been executed, though the actual stopping of the thread | |
203 // still happens asynchronously. Watch IsSamplingThreadRunning() to know | |
204 // when the thread has exited. If |simulate_intervening_start| is true then | |
205 // this method will make it appear to the shutdown task that a new profiler | |
206 // was started between when the idle-shutdown was initiated and when it | |
207 // runs. | |
208 static void PerformSamplingThreadIdleShutdown( | |
209 bool simulate_intervening_start); | |
210 }; | |
211 | |
182 // The callback type used to collect completed profiles. The passed |profiles| | 212 // The callback type used to collect completed profiles. The passed |profiles| |
183 // are move-only. | 213 // are move-only. Other threads, including the UI thread, may block on |
214 // callback completion so this should run as quickly as possible. | |
184 // | 215 // |
185 // IMPORTANT NOTE: the callback is invoked on a thread the profiler | 216 // IMPORTANT NOTE: The callback is invoked on a thread the profiler |
186 // constructs, rather than on the thread used to construct the profiler and | 217 // constructs, rather than on the thread used to construct the profiler and |
187 // set the callback, and thus the callback must be callable on any thread. For | 218 // set the callback, and thus the callback must be callable on any thread. For |
188 // threads with message loops that create StackSamplingProfilers, posting a | 219 // threads with message loops that create StackSamplingProfilers, posting a |
189 // task to the message loop with a copy of the profiles is the recommended | 220 // task to the message loop with the moved (i.e. std::move) profiles is the |
190 // thread-safe callback implementation. | 221 // thread-safe callback implementation. |
191 using CompletedCallback = Callback<void(CallStackProfiles)>; | 222 using CompletedCallback = Callback<void(CallStackProfiles)>; |
192 | 223 |
193 // Creates a profiler that sends completed profiles to |callback|. The second | 224 // Creates a profiler for the CURRENT thread that sends completed profiles |
194 // constructor is for test purposes. | 225 // to |callback|. An optional |test_delegate| can be supplied by tests. |
195 StackSamplingProfiler(PlatformThreadId thread_id, | 226 // The caller must ensure that this object gets destroyed before the current |
196 const SamplingParams& params, | 227 // thread exits. |
197 const CompletedCallback& callback); | 228 StackSamplingProfiler( |
198 StackSamplingProfiler(PlatformThreadId thread_id, | 229 const SamplingParams& params, |
199 const SamplingParams& params, | 230 const CompletedCallback& callback, |
200 const CompletedCallback& callback, | 231 NativeStackSamplerTestDelegate* test_delegate = nullptr); |
201 NativeStackSamplerTestDelegate* test_delegate); | 232 |
233 // Creates a profiler for ANOTHER thread that sends completed profiles to | |
234 // |callback|. An optional |test_delegate| can be supplied by tests. | |
235 // | |
236 // IMPORTANT: The caller must ensure that the thread being sampled does not | |
237 // exit before this object gets destructed or Bad Things(tm) may occur. | |
238 StackSamplingProfiler( | |
239 PlatformThreadId thread_id, | |
240 const SamplingParams& params, | |
241 const CompletedCallback& callback, | |
242 NativeStackSamplerTestDelegate* test_delegate = nullptr); | |
243 | |
202 // Stops any profiling currently taking place before destroying the profiler. | 244 // Stops any profiling currently taking place before destroying the profiler. |
245 // This will block until the callback has been run if profiling has started | |
246 // but not already finished. | |
203 ~StackSamplingProfiler(); | 247 ~StackSamplingProfiler(); |
204 | 248 |
205 // The fire-and-forget interface: starts a profiler and allows it to complete | |
206 // without the caller needing to manage the profiler lifetime. May be invoked | |
207 // from any thread, but requires that the calling thread has a message loop. | |
208 static void StartAndRunAsync(PlatformThreadId thread_id, | |
209 const SamplingParams& params, | |
210 const CompletedCallback& callback); | |
211 | |
212 // Initializes the profiler and starts sampling. | 249 // Initializes the profiler and starts sampling. |
213 void Start(); | 250 void Start(); |
214 | 251 |
215 // Stops the profiler and any ongoing sampling. Calling this function is | 252 // Stops the profiler and any ongoing sampling. This method will return |
216 // optional; if not invoked profiling terminates when all the profiling bursts | 253 // immediately with the callback being run asynchronously. At most one |
217 // specified in the SamplingParams are completed or the profiler is destroyed, | 254 // more stack sample will be taken after this method returns. Calling this |
218 // whichever occurs first. | 255 // function is optional; if not invoked profiling terminates when all the |
256 // profiling bursts specified in the SamplingParams are completed or the | |
257 // profiler object is destroyed, whichever occurs first. | |
219 void Stop(); | 258 void Stop(); |
220 | 259 |
221 // Set the current system state that is recorded with each captured stack | 260 // Set the current system state that is recorded with each captured stack |
222 // frame. This is thread-safe so can be called from anywhere. The parameter | 261 // frame. This is thread-safe so can be called from anywhere. The parameter |
223 // value should be from an enumeration of the appropriate type with values | 262 // value should be from an enumeration of the appropriate type with values |
224 // ranging from 0 to 31, inclusive. This sets bits within Sample field of | 263 // ranging from 0 to 31, inclusive. This sets bits within Sample field of |
225 // |process_milestones|. The actual meanings of these bits are defined | 264 // |process_milestones|. The actual meanings of these bits are defined |
226 // (globally) by the caller(s). | 265 // (globally) by the caller(s). |
227 static void SetProcessMilestone(int milestone); | 266 static void SetProcessMilestone(int milestone); |
228 static void ResetAnnotationsForTesting(); | |
229 | 267 |
230 private: | 268 private: |
269 friend class TestAPI; | |
270 | |
231 // SamplingThread is a separate thread used to suspend and sample stacks from | 271 // SamplingThread is a separate thread used to suspend and sample stacks from |
232 // the target thread. | 272 // the target thread. |
233 class SamplingThread : public PlatformThread::Delegate { | 273 class SamplingThread; |
234 public: | |
235 // Samples stacks using |native_sampler|. When complete, invokes | |
236 // |completed_callback| with the collected call stack profiles. | |
237 // |completed_callback| must be callable on any thread. | |
238 SamplingThread(std::unique_ptr<NativeStackSampler> native_sampler, | |
239 const SamplingParams& params, | |
240 const CompletedCallback& completed_callback); | |
241 ~SamplingThread() override; | |
242 | |
243 // PlatformThread::Delegate: | |
244 void ThreadMain() override; | |
245 | |
246 void Stop(); | |
247 | |
248 private: | |
249 // Collects |profile| from a single burst. If the profiler was stopped | |
250 // during collection, sets |was_stopped| and provides the set of samples | |
251 // collected up to that point. | |
252 void CollectProfile(CallStackProfile* profile, TimeDelta* elapsed_time, | |
253 bool* was_stopped); | |
254 | |
255 // Collects call stack profiles from all bursts, or until the sampling is | |
256 // stopped. If stopped before complete, the last profile in | |
257 // |call_stack_profiles| may contain a partial burst. | |
258 void CollectProfiles(CallStackProfiles* profiles); | |
259 | |
260 std::unique_ptr<NativeStackSampler> native_sampler_; | |
261 const SamplingParams params_; | |
262 | |
263 // If Stop() is called, it signals this event to force the sampling to | |
264 // terminate before all the samples specified in |params_| are collected. | |
265 WaitableEvent stop_event_; | |
266 | |
267 const CompletedCallback completed_callback_; | |
268 | |
269 DISALLOW_COPY_AND_ASSIGN(SamplingThread); | |
270 }; | |
271 | 274 |
272 // Adds annotations to a Sample. | 275 // Adds annotations to a Sample. |
273 static void RecordAnnotations(Sample* sample); | 276 static void RecordAnnotations(Sample* sample); |
274 | 277 |
275 // This global variables holds the current system state and is recorded with | 278 // This global variables holds the current system state and is recorded with |
276 // every captured sample, done on a separate thread which is why updates to | 279 // every captured sample, done on a separate thread which is why updates to |
277 // this must be atomic. A PostTask to move the the updates to that thread | 280 // this must be atomic. A PostTask to move the the updates to that thread |
278 // would skew the timing and a lock could result in deadlock if the thread | 281 // would skew the timing and a lock could result in deadlock if the thread |
279 // making a change was also being profiled and got stopped. | 282 // making a change was also being profiled and got stopped. |
280 static subtle::Atomic32 process_milestones_; | 283 static subtle::Atomic32 process_milestones_; |
281 | 284 |
282 // The thread whose stack will be sampled. | 285 // The thread whose stack will be sampled. |
283 PlatformThreadId thread_id_; | 286 PlatformThreadId thread_id_; |
284 | 287 |
285 const SamplingParams params_; | 288 const SamplingParams params_; |
286 | 289 |
287 std::unique_ptr<SamplingThread> sampling_thread_; | 290 const CompletedCallback completed_callback_; |
288 PlatformThreadHandle sampling_thread_handle_; | |
289 | 291 |
290 const CompletedCallback completed_callback_; | 292 // This starts "signaled", is reset when sampling begins, and is signaled |
293 // when that sampling is complete and the callback done. | |
294 WaitableEvent profiling_inactive_; | |
295 | |
296 // Object that does the native sampling. This is created during construction | |
297 // and later passed to the sampling thread when profiling is started. | |
298 std::unique_ptr<NativeStackSampler> native_sampler_; | |
299 | |
300 // An ID uniquely identifying this collection to the sampling thread. This | |
301 // will be an internal "null" value when no collection has been started. | |
302 int collection_id_; | |
291 | 303 |
292 // Stored until it can be passed to the NativeStackSampler created in Start(). | 304 // Stored until it can be passed to the NativeStackSampler created in Start(). |
293 NativeStackSamplerTestDelegate* const test_delegate_; | 305 NativeStackSamplerTestDelegate* const test_delegate_; |
294 | 306 |
295 DISALLOW_COPY_AND_ASSIGN(StackSamplingProfiler); | 307 DISALLOW_COPY_AND_ASSIGN(StackSamplingProfiler); |
296 }; | 308 }; |
297 | 309 |
298 // These operators permit types to be compared and used in a map of Samples, as | 310 // These operators permit types to be compared and used in a map of Samples, as |
299 // done in tests and by the metrics provider code. | 311 // done in tests and by the metrics provider code. |
300 BASE_EXPORT bool operator==(const StackSamplingProfiler::Module& a, | 312 BASE_EXPORT bool operator==(const StackSamplingProfiler::Module& a, |
301 const StackSamplingProfiler::Module& b); | 313 const StackSamplingProfiler::Module& b); |
302 BASE_EXPORT bool operator==(const StackSamplingProfiler::Sample& a, | 314 BASE_EXPORT bool operator==(const StackSamplingProfiler::Sample& a, |
303 const StackSamplingProfiler::Sample& b); | 315 const StackSamplingProfiler::Sample& b); |
304 BASE_EXPORT bool operator!=(const StackSamplingProfiler::Sample& a, | 316 BASE_EXPORT bool operator!=(const StackSamplingProfiler::Sample& a, |
305 const StackSamplingProfiler::Sample& b); | 317 const StackSamplingProfiler::Sample& b); |
306 BASE_EXPORT bool operator<(const StackSamplingProfiler::Sample& a, | 318 BASE_EXPORT bool operator<(const StackSamplingProfiler::Sample& a, |
307 const StackSamplingProfiler::Sample& b); | 319 const StackSamplingProfiler::Sample& b); |
308 BASE_EXPORT bool operator==(const StackSamplingProfiler::Frame& a, | 320 BASE_EXPORT bool operator==(const StackSamplingProfiler::Frame& a, |
309 const StackSamplingProfiler::Frame& b); | 321 const StackSamplingProfiler::Frame& b); |
310 BASE_EXPORT bool operator<(const StackSamplingProfiler::Frame& a, | 322 BASE_EXPORT bool operator<(const StackSamplingProfiler::Frame& a, |
311 const StackSamplingProfiler::Frame& b); | 323 const StackSamplingProfiler::Frame& b); |
312 | 324 |
313 } // namespace base | 325 } // namespace base |
314 | 326 |
315 #endif // BASE_PROFILER_STACK_SAMPLING_PROFILER_H_ | 327 #endif // BASE_PROFILER_STACK_SAMPLING_PROFILER_H_ |
OLD | NEW |