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