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

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

Issue 1030923002: StackSamplingProfiler clean up (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@lkcr
Patch Set: Created 5 years, 9 months 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 <string> 8 #include <string>
9 #include <vector> 9 #include <vector>
10 10
11 #include "base/base_export.h" 11 #include "base/base_export.h"
12 #include "base/callback.h" 12 #include "base/callback.h"
13 #include "base/files/file_path.h" 13 #include "base/files/file_path.h"
14 #include "base/memory/scoped_ptr.h" 14 #include "base/memory/scoped_ptr.h"
15 #include "base/strings/string16.h" 15 #include "base/strings/string16.h"
16 #include "base/threading/platform_thread.h" 16 #include "base/threading/platform_thread.h"
17 #include "base/time/time.h" 17 #include "base/time/time.h"
18 18
19 namespace base { 19 namespace base {
20 20
21 // StackSamplingProfiler periodically stops a thread to sample its stack, for 21 // StackSamplingProfiler periodically stops a thread to sample its stack, for
22 // the purpose of collecting information about which code paths are 22 // the purpose of collecting information about which code paths are
23 // executing. This information is used in aggregate by UMA to identify hot 23 // executing. This information is used in aggregate by UMA to identify hot
24 // and/or janky code paths. 24 // and/or janky code paths.
25 // 25 //
26 // Sample StackStackSamplingProfiler usage: 26 // Sample StackStackSamplingProfiler usage:
Peter Kasting 2015/03/26 04:29:07 StackStack?
Mike Wittman 2015/03/27 22:42:02 Done.
27 // 27 //
28 // // Create and customize params as desired. 28 // // Create and customize params as desired.
29 // base::StackStackSamplingProfiler::SamplingParams params; 29 // base::StackStackSamplingProfiler::SamplingParams params;
30 // // Any thread's ID may be passed as the target. 30 // // Any thread's ID may be passed as the target.
31 // base::StackSamplingProfiler profiler(base::PlatformThread::CurrentId()), 31 // base::StackSamplingProfiler profiler(base::PlatformThread::CurrentId()),
32 // params); 32 // params);
33 // 33 //
34 // // To process the profiles within Chrome rather than via UMA, set a custom 34 // // To process the profiles within Chrome rather than via UMA, set a custom
35 // // completed callback: 35 // // completed callback:
36 // base::Callback<void(const std::vector<Profile>&)> 36 // base::Callback<void(const std::vector<Profile>&)>
Peter Kasting 2015/03/26 04:29:07 Nit: I think Profile should be base::StackSampling
Mike Wittman 2015/03/27 22:42:03 Used an alias.
37 // thread_safe_callback = ...; 37 // thread_safe_callback = ...;
38 // profiler.SetCustomCompletedCallback(thread_safe_callback); 38 // profiler.SetCustomCompletedCallback(thread_safe_callback);
39 // 39 //
40 // profiler.Start(); 40 // profiler.Start();
41 // // ... work being done on the target thread here ... 41 // // ... work being done on the target thread here ...
42 // profiler.Stop(); // optional, stops collection before complete per params 42 // profiler.Stop(); // optional, stops collection before complete per params
43 // 43 //
44 // When all profiles are complete or the profiler is stopped, if the custom 44 // When all profiles are complete or the profiler is stopped, if the custom
Peter Kasting 2015/03/26 04:29:06 What does it mean for a Profile to be "complete"?
Mike Wittman 2015/03/27 22:42:03 Updated and expanded the comment, including specif
45 // completed callback was set it will be called from the profiler thread with 45 // completed callback was set it will be called from the profiler thread with
46 // the completed profiles. If no callback was set, the profiles are stored 46 // the completed profiles. If no callback was set, the profiles are stored
47 // internally and retrieved for UMA through 47 // internally and retrieved for UMA through
Peter Kasting 2015/03/26 04:29:07 Nit: Odd line wrapping
Mike Wittman 2015/03/27 22:42:03 Done.
48 // GetPendingProfiles(). GetPendingProfiles() should never be called by other 48 // GetPendingProfiles(). GetPendingProfiles() should never be called by other
49 // code; to retrieve profiles for in-process processing, set a completed 49 // code; to retrieve profiles for in-process processing, set a completed
50 // callback. 50 // callback.
51 class BASE_EXPORT StackSamplingProfiler { 51 class BASE_EXPORT StackSamplingProfiler {
52 public: 52 public:
53 // Module represents the module (DLL or exe) corresponding to a stack frame. 53 // Module represents the module (DLL or exe) corresponding to a stack frame.
54 struct Module { 54 struct Module {
55 Module(); 55 Module();
56 ~Module(); 56 ~Module();
57 57
(...skipping 11 matching lines...) Expand all
69 FilePath filename; 69 FilePath filename;
70 }; 70 };
71 71
72 // Frame represents an individual sampled stack frame with module information. 72 // Frame represents an individual sampled stack frame with module information.
73 struct Frame { 73 struct Frame {
74 Frame(); 74 Frame();
75 ~Frame(); 75 ~Frame();
76 76
77 // The sampled instruction pointer within the function. 77 // The sampled instruction pointer within the function.
78 const void* instruction_pointer; 78 const void* instruction_pointer;
79 // Index of the module in the array of modules. We don't represent module 79 // Index of the module in the array of modules. We don't represent module
Peter Kasting 2015/03/26 04:29:07 The array of modules where? Presumably you mean P
Mike Wittman 2015/03/27 22:42:03 Done.
80 // state directly here to save space. 80 // state directly here to save space.
81 int module_index; 81 int module_index;
Peter Kasting 2015/03/26 04:29:07 If this is an index within an object in memory, it
Mike Wittman 2015/03/27 22:42:03 I'm accustomed to using size_t in this case, and d
Peter Kasting 2015/03/27 23:44:46 D'oh! Oh well, that's still probably better, as t
Mike Wittman 2015/03/30 21:01:13 Acknowledged.
82 }; 82 };
83 83
84 // Sample represents a set of stack frames. 84 // Sample represents a set of stack frames.
85 using Sample = std::vector<Frame>; 85 using Sample = std::vector<Frame>;
86 86
87 // Profile represents a set of samples. 87 // Profile represents a set of samples.
88 struct BASE_EXPORT Profile { 88 struct BASE_EXPORT Profile {
89 Profile(); 89 Profile();
90 ~Profile(); 90 ~Profile();
91 91
92 std::vector<Module> modules; 92 std::vector<Module> modules;
93 std::vector<Sample> samples; 93 std::vector<Sample> samples;
94 // Duration of this profile. 94 // Duration of this profile.
95 TimeDelta profile_duration; 95 TimeDelta profile_duration;
96 // Time between samples. 96 // Time between samples.
97 TimeDelta sampling_period; 97 TimeDelta sampling_period;
98 // True if sample ordering is important and should be preserved if and when 98 // True if sample ordering is important and should be preserved if and when
99 // this profile is compressed and processed. 99 // this profile is compressed and processed.
100 bool preserve_sample_ordering; 100 bool preserve_sample_ordering;
101 }; 101 };
102 102
103 // NativeStackSampler abstracts the native implementation required to record a 103 // NativeStackSampler abstracts the native implementation required to record a
104 // stack sample for a given thread. 104 // stack sample for a given thread.
Peter Kasting 2015/03/26 04:29:07 It seems like this class should either be private,
Mike Wittman 2015/03/27 22:42:03 I agree with the sentiment, but I'm not super happ
Peter Kasting 2015/03/27 23:44:46 I definitely agree that the consequences of a priv
Mike Wittman 2015/03/30 21:01:13 OK, I've moved the class to its own file. I was ab
105 class NativeStackSampler { 105 class NativeStackSampler {
106 public: 106 public:
107 virtual ~NativeStackSampler(); 107 virtual ~NativeStackSampler();
108 108
109 // Create a stack sampler that records samples for |thread_handle|. Returns 109 // Create a stack sampler that records samples for |thread_handle|. Returns
Peter Kasting 2015/03/26 04:29:07 All function comments should be declarative ("Crea
Mike Wittman 2015/03/27 22:42:03 I think I've updated all of these.
110 // null if this platform does not support stack sampling. 110 // null if this platform does not support stack sampling.
111 static scoped_ptr<NativeStackSampler> Create(PlatformThreadId thread_id); 111 static scoped_ptr<NativeStackSampler> Create(PlatformThreadId thread_id);
112 112
113 // Notify the sampler that we're starting to record a new profile. This 113 // Notify the sampler that we're starting to record a new profile. This
114 // function is called on the SamplingThread. 114 // function is called on the SamplingThread.
Peter Kasting 2015/03/26 04:29:06 You use "SamplingThread" in reference (presumably)
Mike Wittman 2015/03/27 22:42:03 It's sufficient to refer to it as "the thread used
115 virtual void ProfileRecordingStarting(Profile* profile) = 0; 115 virtual void ProfileRecordingStarting(Profile* profile) = 0;
116 116
117 // Record a stack sample. This function is called on the SamplingThread. 117 // Record a stack sample. This function is called on the SamplingThread.
118 virtual void RecordStackSample(Sample* sample) = 0; 118 virtual void RecordStackSample(Sample* sample) = 0;
119 119
120 // Notify the sampler that we've stopped recording the current profile. This 120 // Notify the sampler that we've stopped recording the current profile. This
121 // function is called on the SamplingThread. 121 // function is called on the SamplingThread.
122 virtual void ProfileRecordingStopped() = 0; 122 virtual void ProfileRecordingStopped() = 0;
123 123
124 protected: 124 protected:
(...skipping 11 matching lines...) Expand all
136 TimeDelta initial_delay; 136 TimeDelta initial_delay;
137 // Number of sampling bursts to perform. Defaults to 1. 137 // Number of sampling bursts to perform. Defaults to 1.
138 int bursts; 138 int bursts;
139 // Interval between sampling bursts. This is the desired duration from the 139 // Interval between sampling bursts. This is the desired duration from the
140 // start of one burst to the start of the next burst. Defaults to 10s. 140 // start of one burst to the start of the next burst. Defaults to 10s.
141 TimeDelta burst_interval; 141 TimeDelta burst_interval;
142 // Number of samples to record per burst. Defaults to 300. 142 // Number of samples to record per burst. Defaults to 300.
143 int samples_per_burst; 143 int samples_per_burst;
144 // Interval between samples during a sampling burst. This is the desired 144 // Interval between samples during a sampling burst. This is the desired
145 // duration from the start of one burst to the start of the next 145 // duration from the start of one burst to the start of the next
146 // burst. Defaults to 100ms. 146 // burst. Defaults to 100ms.
Peter Kasting 2015/03/26 04:29:07 I think you mean "sample" instead of "burst" in a
Mike Wittman 2015/03/27 22:42:03 Done.
147 TimeDelta sampling_interval; 147 TimeDelta sampling_interval;
148 // True if sample ordering is important and should be preserved if and when 148 // True if sample ordering is important and should be preserved if and when
149 // this profile is compressed and processed. Defaults to false. 149 // this profile is compressed and processed. Defaults to false.
150 bool preserve_sample_ordering; 150 bool preserve_sample_ordering;
151 }; 151 };
152 152
153 StackSamplingProfiler(PlatformThreadId thread_id, 153 StackSamplingProfiler(PlatformThreadId thread_id,
154 const SamplingParams& params); 154 const SamplingParams& params);
155 ~StackSamplingProfiler(); 155 ~StackSamplingProfiler();
156 156
157 // Initializes the profiler and starts sampling. 157 // Initializes the profiler and starts sampling.
158 void Start(); 158 void Start();
159 // Stops the profiler and any ongoing sampling. Calling this function is 159 // Stops the profiler and any ongoing sampling. Calling this function is
Peter Kasting 2015/03/26 04:29:07 Nit: Blank line above this
Mike Wittman 2015/03/27 22:42:03 Done.
160 // optional; if not invoked profiling will terminate when all the profiling 160 // optional; if not invoked profiling will terminate when all the profiling
161 // bursts specified in the SamplingParams are completed. 161 // bursts specified in the SamplingParams are completed.
162 void Stop(); 162 void Stop();
163 163
164 // Gets the pending profiles into *|profiles| and clears the internal 164 // Gets the pending profiles into *|profiles| and clears the internal
165 // storage. This function is thread safe. 165 // storage. This function is thread safe.
Peter Kasting 2015/03/26 04:29:06 Nit: Maybe "Moves all pending Profiles from intern
Mike Wittman 2015/03/27 22:42:03 Done.
166 // 166 //
167 // ***This is intended for use only by UMA.*** Callers who want to process the 167 // ***This is intended for use only by UMA.*** Callers who want to process the
168 // collected profiles should use SetCustomCompletedCallback. 168 // collected profiles should use SetCustomCompletedCallback.
169 static void GetPendingProfiles(std::vector<Profile>* profiles); 169 static void GetPendingProfiles(std::vector<Profile>* profiles);
Peter Kasting 2015/03/26 04:29:07 Should we consider making this private and making
Mike Wittman 2015/03/27 22:42:02 I have a follow-on change to this code in https://
Peter Kasting 2015/03/27 23:44:46 Yeah, that seems fine. Dunno if you wanted to cop
Mike Wittman 2015/03/30 21:01:13 I'd prefer to keep them separate to avoid mixing m
170 170
171 // By default, collected profiles are stored internally and can be retrieved 171 // By default, collected profiles are stored internally and can be retrieved
172 // by GetPendingProfiles. If a callback is provided via this function, 172 // by GetPendingProfiles. If a callback is provided via this function,
173 // however, it will be called with the collected profiles instead. Note that 173 // however, it will be called with the collected profiles instead. Note that
174 // this call to the callback occurs *on the profiler thread*. 174 // this call to the callback occurs *on the profiler thread*.
Peter Kasting 2015/03/26 04:29:06 Nit: this -> the
Mike Wittman 2015/03/27 22:42:03 Done.
175 void SetCustomCompletedCallback( 175 void SetCustomCompletedCallback(
176 Callback<void(const std::vector<Profile>&)> callback); 176 Callback<void(const std::vector<Profile>&)> callback);
Peter Kasting 2015/03/26 04:29:07 Consider a public type alias for the function type
Mike Wittman 2015/03/27 22:42:03 I think CompletedCallback is a good idea (and upda
Peter Kasting 2015/03/27 23:44:46 That makes sense.
177 177
178 private: 178 private:
179 class SamplingThread; 179 class SamplingThread;
Peter Kasting 2015/03/26 04:29:07 Comment what this class is.
Mike Wittman 2015/03/27 22:42:03 Done.
180 struct SamplingThreadDeleter { 180 struct SamplingThreadDeleter {
Peter Kasting 2015/03/26 04:29:06 Why is this custom deleter necessary? Your implem
Mike Wittman 2015/03/27 22:42:03 It's not required, but allows the definition of Sa
Peter Kasting 2015/03/27 23:44:46 Urgh. I think the cost is higher than the benefit
Mike Wittman 2015/03/30 21:01:13 Moved the class definition into the class.
181 void operator() (SamplingThread* thread) const; 181 void operator() (SamplingThread* thread) const;
182 }; 182 };
183 183
184 // The thread whose stack will be sampled. 184 // The thread whose stack will be sampled.
185 PlatformThreadId thread_id_; 185 PlatformThreadId thread_id_;
186 186
187 const SamplingParams params_; 187 const SamplingParams params_;
188 188
189 scoped_ptr<SamplingThread, SamplingThreadDeleter> sampling_thread_; 189 scoped_ptr<SamplingThread, SamplingThreadDeleter> sampling_thread_;
190 scoped_ptr<NativeStackSampler> native_sampler_; 190 scoped_ptr<NativeStackSampler> native_sampler_;
191 191
192 Callback<void(const std::vector<Profile>&)> custom_completed_callback_; 192 Callback<void(const std::vector<Profile>&)> custom_completed_callback_;
193 193
194 DISALLOW_COPY_AND_ASSIGN(StackSamplingProfiler); 194 DISALLOW_COPY_AND_ASSIGN(StackSamplingProfiler);
195 }; 195 };
196 196
197 // Defined to allow equality check of Samples. 197 // Defined to allow equality check of Samples.
Peter Kasting 2015/03/26 04:29:07 Where are these actually used? I looked briefly f
Mike Wittman 2015/03/27 22:42:03 They're used by the metrics provider code to suppo
Peter Kasting 2015/03/27 23:44:46 OK. You may want to mention that in the comments
Mike Wittman 2015/03/30 21:01:13 Done.
198 BASE_EXPORT bool operator==(const StackSamplingProfiler::Frame& a, 198 BASE_EXPORT bool operator==(const StackSamplingProfiler::Frame& a,
199 const StackSamplingProfiler::Frame& b); 199 const StackSamplingProfiler::Frame& b);
200 // Defined to allow ordering of Samples. 200 // Defined to allow ordering of Samples.
201 BASE_EXPORT bool operator<(const StackSamplingProfiler::Frame& a, 201 BASE_EXPORT bool operator<(const StackSamplingProfiler::Frame& a,
202 const StackSamplingProfiler::Frame& b); 202 const StackSamplingProfiler::Frame& b);
203 203
204 } // namespace base 204 } // namespace base
205 205
206 #endif // BASE_PROFILER_STACK_SAMPLING_PROFILER_H_ 206 #endif // BASE_PROFILER_STACK_SAMPLING_PROFILER_H_
207
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698