| 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. Just |
| 126 // as a "lifetime" can only move forward, these bits mark the phases of |
| 127 // the processes life as they occur. Bits can be set but never reset. The |
| 128 // actual definition of the individual bits is left to the user of this |
| 129 // module. |
| 130 uint32_t process_phases = 0; |
| 131 }; |
| 112 | 132 |
| 113 // CallStackProfile represents a set of samples. | 133 // CallStackProfile represents a set of samples. |
| 114 struct BASE_EXPORT CallStackProfile { | 134 struct BASE_EXPORT CallStackProfile { |
| 115 CallStackProfile(); | 135 CallStackProfile(); |
| 116 CallStackProfile(CallStackProfile&& other); | 136 CallStackProfile(CallStackProfile&& other); |
| 117 ~CallStackProfile(); | 137 ~CallStackProfile(); |
| 118 | 138 |
| 119 CallStackProfile& operator=(CallStackProfile&& other); | 139 CallStackProfile& operator=(CallStackProfile&& other); |
| 120 | 140 |
| 121 CallStackProfile CopyForTesting() const; | 141 CallStackProfile CopyForTesting() const; |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 194 | 214 |
| 195 // Initializes the profiler and starts sampling. | 215 // Initializes the profiler and starts sampling. |
| 196 void Start(); | 216 void Start(); |
| 197 | 217 |
| 198 // Stops the profiler and any ongoing sampling. Calling this function is | 218 // Stops the profiler and any ongoing sampling. Calling this function is |
| 199 // optional; if not invoked profiling terminates when all the profiling bursts | 219 // optional; if not invoked profiling terminates when all the profiling bursts |
| 200 // specified in the SamplingParams are completed or the profiler is destroyed, | 220 // specified in the SamplingParams are completed or the profiler is destroyed, |
| 201 // whichever occurs first. | 221 // whichever occurs first. |
| 202 void Stop(); | 222 void Stop(); |
| 203 | 223 |
| 224 // Set the current system state that is recorded with each captured stack |
| 225 // frame. This is thread-safe so can be called from anywhere. The parameter |
| 226 // value should be from an enumeration of the appropriate type with values |
| 227 // ranging from 0 to 31, inclusive. This sets bits within Sample field of |
| 228 // |process_phases|. The actual meanings of these bits are defined (globally) |
| 229 // by the caller(s). |
| 230 static void SetProcessPhase(int phase); |
| 231 static void ResetAnnotationsForTesting(); |
| 232 |
| 204 private: | 233 private: |
| 205 // SamplingThread is a separate thread used to suspend and sample stacks from | 234 // SamplingThread is a separate thread used to suspend and sample stacks from |
| 206 // the target thread. | 235 // the target thread. |
| 207 class SamplingThread : public PlatformThread::Delegate { | 236 class SamplingThread : public PlatformThread::Delegate { |
| 208 public: | 237 public: |
| 209 // Samples stacks using |native_sampler|. When complete, invokes | 238 // Samples stacks using |native_sampler|. When complete, invokes |
| 210 // |completed_callback| with the collected call stack profiles. | 239 // |completed_callback| with the collected call stack profiles. |
| 211 // |completed_callback| must be callable on any thread. | 240 // |completed_callback| must be callable on any thread. |
| 212 SamplingThread(std::unique_ptr<NativeStackSampler> native_sampler, | 241 SamplingThread(std::unique_ptr<NativeStackSampler> native_sampler, |
| 213 const SamplingParams& params, | 242 const SamplingParams& params, |
| (...skipping 22 matching lines...) Expand all Loading... |
| 236 | 265 |
| 237 // If Stop() is called, it signals this event to force the sampling to | 266 // If Stop() is called, it signals this event to force the sampling to |
| 238 // terminate before all the samples specified in |params_| are collected. | 267 // terminate before all the samples specified in |params_| are collected. |
| 239 WaitableEvent stop_event_; | 268 WaitableEvent stop_event_; |
| 240 | 269 |
| 241 const CompletedCallback completed_callback_; | 270 const CompletedCallback completed_callback_; |
| 242 | 271 |
| 243 DISALLOW_COPY_AND_ASSIGN(SamplingThread); | 272 DISALLOW_COPY_AND_ASSIGN(SamplingThread); |
| 244 }; | 273 }; |
| 245 | 274 |
| 275 // Adds annotations to a Sample. |
| 276 static void RecordAnnotations(Sample* sample); |
| 277 |
| 278 // This global variables holds the current system state and is recorded with |
| 279 // every captured sample, done on a separate thread which is why updates to |
| 280 // this must be atomic. A PostTask to move the the updates to that thread |
| 281 // would skew the timing and a lock could result in deadlock if the thread |
| 282 // making a change was also being profiled and got stopped. |
| 283 static subtle::Atomic32 process_phases_; |
| 284 |
| 246 // The thread whose stack will be sampled. | 285 // The thread whose stack will be sampled. |
| 247 PlatformThreadId thread_id_; | 286 PlatformThreadId thread_id_; |
| 248 | 287 |
| 249 const SamplingParams params_; | 288 const SamplingParams params_; |
| 250 | 289 |
| 251 std::unique_ptr<SamplingThread> sampling_thread_; | 290 std::unique_ptr<SamplingThread> sampling_thread_; |
| 252 PlatformThreadHandle sampling_thread_handle_; | 291 PlatformThreadHandle sampling_thread_handle_; |
| 253 | 292 |
| 254 const CompletedCallback completed_callback_; | 293 const CompletedCallback completed_callback_; |
| 255 | 294 |
| 256 // Stored until it can be passed to the NativeStackSampler created in Start(). | 295 // Stored until it can be passed to the NativeStackSampler created in Start(). |
| 257 NativeStackSamplerTestDelegate* const test_delegate_; | 296 NativeStackSamplerTestDelegate* const test_delegate_; |
| 258 | 297 |
| 259 DISALLOW_COPY_AND_ASSIGN(StackSamplingProfiler); | 298 DISALLOW_COPY_AND_ASSIGN(StackSamplingProfiler); |
| 260 }; | 299 }; |
| 261 | 300 |
| 262 // These operators permit types to be compared and used in a map of Samples, as | 301 // 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. | 302 // done in tests and by the metrics provider code. |
| 264 BASE_EXPORT bool operator==(const StackSamplingProfiler::Module& a, | 303 BASE_EXPORT bool operator==(const StackSamplingProfiler::Module& a, |
| 265 const StackSamplingProfiler::Module& b); | 304 const StackSamplingProfiler::Module& b); |
| 305 BASE_EXPORT bool operator==(const StackSamplingProfiler::Sample& a, |
| 306 const StackSamplingProfiler::Sample& b); |
| 307 BASE_EXPORT bool operator!=(const StackSamplingProfiler::Sample& a, |
| 308 const StackSamplingProfiler::Sample& b); |
| 309 BASE_EXPORT bool operator<(const StackSamplingProfiler::Sample& a, |
| 310 const StackSamplingProfiler::Sample& b); |
| 266 BASE_EXPORT bool operator==(const StackSamplingProfiler::Frame& a, | 311 BASE_EXPORT bool operator==(const StackSamplingProfiler::Frame& a, |
| 267 const StackSamplingProfiler::Frame& b); | 312 const StackSamplingProfiler::Frame& b); |
| 268 BASE_EXPORT bool operator<(const StackSamplingProfiler::Frame& a, | 313 BASE_EXPORT bool operator<(const StackSamplingProfiler::Frame& a, |
| 269 const StackSamplingProfiler::Frame& b); | 314 const StackSamplingProfiler::Frame& b); |
| 270 | 315 |
| 271 } // namespace base | 316 } // namespace base |
| 272 | 317 |
| 273 #endif // BASE_PROFILER_STACK_SAMPLING_PROFILER_H_ | 318 #endif // BASE_PROFILER_STACK_SAMPLING_PROFILER_H_ |
| OLD | NEW |