Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(61)

Side by Side Diff: base/profiler/stack_sampling_profiler.h

Issue 2444143002: Add process lifetime annotations to stack samples. (Closed)
Patch Set: added comments and fixed some build problems Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « base/profiler/native_stack_sampler_win.cc ('k') | base/profiler/stack_sampling_profiler.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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,
Mike Wittman 2016/10/25 17:47:45 One other thing: being in //base, the profiler is
bcwhite 2016/10/25 21:10:42 I hate to lose the type-enforcement, but I guess t
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
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 ~Sample();
125
126 // These constructors are used only during testing.
127 Sample(const Frame& frame);
128 Sample(const std::vector<Frame>& frames);
129
130 // The entire stack frame when the sample is taken.
131 std::vector<Frame> frames;
132
133 // A bit-field indicating which process phases have passed. This can be
134 // used to tell where in the process lifetime the samples are taken. See
135 // ProcessPhase, above.
136 int32_t process_phases = 0;
137
138 // A bit-field indicating activities which were active when the frame was
139 // captured. See ProcessActivity, above.
140 int32_t current_activities = 0;
141 };
112 142
113 // CallStackProfile represents a set of samples. 143 // CallStackProfile represents a set of samples.
114 struct BASE_EXPORT CallStackProfile { 144 struct BASE_EXPORT CallStackProfile {
115 CallStackProfile(); 145 CallStackProfile();
116 CallStackProfile(const CallStackProfile& other); 146 CallStackProfile(const CallStackProfile& other);
117 ~CallStackProfile(); 147 ~CallStackProfile();
118 148
119 std::vector<Module> modules; 149 std::vector<Module> modules;
120 std::vector<Sample> samples; 150 std::vector<Sample> samples;
121 151
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
182 212
183 // Initializes the profiler and starts sampling. 213 // Initializes the profiler and starts sampling.
184 void Start(); 214 void Start();
185 215
186 // Stops the profiler and any ongoing sampling. Calling this function is 216 // Stops the profiler and any ongoing sampling. Calling this function is
187 // optional; if not invoked profiling terminates when all the profiling bursts 217 // optional; if not invoked profiling terminates when all the profiling bursts
188 // specified in the SamplingParams are completed or the profiler is destroyed, 218 // specified in the SamplingParams are completed or the profiler is destroyed,
189 // whichever occurs first. 219 // whichever occurs first.
190 void Stop(); 220 void Stop();
191 221
222 // Set the current system state that is recorded with each captured stack
223 // frame. These are all thread-safe so can be called from anywhere.
224 static void SetProcessPhase(ProcessPhase phase);
225 static void RecordActivityBegin(ProcessActivity activity);
226 static void RecordActivityEnd(ProcessActivity activity);
227
192 private: 228 private:
193 // SamplingThread is a separate thread used to suspend and sample stacks from 229 // SamplingThread is a separate thread used to suspend and sample stacks from
194 // the target thread. 230 // the target thread.
195 class SamplingThread : public PlatformThread::Delegate { 231 class SamplingThread : public PlatformThread::Delegate {
196 public: 232 public:
197 // Samples stacks using |native_sampler|. When complete, invokes 233 // Samples stacks using |native_sampler|. When complete, invokes
198 // |completed_callback| with the collected call stack profiles. 234 // |completed_callback| with the collected call stack profiles.
199 // |completed_callback| must be callable on any thread. 235 // |completed_callback| must be callable on any thread.
200 SamplingThread(std::unique_ptr<NativeStackSampler> native_sampler, 236 SamplingThread(std::unique_ptr<NativeStackSampler> native_sampler,
201 const SamplingParams& params, 237 const SamplingParams& params,
(...skipping 22 matching lines...) Expand all
224 260
225 // If Stop() is called, it signals this event to force the sampling to 261 // If Stop() is called, it signals this event to force the sampling to
226 // terminate before all the samples specified in |params_| are collected. 262 // terminate before all the samples specified in |params_| are collected.
227 WaitableEvent stop_event_; 263 WaitableEvent stop_event_;
228 264
229 const CompletedCallback completed_callback_; 265 const CompletedCallback completed_callback_;
230 266
231 DISALLOW_COPY_AND_ASSIGN(SamplingThread); 267 DISALLOW_COPY_AND_ASSIGN(SamplingThread);
232 }; 268 };
233 269
270 // These global variables hold current system state. These values are
271 // recorded with every captured sample, done on a separate thread which is
272 // why updates to these must be atomic; a PostTask to move the the updates
273 // to that thread would skew the timing.
274 static subtle::Atomic32 process_phases_;
275 static subtle::Atomic32 current_activities_;
276
234 // The thread whose stack will be sampled. 277 // The thread whose stack will be sampled.
235 PlatformThreadId thread_id_; 278 PlatformThreadId thread_id_;
236 279
237 const SamplingParams params_; 280 const SamplingParams params_;
238 281
239 std::unique_ptr<SamplingThread> sampling_thread_; 282 std::unique_ptr<SamplingThread> sampling_thread_;
240 PlatformThreadHandle sampling_thread_handle_; 283 PlatformThreadHandle sampling_thread_handle_;
241 284
242 const CompletedCallback completed_callback_; 285 const CompletedCallback completed_callback_;
243 286
244 // Stored until it can be passed to the NativeStackSampler created in Start(). 287 // Stored until it can be passed to the NativeStackSampler created in Start().
245 NativeStackSamplerTestDelegate* const test_delegate_; 288 NativeStackSamplerTestDelegate* const test_delegate_;
246 289
247 DISALLOW_COPY_AND_ASSIGN(StackSamplingProfiler); 290 DISALLOW_COPY_AND_ASSIGN(StackSamplingProfiler);
248 }; 291 };
249 292
250 // These operators permit types to be compared and used in a map of Samples, as 293 // 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. 294 // done in tests and by the metrics provider code.
252 BASE_EXPORT bool operator==(const StackSamplingProfiler::Module& a, 295 BASE_EXPORT bool operator==(const StackSamplingProfiler::Module& a,
253 const StackSamplingProfiler::Module& b); 296 const StackSamplingProfiler::Module& b);
297 BASE_EXPORT bool operator==(const StackSamplingProfiler::Sample& a,
298 const StackSamplingProfiler::Sample& b);
299 BASE_EXPORT bool operator!=(const StackSamplingProfiler::Sample& a,
300 const StackSamplingProfiler::Sample& b);
301 BASE_EXPORT bool operator<(const StackSamplingProfiler::Sample& a,
302 const StackSamplingProfiler::Sample& b);
254 BASE_EXPORT bool operator==(const StackSamplingProfiler::Frame& a, 303 BASE_EXPORT bool operator==(const StackSamplingProfiler::Frame& a,
255 const StackSamplingProfiler::Frame& b); 304 const StackSamplingProfiler::Frame& b);
256 BASE_EXPORT bool operator<(const StackSamplingProfiler::Frame& a, 305 BASE_EXPORT bool operator<(const StackSamplingProfiler::Frame& a,
257 const StackSamplingProfiler::Frame& b); 306 const StackSamplingProfiler::Frame& b);
258 307
259 } // namespace base 308 } // namespace base
260 309
261 #endif // BASE_PROFILER_STACK_SAMPLING_PROFILER_H_ 310 #endif // BASE_PROFILER_STACK_SAMPLING_PROFILER_H_
OLDNEW
« no previous file with comments | « base/profiler/native_stack_sampler_win.cc ('k') | base/profiler/stack_sampling_profiler.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698