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 { | |
|
Mike Wittman
2016/10/24 22:28:14
uint32_t (and below)
bcwhite
2016/10/25 14:45:23
I'd rather not since I'm using it as a "flag numbe
Mike Wittman
2016/10/25 17:24:22
Ah, right. Forgot that this was an index and not a
bcwhite
2016/10/25 21:10:42
I assumed it was necessary in order to use it as a
| |
| 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 { |
|
Mike Wittman
2016/10/24 22:28:14
We probably should a add constructor at this point
bcwhite
2016/10/25 14:45:23
Done.
| |
| 122 // The entire stack frame when the sample is taken. | |
| 123 std::vector<Frame> frames; | |
| 124 | |
| 125 // A bit-field indicating which process phases have passed. This can be | |
| 126 // used to tell where in the process lifetime the samples are taken. See | |
| 127 // ProcessPhase, above. | |
| 128 int32_t process_phases; | |
|
Mike Wittman
2016/10/24 22:28:14
uint32_t (and below)
bcwhite
2016/10/25 14:45:23
Atomic32 is always a signed (unfortunately) so mak
Mike Wittman
2016/10/25 17:24:22
The use of Atomic32 is an implementation detail of
bcwhite
2016/10/25 21:10:42
Good enough for me. Perhaps the compilers will be
| |
| 129 | |
| 130 // A bit-field indicating activities which were active when the frame was | |
| 131 // captured. See ProcessActivity, above. | |
| 132 int32_t current_activities; | |
| 133 }; | |
| 112 | 134 |
| 113 // CallStackProfile represents a set of samples. | 135 // CallStackProfile represents a set of samples. |
| 114 struct BASE_EXPORT CallStackProfile { | 136 struct BASE_EXPORT CallStackProfile { |
| 115 CallStackProfile(); | 137 CallStackProfile(); |
| 116 CallStackProfile(const CallStackProfile& other); | 138 CallStackProfile(const CallStackProfile& other); |
| 117 ~CallStackProfile(); | 139 ~CallStackProfile(); |
| 118 | 140 |
| 119 std::vector<Module> modules; | 141 std::vector<Module> modules; |
| 120 std::vector<Sample> samples; | 142 std::vector<Sample> samples; |
| 121 | 143 |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 182 | 204 |
| 183 // Initializes the profiler and starts sampling. | 205 // Initializes the profiler and starts sampling. |
| 184 void Start(); | 206 void Start(); |
| 185 | 207 |
| 186 // Stops the profiler and any ongoing sampling. Calling this function is | 208 // Stops the profiler and any ongoing sampling. Calling this function is |
| 187 // optional; if not invoked profiling terminates when all the profiling bursts | 209 // optional; if not invoked profiling terminates when all the profiling bursts |
| 188 // specified in the SamplingParams are completed or the profiler is destroyed, | 210 // specified in the SamplingParams are completed or the profiler is destroyed, |
| 189 // whichever occurs first. | 211 // whichever occurs first. |
| 190 void Stop(); | 212 void Stop(); |
| 191 | 213 |
| 214 // Set the current system state that is recorded with each captured stack | |
| 215 // frame. | |
| 216 static void SetProcessPhase(ProcessPhase phase); | |
| 217 static void RecordActivityBegin(ProcessActivity activity); | |
| 218 static void RecordActivityEnd(ProcessActivity activity); | |
| 219 | |
| 192 private: | 220 private: |
| 193 // SamplingThread is a separate thread used to suspend and sample stacks from | 221 // SamplingThread is a separate thread used to suspend and sample stacks from |
| 194 // the target thread. | 222 // the target thread. |
| 195 class SamplingThread : public PlatformThread::Delegate { | 223 class SamplingThread : public PlatformThread::Delegate { |
| 196 public: | 224 public: |
| 197 // Samples stacks using |native_sampler|. When complete, invokes | 225 // Samples stacks using |native_sampler|. When complete, invokes |
| 198 // |completed_callback| with the collected call stack profiles. | 226 // |completed_callback| with the collected call stack profiles. |
| 199 // |completed_callback| must be callable on any thread. | 227 // |completed_callback| must be callable on any thread. |
| 200 SamplingThread(std::unique_ptr<NativeStackSampler> native_sampler, | 228 SamplingThread(std::unique_ptr<NativeStackSampler> native_sampler, |
| 201 const SamplingParams& params, | 229 const SamplingParams& params, |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 224 | 252 |
| 225 // If Stop() is called, it signals this event to force the sampling to | 253 // If Stop() is called, it signals this event to force the sampling to |
| 226 // terminate before all the samples specified in |params_| are collected. | 254 // terminate before all the samples specified in |params_| are collected. |
| 227 WaitableEvent stop_event_; | 255 WaitableEvent stop_event_; |
| 228 | 256 |
| 229 const CompletedCallback completed_callback_; | 257 const CompletedCallback completed_callback_; |
| 230 | 258 |
| 231 DISALLOW_COPY_AND_ASSIGN(SamplingThread); | 259 DISALLOW_COPY_AND_ASSIGN(SamplingThread); |
| 232 }; | 260 }; |
| 233 | 261 |
| 262 // These global variables hold current system state. These values are | |
| 263 // recorded with every captured frame. | |
| 264 static volatile subtle::Atomic32 process_phases_; | |
|
Mike Wittman
2016/10/24 22:28:14
I believe declaring subtle::Atomic32 volatile is u
bcwhite
2016/10/25 14:45:23
True. It is done in at least one place (because I
| |
| 265 static volatile subtle::Atomic32 current_activities_; | |
| 266 | |
| 234 // The thread whose stack will be sampled. | 267 // The thread whose stack will be sampled. |
| 235 PlatformThreadId thread_id_; | 268 PlatformThreadId thread_id_; |
| 236 | 269 |
| 237 const SamplingParams params_; | 270 const SamplingParams params_; |
| 238 | 271 |
| 239 std::unique_ptr<SamplingThread> sampling_thread_; | 272 std::unique_ptr<SamplingThread> sampling_thread_; |
| 240 PlatformThreadHandle sampling_thread_handle_; | 273 PlatformThreadHandle sampling_thread_handle_; |
| 241 | 274 |
| 242 const CompletedCallback completed_callback_; | 275 const CompletedCallback completed_callback_; |
| 243 | 276 |
| 244 // Stored until it can be passed to the NativeStackSampler created in Start(). | 277 // Stored until it can be passed to the NativeStackSampler created in Start(). |
| 245 NativeStackSamplerTestDelegate* const test_delegate_; | 278 NativeStackSamplerTestDelegate* const test_delegate_; |
| 246 | 279 |
| 247 DISALLOW_COPY_AND_ASSIGN(StackSamplingProfiler); | 280 DISALLOW_COPY_AND_ASSIGN(StackSamplingProfiler); |
| 248 }; | 281 }; |
| 249 | 282 |
| 250 // These operators permit types to be compared and used in a map of Samples, as | 283 // 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. | 284 // done in tests and by the metrics provider code. |
| 252 BASE_EXPORT bool operator==(const StackSamplingProfiler::Module& a, | 285 BASE_EXPORT bool operator==(const StackSamplingProfiler::Module& a, |
| 253 const StackSamplingProfiler::Module& b); | 286 const StackSamplingProfiler::Module& b); |
| 287 BASE_EXPORT bool operator==(const StackSamplingProfiler::Sample& a, | |
| 288 const StackSamplingProfiler::Sample& b); | |
| 289 BASE_EXPORT bool operator!=(const StackSamplingProfiler::Sample& a, | |
| 290 const StackSamplingProfiler::Sample& b); | |
| 291 BASE_EXPORT bool operator<(const StackSamplingProfiler::Sample& a, | |
| 292 const StackSamplingProfiler::Sample& b); | |
| 254 BASE_EXPORT bool operator==(const StackSamplingProfiler::Frame& a, | 293 BASE_EXPORT bool operator==(const StackSamplingProfiler::Frame& a, |
| 255 const StackSamplingProfiler::Frame& b); | 294 const StackSamplingProfiler::Frame& b); |
| 256 BASE_EXPORT bool operator<(const StackSamplingProfiler::Frame& a, | 295 BASE_EXPORT bool operator<(const StackSamplingProfiler::Frame& a, |
| 257 const StackSamplingProfiler::Frame& b); | 296 const StackSamplingProfiler::Frame& b); |
| 258 | 297 |
| 259 } // namespace base | 298 } // namespace base |
| 260 | 299 |
| 261 #endif // BASE_PROFILER_STACK_SAMPLING_PROFILER_H_ | 300 #endif // BASE_PROFILER_STACK_SAMPLING_PROFILER_H_ |
| OLD | NEW |