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_NATIVE_STACK_SAMPLER_H_ | 5 #ifndef BASE_PROFILER_NATIVE_STACK_SAMPLER_H_ |
6 #define BASE_PROFILER_NATIVE_STACK_SAMPLER_H_ | 6 #define BASE_PROFILER_NATIVE_STACK_SAMPLER_H_ |
7 | 7 |
8 #include <memory> | 8 #include <memory> |
9 | 9 |
10 #include "base/base_export.h" | 10 #include "base/base_export.h" |
11 #include "base/macros.h" | 11 #include "base/macros.h" |
12 #include "base/profiler/stack_sampling_profiler.h" | 12 #include "base/profiler/stack_sampling_profiler.h" |
13 #include "base/threading/platform_thread.h" | 13 #include "base/threading/platform_thread.h" |
14 | 14 |
15 namespace base { | 15 namespace base { |
16 | 16 |
17 class NativeStackSamplerTestDelegate; | 17 class NativeStackSamplerTestDelegate; |
18 | 18 |
19 // NativeStackSampler is an implementation detail of StackSamplingProfiler. It | 19 // NativeStackSampler is an implementation detail of StackSamplingProfiler. It |
20 // abstracts the native implementation required to record a stack sample for a | 20 // abstracts the native implementation required to record a stack sample for a |
21 // given thread. | 21 // given thread. |
22 class NativeStackSampler { | 22 class NativeStackSampler { |
23 public: | 23 public: |
| 24 // This class contains a buffer for stack copies that can be shared across |
| 25 // multiple instances of NativeStackSampler. |
| 26 class StackBuffer { |
| 27 public: |
| 28 StackBuffer(size_t buffer_size); |
| 29 ~StackBuffer(); |
| 30 |
| 31 void* buffer() const { return buffer_.get(); } |
| 32 size_t size() const { return size_; } |
| 33 |
| 34 private: |
| 35 // The word-aligned buffer. |
| 36 const std::unique_ptr<uintptr_t[]> buffer_; |
| 37 |
| 38 // The size of the buffer. |
| 39 const size_t size_; |
| 40 |
| 41 DISALLOW_COPY_AND_ASSIGN(StackBuffer); |
| 42 }; |
| 43 |
24 // The callback type used to add annotations to a sample during collection. | 44 // The callback type used to add annotations to a sample during collection. |
25 // This is passed to the native sampler to be applied at the most appropriate | 45 // This is passed to the native sampler to be applied at the most appropriate |
26 // time. It is a simple function-pointer because the generated code must be | 46 // time. It is a simple function-pointer because the generated code must be |
27 // completely predictable and do nothing that could acquire a mutex; a | 47 // completely predictable and do nothing that could acquire a mutex; a |
28 // Callback object is code outside the control of this object and could, | 48 // Callback object is code outside the control of this object and could, |
29 // for example, acquire a mutex as part of allocating memory for a LOG | 49 // for example, acquire a mutex as part of allocating memory for a LOG |
30 // message. | 50 // message. |
31 using AnnotateCallback = void (*)(StackSamplingProfiler::Sample*); | 51 using AnnotateCallback = void (*)(StackSamplingProfiler::Sample*); |
32 | 52 |
33 virtual ~NativeStackSampler(); | 53 virtual ~NativeStackSampler(); |
34 | 54 |
35 // Creates a stack sampler that records samples for |thread_handle|. Returns | 55 // Creates a stack sampler that records samples for |thread_handle|. Returns |
36 // null if this platform does not support stack sampling. | 56 // null if this platform does not support stack sampling. |
37 static std::unique_ptr<NativeStackSampler> Create( | 57 static std::unique_ptr<NativeStackSampler> Create( |
38 PlatformThreadId thread_id, | 58 PlatformThreadId thread_id, |
39 AnnotateCallback annotator, | 59 AnnotateCallback annotator, |
40 NativeStackSamplerTestDelegate* test_delegate); | 60 NativeStackSamplerTestDelegate* test_delegate); |
41 | 61 |
| 62 // Gets the required size of the stack buffer. |
| 63 static size_t GetStackBufferSize(); |
| 64 |
| 65 // Creates an instance of the a stack buffer that can be used for calls to |
| 66 // any NativeStackSampler object. |
| 67 static std::unique_ptr<StackBuffer> CreateStackBuffer(); |
| 68 |
42 // The following functions are all called on the SamplingThread (not the | 69 // The following functions are all called on the SamplingThread (not the |
43 // thread being sampled). | 70 // thread being sampled). |
44 | 71 |
45 // Notifies the sampler that we're starting to record a new profile. Modules | 72 // Notifies the sampler that we're starting to record a new profile. Modules |
46 // shared across samples in the profile should be recorded in |modules|. | 73 // shared across samples in the profile should be recorded in |modules|. |
47 virtual void ProfileRecordingStarting( | 74 virtual void ProfileRecordingStarting( |
48 std::vector<StackSamplingProfiler::Module>* modules) = 0; | 75 std::vector<StackSamplingProfiler::Module>* modules) = 0; |
49 | 76 |
50 // Records a stack sample to |sample|. | 77 // Records a stack sample to |sample|. |
51 virtual void RecordStackSample(StackSamplingProfiler::Sample* sample) = 0; | 78 virtual void RecordStackSample(StackBuffer* stackbuffer, |
| 79 StackSamplingProfiler::Sample* sample) = 0; |
52 | 80 |
53 // Notifies the sampler that we've stopped recording the current | 81 // Notifies the sampler that we've stopped recording the current |
54 // profile. | 82 // profile. |
55 virtual void ProfileRecordingStopped() = 0; | 83 virtual void ProfileRecordingStopped(StackBuffer* stackbuffer) = 0; |
56 | 84 |
57 protected: | 85 protected: |
58 NativeStackSampler(); | 86 NativeStackSampler(); |
59 | 87 |
60 private: | 88 private: |
61 DISALLOW_COPY_AND_ASSIGN(NativeStackSampler); | 89 DISALLOW_COPY_AND_ASSIGN(NativeStackSampler); |
62 }; | 90 }; |
63 | 91 |
64 // NativeStackSamplerTestDelegate provides seams for test code to execute during | 92 // NativeStackSamplerTestDelegate provides seams for test code to execute during |
65 // stack collection. | 93 // stack collection. |
66 class BASE_EXPORT NativeStackSamplerTestDelegate { | 94 class BASE_EXPORT NativeStackSamplerTestDelegate { |
67 public: | 95 public: |
68 virtual ~NativeStackSamplerTestDelegate(); | 96 virtual ~NativeStackSamplerTestDelegate(); |
69 | 97 |
70 // Called after copying the stack and resuming the target thread, but prior to | 98 // Called after copying the stack and resuming the target thread, but prior to |
71 // walking the stack. Invoked on the SamplingThread. | 99 // walking the stack. Invoked on the SamplingThread. |
72 virtual void OnPreStackWalk() = 0; | 100 virtual void OnPreStackWalk() = 0; |
73 | 101 |
74 protected: | 102 protected: |
75 NativeStackSamplerTestDelegate(); | 103 NativeStackSamplerTestDelegate(); |
76 | 104 |
77 private: | 105 private: |
78 DISALLOW_COPY_AND_ASSIGN(NativeStackSamplerTestDelegate); | 106 DISALLOW_COPY_AND_ASSIGN(NativeStackSamplerTestDelegate); |
79 }; | 107 }; |
80 | 108 |
81 } // namespace base | 109 } // namespace base |
82 | 110 |
83 #endif // BASE_PROFILER_NATIVE_STACK_SAMPLER_H_ | 111 #endif // BASE_PROFILER_NATIVE_STACK_SAMPLER_H_ |
84 | 112 |
OLD | NEW |