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 |
(...skipping 13 matching lines...) Expand all Loading... |
24 // and/or janky code paths. | 24 // and/or janky code paths. |
25 // | 25 // |
26 // Sample StackStackSamplingProfiler usage: | 26 // Sample StackStackSamplingProfiler 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 // // Or, to process the profiles within Chrome rather than via UMA, use a |
35 // // completed callback: | 35 // // custom completed callback: |
36 // base::Callback<void(const std::vector<Profile>&)> | 36 // base::Callback<void(const std::vector<Profile>&)> |
37 // thread_safe_callback = ...; | 37 // thread_safe_callback = ...; |
38 // profiler.SetCustomCompletedCallback(thread_safe_callback); | 38 // base::StackSamplingProfiler profiler(base::PlatformThread::CurrentId()), |
| 39 // params, 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 // |
44 // When all profiles are complete or the profiler is stopped, if the custom | 45 // 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 // 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 // the completed profiles. Otherwise, the default completed callback will be |
47 // internally and retrieved for UMA through | 48 // called with the profiles. It is expected that the the default completed |
48 // GetPendingProfiles(). GetPendingProfiles() should never be called by other | 49 // callback is set by the metrics system to allow profiles to be provided via |
49 // code; to retrieve profiles for in-process processing, set a completed | 50 // UMA. |
50 // callback. | |
51 class BASE_EXPORT StackSamplingProfiler { | 51 class BASE_EXPORT StackSamplingProfiler { |
52 public: | 52 public: |
53 // Module represents the module (DLL or exe) corresponding to a stack frame. | 53 // Module represents the module (DLL or exe) corresponding to a stack frame. |
54 struct BASE_EXPORT Module { | 54 struct BASE_EXPORT Module { |
55 Module(); | 55 Module(); |
56 Module(const void* base_address, const std::string& id, | 56 Module(const void* base_address, const std::string& id, |
57 const FilePath& filename); | 57 const FilePath& filename); |
58 ~Module(); | 58 ~Module(); |
59 | 59 |
60 // Points to the base address of the module. | 60 // Points to the base address of the module. |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
146 int samples_per_burst; | 146 int samples_per_burst; |
147 // Interval between samples during a sampling burst. This is the desired | 147 // Interval between samples during a sampling burst. This is the desired |
148 // duration from the start of one burst to the start of the next | 148 // duration from the start of one burst to the start of the next |
149 // burst. Defaults to 100ms. | 149 // burst. Defaults to 100ms. |
150 TimeDelta sampling_interval; | 150 TimeDelta sampling_interval; |
151 // True if sample ordering is important and should be preserved if and when | 151 // True if sample ordering is important and should be preserved if and when |
152 // this profile is compressed and processed. Defaults to false. | 152 // this profile is compressed and processed. Defaults to false. |
153 bool preserve_sample_ordering; | 153 bool preserve_sample_ordering; |
154 }; | 154 }; |
155 | 155 |
| 156 // Callback function that accepts completed profiles. |
| 157 using CompletedCallback = Callback<void(const std::vector<Profile>&)>; |
| 158 |
| 159 // Creates a profiler that sends completed profiles to the default completed |
| 160 // callback. |
156 StackSamplingProfiler(PlatformThreadId thread_id, | 161 StackSamplingProfiler(PlatformThreadId thread_id, |
157 const SamplingParams& params); | 162 const SamplingParams& params); |
| 163 // Creates a profiler that sends completed profiles to |completed_callback|. |
| 164 StackSamplingProfiler(PlatformThreadId thread_id, |
| 165 const SamplingParams& params, |
| 166 CompletedCallback callback); |
158 ~StackSamplingProfiler(); | 167 ~StackSamplingProfiler(); |
159 | 168 |
160 // Initializes the profiler and starts sampling. | 169 // Initializes the profiler and starts sampling. |
161 void Start(); | 170 void Start(); |
162 // Stops the profiler and any ongoing sampling. Calling this function is | 171 // Stops the profiler and any ongoing sampling. Calling this function is |
163 // optional; if not invoked profiling will terminate when all the profiling | 172 // optional; if not invoked profiling will terminate when all the profiling |
164 // bursts specified in the SamplingParams are completed. | 173 // bursts specified in the SamplingParams are completed. |
165 void Stop(); | 174 void Stop(); |
166 | 175 |
167 // Gets the pending profiles into *|profiles| and clears the internal | 176 // Sets a callback to process profiles collected by profiler instances without |
168 // storage. This function is thread safe. | 177 // a completed callback. Profiles are queued internally until a non-null |
| 178 // callback is provided to this function, |
169 // | 179 // |
170 // ***This is intended for use only by UMA.*** Callers who want to process the | 180 // The callback is typically called on a separate thread, so must be thread |
171 // collected profiles should use SetCustomCompletedCallback. | 181 // safe. If completed profiles are queued when set, it will also be called |
172 static void GetPendingProfiles(std::vector<Profile>* profiles); | 182 // immediately on the calling thread. |
173 | 183 static void SetDefaultCompletedCallback(CompletedCallback callback); |
174 // By default, collected profiles are stored internally and can be retrieved | |
175 // by GetPendingProfiles. If a callback is provided via this function, | |
176 // however, it will be called with the collected profiles instead. Note that | |
177 // this call to the callback occurs *on the profiler thread*. | |
178 void SetCustomCompletedCallback( | |
179 Callback<void(const std::vector<Profile>&)> callback); | |
180 | 184 |
181 private: | 185 private: |
182 class SamplingThread; | 186 class SamplingThread; |
183 struct SamplingThreadDeleter { | 187 struct SamplingThreadDeleter { |
184 void operator() (SamplingThread* thread) const; | 188 void operator() (SamplingThread* thread) const; |
185 }; | 189 }; |
186 | 190 |
187 // The thread whose stack will be sampled. | 191 // The thread whose stack will be sampled. |
188 PlatformThreadId thread_id_; | 192 PlatformThreadId thread_id_; |
189 | 193 |
190 const SamplingParams params_; | 194 const SamplingParams params_; |
191 | 195 |
192 scoped_ptr<SamplingThread, SamplingThreadDeleter> sampling_thread_; | 196 scoped_ptr<SamplingThread, SamplingThreadDeleter> sampling_thread_; |
193 scoped_ptr<NativeStackSampler> native_sampler_; | |
194 | 197 |
195 Callback<void(const std::vector<Profile>&)> custom_completed_callback_; | 198 const CompletedCallback completed_callback_; |
196 | 199 |
197 DISALLOW_COPY_AND_ASSIGN(StackSamplingProfiler); | 200 DISALLOW_COPY_AND_ASSIGN(StackSamplingProfiler); |
198 }; | 201 }; |
199 | 202 |
200 // Defined to allow equality check of Samples. | 203 // Defined to allow equality check of Samples. |
201 BASE_EXPORT bool operator==(const StackSamplingProfiler::Frame& a, | 204 BASE_EXPORT bool operator==(const StackSamplingProfiler::Frame& a, |
202 const StackSamplingProfiler::Frame& b); | 205 const StackSamplingProfiler::Frame& b); |
203 // Defined to allow ordering of Samples. | 206 // Defined to allow ordering of Samples. |
204 BASE_EXPORT bool operator<(const StackSamplingProfiler::Frame& a, | 207 BASE_EXPORT bool operator<(const StackSamplingProfiler::Frame& a, |
205 const StackSamplingProfiler::Frame& b); | 208 const StackSamplingProfiler::Frame& b); |
206 | 209 |
207 } // namespace base | 210 } // namespace base |
208 | 211 |
209 #endif // BASE_PROFILER_STACK_SAMPLING_PROFILER_H_ | 212 #endif // BASE_PROFILER_STACK_SAMPLING_PROFILER_H_ |
OLD | NEW |