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 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 57 // completed callback is called from a thread created by the profiler with the | 58 // completed callback is called from a thread created by the profiler with the |
| 58 // collected profiles. | 59 // collected profiles. |
| 59 // | 60 // |
| 60 // The results of the profiling are passed to the completed callback and consist | 61 // 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 // of a vector of CallStackProfiles. Each CallStackProfile corresponds to a |
| 62 // burst as specified in SamplingParams and contains a set of Samples and | 63 // 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 // Modules. One Sample corresponds to a single recorded stack, and the Modules |
| 64 // record those modules associated with the recorded stack frames. | 65 // record those modules associated with the recorded stack frames. |
| 65 class BASE_EXPORT StackSamplingProfiler { | 66 class BASE_EXPORT StackSamplingProfiler { |
| 66 public: | 67 public: |
| 68 // These types are defined simply for clarity in places that define them | |
| 69 // (using this as the base type for an enum) and in methods that take them | |
| 70 // as parameters. | |
| 71 using ProcessPhase = int; | |
| 72 using ProcessActivity = int; | |
|
Mike Wittman
2016/11/02 17:14:39
Can we avoid defining these typedefs and just use
bcwhite
2016/11/02 20:10:45
I had wanted to clearly define what is being passe
| |
| 73 | |
| 67 // Module represents the module (DLL or exe) corresponding to a stack frame. | 74 // Module represents the module (DLL or exe) corresponding to a stack frame. |
| 68 struct BASE_EXPORT Module { | 75 struct BASE_EXPORT Module { |
| 69 Module(); | 76 Module(); |
| 70 Module(uintptr_t base_address, | 77 Module(uintptr_t base_address, |
| 71 const std::string& id, | 78 const std::string& id, |
| 72 const FilePath& filename); | 79 const FilePath& filename); |
| 73 ~Module(); | 80 ~Module(); |
| 74 | 81 |
| 75 // Points to the base address of the module. | 82 // Points to the base address of the module. |
| 76 uintptr_t base_address; | 83 uintptr_t base_address; |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 100 Frame(); | 107 Frame(); |
| 101 | 108 |
| 102 // The sampled instruction pointer within the function. | 109 // The sampled instruction pointer within the function. |
| 103 uintptr_t instruction_pointer; | 110 uintptr_t instruction_pointer; |
| 104 | 111 |
| 105 // Index of the module in CallStackProfile::modules. We don't represent | 112 // Index of the module in CallStackProfile::modules. We don't represent |
| 106 // module state directly here to save space. | 113 // module state directly here to save space. |
| 107 size_t module_index; | 114 size_t module_index; |
| 108 }; | 115 }; |
| 109 | 116 |
| 110 // Sample represents a set of stack frames. | 117 // Sample represents a set of stack frames with some extra information. |
| 111 using Sample = std::vector<Frame>; | 118 struct BASE_EXPORT Sample { |
| 119 Sample(); | |
| 120 Sample(const Sample& sample); | |
| 121 ~Sample(); | |
| 122 | |
| 123 // These constructors are used only during testing. | |
| 124 Sample(const Frame& frame); | |
| 125 Sample(const std::vector<Frame>& frames); | |
| 126 | |
| 127 // The entire stack frame when the sample is taken. | |
| 128 std::vector<Frame> frames; | |
| 129 | |
| 130 // A bit-field indicating which process phases have passed. This can be | |
| 131 // used to tell where in the process lifetime the samples are taken. See | |
| 132 // ProcessPhase, above. | |
| 133 uint32_t process_phases = 0; | |
| 134 | |
| 135 // A bit-field indicating activities which were active when the frame was | |
| 136 // captured. See ProcessActivity, above. | |
| 137 uint32_t current_activities = 0; | |
| 138 }; | |
| 112 | 139 |
| 113 // CallStackProfile represents a set of samples. | 140 // CallStackProfile represents a set of samples. |
| 114 struct BASE_EXPORT CallStackProfile { | 141 struct BASE_EXPORT CallStackProfile { |
| 115 CallStackProfile(); | 142 CallStackProfile(); |
| 116 CallStackProfile(CallStackProfile&& other); | 143 CallStackProfile(CallStackProfile&& other); |
| 117 ~CallStackProfile(); | 144 ~CallStackProfile(); |
| 118 | 145 |
| 119 CallStackProfile& operator=(CallStackProfile&& other); | 146 CallStackProfile& operator=(CallStackProfile&& other); |
| 120 | 147 |
| 121 CallStackProfile CopyForTesting() const; | 148 CallStackProfile CopyForTesting() const; |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 194 | 221 |
| 195 // Initializes the profiler and starts sampling. | 222 // Initializes the profiler and starts sampling. |
| 196 void Start(); | 223 void Start(); |
| 197 | 224 |
| 198 // Stops the profiler and any ongoing sampling. Calling this function is | 225 // Stops the profiler and any ongoing sampling. Calling this function is |
| 199 // optional; if not invoked profiling terminates when all the profiling bursts | 226 // optional; if not invoked profiling terminates when all the profiling bursts |
| 200 // specified in the SamplingParams are completed or the profiler is destroyed, | 227 // specified in the SamplingParams are completed or the profiler is destroyed, |
| 201 // whichever occurs first. | 228 // whichever occurs first. |
| 202 void Stop(); | 229 void Stop(); |
| 203 | 230 |
| 231 // Set the current system state that is recorded with each captured stack | |
| 232 // frame. These are all thread-safe so can be called from anywhere. | |
|
Mike Wittman
2016/11/02 17:14:39
Document that the valid values are between 0 and 3
bcwhite
2016/11/02 20:10:45
Done.
| |
| 233 static void SetProcessPhase(ProcessPhase phase); | |
| 234 static void RecordActivityBegin(ProcessActivity activity); | |
| 235 static void RecordActivityEnd(ProcessActivity activity); | |
| 236 | |
| 204 private: | 237 private: |
| 205 // SamplingThread is a separate thread used to suspend and sample stacks from | 238 // SamplingThread is a separate thread used to suspend and sample stacks from |
| 206 // the target thread. | 239 // the target thread. |
| 207 class SamplingThread : public PlatformThread::Delegate { | 240 class SamplingThread : public PlatformThread::Delegate { |
| 208 public: | 241 public: |
| 209 // Samples stacks using |native_sampler|. When complete, invokes | 242 // Samples stacks using |native_sampler|. When complete, invokes |
| 210 // |completed_callback| with the collected call stack profiles. | 243 // |completed_callback| with the collected call stack profiles. |
| 211 // |completed_callback| must be callable on any thread. | 244 // |completed_callback| must be callable on any thread. |
| 212 SamplingThread(std::unique_ptr<NativeStackSampler> native_sampler, | 245 SamplingThread(std::unique_ptr<NativeStackSampler> native_sampler, |
| 213 const SamplingParams& params, | 246 const SamplingParams& params, |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 236 | 269 |
| 237 // If Stop() is called, it signals this event to force the sampling to | 270 // If Stop() is called, it signals this event to force the sampling to |
| 238 // terminate before all the samples specified in |params_| are collected. | 271 // terminate before all the samples specified in |params_| are collected. |
| 239 WaitableEvent stop_event_; | 272 WaitableEvent stop_event_; |
| 240 | 273 |
| 241 const CompletedCallback completed_callback_; | 274 const CompletedCallback completed_callback_; |
| 242 | 275 |
| 243 DISALLOW_COPY_AND_ASSIGN(SamplingThread); | 276 DISALLOW_COPY_AND_ASSIGN(SamplingThread); |
| 244 }; | 277 }; |
| 245 | 278 |
| 279 // Adds "phases" and "activities" annotations to a Sample. | |
| 280 static void RecordAnnotations(Sample* sample); | |
| 281 | |
| 282 // These global variables hold current system state. These values are | |
| 283 // recorded with every captured sample, done on a separate thread which is | |
| 284 // why updates to these must be atomic. A PostTask to move the the updates | |
| 285 // to that thread would skew the timing and a lock could result in deadlock | |
| 286 // if the thread making a change was also being profiled and got stopped. | |
| 287 static subtle::Atomic32 process_phases_; | |
| 288 static subtle::Atomic32 current_activities_; | |
| 289 | |
| 246 // The thread whose stack will be sampled. | 290 // The thread whose stack will be sampled. |
| 247 PlatformThreadId thread_id_; | 291 PlatformThreadId thread_id_; |
| 248 | 292 |
| 249 const SamplingParams params_; | 293 const SamplingParams params_; |
| 250 | 294 |
| 251 std::unique_ptr<SamplingThread> sampling_thread_; | 295 std::unique_ptr<SamplingThread> sampling_thread_; |
| 252 PlatformThreadHandle sampling_thread_handle_; | 296 PlatformThreadHandle sampling_thread_handle_; |
| 253 | 297 |
| 254 const CompletedCallback completed_callback_; | 298 const CompletedCallback completed_callback_; |
| 255 | 299 |
| 256 // Stored until it can be passed to the NativeStackSampler created in Start(). | 300 // Stored until it can be passed to the NativeStackSampler created in Start(). |
| 257 NativeStackSamplerTestDelegate* const test_delegate_; | 301 NativeStackSamplerTestDelegate* const test_delegate_; |
| 258 | 302 |
| 259 DISALLOW_COPY_AND_ASSIGN(StackSamplingProfiler); | 303 DISALLOW_COPY_AND_ASSIGN(StackSamplingProfiler); |
| 260 }; | 304 }; |
| 261 | 305 |
| 262 // These operators permit types to be compared and used in a map of Samples, as | 306 // 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. | 307 // done in tests and by the metrics provider code. |
| 264 BASE_EXPORT bool operator==(const StackSamplingProfiler::Module& a, | 308 BASE_EXPORT bool operator==(const StackSamplingProfiler::Module& a, |
| 265 const StackSamplingProfiler::Module& b); | 309 const StackSamplingProfiler::Module& b); |
| 310 BASE_EXPORT bool operator==(const StackSamplingProfiler::Sample& a, | |
| 311 const StackSamplingProfiler::Sample& b); | |
| 312 BASE_EXPORT bool operator!=(const StackSamplingProfiler::Sample& a, | |
| 313 const StackSamplingProfiler::Sample& b); | |
| 314 BASE_EXPORT bool operator<(const StackSamplingProfiler::Sample& a, | |
| 315 const StackSamplingProfiler::Sample& b); | |
| 266 BASE_EXPORT bool operator==(const StackSamplingProfiler::Frame& a, | 316 BASE_EXPORT bool operator==(const StackSamplingProfiler::Frame& a, |
| 267 const StackSamplingProfiler::Frame& b); | 317 const StackSamplingProfiler::Frame& b); |
| 268 BASE_EXPORT bool operator<(const StackSamplingProfiler::Frame& a, | 318 BASE_EXPORT bool operator<(const StackSamplingProfiler::Frame& a, |
| 269 const StackSamplingProfiler::Frame& b); | 319 const StackSamplingProfiler::Frame& b); |
| 270 | 320 |
| 271 } // namespace base | 321 } // namespace base |
| 272 | 322 |
| 273 #endif // BASE_PROFILER_STACK_SAMPLING_PROFILER_H_ | 323 #endif // BASE_PROFILER_STACK_SAMPLING_PROFILER_H_ |
| OLD | NEW |