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