OLD | NEW |
(Empty) | |
| 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_PROFILER_STACK_SAMPLING_PROFILER_H_ |
| 6 #define BASE_PROFILER_STACK_SAMPLING_PROFILER_H_ |
| 7 |
| 8 #include <string> |
| 9 #include <vector> |
| 10 |
| 11 #include "base/base_export.h" |
| 12 #include "base/files/file_path.h" |
| 13 #include "base/memory/scoped_ptr.h" |
| 14 #include "base/strings/string16.h" |
| 15 #include "base/threading/platform_thread.h" |
| 16 #include "base/time/time.h" |
| 17 |
| 18 namespace base { |
| 19 |
| 20 // StackSamplingProfiler periodically stops a thread to sample its stack, for |
| 21 // the purpose of collecting information about which code paths are |
| 22 // executing. This information is used in aggregate by UMA to identify hot |
| 23 // and/or janky code paths. The current implementation samples periodically for |
| 24 // a short period after invoking Start. |
| 25 // |
| 26 // To use: create a StackSamplingProfiler, call Start to start the sampling, |
| 27 // then (optionally) call Stop to stop the sampling. |
| 28 class BASE_EXPORT StackSamplingProfiler { |
| 29 public: |
| 30 // Module represents the module (DLL or exe) corresponding to a stack frame. |
| 31 struct Module { |
| 32 // An opaque binary string that uniquely identifies a particular program |
| 33 // version with high probability. This is parsed from headers of the loaded |
| 34 // module. |
| 35 // For binaries generated by GNU tools: |
| 36 // Contents of the .note.gnu.build-id field. |
| 37 // On Windows: |
| 38 // GUID + AGE in the debug image headers of a module. |
| 39 std::string id; |
| 40 // The filename of the module. |
| 41 base::FilePath filename; |
| 42 }; |
| 43 |
| 44 // Frame represents an individual sampled stack frame with module information. |
| 45 struct Frame { |
| 46 // Instruction pointer subtracted by module base. |
| 47 uint64 ip_offset; |
| 48 // Index of the module in the array of modules. We don't represent module |
| 49 // state directly here to save space. |
| 50 int module_index; |
| 51 }; |
| 52 |
| 53 // Sample represents a set of stack frames. |
| 54 using Sample = std::vector<Frame>; |
| 55 |
| 56 // Profile represents a set of samples. |
| 57 struct Profile { |
| 58 Profile(); |
| 59 ~Profile(); |
| 60 |
| 61 std::vector<Module> modules; |
| 62 std::vector<Sample> samples; |
| 63 // Duration of this profile. |
| 64 base::TimeDelta profile_duration; |
| 65 // Time between samples. |
| 66 base::TimeDelta sampling_period; |
| 67 }; |
| 68 |
| 69 // NativeStackSampler abstracts the native implementation required to record a |
| 70 // stack sample for a given thread. |
| 71 class NativeStackSampler { |
| 72 public: |
| 73 virtual ~NativeStackSampler(); |
| 74 |
| 75 // Create a stack sampler that records samples for |thread_handle|. Returns |
| 76 // null if this platform does not support stack sampling. |
| 77 static scoped_ptr<NativeStackSampler> Create(PlatformThreadId thread_id); |
| 78 |
| 79 // Notify the sampler that we're starting to record a new profile. This |
| 80 // function is called on the SamplingThread. |
| 81 virtual void ProfileRecordingStarting(Profile* profile) = 0; |
| 82 |
| 83 // Record a stack sample. This function is called on the SamplingThread. |
| 84 virtual void RecordStackSample(Sample* sample) = 0; |
| 85 |
| 86 // Notify the sampler that we've stopped recording the current profile. This |
| 87 // function is called on the SamplingThread. |
| 88 virtual void ProfileRecordingStopped() = 0; |
| 89 |
| 90 protected: |
| 91 NativeStackSampler(); |
| 92 |
| 93 private: |
| 94 DISALLOW_COPY_AND_ASSIGN(NativeStackSampler); |
| 95 }; |
| 96 |
| 97 StackSamplingProfiler(PlatformThreadId thread_id); |
| 98 ~StackSamplingProfiler(); |
| 99 |
| 100 // Initializes the profiler and starts sampling. |
| 101 void Start(); |
| 102 // Stops the profiler and any ongoing sampling. |
| 103 void Stop(); |
| 104 |
| 105 // Gets the pending profiles into *|profiles|. This function is thread safe. |
| 106 static void GetPendingProfiles(std::vector<Profile>* profiles); |
| 107 |
| 108 private: |
| 109 class SamplingThread; |
| 110 struct SamplingThreadDeleter { |
| 111 void operator() (SamplingThread* thread) const; |
| 112 }; |
| 113 |
| 114 void CollectPendingProfiles(const std::vector<Profile>& profiles); |
| 115 |
| 116 PlatformThreadId thread_id_; |
| 117 |
| 118 scoped_ptr<SamplingThread, SamplingThreadDeleter> sampling_thread_; |
| 119 scoped_ptr<NativeStackSampler> native_sampler_; |
| 120 |
| 121 DISALLOW_COPY_AND_ASSIGN(StackSamplingProfiler); |
| 122 }; |
| 123 |
| 124 // Defined to allow equality check of Samples. |
| 125 BASE_EXPORT bool operator==(const StackSamplingProfiler::Frame &a, |
| 126 const StackSamplingProfiler::Frame &b); |
| 127 |
| 128 } // namespace base |
| 129 |
| 130 #endif // BASE_PROFILER_STACK_SAMPLING_PROFILER_H_ |
OLD | NEW |