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 215 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
226 // value should be from an enumeration of the appropriate type with values | 226 // value should be from an enumeration of the appropriate type with values |
227 // ranging from 0 to 31, inclusive. This sets bits within Sample field of | 227 // ranging from 0 to 31, inclusive. This sets bits within Sample field of |
228 // |process_phases|. The actual meanings of these bits are defined (globally) | 228 // |process_phases|. The actual meanings of these bits are defined (globally) |
229 // by the caller(s). | 229 // by the caller(s). |
230 static void SetProcessPhase(int phase); | 230 static void SetProcessPhase(int phase); |
231 static void ResetAnnotationsForTesting(); | 231 static void ResetAnnotationsForTesting(); |
232 | 232 |
233 private: | 233 private: |
234 // SamplingThread is a separate thread used to suspend and sample stacks from | 234 // SamplingThread is a separate thread used to suspend and sample stacks from |
235 // the target thread. | 235 // the target thread. |
236 class SamplingThread : public PlatformThread::Delegate { | 236 class SamplingThread; |
237 public: | |
238 // Samples stacks using |native_sampler|. When complete, invokes | |
239 // |completed_callback| with the collected call stack profiles. | |
240 // |completed_callback| must be callable on any thread. | |
241 SamplingThread(std::unique_ptr<NativeStackSampler> native_sampler, | |
242 const SamplingParams& params, | |
243 const CompletedCallback& completed_callback); | |
244 ~SamplingThread() override; | |
245 | |
246 // PlatformThread::Delegate: | |
247 void ThreadMain() override; | |
248 | |
249 void Stop(); | |
250 | |
251 private: | |
252 // Collects |profile| from a single burst. If the profiler was stopped | |
253 // during collection, sets |was_stopped| and provides the set of samples | |
254 // collected up to that point. | |
255 void CollectProfile(CallStackProfile* profile, TimeDelta* elapsed_time, | |
256 bool* was_stopped); | |
257 | |
258 // Collects call stack profiles from all bursts, or until the sampling is | |
259 // stopped. If stopped before complete, the last profile in | |
260 // |call_stack_profiles| may contain a partial burst. | |
261 void CollectProfiles(CallStackProfiles* profiles); | |
262 | |
263 std::unique_ptr<NativeStackSampler> native_sampler_; | |
264 const SamplingParams params_; | |
265 | |
266 // If Stop() is called, it signals this event to force the sampling to | |
267 // terminate before all the samples specified in |params_| are collected. | |
268 WaitableEvent stop_event_; | |
269 | |
270 const CompletedCallback completed_callback_; | |
271 | |
272 DISALLOW_COPY_AND_ASSIGN(SamplingThread); | |
273 }; | |
274 | 237 |
275 // Adds annotations to a Sample. | 238 // Adds annotations to a Sample. |
276 static void RecordAnnotations(Sample* sample); | 239 static void RecordAnnotations(Sample* sample); |
277 | 240 |
278 // This global variables holds the current system state and is recorded with | 241 // This global variables holds the current system state and is recorded with |
279 // every captured sample, done on a separate thread which is why updates to | 242 // every captured sample, done on a separate thread which is why updates to |
280 // this must be atomic. A PostTask to move the the updates to that thread | 243 // this must be atomic. A PostTask to move the the updates to that thread |
281 // would skew the timing and a lock could result in deadlock if the thread | 244 // would skew the timing and a lock could result in deadlock if the thread |
282 // making a change was also being profiled and got stopped. | 245 // making a change was also being profiled and got stopped. |
283 static subtle::Atomic32 process_phases_; | 246 static subtle::Atomic32 process_phases_; |
284 | 247 |
285 // The thread whose stack will be sampled. | 248 // The thread whose stack will be sampled. |
286 PlatformThreadId thread_id_; | 249 PlatformThreadId thread_id_; |
287 | 250 |
288 const SamplingParams params_; | 251 const SamplingParams params_; |
289 | 252 |
290 std::unique_ptr<SamplingThread> sampling_thread_; | 253 const CompletedCallback completed_callback_; |
291 PlatformThreadHandle sampling_thread_handle_; | |
292 | 254 |
293 const CompletedCallback completed_callback_; | 255 // An ID uniquely identifying this collection to the sampling thread. |
| 256 int collection_id_ = -1; |
294 | 257 |
295 // Stored until it can be passed to the NativeStackSampler created in Start(). | 258 // Stored until it can be passed to the NativeStackSampler created in Start(). |
296 NativeStackSamplerTestDelegate* const test_delegate_; | 259 NativeStackSamplerTestDelegate* const test_delegate_; |
297 | 260 |
298 DISALLOW_COPY_AND_ASSIGN(StackSamplingProfiler); | 261 DISALLOW_COPY_AND_ASSIGN(StackSamplingProfiler); |
299 }; | 262 }; |
300 | 263 |
301 // These operators permit types to be compared and used in a map of Samples, as | 264 // These operators permit types to be compared and used in a map of Samples, as |
302 // done in tests and by the metrics provider code. | 265 // done in tests and by the metrics provider code. |
303 BASE_EXPORT bool operator==(const StackSamplingProfiler::Module& a, | 266 BASE_EXPORT bool operator==(const StackSamplingProfiler::Module& a, |
304 const StackSamplingProfiler::Module& b); | 267 const StackSamplingProfiler::Module& b); |
305 BASE_EXPORT bool operator==(const StackSamplingProfiler::Sample& a, | 268 BASE_EXPORT bool operator==(const StackSamplingProfiler::Sample& a, |
306 const StackSamplingProfiler::Sample& b); | 269 const StackSamplingProfiler::Sample& b); |
307 BASE_EXPORT bool operator!=(const StackSamplingProfiler::Sample& a, | 270 BASE_EXPORT bool operator!=(const StackSamplingProfiler::Sample& a, |
308 const StackSamplingProfiler::Sample& b); | 271 const StackSamplingProfiler::Sample& b); |
309 BASE_EXPORT bool operator<(const StackSamplingProfiler::Sample& a, | 272 BASE_EXPORT bool operator<(const StackSamplingProfiler::Sample& a, |
310 const StackSamplingProfiler::Sample& b); | 273 const StackSamplingProfiler::Sample& b); |
311 BASE_EXPORT bool operator==(const StackSamplingProfiler::Frame& a, | 274 BASE_EXPORT bool operator==(const StackSamplingProfiler::Frame& a, |
312 const StackSamplingProfiler::Frame& b); | 275 const StackSamplingProfiler::Frame& b); |
313 BASE_EXPORT bool operator<(const StackSamplingProfiler::Frame& a, | 276 BASE_EXPORT bool operator<(const StackSamplingProfiler::Frame& a, |
314 const StackSamplingProfiler::Frame& b); | 277 const StackSamplingProfiler::Frame& b); |
315 | 278 |
316 } // namespace base | 279 } // namespace base |
317 | 280 |
318 #endif // BASE_PROFILER_STACK_SAMPLING_PROFILER_H_ | 281 #endif // BASE_PROFILER_STACK_SAMPLING_PROFILER_H_ |
OLD | NEW |