Chromium Code Reviews| 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 <stddef.h> | 8 #include <stddef.h> |
| 9 | 9 |
| 10 #include <memory> | 10 #include <memory> |
| 11 #include <string> | 11 #include <string> |
| 12 #include <vector> | 12 #include <vector> |
| 13 | 13 |
| 14 #include "base/atomicops.h" | |
| 14 #include "base/base_export.h" | 15 #include "base/base_export.h" |
| 15 #include "base/callback.h" | 16 #include "base/callback.h" |
| 16 #include "base/files/file_path.h" | 17 #include "base/files/file_path.h" |
| 17 #include "base/macros.h" | 18 #include "base/macros.h" |
| 18 #include "base/strings/string16.h" | 19 #include "base/strings/string16.h" |
| 19 #include "base/synchronization/waitable_event.h" | 20 #include "base/synchronization/waitable_event.h" |
| 20 #include "base/threading/platform_thread.h" | 21 #include "base/threading/platform_thread.h" |
| 21 #include "base/time/time.h" | 22 #include "base/time/time.h" |
| 22 | 23 |
| 23 namespace base { | 24 namespace base { |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 100 Frame(); | 101 Frame(); |
| 101 | 102 |
| 102 // The sampled instruction pointer within the function. | 103 // The sampled instruction pointer within the function. |
| 103 uintptr_t instruction_pointer; | 104 uintptr_t instruction_pointer; |
| 104 | 105 |
| 105 // Index of the module in CallStackProfile::modules. We don't represent | 106 // Index of the module in CallStackProfile::modules. We don't represent |
| 106 // module state directly here to save space. | 107 // module state directly here to save space. |
| 107 size_t module_index; | 108 size_t module_index; |
| 108 }; | 109 }; |
| 109 | 110 |
| 110 // Sample represents a set of stack frames. | 111 // Sample represents a set of stack frames with some extra information. |
| 111 using Sample = std::vector<Frame>; | 112 struct BASE_EXPORT Sample { |
| 113 Sample(); | |
| 114 Sample(const Sample& sample); | |
| 115 ~Sample(); | |
| 116 | |
| 117 // These constructors are used only during testing. | |
| 118 Sample(const Frame& frame); | |
| 119 Sample(const std::vector<Frame>& frames); | |
| 120 | |
| 121 // The entire stack frame when the sample is taken. | |
| 122 std::vector<Frame> frames; | |
| 123 | |
| 124 // A bit-field indicating which process phases have passed. This can be | |
| 125 // used to tell where in the process lifetime the samples are taken. See | |
| 126 // ProcessPhase, above. | |
|
Alexei Svitkine (slow)
2016/11/07 23:22:07
This comment is not correct anymore since there's
bcwhite
2016/11/08 01:02:36
Done.
| |
| 127 uint32_t process_phases = 0; | |
| 128 | |
| 129 // A bit-field indicating activities which were active when the frame was | |
| 130 // captured. See ProcessActivity, above. | |
|
Alexei Svitkine (slow)
2016/11/07 23:22:07
I think there needs to be a more in depth comment
bcwhite
2016/11/08 01:02:36
Done.
| |
| 131 uint32_t current_activities = 0; | |
| 132 }; | |
| 112 | 133 |
| 113 // CallStackProfile represents a set of samples. | 134 // CallStackProfile represents a set of samples. |
| 114 struct BASE_EXPORT CallStackProfile { | 135 struct BASE_EXPORT CallStackProfile { |
| 115 CallStackProfile(); | 136 CallStackProfile(); |
| 116 CallStackProfile(CallStackProfile&& other); | 137 CallStackProfile(CallStackProfile&& other); |
| 117 ~CallStackProfile(); | 138 ~CallStackProfile(); |
| 118 | 139 |
| 119 CallStackProfile& operator=(CallStackProfile&& other); | 140 CallStackProfile& operator=(CallStackProfile&& other); |
| 120 | 141 |
| 121 CallStackProfile CopyForTesting() const; | 142 CallStackProfile CopyForTesting() const; |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 194 | 215 |
| 195 // Initializes the profiler and starts sampling. | 216 // Initializes the profiler and starts sampling. |
| 196 void Start(); | 217 void Start(); |
| 197 | 218 |
| 198 // Stops the profiler and any ongoing sampling. Calling this function is | 219 // Stops the profiler and any ongoing sampling. Calling this function is |
| 199 // optional; if not invoked profiling terminates when all the profiling bursts | 220 // optional; if not invoked profiling terminates when all the profiling bursts |
| 200 // specified in the SamplingParams are completed or the profiler is destroyed, | 221 // specified in the SamplingParams are completed or the profiler is destroyed, |
| 201 // whichever occurs first. | 222 // whichever occurs first. |
| 202 void Stop(); | 223 void Stop(); |
| 203 | 224 |
| 225 // Set the current system state that is recorded with each captured stack | |
| 226 // frame. These are all thread-safe so can be called from anywhere. The | |
| 227 // parameter values should be from an enumeration of the appropriate type | |
| 228 // with values ranging from 0 to 31, inclusive, and set bits within Sample | |
| 229 // fields of |process_phases| and |current_activities|. The actual meanings | |
| 230 // of these bits are defined (globally) by the caller(s). | |
| 231 static void SetProcessPhase(int phase); | |
| 232 static void RecordActivityBegin(int activity); | |
| 233 static void RecordActivityEnd(int activity); | |
| 234 static void ResetPhaseAndActivityForTesting(); | |
| 235 | |
| 204 private: | 236 private: |
| 205 // SamplingThread is a separate thread used to suspend and sample stacks from | 237 // SamplingThread is a separate thread used to suspend and sample stacks from |
| 206 // the target thread. | 238 // the target thread. |
| 207 class SamplingThread : public PlatformThread::Delegate { | 239 class SamplingThread : public PlatformThread::Delegate { |
| 208 public: | 240 public: |
| 209 // Samples stacks using |native_sampler|. When complete, invokes | 241 // Samples stacks using |native_sampler|. When complete, invokes |
| 210 // |completed_callback| with the collected call stack profiles. | 242 // |completed_callback| with the collected call stack profiles. |
| 211 // |completed_callback| must be callable on any thread. | 243 // |completed_callback| must be callable on any thread. |
| 212 SamplingThread(std::unique_ptr<NativeStackSampler> native_sampler, | 244 SamplingThread(std::unique_ptr<NativeStackSampler> native_sampler, |
| 213 const SamplingParams& params, | 245 const SamplingParams& params, |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 236 | 268 |
| 237 // If Stop() is called, it signals this event to force the sampling to | 269 // If Stop() is called, it signals this event to force the sampling to |
| 238 // terminate before all the samples specified in |params_| are collected. | 270 // terminate before all the samples specified in |params_| are collected. |
| 239 WaitableEvent stop_event_; | 271 WaitableEvent stop_event_; |
| 240 | 272 |
| 241 const CompletedCallback completed_callback_; | 273 const CompletedCallback completed_callback_; |
| 242 | 274 |
| 243 DISALLOW_COPY_AND_ASSIGN(SamplingThread); | 275 DISALLOW_COPY_AND_ASSIGN(SamplingThread); |
| 244 }; | 276 }; |
| 245 | 277 |
| 278 // Adds "phases" and "activities" annotations to a Sample. | |
| 279 static void RecordAnnotations(Sample* sample); | |
| 280 | |
| 281 // These global variables hold current system state. These values are | |
| 282 // recorded with every captured sample, done on a separate thread which is | |
| 283 // why updates to these must be atomic. A PostTask to move the the updates | |
| 284 // to that thread would skew the timing and a lock could result in deadlock | |
| 285 // if the thread making a change was also being profiled and got stopped. | |
| 286 static subtle::Atomic32 process_phases_; | |
| 287 static subtle::Atomic32 current_activities_; | |
| 288 | |
| 246 // The thread whose stack will be sampled. | 289 // The thread whose stack will be sampled. |
| 247 PlatformThreadId thread_id_; | 290 PlatformThreadId thread_id_; |
| 248 | 291 |
| 249 const SamplingParams params_; | 292 const SamplingParams params_; |
| 250 | 293 |
| 251 std::unique_ptr<SamplingThread> sampling_thread_; | 294 std::unique_ptr<SamplingThread> sampling_thread_; |
| 252 PlatformThreadHandle sampling_thread_handle_; | 295 PlatformThreadHandle sampling_thread_handle_; |
| 253 | 296 |
| 254 const CompletedCallback completed_callback_; | 297 const CompletedCallback completed_callback_; |
| 255 | 298 |
| 256 // Stored until it can be passed to the NativeStackSampler created in Start(). | 299 // Stored until it can be passed to the NativeStackSampler created in Start(). |
| 257 NativeStackSamplerTestDelegate* const test_delegate_; | 300 NativeStackSamplerTestDelegate* const test_delegate_; |
| 258 | 301 |
| 259 DISALLOW_COPY_AND_ASSIGN(StackSamplingProfiler); | 302 DISALLOW_COPY_AND_ASSIGN(StackSamplingProfiler); |
| 260 }; | 303 }; |
| 261 | 304 |
| 262 // These operators permit types to be compared and used in a map of Samples, as | 305 // These operators permit types to be compared and used in a map of Samples, as |
| 263 // done in tests and by the metrics provider code. | 306 // done in tests and by the metrics provider code. |
| 264 BASE_EXPORT bool operator==(const StackSamplingProfiler::Module& a, | 307 BASE_EXPORT bool operator==(const StackSamplingProfiler::Module& a, |
| 265 const StackSamplingProfiler::Module& b); | 308 const StackSamplingProfiler::Module& b); |
| 309 BASE_EXPORT bool operator==(const StackSamplingProfiler::Sample& a, | |
| 310 const StackSamplingProfiler::Sample& b); | |
| 311 BASE_EXPORT bool operator!=(const StackSamplingProfiler::Sample& a, | |
| 312 const StackSamplingProfiler::Sample& b); | |
| 313 BASE_EXPORT bool operator<(const StackSamplingProfiler::Sample& a, | |
| 314 const StackSamplingProfiler::Sample& b); | |
| 266 BASE_EXPORT bool operator==(const StackSamplingProfiler::Frame& a, | 315 BASE_EXPORT bool operator==(const StackSamplingProfiler::Frame& a, |
| 267 const StackSamplingProfiler::Frame& b); | 316 const StackSamplingProfiler::Frame& b); |
| 268 BASE_EXPORT bool operator<(const StackSamplingProfiler::Frame& a, | 317 BASE_EXPORT bool operator<(const StackSamplingProfiler::Frame& a, |
| 269 const StackSamplingProfiler::Frame& b); | 318 const StackSamplingProfiler::Frame& b); |
| 270 | 319 |
| 271 } // namespace base | 320 } // namespace base |
| 272 | 321 |
| 273 #endif // BASE_PROFILER_STACK_SAMPLING_PROFILER_H_ | 322 #endif // BASE_PROFILER_STACK_SAMPLING_PROFILER_H_ |
| OLD | NEW |