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