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> |
11 #include <string> | 11 #include <string> |
12 #include <vector> | 12 #include <vector> |
13 | 13 |
14 #include "base/atomicops.h" | 14 #include "base/atomicops.h" |
15 #include "base/base_export.h" | 15 #include "base/base_export.h" |
16 #include "base/callback.h" | 16 #include "base/callback.h" |
17 #include "base/files/file_path.h" | 17 #include "base/files/file_path.h" |
18 #include "base/macros.h" | 18 #include "base/macros.h" |
19 #include "base/strings/string16.h" | 19 #include "base/strings/string16.h" |
20 #include "base/synchronization/waitable_event.h" | 20 #include "base/synchronization/waitable_event.h" |
21 #include "base/threading/platform_thread.h" | 21 #include "base/threading/platform_thread.h" |
22 #include "base/time/time.h" | 22 #include "base/time/time.h" |
23 | 23 |
24 namespace base { | 24 namespace base { |
25 | 25 |
26 class NativeStackSampler; | 26 class NativeStackSampler; |
27 class NativeStackSamplerTestDelegate; | 27 class NativeStackSamplerTestDelegate; |
28 class WaitableEvent; | |
28 | 29 |
29 // StackSamplingProfiler periodically stops a thread to sample its stack, for | 30 // StackSamplingProfiler periodically stops a thread to sample its stack, for |
30 // the purpose of collecting information about which code paths are | 31 // the purpose of collecting information about which code paths are |
31 // executing. This information is used in aggregate by UMA to identify hot | 32 // executing. This information is used in aggregate by UMA to identify hot |
32 // and/or janky code paths. | 33 // and/or janky code paths. |
33 // | 34 // |
34 // Sample StackSamplingProfiler usage: | 35 // Sample StackSamplingProfiler usage: |
35 // | 36 // |
36 // // Create and customize params as desired. | 37 // // Create and customize params as desired. |
37 // base::StackStackSamplingProfiler::SamplingParams params; | 38 // base::StackStackSamplingProfiler::SamplingParams params; |
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
172 TimeDelta burst_interval = TimeDelta::FromSeconds(10); | 173 TimeDelta burst_interval = TimeDelta::FromSeconds(10); |
173 | 174 |
174 // Number of samples to record per burst. | 175 // Number of samples to record per burst. |
175 int samples_per_burst = 300; | 176 int samples_per_burst = 300; |
176 | 177 |
177 // Interval between samples during a sampling burst. This is the desired | 178 // 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. | 179 // duration from the start of one sample to the start of the next sample. |
179 TimeDelta sampling_interval = TimeDelta::FromMilliseconds(100); | 180 TimeDelta sampling_interval = TimeDelta::FromMilliseconds(100); |
180 }; | 181 }; |
181 | 182 |
183 // Testing support. | |
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 // Returns whether the sampling thread is currently running or not. | |
191 static bool IsSamplingThreadRunning(); | |
192 | |
193 // Disables inherent idle-shutdown behavior. | |
194 static void DisableIdleShutdown(); | |
195 | |
196 // Initiates an idle shutdown task, as though the idle timer had expired, | |
197 // causing the thread to exit. There is no "idle" check so this must be | |
198 // called only when all sampling tasks have completed. This blocks until | |
199 // the task has been executed, though the actual stopping of the thread | |
200 // still happens asynchronously. Watch IsSamplingThreadRunningForTesting() | |
Mike Wittman
2017/03/28 19:32:02
IsSamplingThreadRunningForTesting => IsSamplingThr
bcwhite
2017/03/29 14:56:58
Done.
| |
201 // to know when the thread has exited. If |simulate_start| is true then this | |
202 // method will make it appear to the shutdown task that a new profiler was | |
203 // started between when the idle-shutdown was initiated and when it runs. | |
204 static void PerformSamplingThreadIdleShutdown(bool simulate_start); | |
Mike Wittman
2017/03/28 19:32:02
Can we call this simulate_intervening_start, to ma
bcwhite
2017/03/29 14:56:58
Adding "intervening" wouldn't indicate anything ab
Mike Wittman
2017/03/30 16:18:38
It would indicate the temporal relationship of the
bcwhite
2017/03/30 18:54:51
Done.
| |
205 }; | |
206 | |
182 // The callback type used to collect completed profiles. The passed |profiles| | 207 // The callback type used to collect completed profiles. The passed |profiles| |
183 // are move-only. | 208 // are move-only. Other threads, including the UI thread, may block on |
209 // callback completion so this should run as quickly as possible. | |
184 // | 210 // |
185 // IMPORTANT NOTE: the callback is invoked on a thread the profiler | 211 // IMPORTANT NOTE: The callback is invoked on a thread the profiler |
186 // constructs, rather than on the thread used to construct the profiler and | 212 // 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 | 213 // set the callback, and thus the callback must be callable on any thread. For |
188 // threads with message loops that create StackSamplingProfilers, posting a | 214 // threads with message loops that create StackSamplingProfilers, posting a |
189 // task to the message loop with a copy of the profiles is the recommended | 215 // task to the message loop with the moved (i.e. std::move) profiles is the |
190 // thread-safe callback implementation. | 216 // thread-safe callback implementation. |
191 using CompletedCallback = Callback<void(CallStackProfiles)>; | 217 using CompletedCallback = Callback<void(CallStackProfiles)>; |
192 | 218 |
193 // Creates a profiler that sends completed profiles to |callback|. The second | 219 // Creates a profiler for the CURRENT thread that sends completed profiles |
194 // constructor is for test purposes. | 220 // to |callback|. An optional |test_delegate| can be supplied by tests. |
195 StackSamplingProfiler(PlatformThreadId thread_id, | 221 // The caller must ensure that this object gets destroyed before the current |
196 const SamplingParams& params, | 222 // thread exits. |
197 const CompletedCallback& callback); | 223 StackSamplingProfiler( |
198 StackSamplingProfiler(PlatformThreadId thread_id, | 224 const SamplingParams& params, |
199 const SamplingParams& params, | 225 const CompletedCallback& callback, |
200 const CompletedCallback& callback, | 226 NativeStackSamplerTestDelegate* test_delegate = nullptr); |
201 NativeStackSamplerTestDelegate* test_delegate); | 227 |
228 // Creates a profiler for ANOTHER thread that sends completed profiles to | |
229 // |callback|. An optional |test_delegate| can be supplied by tests. | |
230 // | |
231 // IMPORTANT: The caller must ensure that the thread being sampled does not | |
232 // exit before this object gets destructed or Bad Things(tm) may occur. | |
233 StackSamplingProfiler( | |
234 PlatformThreadId thread_id, | |
235 const SamplingParams& params, | |
236 const CompletedCallback& callback, | |
237 NativeStackSamplerTestDelegate* test_delegate = nullptr); | |
238 | |
202 // Stops any profiling currently taking place before destroying the profiler. | 239 // Stops any profiling currently taking place before destroying the profiler. |
240 // This will block until the callback has been run. | |
203 ~StackSamplingProfiler(); | 241 ~StackSamplingProfiler(); |
204 | 242 |
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. | 243 // Initializes the profiler and starts sampling. |
213 void Start(); | 244 void Start(); |
214 | 245 |
215 // Stops the profiler and any ongoing sampling. Calling this function is | 246 // Stops the profiler and any ongoing sampling. This method will return |
216 // optional; if not invoked profiling terminates when all the profiling bursts | 247 // immediately with the callback being run asynchronously. At most one |
217 // specified in the SamplingParams are completed or the profiler is destroyed, | 248 // more stack sample will be taken after this method returns. Calling this |
218 // whichever occurs first. | 249 // function is optional; if not invoked profiling terminates when all the |
250 // profiling bursts specified in the SamplingParams are completed or the | |
251 // profiler object is destroyed, whichever occurs first. | |
219 void Stop(); | 252 void Stop(); |
220 | 253 |
221 // Set the current system state that is recorded with each captured stack | 254 // 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 | 255 // 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 | 256 // 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 | 257 // ranging from 0 to 31, inclusive. This sets bits within Sample field of |
225 // |process_milestones|. The actual meanings of these bits are defined | 258 // |process_milestones|. The actual meanings of these bits are defined |
226 // (globally) by the caller(s). | 259 // (globally) by the caller(s). |
227 static void SetProcessMilestone(int milestone); | 260 static void SetProcessMilestone(int milestone); |
228 static void ResetAnnotationsForTesting(); | 261 static void ResetAnnotationsForTesting(); |
229 | 262 |
230 private: | 263 private: |
264 friend class TestAPI; | |
265 | |
231 // SamplingThread is a separate thread used to suspend and sample stacks from | 266 // SamplingThread is a separate thread used to suspend and sample stacks from |
232 // the target thread. | 267 // the target thread. |
233 class SamplingThread : public PlatformThread::Delegate { | 268 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 | 269 |
272 // Adds annotations to a Sample. | 270 // Adds annotations to a Sample. |
273 static void RecordAnnotations(Sample* sample); | 271 static void RecordAnnotations(Sample* sample); |
274 | 272 |
275 // This global variables holds the current system state and is recorded with | 273 // 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 | 274 // 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 | 275 // 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 | 276 // 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. | 277 // making a change was also being profiled and got stopped. |
280 static subtle::Atomic32 process_milestones_; | 278 static subtle::Atomic32 process_milestones_; |
281 | 279 |
282 // The thread whose stack will be sampled. | 280 // The thread whose stack will be sampled. |
283 PlatformThreadId thread_id_; | 281 PlatformThreadId thread_id_; |
284 | 282 |
285 const SamplingParams params_; | 283 const SamplingParams params_; |
286 | 284 |
287 std::unique_ptr<SamplingThread> sampling_thread_; | 285 const CompletedCallback completed_callback_; |
288 PlatformThreadHandle sampling_thread_handle_; | |
289 | 286 |
290 const CompletedCallback completed_callback_; | 287 // This starts "signaled", is reset when sampling begins, and is signaled |
288 // when that sampling is complete and the callback done. | |
289 WaitableEvent profiling_inactive_; | |
290 | |
291 // Object that does the native sampling. This is created during construction | |
292 // and later passed to the sampling thread when profiling is started. | |
293 std::unique_ptr<NativeStackSampler> native_sampler_; | |
294 | |
295 // An ID uniquely identifying this collection to the sampling thread. This | |
296 // will be an internal "null" value when no collection has been started. | |
297 int collection_id_; | |
291 | 298 |
292 // Stored until it can be passed to the NativeStackSampler created in Start(). | 299 // Stored until it can be passed to the NativeStackSampler created in Start(). |
293 NativeStackSamplerTestDelegate* const test_delegate_; | 300 NativeStackSamplerTestDelegate* const test_delegate_; |
294 | 301 |
295 DISALLOW_COPY_AND_ASSIGN(StackSamplingProfiler); | 302 DISALLOW_COPY_AND_ASSIGN(StackSamplingProfiler); |
296 }; | 303 }; |
297 | 304 |
298 // These operators permit types to be compared and used in a map of Samples, as | 305 // 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. | 306 // done in tests and by the metrics provider code. |
300 BASE_EXPORT bool operator==(const StackSamplingProfiler::Module& a, | 307 BASE_EXPORT bool operator==(const StackSamplingProfiler::Module& a, |
301 const StackSamplingProfiler::Module& b); | 308 const StackSamplingProfiler::Module& b); |
302 BASE_EXPORT bool operator==(const StackSamplingProfiler::Sample& a, | 309 BASE_EXPORT bool operator==(const StackSamplingProfiler::Sample& a, |
303 const StackSamplingProfiler::Sample& b); | 310 const StackSamplingProfiler::Sample& b); |
304 BASE_EXPORT bool operator!=(const StackSamplingProfiler::Sample& a, | 311 BASE_EXPORT bool operator!=(const StackSamplingProfiler::Sample& a, |
305 const StackSamplingProfiler::Sample& b); | 312 const StackSamplingProfiler::Sample& b); |
306 BASE_EXPORT bool operator<(const StackSamplingProfiler::Sample& a, | 313 BASE_EXPORT bool operator<(const StackSamplingProfiler::Sample& a, |
307 const StackSamplingProfiler::Sample& b); | 314 const StackSamplingProfiler::Sample& b); |
308 BASE_EXPORT bool operator==(const StackSamplingProfiler::Frame& a, | 315 BASE_EXPORT bool operator==(const StackSamplingProfiler::Frame& a, |
309 const StackSamplingProfiler::Frame& b); | 316 const StackSamplingProfiler::Frame& b); |
310 BASE_EXPORT bool operator<(const StackSamplingProfiler::Frame& a, | 317 BASE_EXPORT bool operator<(const StackSamplingProfiler::Frame& a, |
311 const StackSamplingProfiler::Frame& b); | 318 const StackSamplingProfiler::Frame& b); |
312 | 319 |
313 } // namespace base | 320 } // namespace base |
314 | 321 |
315 #endif // BASE_PROFILER_STACK_SAMPLING_PROFILER_H_ | 322 #endif // BASE_PROFILER_STACK_SAMPLING_PROFILER_H_ |
OLD | NEW |