Chromium Code Reviews| 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 data structures that can be shared across multiple | |
| 25 // instances of NativeStackSampler so long as they do not run concurrently. | |
| 26 class StackBuffer { | |
| 27 public: | |
| 28 StackBuffer(size_t buffer_size); | |
| 29 ~StackBuffer(); | |
| 30 | |
| 31 void* buffer() { return buffer_.get(); } | |
|
Mike Wittman
2017/05/10 17:17:21
nit: make both accessors const
bcwhite
2017/05/11 16:56:31
Done.
| |
| 32 size_t size() { return size_; } | |
| 33 | |
| 34 private: | |
| 35 // The word-aligned buffer. | |
| 36 const std::unique_ptr<int[]> buffer_; | |
|
Mike Wittman
2017/05/10 17:17:21
uintptr_t for 8 byte words on x86_64
bcwhite
2017/05/11 16:56:31
Done.
| |
| 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 // Creates an instance of the a stack buffer that can be used for calls to | |
| 63 // any NativeStackSampler object so long as such calls are not concurrent. | |
| 64 static std::unique_ptr<StackBuffer> CreateStackBuffer(); | |
| 65 | |
| 42 // The following functions are all called on the SamplingThread (not the | 66 // The following functions are all called on the SamplingThread (not the |
| 43 // thread being sampled). | 67 // thread being sampled). |
| 44 | 68 |
| 45 // Notifies the sampler that we're starting to record a new profile. Modules | 69 // 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|. | 70 // shared across samples in the profile should be recorded in |modules|. |
| 47 virtual void ProfileRecordingStarting( | 71 virtual void ProfileRecordingStarting( |
| 48 std::vector<StackSamplingProfiler::Module>* modules) = 0; | 72 std::vector<StackSamplingProfiler::Module>* modules) = 0; |
| 49 | 73 |
| 50 // Records a stack sample to |sample|. | 74 // Records a stack sample to |sample|. |
| 51 virtual void RecordStackSample(StackSamplingProfiler::Sample* sample) = 0; | 75 virtual void RecordStackSample(StackBuffer* stackbuffer, |
| 76 StackSamplingProfiler::Sample* sample) = 0; | |
| 52 | 77 |
| 53 // Notifies the sampler that we've stopped recording the current | 78 // Notifies the sampler that we've stopped recording the current |
| 54 // profile. | 79 // profile. |
| 55 virtual void ProfileRecordingStopped() = 0; | 80 virtual void ProfileRecordingStopped(StackBuffer* stackbuffer) = 0; |
| 56 | 81 |
| 57 protected: | 82 protected: |
| 58 NativeStackSampler(); | 83 NativeStackSampler(); |
| 59 | 84 |
| 60 private: | 85 private: |
| 61 DISALLOW_COPY_AND_ASSIGN(NativeStackSampler); | 86 DISALLOW_COPY_AND_ASSIGN(NativeStackSampler); |
| 62 }; | 87 }; |
| 63 | 88 |
| 64 // NativeStackSamplerTestDelegate provides seams for test code to execute during | 89 // NativeStackSamplerTestDelegate provides seams for test code to execute during |
| 65 // stack collection. | 90 // stack collection. |
| 66 class BASE_EXPORT NativeStackSamplerTestDelegate { | 91 class BASE_EXPORT NativeStackSamplerTestDelegate { |
| 67 public: | 92 public: |
| 68 virtual ~NativeStackSamplerTestDelegate(); | 93 virtual ~NativeStackSamplerTestDelegate(); |
| 69 | 94 |
| 70 // Called after copying the stack and resuming the target thread, but prior to | 95 // Called after copying the stack and resuming the target thread, but prior to |
| 71 // walking the stack. Invoked on the SamplingThread. | 96 // walking the stack. Invoked on the SamplingThread. |
| 72 virtual void OnPreStackWalk() = 0; | 97 virtual void OnPreStackWalk() = 0; |
| 73 | 98 |
| 74 protected: | 99 protected: |
| 75 NativeStackSamplerTestDelegate(); | 100 NativeStackSamplerTestDelegate(); |
| 76 | 101 |
| 77 private: | 102 private: |
| 78 DISALLOW_COPY_AND_ASSIGN(NativeStackSamplerTestDelegate); | 103 DISALLOW_COPY_AND_ASSIGN(NativeStackSamplerTestDelegate); |
| 79 }; | 104 }; |
| 80 | 105 |
| 81 } // namespace base | 106 } // namespace base |
| 82 | 107 |
| 83 #endif // BASE_PROFILER_NATIVE_STACK_SAMPLER_H_ | 108 #endif // BASE_PROFILER_NATIVE_STACK_SAMPLER_H_ |
| 84 | 109 |
| OLD | NEW |