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

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

Issue 1030923002: StackSamplingProfiler clean up (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@lkcr
Patch Set: address comments Created 5 years, 8 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 #include "base/profiler/stack_sampling_profiler.h" 5 #include "base/profiler/stack_sampling_profiler.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/callback.h" 10 #include "base/callback.h"
11 #include "base/memory/singleton.h" 11 #include "base/memory/singleton.h"
12 #include "base/profiler/native_stack_sampler.h"
12 #include "base/synchronization/lock.h" 13 #include "base/synchronization/lock.h"
13 #include "base/synchronization/waitable_event.h"
14 #include "base/timer/elapsed_timer.h" 14 #include "base/timer/elapsed_timer.h"
15 15
16 template <typename T> struct DefaultSingletonTraits;
17
18 namespace base { 16 namespace base {
19 17
18 // PendingProfiles ------------------------------------------------------------
19
20 namespace { 20 namespace {
21 21
22 // Thread-safe singleton class that stores collected profiles waiting to be 22 // Thread-safe singleton class that stores collected call stack profiles waiting
23 // processed. 23 // to be processed.
24 class PendingProfiles { 24 class PendingProfiles {
25 public: 25 public:
26 PendingProfiles();
27 ~PendingProfiles(); 26 ~PendingProfiles();
28 27
29 static PendingProfiles* GetInstance(); 28 static PendingProfiles* GetInstance();
30 29
31 // Appends |profiles|. This function is thread safe. 30 // Appends |profiles| to |profiles_|. This function is thread safe.
32 void PutProfiles(const std::vector<StackSamplingProfiler::Profile>& profiles); 31 void AppendProfiles(
33 // Gets the pending profiles into *|profiles|. This function is thread safe. 32 const std::vector<StackSamplingProfiler::CallStackProfile>& profiles);
34 void GetProfiles(std::vector<StackSamplingProfiler::Profile>* profiles); 33
34 // Copies the pending profiles from |profiles_| into |profiles|, and clears
35 // |profiles_|. This function is thread safe.
Peter Kasting 2015/03/31 01:51:25 Nit: Again, I think "This function may be called o
Mike Wittman 2015/03/31 19:29:41 Done.
36 void GetAndClearPendingProfiles(
37 std::vector<StackSamplingProfiler::CallStackProfile>* profiles);
35 38
36 private: 39 private:
40 friend struct DefaultSingletonTraits<PendingProfiles>;
41 PendingProfiles();
Peter Kasting 2015/03/31 01:51:25 Nit: I'd put a blank line above this
Mike Wittman 2015/03/31 19:29:41 Done.
42
37 Lock profiles_lock_; 43 Lock profiles_lock_;
38 std::vector<StackSamplingProfiler::Profile> profiles_; 44 std::vector<StackSamplingProfiler::CallStackProfile> profiles_;
39 45
40 DISALLOW_COPY_AND_ASSIGN(PendingProfiles); 46 DISALLOW_COPY_AND_ASSIGN(PendingProfiles);
41 }; 47 };
42 48
43 PendingProfiles::PendingProfiles() {} 49 PendingProfiles::PendingProfiles() {}
44 50
45 PendingProfiles::~PendingProfiles() {} 51 PendingProfiles::~PendingProfiles() {}
46 52
47 // static 53 // static
48 PendingProfiles* PendingProfiles::GetInstance() { 54 PendingProfiles* PendingProfiles::GetInstance() {
49 return Singleton<PendingProfiles>::get(); 55 return Singleton<PendingProfiles>::get();
50 } 56 }
51 57
52 void PendingProfiles::PutProfiles( 58 void PendingProfiles::AppendProfiles(
53 const std::vector<StackSamplingProfiler::Profile>& profiles) { 59 const std::vector<StackSamplingProfiler::CallStackProfile>& profiles) {
54 AutoLock scoped_lock(profiles_lock_); 60 AutoLock scoped_lock(profiles_lock_);
55 profiles_.insert(profiles_.end(), profiles.begin(), profiles.end()); 61 profiles_.insert(profiles_.end(), profiles.begin(), profiles.end());
56 } 62 }
57 63
58 void PendingProfiles::GetProfiles( 64 void PendingProfiles::GetAndClearPendingProfiles(
59 std::vector<StackSamplingProfiler::Profile>* profiles) { 65 std::vector<StackSamplingProfiler::CallStackProfile>* profiles) {
60 profiles->clear(); 66 profiles->clear();
61 67
62 AutoLock scoped_lock(profiles_lock_); 68 AutoLock scoped_lock(profiles_lock_);
63 profiles_.swap(*profiles); 69 profiles_.swap(*profiles);
64 } 70 }
71
65 } // namespace 72 } // namespace
66 73
74 // StackSamplingProfiler::Module ----------------------------------------------
75
67 StackSamplingProfiler::Module::Module() : base_address(nullptr) {} 76 StackSamplingProfiler::Module::Module() : base_address(nullptr) {}
68 77
69 StackSamplingProfiler::Module::~Module() {} 78 StackSamplingProfiler::Module::~Module() {}
70 79
71 StackSamplingProfiler::Frame::Frame() 80 // StackSamplingProfiler::Frame -----------------------------------------------
72 : instruction_pointer(nullptr), 81
73 module_index(-1) {} 82 StackSamplingProfiler::Frame::Frame(const void* instruction_pointer,
83 size_t module_index)
84 : instruction_pointer(instruction_pointer),
85 module_index(module_index) {}
74 86
75 StackSamplingProfiler::Frame::~Frame() {} 87 StackSamplingProfiler::Frame::~Frame() {}
76 88
77 StackSamplingProfiler::Profile::Profile() : preserve_sample_ordering(false) {} 89 // StackSamplingProfiler::CallStackProfile ------------------------------------
78 90
79 StackSamplingProfiler::Profile::~Profile() {} 91 StackSamplingProfiler::CallStackProfile::CallStackProfile()
92 : preserve_sample_ordering(false) {}
80 93
81 class StackSamplingProfiler::SamplingThread : public PlatformThread::Delegate { 94 StackSamplingProfiler::CallStackProfile::~CallStackProfile() {}
82 public:
83 // Samples stacks using |native_sampler|. When complete, invokes
84 // |profiles_callback| with the collected profiles. |profiles_callback| must
85 // be thread-safe and may consume the contents of the vector.
86 SamplingThread(
87 scoped_ptr<NativeStackSampler> native_sampler,
88 const SamplingParams& params,
89 Callback<void(const std::vector<Profile>&)> completed_callback);
90 ~SamplingThread() override;
91 95
92 // Implementation of PlatformThread::Delegate: 96 // StackSamplingProfiler::SamplingThread --------------------------------------
93 void ThreadMain() override;
94
95 void Stop();
96
97 private:
98 // Collects a profile from a single burst. Returns true if the profile was
99 // collected, or false if collection was stopped before it completed.
100 bool CollectProfile(Profile* profile, TimeDelta* elapsed_time);
101 // Collects profiles from all bursts, or until the sampling is stopped. If
102 // stopped before complete, |profiles| will contains only full bursts.
103 void CollectProfiles(std::vector<Profile>* profiles);
104
105 scoped_ptr<NativeStackSampler> native_sampler_;
106
107 const SamplingParams params_;
108
109 WaitableEvent stop_event_;
110
111 Callback<void(const std::vector<Profile>&)> completed_callback_;
112
113 DISALLOW_COPY_AND_ASSIGN(SamplingThread);
114 };
115 97
116 StackSamplingProfiler::SamplingThread::SamplingThread( 98 StackSamplingProfiler::SamplingThread::SamplingThread(
117 scoped_ptr<NativeStackSampler> native_sampler, 99 scoped_ptr<NativeStackSampler> native_sampler,
118 const SamplingParams& params, 100 const SamplingParams& params,
119 Callback<void(const std::vector<Profile>&)> completed_callback) 101 CompletedCallback completed_callback)
120 : native_sampler_(native_sampler.Pass()), 102 : native_sampler_(native_sampler.Pass()),
121 params_(params), 103 params_(params),
122 stop_event_(false, false), 104 stop_event_(false, false),
123 completed_callback_(completed_callback) { 105 completed_callback_(completed_callback) {
124 } 106 }
125 107
126 StackSamplingProfiler::SamplingThread::~SamplingThread() {} 108 StackSamplingProfiler::SamplingThread::~SamplingThread() {}
127 109
128 void StackSamplingProfiler::SamplingThread::ThreadMain() { 110 void StackSamplingProfiler::SamplingThread::ThreadMain() {
129 PlatformThread::SetName("Chrome_SamplingProfilerThread"); 111 PlatformThread::SetName("Chrome_SamplingProfilerThread");
130 112
131 std::vector<Profile> profiles; 113 std::vector<CallStackProfile> profiles;
132 CollectProfiles(&profiles); 114 CollectProfiles(&profiles);
133 completed_callback_.Run(profiles); 115 completed_callback_.Run(profiles);
134 } 116 }
135 117
118 // Depending on how long the sampling takes and the length of the sampling
119 // interval, a burst of samples could take arbitrarily longer than
120 // samples_per_burst * sampling_interval. In this case, we (somewhat
121 // arbitrarily) honor the number of samples requested rather than strictly
122 // adhering to the sampling intervals. Once we have established users for the
123 // StackSamplingProfiler and the collected data to judge, we may go the other
124 // way or make this behavior configurable.
136 bool StackSamplingProfiler::SamplingThread::CollectProfile( 125 bool StackSamplingProfiler::SamplingThread::CollectProfile(
137 Profile* profile, 126 CallStackProfile* profile,
138 TimeDelta* elapsed_time) { 127 TimeDelta* elapsed_time) {
139 ElapsedTimer profile_timer; 128 ElapsedTimer profile_timer;
140 Profile current_profile; 129 CallStackProfile current_profile;
141 native_sampler_->ProfileRecordingStarting(&current_profile); 130 native_sampler_->ProfileRecordingStarting(&current_profile);
142 current_profile.sampling_period = params_.sampling_interval; 131 current_profile.sampling_period = params_.sampling_interval;
143 bool stopped_early = false; 132 bool burst_completed = true;
133 TimeDelta previous_elapsed_sample_time;
144 for (int i = 0; i < params_.samples_per_burst; ++i) { 134 for (int i = 0; i < params_.samples_per_burst; ++i) {
135 if (i != 0) {
136 // Always wait, even if for 0 seconds, so we can observe a signal on
137 // stop_event_.
138 if (stop_event_.TimedWait(
139 std::max(params_.sampling_interval - previous_elapsed_sample_time,
140 TimeDelta()))) {
141 burst_completed = false;
142 break;
143 }
144 }
145 ElapsedTimer sample_timer; 145 ElapsedTimer sample_timer;
146 current_profile.samples.push_back(Sample()); 146 current_profile.samples.push_back(Sample());
147 native_sampler_->RecordStackSample(&current_profile.samples.back()); 147 native_sampler_->RecordStackSample(&current_profile.samples.back());
148 TimeDelta elapsed_sample_time = sample_timer.Elapsed(); 148 previous_elapsed_sample_time = sample_timer.Elapsed();
149 if (i != params_.samples_per_burst - 1) {
150 if (stop_event_.TimedWait(
151 std::max(params_.sampling_interval - elapsed_sample_time,
152 TimeDelta()))) {
153 stopped_early = true;
154 break;
155 }
156 }
157 } 149 }
158 150
159 *elapsed_time = profile_timer.Elapsed(); 151 *elapsed_time = profile_timer.Elapsed();
160 current_profile.profile_duration = *elapsed_time; 152 current_profile.profile_duration = *elapsed_time;
161 native_sampler_->ProfileRecordingStopped(); 153 native_sampler_->ProfileRecordingStopped();
162 154
163 if (!stopped_early) 155 if (burst_completed)
164 *profile = current_profile; 156 *profile = current_profile;
165 157
166 return !stopped_early; 158 return burst_completed;
167 } 159 }
168 160
161 // In an analogous manner to CollectProfile() and samples exceeding the expected
162 // total sampling time, bursts may also exceed the burst_interval. We adopt the
163 // same wait-and-see approach here.
169 void StackSamplingProfiler::SamplingThread::CollectProfiles( 164 void StackSamplingProfiler::SamplingThread::CollectProfiles(
170 std::vector<Profile>* profiles) { 165 std::vector<CallStackProfile>* profiles) {
171 if (stop_event_.TimedWait(params_.initial_delay)) 166 if (stop_event_.TimedWait(params_.initial_delay))
172 return; 167 return;
173 168
169 TimeDelta previous_elapsed_profile_time;
174 for (int i = 0; i < params_.bursts; ++i) { 170 for (int i = 0; i < params_.bursts; ++i) {
175 Profile profile; 171 if (i != 0) {
176 TimeDelta elapsed_profile_time; 172 // Always wait, even if for 0 seconds, so we can observe a signal on
177 if (CollectProfile(&profile, &elapsed_profile_time)) 173 // stop_event_.
178 profiles->push_back(profile); 174 if (stop_event_.TimedWait(
179 else 175 std::max(params_.burst_interval - previous_elapsed_profile_time,
176 TimeDelta())))
177 return;
178 }
179
180 CallStackProfile profile;
181 if (!CollectProfile(&profile, &previous_elapsed_profile_time))
180 return; 182 return;
181 183 profiles->push_back(profile);
182 if (stop_event_.TimedWait(
183 std::max(params_.burst_interval - elapsed_profile_time,
184 TimeDelta())))
185 return;
186 } 184 }
187 } 185 }
188 186
189 void StackSamplingProfiler::SamplingThread::Stop() { 187 void StackSamplingProfiler::SamplingThread::Stop() {
190 stop_event_.Signal(); 188 stop_event_.Signal();
191 } 189 }
192 190
193 void StackSamplingProfiler::SamplingThreadDeleter::operator()( 191 // StackSamplingProfiler ------------------------------------------------------
194 SamplingThread* thread) const {
195 delete thread;
196 }
197
198 StackSamplingProfiler::NativeStackSampler::NativeStackSampler() {}
199
200 StackSamplingProfiler::NativeStackSampler::~NativeStackSampler() {}
201 192
202 StackSamplingProfiler::SamplingParams::SamplingParams() 193 StackSamplingProfiler::SamplingParams::SamplingParams()
203 : initial_delay(TimeDelta::FromMilliseconds(0)), 194 : initial_delay(TimeDelta::FromMilliseconds(0)),
204 bursts(1), 195 bursts(1),
205 burst_interval(TimeDelta::FromMilliseconds(10000)), 196 burst_interval(TimeDelta::FromMilliseconds(10000)),
206 samples_per_burst(300), 197 samples_per_burst(300),
207 sampling_interval(TimeDelta::FromMilliseconds(100)), 198 sampling_interval(TimeDelta::FromMilliseconds(100)),
208 preserve_sample_ordering(false) { 199 preserve_sample_ordering(false) {
209 } 200 }
210 201
211 StackSamplingProfiler::StackSamplingProfiler(PlatformThreadId thread_id, 202 StackSamplingProfiler::StackSamplingProfiler(PlatformThreadId thread_id,
212 const SamplingParams& params) 203 const SamplingParams& params)
213 : thread_id_(thread_id), params_(params) {} 204 : thread_id_(thread_id), params_(params) {}
214 205
215 StackSamplingProfiler::~StackSamplingProfiler() {} 206 StackSamplingProfiler::~StackSamplingProfiler() {}
216 207
217 void StackSamplingProfiler::Start() { 208 void StackSamplingProfiler::Start() {
218 native_sampler_ = NativeStackSampler::Create(thread_id_); 209 scoped_ptr<NativeStackSampler> native_sampler =
219 if (!native_sampler_) 210 NativeStackSampler::Create(thread_id_);
211 if (!native_sampler)
220 return; 212 return;
221 213
222 sampling_thread_.reset( 214 sampling_thread_.reset(
223 new SamplingThread( 215 new SamplingThread(
224 native_sampler_.Pass(), params_, 216 native_sampler.Pass(), params_,
225 (custom_completed_callback_.is_null() ? 217 (custom_completed_callback_.is_null() ?
226 Bind(&PendingProfiles::PutProfiles, 218 Bind(&PendingProfiles::AppendProfiles,
227 Unretained(PendingProfiles::GetInstance())) : 219 Unretained(PendingProfiles::GetInstance())) :
228 custom_completed_callback_))); 220 custom_completed_callback_)));
229 if (!PlatformThread::CreateNonJoinable(0, sampling_thread_.get())) 221 if (!PlatformThread::CreateNonJoinable(0, sampling_thread_.get()))
230 LOG(ERROR) << "failed to create thread"; 222 sampling_thread_.reset();
231 } 223 }
232 224
233 void StackSamplingProfiler::Stop() { 225 void StackSamplingProfiler::Stop() {
234 if (sampling_thread_) 226 if (sampling_thread_)
235 sampling_thread_->Stop(); 227 sampling_thread_->Stop();
236 } 228 }
237 229
238 // static 230 // static
239 void StackSamplingProfiler::GetPendingProfiles(std::vector<Profile>* profiles) { 231 void StackSamplingProfiler::GetPendingProfiles(
240 PendingProfiles::GetInstance()->GetProfiles(profiles); 232 std::vector<CallStackProfile>* profiles) {
233 PendingProfiles::GetInstance()->GetAndClearPendingProfiles(profiles);
241 } 234 }
242 235
243 void StackSamplingProfiler::SetCustomCompletedCallback( 236 // StackSamplingProfiler::Frame global functions ------------------------------
244 Callback<void(const std::vector<Profile>&)> callback) {
245 custom_completed_callback_ = callback;
246 }
247 237
248 bool operator==(const StackSamplingProfiler::Frame &a, 238 bool operator==(const StackSamplingProfiler::Frame &a,
249 const StackSamplingProfiler::Frame &b) { 239 const StackSamplingProfiler::Frame &b) {
250 return a.instruction_pointer == b.instruction_pointer && 240 return a.instruction_pointer == b.instruction_pointer &&
251 a.module_index == b.module_index; 241 a.module_index == b.module_index;
252 } 242 }
253 243
254 bool operator<(const StackSamplingProfiler::Frame &a, 244 bool operator<(const StackSamplingProfiler::Frame &a,
255 const StackSamplingProfiler::Frame &b) { 245 const StackSamplingProfiler::Frame &b) {
256 return (a.module_index < b.module_index) || 246 return (a.module_index < b.module_index) ||
257 (a.module_index == b.module_index && 247 (a.module_index == b.module_index &&
258 a.instruction_pointer < b.instruction_pointer); 248 a.instruction_pointer < b.instruction_pointer);
259 } 249 }
260 250
261 } // namespace base 251 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698