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/callback.h" |
| 13 #include "base/files/file_path.h" |
| 14 #include "base/memory/scoped_ptr.h" |
| 15 #include "base/strings/string16.h" |
| 16 #include "base/threading/platform_thread.h" |
| 17 #include "base/time/time.h" |
| 18 |
| 19 namespace base { |
| 20 |
| 21 // StackSamplingProfiler periodically stops a thread to sample its stack, for |
| 22 // the purpose of collecting information about which code paths are |
| 23 // executing. This information is used in aggregate by UMA to identify hot |
| 24 // and/or janky code paths. |
| 25 // |
| 26 // Sample StackStackSamplingProfiler usage: |
| 27 // |
| 28 // // Create and customize params as desired. |
| 29 // base::StackStackSamplingProfiler::SamplingParams params; |
| 30 // // Any thread's ID may be passed as the target. |
| 31 // base::StackSamplingProfiler profiler(base::PlatformThread::CurrentId()), |
| 32 // params); |
| 33 // |
| 34 // // To process the profiles within Chrome rather than via UMA, set a custom |
| 35 // // completed callback: |
| 36 // base::Callback<void(const std::vector<Profile>&)> |
| 37 // thread_safe_callback = ...; |
| 38 // profiler.SetCustomCompletedCallback(thread_safe_callback); |
| 39 // |
| 40 // profiler.Start(); |
| 41 // // ... work being done on the target thread here ... |
| 42 // profiler.Stop(); // optional, stops collection before complete per params |
| 43 // |
| 44 // When all profiles are complete or the profiler is stopped, if the custom |
| 45 // completed callback was set it will be called from the profiler thread with |
| 46 // the completed profiles. If no callback was set, the profiles are stored |
| 47 // internally and retrieved for UMA through |
| 48 // GetPendingProfiles(). GetPendingProfiles() should never be called by other |
| 49 // code; to retrieve profiles for in-process processing, set a completed |
| 50 // callback. |
| 51 class BASE_EXPORT StackSamplingProfiler { |
| 52 public: |
| 53 // Module represents the module (DLL or exe) corresponding to a stack frame. |
| 54 struct Module { |
| 55 // Points to the base address of the module. |
| 56 const void* base_address; |
| 57 // An opaque binary string that uniquely identifies a particular program |
| 58 // version with high probability. This is parsed from headers of the loaded |
| 59 // module. |
| 60 // For binaries generated by GNU tools: |
| 61 // Contents of the .note.gnu.build-id field. |
| 62 // On Windows: |
| 63 // GUID + AGE in the debug image headers of a module. |
| 64 std::string id; |
| 65 // The filename of the module. |
| 66 FilePath filename; |
| 67 }; |
| 68 |
| 69 // Frame represents an individual sampled stack frame with module information. |
| 70 struct Frame { |
| 71 // The sampled instruction pointer within the function. |
| 72 const void* instruction_pointer; |
| 73 // Index of the module in the array of modules. We don't represent module |
| 74 // state directly here to save space. |
| 75 int module_index; |
| 76 }; |
| 77 |
| 78 // Sample represents a set of stack frames. |
| 79 using Sample = std::vector<Frame>; |
| 80 |
| 81 // Profile represents a set of samples. |
| 82 struct BASE_EXPORT Profile { |
| 83 Profile(); |
| 84 ~Profile(); |
| 85 |
| 86 std::vector<Module> modules; |
| 87 std::vector<Sample> samples; |
| 88 // Duration of this profile. |
| 89 TimeDelta profile_duration; |
| 90 // Time between samples. |
| 91 TimeDelta sampling_period; |
| 92 // True if sample ordering is important and should be preserved if and when |
| 93 // this profile is compressed and processed. |
| 94 bool preserve_sample_ordering; |
| 95 }; |
| 96 |
| 97 // NativeStackSampler abstracts the native implementation required to record a |
| 98 // stack sample for a given thread. |
| 99 class NativeStackSampler { |
| 100 public: |
| 101 virtual ~NativeStackSampler(); |
| 102 |
| 103 // Create a stack sampler that records samples for |thread_handle|. Returns |
| 104 // null if this platform does not support stack sampling. |
| 105 static scoped_ptr<NativeStackSampler> Create(PlatformThreadId thread_id); |
| 106 |
| 107 // Notify the sampler that we're starting to record a new profile. This |
| 108 // function is called on the SamplingThread. |
| 109 virtual void ProfileRecordingStarting(Profile* profile) = 0; |
| 110 |
| 111 // Record a stack sample. This function is called on the SamplingThread. |
| 112 virtual void RecordStackSample(Sample* sample) = 0; |
| 113 |
| 114 // Notify the sampler that we've stopped recording the current profile. This |
| 115 // function is called on the SamplingThread. |
| 116 virtual void ProfileRecordingStopped() = 0; |
| 117 |
| 118 protected: |
| 119 NativeStackSampler(); |
| 120 |
| 121 private: |
| 122 DISALLOW_COPY_AND_ASSIGN(NativeStackSampler); |
| 123 }; |
| 124 |
| 125 // Represents parameters that configure the sampling. |
| 126 struct BASE_EXPORT SamplingParams { |
| 127 SamplingParams(); |
| 128 |
| 129 // Time to delay before first samples are taken. Defaults to 0. |
| 130 TimeDelta initial_delay; |
| 131 // Number of sampling bursts to perform. Defaults to 1. |
| 132 int bursts; |
| 133 // Interval between sampling bursts. This is the desired duration from the |
| 134 // start of one burst to the start of the next burst. Defaults to 10s. |
| 135 TimeDelta burst_interval; |
| 136 // Number of samples to record per burst. Defaults to 300. |
| 137 int samples_per_burst; |
| 138 // Interval between samples during a sampling burst. This is the desired |
| 139 // duration from the start of one burst to the start of the next |
| 140 // burst. Defaults to 100ms. |
| 141 TimeDelta sampling_interval; |
| 142 // True if sample ordering is important and should be preserved if and when |
| 143 // this profile is compressed and processed. Defaults to false. |
| 144 bool preserve_sample_ordering; |
| 145 }; |
| 146 |
| 147 StackSamplingProfiler(PlatformThreadId thread_id, |
| 148 const SamplingParams& params); |
| 149 ~StackSamplingProfiler(); |
| 150 |
| 151 // Initializes the profiler and starts sampling. |
| 152 void Start(); |
| 153 // Stops the profiler and any ongoing sampling. Calling this function is |
| 154 // optional; if not invoked profiling will terminate when all the profiling |
| 155 // bursts specified in the SamplingParams are completed. |
| 156 void Stop(); |
| 157 |
| 158 // Gets the pending profiles into *|profiles| and clears the internal |
| 159 // storage. This function is thread safe. |
| 160 // |
| 161 // ***This is intended for use only by UMA.*** Callers who want to process the |
| 162 // collected profiles should use SetCustomCompletedCallback. |
| 163 static void GetPendingProfiles(std::vector<Profile>* profiles); |
| 164 |
| 165 // By default, collected profiles are stored internally and can be retrieved |
| 166 // by GetPendingProfiles. If a callback is provided via this function, |
| 167 // however, it will be called with the collected profiles instead. Note that |
| 168 // this call to the callback occurs *on the profiler thread*. |
| 169 void SetCustomCompletedCallback( |
| 170 Callback<void(const std::vector<Profile>&)> callback); |
| 171 |
| 172 private: |
| 173 class SamplingThread; |
| 174 struct SamplingThreadDeleter { |
| 175 void operator() (SamplingThread* thread) const; |
| 176 }; |
| 177 |
| 178 // The thread whose stack will be sampled. |
| 179 PlatformThreadId thread_id_; |
| 180 |
| 181 const SamplingParams params_; |
| 182 |
| 183 scoped_ptr<SamplingThread, SamplingThreadDeleter> sampling_thread_; |
| 184 scoped_ptr<NativeStackSampler> native_sampler_; |
| 185 |
| 186 Callback<void(const std::vector<Profile>&)> custom_completed_callback_; |
| 187 |
| 188 DISALLOW_COPY_AND_ASSIGN(StackSamplingProfiler); |
| 189 }; |
| 190 |
| 191 // Defined to allow equality check of Samples. |
| 192 BASE_EXPORT bool operator==(const StackSamplingProfiler::Frame &a, |
| 193 const StackSamplingProfiler::Frame &b); |
| 194 // Defined to allow ordering of Samples. |
| 195 BASE_EXPORT bool operator<(const StackSamplingProfiler::Frame &a, |
| 196 const StackSamplingProfiler::Frame &b); |
| 197 |
| 198 } // namespace base |
| 199 |
| 200 #endif // BASE_PROFILER_STACK_SAMPLING_PROFILER_H_ |
OLD | NEW |