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
3 // found in the LICENSE file.
4 
5 #ifndef BASE_TRACE_EVENT_TRACE_SAMPLING_THREAD_H_
6 #define BASE_TRACE_EVENT_TRACE_SAMPLING_THREAD_H_
7 
8 #include "base/synchronization/cancellation_flag.h"
9 #include "base/synchronization/waitable_event.h"
10 #include "base/trace_event/trace_event.h"
11 
12 namespace base {
13 namespace trace_event {
14 
15 class TraceBucketData;
16 typedef base::Callback<void(TraceBucketData*)> TraceSampleCallback;
17 
18 // This object must be created on the IO thread.
19 class TraceSamplingThread : public PlatformThread::Delegate {
20  public:
21   TraceSamplingThread();
22   ~TraceSamplingThread() override;
23 
24   // Implementation of PlatformThread::Delegate:
25   void ThreadMain() override;
26 
27   static void DefaultSamplingCallback(TraceBucketData* bucket_data);
28 
29   void Stop();
30   void WaitSamplingEventForTesting();
31 
32  private:
33   friend class TraceLog;
34 
35   void GetSamples();
36   // Not thread-safe. Once the ThreadMain has been called, this can no longer
37   // be called.
38   void RegisterSampleBucket(TRACE_EVENT_API_ATOMIC_WORD* bucket,
39                             const char* const name,
40                             TraceSampleCallback callback);
41   // Splits a combined "category\0name" into the two component parts.
42   static void ExtractCategoryAndName(const char* combined,
43                                      const char** category,
44                                      const char** name);
45   std::vector<TraceBucketData> sample_buckets_;
46   bool thread_running_;
47   CancellationFlag cancellation_flag_;
48   WaitableEvent waitable_event_for_testing_;
49 };
50 
51 }  // namespace trace_event
52 }  // namespace base
53 
54 #endif  // BASE_TRACE_EVENT_TRACE_SAMPLING_THREAD_H_
55