Chromium Code Reviews| 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 |
| 11 #include "base/base_export.h" | 11 #include "base/base_export.h" |
| 12 #include "base/callback.h" | 12 #include "base/callback.h" |
| 13 #include "base/files/file_path.h" | 13 #include "base/files/file_path.h" |
| 14 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
| 15 #include "base/strings/string16.h" | 15 #include "base/strings/string16.h" |
| 16 #include "base/synchronization/waitable_event.h" | 16 #include "base/synchronization/waitable_event.h" |
| 17 #include "base/threading/platform_thread.h" | 17 #include "base/threading/platform_thread.h" |
| 18 #include "base/time/time.h" | 18 #include "base/time/time.h" |
| 19 | 19 |
| 20 namespace base { | 20 namespace base { |
| 21 | 21 |
| 22 class NativeStackSampler; | 22 class NativeStackSampler; |
| 23 class NativeStackSamplerTestDelegate; | |
| 23 | 24 |
| 24 // StackSamplingProfiler periodically stops a thread to sample its stack, for | 25 // StackSamplingProfiler periodically stops a thread to sample its stack, for |
| 25 // the purpose of collecting information about which code paths are | 26 // the purpose of collecting information about which code paths are |
| 26 // executing. This information is used in aggregate by UMA to identify hot | 27 // executing. This information is used in aggregate by UMA to identify hot |
| 27 // and/or janky code paths. | 28 // and/or janky code paths. |
| 28 // | 29 // |
| 29 // Sample StackSamplingProfiler usage: | 30 // Sample StackSamplingProfiler usage: |
| 30 // | 31 // |
| 31 // // Create and customize params as desired. | 32 // // Create and customize params as desired. |
| 32 // base::StackStackSamplingProfiler::SamplingParams params; | 33 // base::StackStackSamplingProfiler::SamplingParams params; |
| (...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 149 // The callback type used to collect completed profiles. | 150 // The callback type used to collect completed profiles. |
| 150 // | 151 // |
| 151 // IMPORTANT NOTE: the callback is invoked on a thread the profiler | 152 // IMPORTANT NOTE: the callback is invoked on a thread the profiler |
| 152 // constructs, rather than on the thread used to construct the profiler and | 153 // constructs, rather than on the thread used to construct the profiler and |
| 153 // set the callback, and thus the callback must be callable on any thread. For | 154 // set the callback, and thus the callback must be callable on any thread. For |
| 154 // threads with message loops that create StackSamplingProfilers, posting a | 155 // threads with message loops that create StackSamplingProfilers, posting a |
| 155 // task to the message loop with a copy of the profiles is the recommended | 156 // task to the message loop with a copy of the profiles is the recommended |
| 156 // thread-safe callback implementation. | 157 // thread-safe callback implementation. |
| 157 using CompletedCallback = Callback<void(const CallStackProfiles&)>; | 158 using CompletedCallback = Callback<void(const CallStackProfiles&)>; |
| 158 | 159 |
| 159 // Creates a profiler that sends completed profiles to |callback|. | 160 // Creates a profiler that sends completed profiles to |callback|. The second |
| 161 // constructor is for test purposes. | |
|
brucedawson
2015/10/30 00:15:59
Wouldn't it be simpler to have test_delegate have
Mike Wittman
2015/10/30 17:08:07
I'm not sure. Although default arguments are permi
| |
| 160 StackSamplingProfiler(PlatformThreadId thread_id, | 162 StackSamplingProfiler(PlatformThreadId thread_id, |
| 161 const SamplingParams& params, | 163 const SamplingParams& params, |
| 162 const CompletedCallback& callback); | 164 const CompletedCallback& callback); |
| 165 StackSamplingProfiler(PlatformThreadId thread_id, | |
| 166 const SamplingParams& params, | |
| 167 const CompletedCallback& callback, | |
| 168 NativeStackSamplerTestDelegate* test_delegate); | |
| 163 // Stops any profiling currently taking place before destroying the profiler. | 169 // Stops any profiling currently taking place before destroying the profiler. |
| 164 ~StackSamplingProfiler(); | 170 ~StackSamplingProfiler(); |
| 165 | 171 |
| 166 // The fire-and-forget interface: starts a profiler and allows it to complete | 172 // The fire-and-forget interface: starts a profiler and allows it to complete |
| 167 // without the caller needing to manage the profiler lifetime. May be invoked | 173 // without the caller needing to manage the profiler lifetime. May be invoked |
| 168 // from any thread, but requires that the calling thread has a message loop. | 174 // from any thread, but requires that the calling thread has a message loop. |
| 169 static void StartAndRunAsync(PlatformThreadId thread_id, | 175 static void StartAndRunAsync(PlatformThreadId thread_id, |
| 170 const SamplingParams& params, | 176 const SamplingParams& params, |
| 171 const CompletedCallback& callback); | 177 const CompletedCallback& callback); |
| 172 | 178 |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 224 // The thread whose stack will be sampled. | 230 // The thread whose stack will be sampled. |
| 225 PlatformThreadId thread_id_; | 231 PlatformThreadId thread_id_; |
| 226 | 232 |
| 227 const SamplingParams params_; | 233 const SamplingParams params_; |
| 228 | 234 |
| 229 scoped_ptr<SamplingThread> sampling_thread_; | 235 scoped_ptr<SamplingThread> sampling_thread_; |
| 230 PlatformThreadHandle sampling_thread_handle_; | 236 PlatformThreadHandle sampling_thread_handle_; |
| 231 | 237 |
| 232 const CompletedCallback completed_callback_; | 238 const CompletedCallback completed_callback_; |
| 233 | 239 |
| 240 // Stored until it can be passed to the NativeStackSampler created in Start(). | |
| 241 NativeStackSamplerTestDelegate* const test_delegate_; | |
| 242 | |
| 234 DISALLOW_COPY_AND_ASSIGN(StackSamplingProfiler); | 243 DISALLOW_COPY_AND_ASSIGN(StackSamplingProfiler); |
| 235 }; | 244 }; |
| 236 | 245 |
| 237 // 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, |
| 238 // which requires us to define a few operators. | 247 // which requires us to define a few operators. |
| 239 BASE_EXPORT bool operator==(const StackSamplingProfiler::Frame& a, | 248 BASE_EXPORT bool operator==(const StackSamplingProfiler::Frame& a, |
| 240 const StackSamplingProfiler::Frame& b); | 249 const StackSamplingProfiler::Frame& b); |
| 241 BASE_EXPORT bool operator<(const StackSamplingProfiler::Frame& a, | 250 BASE_EXPORT bool operator<(const StackSamplingProfiler::Frame& a, |
| 242 const StackSamplingProfiler::Frame& b); | 251 const StackSamplingProfiler::Frame& b); |
| 243 | 252 |
| 244 } // namespace base | 253 } // namespace base |
| 245 | 254 |
| 246 #endif // BASE_PROFILER_STACK_SAMPLING_PROFILER_H_ | 255 #endif // BASE_PROFILER_STACK_SAMPLING_PROFILER_H_ |
| OLD | NEW |