1*635a8641SAndroid Build Coastguard Worker // Copyright 2015 The Chromium Authors. All rights reserved. 2*635a8641SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be 3*635a8641SAndroid Build Coastguard Worker // found in the LICENSE file. 4*635a8641SAndroid Build Coastguard Worker 5*635a8641SAndroid Build Coastguard Worker #ifndef BASE_PROFILER_NATIVE_STACK_SAMPLER_H_ 6*635a8641SAndroid Build Coastguard Worker #define BASE_PROFILER_NATIVE_STACK_SAMPLER_H_ 7*635a8641SAndroid Build Coastguard Worker 8*635a8641SAndroid Build Coastguard Worker #include <memory> 9*635a8641SAndroid Build Coastguard Worker 10*635a8641SAndroid Build Coastguard Worker #include "base/base_export.h" 11*635a8641SAndroid Build Coastguard Worker #include "base/macros.h" 12*635a8641SAndroid Build Coastguard Worker #include "base/profiler/stack_sampling_profiler.h" 13*635a8641SAndroid Build Coastguard Worker #include "base/threading/platform_thread.h" 14*635a8641SAndroid Build Coastguard Worker 15*635a8641SAndroid Build Coastguard Worker namespace base { 16*635a8641SAndroid Build Coastguard Worker 17*635a8641SAndroid Build Coastguard Worker class NativeStackSamplerTestDelegate; 18*635a8641SAndroid Build Coastguard Worker 19*635a8641SAndroid Build Coastguard Worker // NativeStackSampler is an implementation detail of StackSamplingProfiler. It 20*635a8641SAndroid Build Coastguard Worker // abstracts the native implementation required to record a set of stack frames 21*635a8641SAndroid Build Coastguard Worker // for a given thread. 22*635a8641SAndroid Build Coastguard Worker class NativeStackSampler { 23*635a8641SAndroid Build Coastguard Worker public: 24*635a8641SAndroid Build Coastguard Worker // This class contains a buffer for stack copies that can be shared across 25*635a8641SAndroid Build Coastguard Worker // multiple instances of NativeStackSampler. 26*635a8641SAndroid Build Coastguard Worker class StackBuffer { 27*635a8641SAndroid Build Coastguard Worker public: 28*635a8641SAndroid Build Coastguard Worker StackBuffer(size_t buffer_size); 29*635a8641SAndroid Build Coastguard Worker ~StackBuffer(); 30*635a8641SAndroid Build Coastguard Worker buffer()31*635a8641SAndroid Build Coastguard Worker void* buffer() const { return buffer_.get(); } size()32*635a8641SAndroid Build Coastguard Worker size_t size() const { return size_; } 33*635a8641SAndroid Build Coastguard Worker 34*635a8641SAndroid Build Coastguard Worker private: 35*635a8641SAndroid Build Coastguard Worker // The word-aligned buffer. 36*635a8641SAndroid Build Coastguard Worker const std::unique_ptr<uintptr_t[]> buffer_; 37*635a8641SAndroid Build Coastguard Worker 38*635a8641SAndroid Build Coastguard Worker // The size of the buffer. 39*635a8641SAndroid Build Coastguard Worker const size_t size_; 40*635a8641SAndroid Build Coastguard Worker 41*635a8641SAndroid Build Coastguard Worker DISALLOW_COPY_AND_ASSIGN(StackBuffer); 42*635a8641SAndroid Build Coastguard Worker }; 43*635a8641SAndroid Build Coastguard Worker 44*635a8641SAndroid Build Coastguard Worker virtual ~NativeStackSampler(); 45*635a8641SAndroid Build Coastguard Worker 46*635a8641SAndroid Build Coastguard Worker // Creates a stack sampler that records samples for thread with |thread_id|. 47*635a8641SAndroid Build Coastguard Worker // Returns null if this platform does not support stack sampling. 48*635a8641SAndroid Build Coastguard Worker static std::unique_ptr<NativeStackSampler> Create( 49*635a8641SAndroid Build Coastguard Worker PlatformThreadId thread_id, 50*635a8641SAndroid Build Coastguard Worker NativeStackSamplerTestDelegate* test_delegate); 51*635a8641SAndroid Build Coastguard Worker 52*635a8641SAndroid Build Coastguard Worker // Gets the required size of the stack buffer. 53*635a8641SAndroid Build Coastguard Worker static size_t GetStackBufferSize(); 54*635a8641SAndroid Build Coastguard Worker 55*635a8641SAndroid Build Coastguard Worker // Creates an instance of the a stack buffer that can be used for calls to 56*635a8641SAndroid Build Coastguard Worker // any NativeStackSampler object. 57*635a8641SAndroid Build Coastguard Worker static std::unique_ptr<StackBuffer> CreateStackBuffer(); 58*635a8641SAndroid Build Coastguard Worker 59*635a8641SAndroid Build Coastguard Worker // The following functions are all called on the SamplingThread (not the 60*635a8641SAndroid Build Coastguard Worker // thread being sampled). 61*635a8641SAndroid Build Coastguard Worker 62*635a8641SAndroid Build Coastguard Worker // Notifies the sampler that we're starting to record a new profile. 63*635a8641SAndroid Build Coastguard Worker virtual void ProfileRecordingStarting() = 0; 64*635a8641SAndroid Build Coastguard Worker 65*635a8641SAndroid Build Coastguard Worker // Records a set of internal frames and returns them. 66*635a8641SAndroid Build Coastguard Worker virtual std::vector<StackSamplingProfiler::InternalFrame> RecordStackFrames( 67*635a8641SAndroid Build Coastguard Worker StackBuffer* stackbuffer, 68*635a8641SAndroid Build Coastguard Worker StackSamplingProfiler::ProfileBuilder* profile_builder) = 0; 69*635a8641SAndroid Build Coastguard Worker 70*635a8641SAndroid Build Coastguard Worker protected: 71*635a8641SAndroid Build Coastguard Worker NativeStackSampler(); 72*635a8641SAndroid Build Coastguard Worker 73*635a8641SAndroid Build Coastguard Worker private: 74*635a8641SAndroid Build Coastguard Worker DISALLOW_COPY_AND_ASSIGN(NativeStackSampler); 75*635a8641SAndroid Build Coastguard Worker }; 76*635a8641SAndroid Build Coastguard Worker 77*635a8641SAndroid Build Coastguard Worker // NativeStackSamplerTestDelegate provides seams for test code to execute during 78*635a8641SAndroid Build Coastguard Worker // stack collection. 79*635a8641SAndroid Build Coastguard Worker class BASE_EXPORT NativeStackSamplerTestDelegate { 80*635a8641SAndroid Build Coastguard Worker public: 81*635a8641SAndroid Build Coastguard Worker virtual ~NativeStackSamplerTestDelegate(); 82*635a8641SAndroid Build Coastguard Worker 83*635a8641SAndroid Build Coastguard Worker // Called after copying the stack and resuming the target thread, but prior to 84*635a8641SAndroid Build Coastguard Worker // walking the stack. Invoked on the SamplingThread. 85*635a8641SAndroid Build Coastguard Worker virtual void OnPreStackWalk() = 0; 86*635a8641SAndroid Build Coastguard Worker 87*635a8641SAndroid Build Coastguard Worker protected: 88*635a8641SAndroid Build Coastguard Worker NativeStackSamplerTestDelegate(); 89*635a8641SAndroid Build Coastguard Worker 90*635a8641SAndroid Build Coastguard Worker private: 91*635a8641SAndroid Build Coastguard Worker DISALLOW_COPY_AND_ASSIGN(NativeStackSamplerTestDelegate); 92*635a8641SAndroid Build Coastguard Worker }; 93*635a8641SAndroid Build Coastguard Worker 94*635a8641SAndroid Build Coastguard Worker } // namespace base 95*635a8641SAndroid Build Coastguard Worker 96*635a8641SAndroid Build Coastguard Worker #endif // BASE_PROFILER_NATIVE_STACK_SAMPLER_H_ 97*635a8641SAndroid Build Coastguard Worker 98