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

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

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

Powered by Google App Engine
This is Rietveld 408576698