Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(131)

Side by Side Diff: base/profiler/stack_sampling_profiler.h

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

Powered by Google App Engine
This is Rietveld 408576698