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

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

Powered by Google App Engine
This is Rietveld 408576698