| 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 // |
| 35 // IMPORTANT: Only threads guaranteed to live beyond the lifetime of the |
| 36 // profiler object can be safely sampled. Sampling the activity of the current |
| 37 // thread within the scope of this object is safe, but sampling any other |
| 38 // thread could cause Bad Things(tm) to occur should that thread exit before |
| 39 // sampling is complete. USE WITH CAUTION! |
| 40 // |
| 34 // Sample StackSamplingProfiler usage: | 41 // Sample StackSamplingProfiler usage: |
| 35 // | 42 // |
| 36 // // Create and customize params as desired. | 43 // // Create and customize params as desired. |
| 37 // base::StackStackSamplingProfiler::SamplingParams params; | 44 // base::StackStackSamplingProfiler::SamplingParams params; |
| 38 // // Any thread's ID may be passed as the target. | 45 // // Any thread's ID may be passed as the target. |
| 39 // base::StackSamplingProfiler profiler(base::PlatformThread::CurrentId()), | 46 // base::StackSamplingProfiler profiler(base::PlatformThread::CurrentId()), |
| 40 // params); | 47 // params); |
| 41 // | 48 // |
| 42 // // Or, to process the profiles within Chrome rather than via UMA, use a | 49 // // Or, to process the profiles within Chrome rather than via UMA, use a |
| 43 // // custom completed callback: | 50 // // custom completed callback: |
| (...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 198 StackSamplingProfiler(PlatformThreadId thread_id, | 205 StackSamplingProfiler(PlatformThreadId thread_id, |
| 199 const SamplingParams& params, | 206 const SamplingParams& params, |
| 200 const CompletedCallback& callback); | 207 const CompletedCallback& callback); |
| 201 StackSamplingProfiler(PlatformThreadId thread_id, | 208 StackSamplingProfiler(PlatformThreadId thread_id, |
| 202 const SamplingParams& params, | 209 const SamplingParams& params, |
| 203 const CompletedCallback& callback, | 210 const CompletedCallback& callback, |
| 204 NativeStackSamplerTestDelegate* test_delegate); | 211 NativeStackSamplerTestDelegate* test_delegate); |
| 205 // Stops any profiling currently taking place before destroying the profiler. | 212 // Stops any profiling currently taking place before destroying the profiler. |
| 206 ~StackSamplingProfiler(); | 213 ~StackSamplingProfiler(); |
| 207 | 214 |
| 208 // The fire-and-forget interface: starts a profiler and allows it to complete | |
| 209 // without the caller needing to manage the profiler lifetime. May be invoked | |
| 210 // from any thread, but requires that the calling thread has a message loop. | |
| 211 static void StartAndRunAsync(PlatformThreadId thread_id, | |
| 212 const SamplingParams& params, | |
| 213 const CompletedCallback& callback); | |
| 214 | |
| 215 // Initializes the profiler and starts sampling. | 215 // Initializes the profiler and starts sampling. |
| 216 void Start(); | 216 void Start(); |
| 217 | 217 |
| 218 // Stops the profiler and any ongoing sampling. Calling this function is | 218 // Stops the profiler and any ongoing sampling. Calling this function is |
| 219 // optional; if not invoked profiling terminates when all the profiling bursts | 219 // optional; if not invoked profiling terminates when all the profiling bursts |
| 220 // specified in the SamplingParams are completed or the profiler is destroyed, | 220 // specified in the SamplingParams are completed or the profiler is destroyed, |
| 221 // whichever occurs first. | 221 // whichever occurs first. If |done| is not null, a signal will be sent to it |
| 222 void Stop(); | 222 // once the sampling has stopped and the callback completed. |
| 223 void Stop(WaitableEvent* done); |
| 223 | 224 |
| 224 // Stops all active profiles and cleans up any resources in anticipation of | 225 // Stops all active profiles and cleans up any resources in anticipation of |
| 225 // a shutdown of the current process. | 226 // a shutdown of the current process. |
| 226 static void Shutdown(); | 227 static void Shutdown(); |
| 227 | 228 |
| 228 // Resets the "shutdown" state so that the sampling thread can be restarted. | 229 // Resets the "shutdown" state so that the sampling thread can be restarted. |
| 229 static void UndoShutdownForTesting(); | 230 static void UndoShutdownForTesting(); |
| 230 | 231 |
| 231 // Returns whether the sampling thread is currently running or not. | 232 // Returns whether the sampling thread is currently running or not. |
| 232 static bool IsSamplingThreadRunningForTesting(); | 233 static bool IsSamplingThreadRunningForTesting(); |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 297 BASE_EXPORT bool operator<(const StackSamplingProfiler::Sample& a, | 298 BASE_EXPORT bool operator<(const StackSamplingProfiler::Sample& a, |
| 298 const StackSamplingProfiler::Sample& b); | 299 const StackSamplingProfiler::Sample& b); |
| 299 BASE_EXPORT bool operator==(const StackSamplingProfiler::Frame& a, | 300 BASE_EXPORT bool operator==(const StackSamplingProfiler::Frame& a, |
| 300 const StackSamplingProfiler::Frame& b); | 301 const StackSamplingProfiler::Frame& b); |
| 301 BASE_EXPORT bool operator<(const StackSamplingProfiler::Frame& a, | 302 BASE_EXPORT bool operator<(const StackSamplingProfiler::Frame& a, |
| 302 const StackSamplingProfiler::Frame& b); | 303 const StackSamplingProfiler::Frame& b); |
| 303 | 304 |
| 304 } // namespace base | 305 } // namespace base |
| 305 | 306 |
| 306 #endif // BASE_PROFILER_STACK_SAMPLING_PROFILER_H_ | 307 #endif // BASE_PROFILER_STACK_SAMPLING_PROFILER_H_ |
| OLD | NEW |