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. For |
| 162 // threads with message loops that create StackSamplingProfilers, posting a |
| 163 // task to the message loop with a copy of the profiles is the recommended |
| 164 // thread-safe callback implementation. |
| 165 using CompletedCallback = Callback<void(const CallStackProfiles&)>; |
| 166 |
156 StackSamplingProfiler(PlatformThreadId thread_id, | 167 StackSamplingProfiler(PlatformThreadId thread_id, |
157 const SamplingParams& params); | 168 const SamplingParams& params); |
158 ~StackSamplingProfiler(); | 169 ~StackSamplingProfiler(); |
159 | 170 |
160 // Initializes the profiler and starts sampling. | 171 // Initializes the profiler and starts sampling. |
161 void Start(); | 172 void Start(); |
| 173 |
162 // Stops the profiler and any ongoing sampling. Calling this function is | 174 // Stops the profiler and any ongoing sampling. Calling this function is |
163 // optional; if not invoked profiling will terminate when all the profiling | 175 // optional; if not invoked profiling terminates when all the profiling bursts |
164 // bursts specified in the SamplingParams are completed. | 176 // specified in the SamplingParams are completed. |
165 void Stop(); | 177 void Stop(); |
166 | 178 |
167 // Gets the pending profiles into *|profiles| and clears the internal | 179 // Moves all pending call stack profiles from internal storage to |
168 // storage. This function is thread safe. | 180 // |profiles|. This function is thread safe. |
169 // | 181 // |
170 // ***This is intended for use only by UMA.*** Callers who want to process the | 182 // ***This is intended for use only by UMA.*** Callers who want to process the |
171 // collected profiles should use SetCustomCompletedCallback. | 183 // collected profiles should use SetCustomCompletedCallback. |
172 static void GetPendingProfiles(std::vector<Profile>* profiles); | 184 static void GetPendingProfiles(CallStackProfiles* call_stack_profiles); |
173 | 185 |
174 // By default, collected profiles are stored internally and can be retrieved | 186 // By default, collected call stack profiles are stored internally and can be |
175 // by GetPendingProfiles. If a callback is provided via this function, | 187 // retrieved by GetPendingProfiles. If a callback is provided via this |
176 // however, it will be called with the collected profiles instead. Note that | 188 // function, however, it is called with the collected profiles instead. |
177 // this call to the callback occurs *on the profiler thread*. | 189 void set_custom_completed_callback(CompletedCallback callback) { |
178 void SetCustomCompletedCallback( | 190 custom_completed_callback_ = callback; |
179 Callback<void(const std::vector<Profile>&)> callback); | 191 } |
180 | 192 |
181 private: | 193 private: |
182 class SamplingThread; | 194 // SamplingThread is a separate thread used to suspend and sample stacks from |
183 struct SamplingThreadDeleter { | 195 // the target thread. |
184 void operator() (SamplingThread* thread) const; | 196 class SamplingThread : public PlatformThread::Delegate { |
| 197 public: |
| 198 // Samples stacks using |native_sampler|. When complete, invokes |
| 199 // |completed_callback| with the collected call stack profiles. |
| 200 // |completed_callback| must be callable on any thread. |
| 201 SamplingThread(scoped_ptr<NativeStackSampler> native_sampler, |
| 202 const SamplingParams& params, |
| 203 CompletedCallback completed_callback); |
| 204 ~SamplingThread() override; |
| 205 |
| 206 // PlatformThread::Delegate: |
| 207 void ThreadMain() override; |
| 208 |
| 209 void Stop(); |
| 210 |
| 211 private: |
| 212 // Collects a call stack profile from a single burst. Returns true if the |
| 213 // profile was collected, or false if collection was stopped before it |
| 214 // completed. |
| 215 bool CollectProfile(CallStackProfile* profile, TimeDelta* elapsed_time); |
| 216 |
| 217 // Collects call stack profiles from all bursts, or until the sampling is |
| 218 // stopped. If stopped before complete, |call_stack_profiles| will contain |
| 219 // only full bursts. |
| 220 void CollectProfiles(CallStackProfiles* profiles); |
| 221 |
| 222 scoped_ptr<NativeStackSampler> native_sampler_; |
| 223 const SamplingParams params_; |
| 224 |
| 225 // If Stop() is called, it signals this event to force the sampling to |
| 226 // terminate before all the samples specified in |params_| are collected. |
| 227 WaitableEvent stop_event_; |
| 228 |
| 229 CompletedCallback completed_callback_; |
| 230 |
| 231 DISALLOW_COPY_AND_ASSIGN(SamplingThread); |
185 }; | 232 }; |
186 | 233 |
187 // The thread whose stack will be sampled. | 234 // The thread whose stack will be sampled. |
188 PlatformThreadId thread_id_; | 235 PlatformThreadId thread_id_; |
189 | 236 |
190 const SamplingParams params_; | 237 const SamplingParams params_; |
191 | 238 |
192 scoped_ptr<SamplingThread, SamplingThreadDeleter> sampling_thread_; | 239 scoped_ptr<SamplingThread> sampling_thread_; |
193 scoped_ptr<NativeStackSampler> native_sampler_; | |
194 | 240 |
195 Callback<void(const std::vector<Profile>&)> custom_completed_callback_; | 241 CompletedCallback custom_completed_callback_; |
196 | 242 |
197 DISALLOW_COPY_AND_ASSIGN(StackSamplingProfiler); | 243 DISALLOW_COPY_AND_ASSIGN(StackSamplingProfiler); |
198 }; | 244 }; |
199 | 245 |
200 // Defined to allow equality check of Samples. | 246 // The metrics provider code wants to put Samples in a map and compare them, |
| 247 // which requires us to define a few operators. |
201 BASE_EXPORT bool operator==(const StackSamplingProfiler::Frame& a, | 248 BASE_EXPORT bool operator==(const StackSamplingProfiler::Frame& a, |
202 const StackSamplingProfiler::Frame& b); | 249 const StackSamplingProfiler::Frame& b); |
203 // Defined to allow ordering of Samples. | |
204 BASE_EXPORT bool operator<(const StackSamplingProfiler::Frame& a, | 250 BASE_EXPORT bool operator<(const StackSamplingProfiler::Frame& a, |
205 const StackSamplingProfiler::Frame& b); | 251 const StackSamplingProfiler::Frame& b); |
206 | 252 |
207 } // namespace base | 253 } // namespace base |
208 | 254 |
209 #endif // BASE_PROFILER_STACK_SAMPLING_PROFILER_H_ | 255 #endif // BASE_PROFILER_STACK_SAMPLING_PROFILER_H_ |
OLD | NEW |