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 enum ProcessPhase : int { | |
| 69 // TODO: Expand this. | |
| 70 FirstNonEmptyPaint, | |
| 71 }; | |
| 72 | |
| 73 enum ProcessActivity : int { | |
| 74 // TODO: Expand this, too. | |
| 75 }; | |
| 76 | |
| 67 // Module represents the module (DLL or exe) corresponding to a stack frame. | 77 // Module represents the module (DLL or exe) corresponding to a stack frame. |
| 68 struct BASE_EXPORT Module { | 78 struct BASE_EXPORT Module { |
| 69 Module(); | 79 Module(); |
| 70 Module(uintptr_t base_address, | 80 Module(uintptr_t base_address, |
| 71 const std::string& id, | 81 const std::string& id, |
| 72 const FilePath& filename); | 82 const FilePath& filename); |
| 73 ~Module(); | 83 ~Module(); |
| 74 | 84 |
| 75 // Points to the base address of the module. | 85 // Points to the base address of the module. |
| 76 uintptr_t base_address; | 86 uintptr_t base_address; |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 100 Frame(); | 110 Frame(); |
| 101 | 111 |
| 102 // The sampled instruction pointer within the function. | 112 // The sampled instruction pointer within the function. |
| 103 uintptr_t instruction_pointer; | 113 uintptr_t instruction_pointer; |
| 104 | 114 |
| 105 // Index of the module in CallStackProfile::modules. We don't represent | 115 // Index of the module in CallStackProfile::modules. We don't represent |
| 106 // module state directly here to save space. | 116 // module state directly here to save space. |
| 107 size_t module_index; | 117 size_t module_index; |
| 108 }; | 118 }; |
| 109 | 119 |
| 110 // Sample represents a set of stack frames. | 120 // Sample represents a set of stack frames with some extra information. |
| 111 using Sample = std::vector<Frame>; | 121 struct BASE_EXPORT Sample { |
| 122 Sample(); | |
| 123 Sample(const Sample& sample); | |
| 124 | |
| 125 // These constructors are used during testing. | |
| 126 Sample(const Frame& frame); | |
| 127 Sample(const std::vector<Frame>& frames); | |
| 128 | |
| 129 // The entire stack frame when the sample is taken. | |
| 130 std::vector<Frame> frames; | |
| 131 | |
| 132 // A bit-field indicating which process phases have passed. This can be | |
| 133 // used to tell where in the process lifetime the samples are taken. See | |
| 134 // ProcessPhase, above. | |
| 135 int32_t process_phases = 0; | |
| 136 | |
| 137 // A bit-field indicating activities which were active when the frame was | |
| 138 // captured. See ProcessActivity, above. | |
| 139 int32_t current_activities = 0; | |
| 140 }; | |
| 112 | 141 |
| 113 // CallStackProfile represents a set of samples. | 142 // CallStackProfile represents a set of samples. |
| 114 struct BASE_EXPORT CallStackProfile { | 143 struct BASE_EXPORT CallStackProfile { |
| 115 CallStackProfile(); | 144 CallStackProfile(); |
| 116 CallStackProfile(const CallStackProfile& other); | 145 CallStackProfile(const CallStackProfile& other); |
| 117 ~CallStackProfile(); | 146 ~CallStackProfile(); |
| 118 | 147 |
| 119 std::vector<Module> modules; | 148 std::vector<Module> modules; |
| 120 std::vector<Sample> samples; | 149 std::vector<Sample> samples; |
| 121 | 150 |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 182 | 211 |
| 183 // Initializes the profiler and starts sampling. | 212 // Initializes the profiler and starts sampling. |
| 184 void Start(); | 213 void Start(); |
| 185 | 214 |
| 186 // Stops the profiler and any ongoing sampling. Calling this function is | 215 // Stops the profiler and any ongoing sampling. Calling this function is |
| 187 // optional; if not invoked profiling terminates when all the profiling bursts | 216 // optional; if not invoked profiling terminates when all the profiling bursts |
| 188 // specified in the SamplingParams are completed or the profiler is destroyed, | 217 // specified in the SamplingParams are completed or the profiler is destroyed, |
| 189 // whichever occurs first. | 218 // whichever occurs first. |
| 190 void Stop(); | 219 void Stop(); |
| 191 | 220 |
| 221 // Set the current system state that is recorded with each captured stack | |
| 222 // frame. | |
| 223 static void SetProcessPhase(ProcessPhase phase); | |
| 224 static void RecordActivityBegin(ProcessActivity activity); | |
| 225 static void RecordActivityEnd(ProcessActivity activity); | |
| 226 | |
| 192 private: | 227 private: |
| 193 // SamplingThread is a separate thread used to suspend and sample stacks from | 228 // SamplingThread is a separate thread used to suspend and sample stacks from |
| 194 // the target thread. | 229 // the target thread. |
| 195 class SamplingThread : public PlatformThread::Delegate { | 230 class SamplingThread : public PlatformThread::Delegate { |
| 196 public: | 231 public: |
| 197 // Samples stacks using |native_sampler|. When complete, invokes | 232 // Samples stacks using |native_sampler|. When complete, invokes |
| 198 // |completed_callback| with the collected call stack profiles. | 233 // |completed_callback| with the collected call stack profiles. |
| 199 // |completed_callback| must be callable on any thread. | 234 // |completed_callback| must be callable on any thread. |
| 200 SamplingThread(std::unique_ptr<NativeStackSampler> native_sampler, | 235 SamplingThread(std::unique_ptr<NativeStackSampler> native_sampler, |
| 201 const SamplingParams& params, | 236 const SamplingParams& params, |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 224 | 259 |
| 225 // If Stop() is called, it signals this event to force the sampling to | 260 // If Stop() is called, it signals this event to force the sampling to |
| 226 // terminate before all the samples specified in |params_| are collected. | 261 // terminate before all the samples specified in |params_| are collected. |
| 227 WaitableEvent stop_event_; | 262 WaitableEvent stop_event_; |
| 228 | 263 |
| 229 const CompletedCallback completed_callback_; | 264 const CompletedCallback completed_callback_; |
| 230 | 265 |
| 231 DISALLOW_COPY_AND_ASSIGN(SamplingThread); | 266 DISALLOW_COPY_AND_ASSIGN(SamplingThread); |
| 232 }; | 267 }; |
| 233 | 268 |
| 269 // These global variables hold current system state. These values are | |
| 270 // recorded with every captured frame. | |
| 271 static subtle::Atomic32 process_phases_; | |
| 272 static subtle::Atomic32 current_activities_; | |
|
Alexei Svitkine (slow)
2016/10/25 14:49:08
Do we need to use atomics here?
Can't we just alw
bcwhite
2016/10/25 15:02:22
I don't know if we can guarantee that or not but i
Alexei Svitkine (slow)
2016/10/25 15:25:58
In terms of guarantee, I was thinking the code can
bcwhite
2016/10/25 16:22:26
Done.
Mike Wittman
2016/10/25 17:24:22
It's also worth mentioning that these have to be a
bcwhite
2016/10/25 21:10:42
Done.
| |
| 273 | |
| 234 // The thread whose stack will be sampled. | 274 // The thread whose stack will be sampled. |
| 235 PlatformThreadId thread_id_; | 275 PlatformThreadId thread_id_; |
| 236 | 276 |
| 237 const SamplingParams params_; | 277 const SamplingParams params_; |
| 238 | 278 |
| 239 std::unique_ptr<SamplingThread> sampling_thread_; | 279 std::unique_ptr<SamplingThread> sampling_thread_; |
| 240 PlatformThreadHandle sampling_thread_handle_; | 280 PlatformThreadHandle sampling_thread_handle_; |
| 241 | 281 |
| 242 const CompletedCallback completed_callback_; | 282 const CompletedCallback completed_callback_; |
| 243 | 283 |
| 244 // Stored until it can be passed to the NativeStackSampler created in Start(). | 284 // Stored until it can be passed to the NativeStackSampler created in Start(). |
| 245 NativeStackSamplerTestDelegate* const test_delegate_; | 285 NativeStackSamplerTestDelegate* const test_delegate_; |
| 246 | 286 |
| 247 DISALLOW_COPY_AND_ASSIGN(StackSamplingProfiler); | 287 DISALLOW_COPY_AND_ASSIGN(StackSamplingProfiler); |
| 248 }; | 288 }; |
| 249 | 289 |
| 250 // These operators permit types to be compared and used in a map of Samples, as | 290 // These operators permit types to be compared and used in a map of Samples, as |
| 251 // done in tests and by the metrics provider code. | 291 // done in tests and by the metrics provider code. |
| 252 BASE_EXPORT bool operator==(const StackSamplingProfiler::Module& a, | 292 BASE_EXPORT bool operator==(const StackSamplingProfiler::Module& a, |
| 253 const StackSamplingProfiler::Module& b); | 293 const StackSamplingProfiler::Module& b); |
| 294 BASE_EXPORT bool operator==(const StackSamplingProfiler::Sample& a, | |
| 295 const StackSamplingProfiler::Sample& b); | |
| 296 BASE_EXPORT bool operator!=(const StackSamplingProfiler::Sample& a, | |
| 297 const StackSamplingProfiler::Sample& b); | |
| 298 BASE_EXPORT bool operator<(const StackSamplingProfiler::Sample& a, | |
| 299 const StackSamplingProfiler::Sample& b); | |
| 254 BASE_EXPORT bool operator==(const StackSamplingProfiler::Frame& a, | 300 BASE_EXPORT bool operator==(const StackSamplingProfiler::Frame& a, |
| 255 const StackSamplingProfiler::Frame& b); | 301 const StackSamplingProfiler::Frame& b); |
| 256 BASE_EXPORT bool operator<(const StackSamplingProfiler::Frame& a, | 302 BASE_EXPORT bool operator<(const StackSamplingProfiler::Frame& a, |
| 257 const StackSamplingProfiler::Frame& b); | 303 const StackSamplingProfiler::Frame& b); |
| 258 | 304 |
| 259 } // namespace base | 305 } // namespace base |
| 260 | 306 |
| 261 #endif // BASE_PROFILER_STACK_SAMPLING_PROFILER_H_ | 307 #endif // BASE_PROFILER_STACK_SAMPLING_PROFILER_H_ |
| OLD | NEW |