| 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/threading/platform_thread.h" | 17 #include "base/threading/platform_thread.h" |
| 17 #include "base/time/time.h" | 18 #include "base/time/time.h" |
| 18 | 19 |
| 19 namespace base { | 20 namespace base { |
| 20 | 21 |
| 22 class NativeStackSampler; |
| 23 |
| 21 // StackSamplingProfiler periodically stops a thread to sample its stack, for | 24 // StackSamplingProfiler periodically stops a thread to sample its stack, for |
| 22 // the purpose of collecting information about which code paths are | 25 // the purpose of collecting information about which code paths are |
| 23 // executing. This information is used in aggregate by UMA to identify hot | 26 // executing. This information is used in aggregate by UMA to identify hot |
| 24 // and/or janky code paths. | 27 // and/or janky code paths. |
| 25 // | 28 // |
| 26 // Sample StackStackSamplingProfiler usage: | 29 // Sample StackSamplingProfiler usage: |
| 27 // | 30 // |
| 28 // // Create and customize params as desired. | 31 // // Create and customize params as desired. |
| 29 // base::StackStackSamplingProfiler::SamplingParams params; | 32 // base::StackStackSamplingProfiler::SamplingParams params; |
| 30 // // Any thread's ID may be passed as the target. | 33 // // Any thread's ID may be passed as the target. |
| 31 // base::StackSamplingProfiler profiler(base::PlatformThread::CurrentId()), | 34 // base::StackSamplingProfiler profiler(base::PlatformThread::CurrentId()), |
| 32 // params); | 35 // params); |
| 33 // | 36 // |
| 34 // // To process the profiles within Chrome rather than via UMA, set a custom | 37 // // To process the call stack profiles within Chrome rather than via UMA, |
| 35 // // completed callback: | 38 // // set a custom completed callback: |
| 36 // base::Callback<void(const std::vector<Profile>&)> | 39 // base::StackStackSamplingProfiler::CompletedCallback |
| 37 // thread_safe_callback = ...; | 40 // thread_safe_callback = ...; |
| 38 // profiler.SetCustomCompletedCallback(thread_safe_callback); | 41 // profiler.SetCustomCompletedCallback(thread_safe_callback); |
| 39 // | 42 // |
| 40 // profiler.Start(); | 43 // profiler.Start(); |
| 41 // // ... work being done on the target thread here ... | 44 // // ... work being done on the target thread here ... |
| 42 // profiler.Stop(); // optional, stops collection before complete per params | 45 // profiler.Stop(); // optional, stops collection before complete per params |
| 43 // | 46 // |
| 44 // When all profiles are complete or the profiler is stopped, if the custom | 47 // The default SamplingParams causes stacks to be recorded in a single burst at |
| 45 // completed callback was set it will be called from the profiler thread with | 48 // a 10Hz interval for a total of 30 seconds. All of these parameters may be |
| 46 // the completed profiles. If no callback was set, the profiles are stored | 49 // altered as desired. |
| 47 // internally and retrieved for UMA through | 50 // |
| 48 // GetPendingProfiles(). GetPendingProfiles() should never be called by other | 51 // When all call stack profiles are complete or the profiler is stopped, if the |
| 49 // code; to retrieve profiles for in-process processing, set a completed | 52 // custom completed callback was set it is called from the profiler thread with |
| 50 // callback. | 53 // the completed profiles. A profile is considered complete if all requested |
| 54 // 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 // internally and retrieved for UMA through GetPendingProfiles(). |
| 57 // GetPendingProfiles() should never be called by other code; to retrieve |
| 58 // profiles for in-process processing, set a completed callback. |
| 59 // |
| 60 // 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 // 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 // record those modules associated with the recorded stack frames. |
| 51 class BASE_EXPORT StackSamplingProfiler { | 65 class BASE_EXPORT StackSamplingProfiler { |
| 52 public: | 66 public: |
| 53 // Module represents the module (DLL or exe) corresponding to a stack frame. | 67 // Module represents the module (DLL or exe) corresponding to a stack frame. |
| 54 struct BASE_EXPORT Module { | 68 struct BASE_EXPORT Module { |
| 55 Module(); | 69 Module(); |
| 56 Module(const void* base_address, const std::string& id, | 70 Module(const void* base_address, const std::string& id, |
| 57 const FilePath& filename); | 71 const FilePath& filename); |
| 58 ~Module(); | 72 ~Module(); |
| 59 | 73 |
| 60 // Points to the base address of the module. | 74 // Points to the base address of the module. |
| 61 const void* base_address; | 75 const void* base_address; |
| 76 |
| 62 // An opaque binary string that uniquely identifies a particular program | 77 // An opaque binary string that uniquely identifies a particular program |
| 63 // version with high probability. This is parsed from headers of the loaded | 78 // version with high probability. This is parsed from headers of the loaded |
| 64 // module. | 79 // module. |
| 65 // For binaries generated by GNU tools: | 80 // For binaries generated by GNU tools: |
| 66 // Contents of the .note.gnu.build-id field. | 81 // Contents of the .note.gnu.build-id field. |
| 67 // On Windows: | 82 // On Windows: |
| 68 // GUID + AGE in the debug image headers of a module. | 83 // GUID + AGE in the debug image headers of a module. |
| 69 std::string id; | 84 std::string id; |
| 85 |
| 70 // The filename of the module. | 86 // The filename of the module. |
| 71 FilePath filename; | 87 FilePath filename; |
| 72 }; | 88 }; |
| 73 | 89 |
| 74 // Frame represents an individual sampled stack frame with module information. | 90 // Frame represents an individual sampled stack frame with module information. |
| 75 struct BASE_EXPORT Frame { | 91 struct BASE_EXPORT Frame { |
| 76 Frame(); | 92 // Identifies an unknown module. |
| 77 Frame(const void* instruction_pointer, int module_index); | 93 static const size_t kUnknownModuleIndex = static_cast<size_t>(-1); |
| 94 |
| 95 Frame(const void* instruction_pointer, size_t module_index); |
| 78 ~Frame(); | 96 ~Frame(); |
| 79 | 97 |
| 80 // The sampled instruction pointer within the function. | 98 // The sampled instruction pointer within the function. |
| 81 const void* instruction_pointer; | 99 const void* instruction_pointer; |
| 82 // Index of the module in the array of modules. We don't represent module | 100 |
| 83 // state directly here to save space. | 101 // Index of the module in CallStackProfile::modules. We don't represent |
| 84 int module_index; | 102 // module state directly here to save space. |
| 103 size_t module_index; |
| 85 }; | 104 }; |
| 86 | 105 |
| 87 // Sample represents a set of stack frames. | 106 // Sample represents a set of stack frames. |
| 88 using Sample = std::vector<Frame>; | 107 using Sample = std::vector<Frame>; |
| 89 | 108 |
| 90 // Profile represents a set of samples. | 109 // CallStackProfile represents a set of samples. |
| 91 struct BASE_EXPORT Profile { | 110 struct BASE_EXPORT CallStackProfile { |
| 92 Profile(); | 111 CallStackProfile(); |
| 93 ~Profile(); | 112 ~CallStackProfile(); |
| 94 | 113 |
| 95 std::vector<Module> modules; | 114 std::vector<Module> modules; |
| 96 std::vector<Sample> samples; | 115 std::vector<Sample> samples; |
| 116 |
| 97 // Duration of this profile. | 117 // Duration of this profile. |
| 98 TimeDelta profile_duration; | 118 TimeDelta profile_duration; |
| 119 |
| 99 // Time between samples. | 120 // Time between samples. |
| 100 TimeDelta sampling_period; | 121 TimeDelta sampling_period; |
| 122 |
| 101 // True if sample ordering is important and should be preserved if and when | 123 // True if sample ordering is important and should be preserved if and when |
| 102 // this profile is compressed and processed. | 124 // this profile is compressed and processed. |
| 103 bool preserve_sample_ordering; | 125 bool preserve_sample_ordering; |
| 104 }; | 126 }; |
| 105 | 127 |
| 106 // NativeStackSampler abstracts the native implementation required to record a | 128 using CallStackProfiles = std::vector<CallStackProfile>; |
| 107 // stack sample for a given thread. | |
| 108 class NativeStackSampler { | |
| 109 public: | |
| 110 virtual ~NativeStackSampler(); | |
| 111 | |
| 112 // Create a stack sampler that records samples for |thread_handle|. Returns | |
| 113 // null if this platform does not support stack sampling. | |
| 114 static scoped_ptr<NativeStackSampler> Create(PlatformThreadId thread_id); | |
| 115 | |
| 116 // Notify the sampler that we're starting to record a new profile. This | |
| 117 // function is called on the SamplingThread. | |
| 118 virtual void ProfileRecordingStarting(Profile* profile) = 0; | |
| 119 | |
| 120 // Record a stack sample. This function is called on the SamplingThread. | |
| 121 virtual void RecordStackSample(Sample* sample) = 0; | |
| 122 | |
| 123 // Notify the sampler that we've stopped recording the current profile. This | |
| 124 // function is called on the SamplingThread. | |
| 125 virtual void ProfileRecordingStopped() = 0; | |
| 126 | |
| 127 protected: | |
| 128 NativeStackSampler(); | |
| 129 | |
| 130 private: | |
| 131 DISALLOW_COPY_AND_ASSIGN(NativeStackSampler); | |
| 132 }; | |
| 133 | 129 |
| 134 // Represents parameters that configure the sampling. | 130 // Represents parameters that configure the sampling. |
| 135 struct BASE_EXPORT SamplingParams { | 131 struct BASE_EXPORT SamplingParams { |
| 136 SamplingParams(); | 132 SamplingParams(); |
| 137 | 133 |
| 138 // Time to delay before first samples are taken. Defaults to 0. | 134 // Time to delay before first samples are taken. Defaults to 0. |
| 139 TimeDelta initial_delay; | 135 TimeDelta initial_delay; |
| 136 |
| 140 // Number of sampling bursts to perform. Defaults to 1. | 137 // Number of sampling bursts to perform. Defaults to 1. |
| 141 int bursts; | 138 int bursts; |
| 139 |
| 142 // Interval between sampling bursts. This is the desired duration from the | 140 // Interval between sampling bursts. This is the desired duration from the |
| 143 // start of one burst to the start of the next burst. Defaults to 10s. | 141 // start of one burst to the start of the next burst. Defaults to 10s. |
| 144 TimeDelta burst_interval; | 142 TimeDelta burst_interval; |
| 143 |
| 145 // Number of samples to record per burst. Defaults to 300. | 144 // Number of samples to record per burst. Defaults to 300. |
| 146 int samples_per_burst; | 145 int samples_per_burst; |
| 146 |
| 147 // Interval between samples during a sampling burst. This is the desired | 147 // Interval between samples during a sampling burst. This is the desired |
| 148 // duration from the start of one burst to the start of the next | 148 // duration from the start of one sample to the start of the next |
| 149 // burst. Defaults to 100ms. | 149 // sample. Defaults to 100ms. |
| 150 TimeDelta sampling_interval; | 150 TimeDelta sampling_interval; |
| 151 |
| 151 // True if sample ordering is important and should be preserved if and when | 152 // True if sample ordering is important and should be preserved if and when |
| 152 // this profile is compressed and processed. Defaults to false. | 153 // this profile is compressed and processed. Defaults to false. |
| 153 bool preserve_sample_ordering; | 154 bool preserve_sample_ordering; |
| 154 }; | 155 }; |
| 155 | 156 |
| 157 // The callback type used to collect completed profiles. |
| 158 // |
| 159 // 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 // set the callback, and thus the callback must be callable on any thread. |
| 162 using CompletedCallback = Callback<void(const CallStackProfiles&)>; |
| 163 |
| 156 StackSamplingProfiler(PlatformThreadId thread_id, | 164 StackSamplingProfiler(PlatformThreadId thread_id, |
| 157 const SamplingParams& params); | 165 const SamplingParams& params); |
| 158 ~StackSamplingProfiler(); | 166 ~StackSamplingProfiler(); |
| 159 | 167 |
| 160 // Initializes the profiler and starts sampling. | 168 // Initializes the profiler and starts sampling. |
| 161 void Start(); | 169 void Start(); |
| 170 |
| 162 // Stops the profiler and any ongoing sampling. Calling this function is | 171 // Stops the profiler and any ongoing sampling. Calling this function is |
| 163 // optional; if not invoked profiling will terminate when all the profiling | 172 // optional; if not invoked profiling terminates when all the profiling bursts |
| 164 // bursts specified in the SamplingParams are completed. | 173 // specified in the SamplingParams are completed. |
| 165 void Stop(); | 174 void Stop(); |
| 166 | 175 |
| 167 // Gets the pending profiles into *|profiles| and clears the internal | 176 // Moves all pending call stack profiles from internal storage to |
| 168 // storage. This function is thread safe. | 177 // |profiles|. This function is thread safe. |
| 169 // | 178 // |
| 170 // ***This is intended for use only by UMA.*** Callers who want to process the | 179 // ***This is intended for use only by UMA.*** Callers who want to process the |
| 171 // collected profiles should use SetCustomCompletedCallback. | 180 // collected profiles should use SetCustomCompletedCallback. |
| 172 static void GetPendingProfiles(std::vector<Profile>* profiles); | 181 static void GetPendingProfiles(CallStackProfiles* call_stack_profiles); |
| 173 | 182 |
| 174 // By default, collected profiles are stored internally and can be retrieved | 183 // By default, collected call stack profiles are stored internally and can be |
| 175 // by GetPendingProfiles. If a callback is provided via this function, | 184 // retrieved by GetPendingProfiles. If a callback is provided via this |
| 176 // however, it will be called with the collected profiles instead. Note that | 185 // function, however, it is called with the collected profiles instead. |
| 177 // this call to the callback occurs *on the profiler thread*. | 186 void set_custom_completed_callback(CompletedCallback callback) { |
| 178 void SetCustomCompletedCallback( | 187 custom_completed_callback_ = callback; |
| 179 Callback<void(const std::vector<Profile>&)> callback); | 188 } |
| 180 | 189 |
| 181 private: | 190 private: |
| 182 class SamplingThread; | 191 // SamplingThread is a separate thread used to suspend and sample stacks from |
| 183 struct SamplingThreadDeleter { | 192 // the target thread. |
| 184 void operator() (SamplingThread* thread) const; | 193 class SamplingThread : public PlatformThread::Delegate { |
| 194 public: |
| 195 // Samples stacks using |native_sampler|. When complete, invokes |
| 196 // |completed_callback| with the collected call stack profiles. |
| 197 // |completed_callback| must be callable on any thread. |
| 198 SamplingThread(scoped_ptr<NativeStackSampler> native_sampler, |
| 199 const SamplingParams& params, |
| 200 CompletedCallback completed_callback); |
| 201 ~SamplingThread() override; |
| 202 |
| 203 // PlatformThread::Delegate: |
| 204 void ThreadMain() override; |
| 205 |
| 206 void Stop(); |
| 207 |
| 208 private: |
| 209 // Collects a call stack profile from a single burst. Returns true if the |
| 210 // profile was collected, or false if collection was stopped before it |
| 211 // completed. |
| 212 bool CollectProfile(CallStackProfile* profile, TimeDelta* elapsed_time); |
| 213 |
| 214 // Collects call stack profiles from all bursts, or until the sampling is |
| 215 // stopped. If stopped before complete, |call_stack_profiles| will contain |
| 216 // only full bursts. |
| 217 void CollectProfiles(CallStackProfiles* profiles); |
| 218 |
| 219 scoped_ptr<NativeStackSampler> native_sampler_; |
| 220 const SamplingParams params_; |
| 221 |
| 222 // If Stop() is called, it signals this event to force the sampling to |
| 223 // terminate before all the samples specified in |params_| are collected. |
| 224 WaitableEvent stop_event_; |
| 225 |
| 226 CompletedCallback completed_callback_; |
| 227 |
| 228 DISALLOW_COPY_AND_ASSIGN(SamplingThread); |
| 185 }; | 229 }; |
| 186 | 230 |
| 187 // The thread whose stack will be sampled. | 231 // The thread whose stack will be sampled. |
| 188 PlatformThreadId thread_id_; | 232 PlatformThreadId thread_id_; |
| 189 | 233 |
| 190 const SamplingParams params_; | 234 const SamplingParams params_; |
| 191 | 235 |
| 192 scoped_ptr<SamplingThread, SamplingThreadDeleter> sampling_thread_; | 236 scoped_ptr<SamplingThread> sampling_thread_; |
| 193 scoped_ptr<NativeStackSampler> native_sampler_; | |
| 194 | 237 |
| 195 Callback<void(const std::vector<Profile>&)> custom_completed_callback_; | 238 CompletedCallback custom_completed_callback_; |
| 196 | 239 |
| 197 DISALLOW_COPY_AND_ASSIGN(StackSamplingProfiler); | 240 DISALLOW_COPY_AND_ASSIGN(StackSamplingProfiler); |
| 198 }; | 241 }; |
| 199 | 242 |
| 200 // Defined to allow equality check of Samples. | 243 // The metrics provider code wants to put Samples in a map and compare them, |
| 244 // which requires us to define a few operators. |
| 201 BASE_EXPORT bool operator==(const StackSamplingProfiler::Frame& a, | 245 BASE_EXPORT bool operator==(const StackSamplingProfiler::Frame& a, |
| 202 const StackSamplingProfiler::Frame& b); | 246 const StackSamplingProfiler::Frame& b); |
| 203 // Defined to allow ordering of Samples. | |
| 204 BASE_EXPORT bool operator<(const StackSamplingProfiler::Frame& a, | 247 BASE_EXPORT bool operator<(const StackSamplingProfiler::Frame& a, |
| 205 const StackSamplingProfiler::Frame& b); | 248 const StackSamplingProfiler::Frame& b); |
| 206 | 249 |
| 207 } // namespace base | 250 } // namespace base |
| 208 | 251 |
| 209 #endif // BASE_PROFILER_STACK_SAMPLING_PROFILER_H_ | 252 #endif // BASE_PROFILER_STACK_SAMPLING_PROFILER_H_ |
| OLD | NEW |