OLD | NEW |
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 // DefaultProfileProcessor ---------------------------------------------------- |
| 19 |
20 namespace { | 20 namespace { |
21 | 21 |
22 // Thread-safe singleton class that stores collected profiles waiting to be | 22 // Singleton class responsible for providing the default processing for profiles |
23 // processed. | 23 // (i.e. for profiles generated by profilers without their own completed |
24 class PendingProfiles { | 24 // callback). |
| 25 class DefaultProfileProcessor { |
25 public: | 26 public: |
26 PendingProfiles(); | 27 using CompletedCallback = StackSamplingProfiler::CompletedCallback; |
27 ~PendingProfiles(); | |
28 | 28 |
29 static PendingProfiles* GetInstance(); | 29 ~DefaultProfileProcessor(); |
30 | 30 |
31 // Appends |profiles|. This function is thread safe. | 31 static DefaultProfileProcessor* GetInstance(); |
32 void PutProfiles(const std::vector<StackSamplingProfiler::Profile>& profiles); | 32 |
33 // Gets the pending profiles into *|profiles|. This function is thread safe. | 33 // Sets the callback to use for processing profiles captured without a |
34 void GetProfiles(std::vector<StackSamplingProfiler::Profile>* profiles); | 34 // per-profiler completed callback. Pending completed profiles are stored in |
| 35 // this object until a non-null callback is provided here. This function is |
| 36 // thread-safe. |
| 37 void SetCompletedCallback(CompletedCallback callback); |
| 38 |
| 39 // Processes |profiles|. This function is thread safe. |
| 40 void ProcessProfiles( |
| 41 const StackSamplingProfiler::CallStackProfiles& profiles); |
35 | 42 |
36 private: | 43 private: |
| 44 friend struct DefaultSingletonTraits<DefaultProfileProcessor>; |
| 45 |
| 46 DefaultProfileProcessor(); |
| 47 |
| 48 // Copies the pending profiles from |profiles_| into |profiles|, and clears |
| 49 // |profiles_|. This function may be called on any thread. |
| 50 void GetAndClearPendingProfiles( |
| 51 StackSamplingProfiler::CallStackProfiles* profiles); |
| 52 |
| 53 // Gets the current completed callback, with proper locking. |
| 54 CompletedCallback GetCompletedCallback() const; |
| 55 |
| 56 mutable Lock callback_lock_; |
| 57 CompletedCallback default_completed_callback_; |
| 58 |
37 Lock profiles_lock_; | 59 Lock profiles_lock_; |
38 std::vector<StackSamplingProfiler::Profile> profiles_; | 60 StackSamplingProfiler::CallStackProfiles profiles_; |
39 | 61 |
40 DISALLOW_COPY_AND_ASSIGN(PendingProfiles); | 62 DISALLOW_COPY_AND_ASSIGN(DefaultProfileProcessor); |
41 }; | 63 }; |
42 | 64 |
43 PendingProfiles::PendingProfiles() {} | 65 DefaultProfileProcessor::~DefaultProfileProcessor() {} |
44 | |
45 PendingProfiles::~PendingProfiles() {} | |
46 | 66 |
47 // static | 67 // static |
48 PendingProfiles* PendingProfiles::GetInstance() { | 68 DefaultProfileProcessor* DefaultProfileProcessor::GetInstance() { |
49 return Singleton<PendingProfiles>::get(); | 69 return Singleton<DefaultProfileProcessor>::get(); |
50 } | 70 } |
51 | 71 |
52 void PendingProfiles::PutProfiles( | 72 void DefaultProfileProcessor::SetCompletedCallback(CompletedCallback callback) { |
53 const std::vector<StackSamplingProfiler::Profile>& profiles) { | 73 { |
54 AutoLock scoped_lock(profiles_lock_); | 74 AutoLock scoped_lock(callback_lock_); |
55 profiles_.insert(profiles_.end(), profiles.begin(), profiles.end()); | 75 default_completed_callback_ = callback; |
| 76 } |
| 77 |
| 78 if (!callback.is_null()) { |
| 79 // Provide any pending profiles to the callback immediately. |
| 80 StackSamplingProfiler::CallStackProfiles profiles; |
| 81 GetAndClearPendingProfiles(&profiles); |
| 82 if (!profiles.empty()) |
| 83 callback.Run(profiles); |
| 84 } |
56 } | 85 } |
57 | 86 |
58 void PendingProfiles::GetProfiles( | 87 void DefaultProfileProcessor::ProcessProfiles( |
59 std::vector<StackSamplingProfiler::Profile>* profiles) { | 88 const StackSamplingProfiler::CallStackProfiles& profiles) { |
| 89 CompletedCallback callback = GetCompletedCallback(); |
| 90 |
| 91 // Store pending profiles if we don't have a valid callback. |
| 92 if (!callback.is_null()) { |
| 93 callback.Run(profiles); |
| 94 } else { |
| 95 AutoLock scoped_lock(profiles_lock_); |
| 96 profiles_.insert(profiles_.end(), profiles.begin(), profiles.end()); |
| 97 } |
| 98 } |
| 99 |
| 100 DefaultProfileProcessor::DefaultProfileProcessor() {} |
| 101 |
| 102 void DefaultProfileProcessor::GetAndClearPendingProfiles( |
| 103 StackSamplingProfiler::CallStackProfiles* profiles) { |
60 profiles->clear(); | 104 profiles->clear(); |
61 | 105 |
62 AutoLock scoped_lock(profiles_lock_); | 106 AutoLock scoped_lock(profiles_lock_); |
63 profiles_.swap(*profiles); | 107 profiles_.swap(*profiles); |
64 } | 108 } |
| 109 |
| 110 DefaultProfileProcessor::CompletedCallback |
| 111 DefaultProfileProcessor::GetCompletedCallback() const { |
| 112 AutoLock scoped_lock(callback_lock_); |
| 113 return default_completed_callback_; |
| 114 } |
| 115 |
65 } // namespace | 116 } // namespace |
66 | 117 |
| 118 // StackSamplingProfiler::Module ---------------------------------------------- |
| 119 |
67 StackSamplingProfiler::Module::Module() : base_address(nullptr) {} | 120 StackSamplingProfiler::Module::Module() : base_address(nullptr) {} |
| 121 StackSamplingProfiler::Module::Module(const void* base_address, |
| 122 const std::string& id, |
| 123 const FilePath& filename) |
| 124 : base_address(base_address), id(id), filename(filename) {} |
68 | 125 |
69 StackSamplingProfiler::Module::~Module() {} | 126 StackSamplingProfiler::Module::~Module() {} |
70 | 127 |
71 StackSamplingProfiler::Frame::Frame() | 128 // StackSamplingProfiler::Frame ----------------------------------------------- |
72 : instruction_pointer(nullptr), | 129 |
73 module_index(-1) {} | 130 StackSamplingProfiler::Frame::Frame(const void* instruction_pointer, |
| 131 size_t module_index) |
| 132 : instruction_pointer(instruction_pointer), |
| 133 module_index(module_index) {} |
74 | 134 |
75 StackSamplingProfiler::Frame::~Frame() {} | 135 StackSamplingProfiler::Frame::~Frame() {} |
76 | 136 |
77 StackSamplingProfiler::Profile::Profile() : preserve_sample_ordering(false) {} | 137 // StackSamplingProfiler::CallStackProfile ------------------------------------ |
78 | 138 |
79 StackSamplingProfiler::Profile::~Profile() {} | 139 StackSamplingProfiler::CallStackProfile::CallStackProfile() |
| 140 : preserve_sample_ordering(false), user_data(0) {} |
80 | 141 |
81 class StackSamplingProfiler::SamplingThread : public PlatformThread::Delegate { | 142 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 | 143 |
92 // Implementation of PlatformThread::Delegate: | 144 // 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 | 145 |
116 StackSamplingProfiler::SamplingThread::SamplingThread( | 146 StackSamplingProfiler::SamplingThread::SamplingThread( |
117 scoped_ptr<NativeStackSampler> native_sampler, | 147 scoped_ptr<NativeStackSampler> native_sampler, |
118 const SamplingParams& params, | 148 const SamplingParams& params, |
119 Callback<void(const std::vector<Profile>&)> completed_callback) | 149 CompletedCallback completed_callback) |
120 : native_sampler_(native_sampler.Pass()), | 150 : native_sampler_(native_sampler.Pass()), |
121 params_(params), | 151 params_(params), |
122 stop_event_(false, false), | 152 stop_event_(false, false), |
123 completed_callback_(completed_callback) { | 153 completed_callback_(completed_callback) { |
124 } | 154 } |
125 | 155 |
126 StackSamplingProfiler::SamplingThread::~SamplingThread() {} | 156 StackSamplingProfiler::SamplingThread::~SamplingThread() {} |
127 | 157 |
128 void StackSamplingProfiler::SamplingThread::ThreadMain() { | 158 void StackSamplingProfiler::SamplingThread::ThreadMain() { |
129 PlatformThread::SetName("Chrome_SamplingProfilerThread"); | 159 PlatformThread::SetName("Chrome_SamplingProfilerThread"); |
130 | 160 |
131 std::vector<Profile> profiles; | 161 CallStackProfiles profiles; |
132 CollectProfiles(&profiles); | 162 CollectProfiles(&profiles); |
133 completed_callback_.Run(profiles); | 163 completed_callback_.Run(profiles); |
134 } | 164 } |
135 | 165 |
| 166 // Depending on how long the sampling takes and the length of the sampling |
| 167 // interval, a burst of samples could take arbitrarily longer than |
| 168 // samples_per_burst * sampling_interval. In this case, we (somewhat |
| 169 // arbitrarily) honor the number of samples requested rather than strictly |
| 170 // adhering to the sampling intervals. Once we have established users for the |
| 171 // StackSamplingProfiler and the collected data to judge, we may go the other |
| 172 // way or make this behavior configurable. |
136 bool StackSamplingProfiler::SamplingThread::CollectProfile( | 173 bool StackSamplingProfiler::SamplingThread::CollectProfile( |
137 Profile* profile, | 174 CallStackProfile* profile, |
138 TimeDelta* elapsed_time) { | 175 TimeDelta* elapsed_time) { |
139 ElapsedTimer profile_timer; | 176 ElapsedTimer profile_timer; |
140 Profile current_profile; | 177 CallStackProfile current_profile; |
141 native_sampler_->ProfileRecordingStarting(¤t_profile); | 178 native_sampler_->ProfileRecordingStarting(¤t_profile.modules); |
142 current_profile.sampling_period = params_.sampling_interval; | 179 current_profile.sampling_period = params_.sampling_interval; |
143 bool stopped_early = false; | 180 bool burst_completed = true; |
| 181 TimeDelta previous_elapsed_sample_time; |
144 for (int i = 0; i < params_.samples_per_burst; ++i) { | 182 for (int i = 0; i < params_.samples_per_burst; ++i) { |
| 183 if (i != 0) { |
| 184 // Always wait, even if for 0 seconds, so we can observe a signal on |
| 185 // stop_event_. |
| 186 if (stop_event_.TimedWait( |
| 187 std::max(params_.sampling_interval - previous_elapsed_sample_time, |
| 188 TimeDelta()))) { |
| 189 burst_completed = false; |
| 190 break; |
| 191 } |
| 192 } |
145 ElapsedTimer sample_timer; | 193 ElapsedTimer sample_timer; |
146 current_profile.samples.push_back(Sample()); | 194 current_profile.samples.push_back(Sample()); |
147 native_sampler_->RecordStackSample(¤t_profile.samples.back()); | 195 native_sampler_->RecordStackSample(¤t_profile.samples.back()); |
148 TimeDelta elapsed_sample_time = sample_timer.Elapsed(); | 196 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 } | 197 } |
158 | 198 |
159 *elapsed_time = profile_timer.Elapsed(); | 199 *elapsed_time = profile_timer.Elapsed(); |
160 current_profile.profile_duration = *elapsed_time; | 200 current_profile.profile_duration = *elapsed_time; |
| 201 current_profile.preserve_sample_ordering = params_.preserve_sample_ordering; |
| 202 current_profile.user_data = params_.user_data; |
161 native_sampler_->ProfileRecordingStopped(); | 203 native_sampler_->ProfileRecordingStopped(); |
162 | 204 |
163 if (!stopped_early) | 205 if (burst_completed) |
164 *profile = current_profile; | 206 *profile = current_profile; |
165 | 207 |
166 return !stopped_early; | 208 return burst_completed; |
167 } | 209 } |
168 | 210 |
| 211 // In an analogous manner to CollectProfile() and samples exceeding the expected |
| 212 // total sampling time, bursts may also exceed the burst_interval. We adopt the |
| 213 // same wait-and-see approach here. |
169 void StackSamplingProfiler::SamplingThread::CollectProfiles( | 214 void StackSamplingProfiler::SamplingThread::CollectProfiles( |
170 std::vector<Profile>* profiles) { | 215 CallStackProfiles* profiles) { |
171 if (stop_event_.TimedWait(params_.initial_delay)) | 216 if (stop_event_.TimedWait(params_.initial_delay)) |
172 return; | 217 return; |
173 | 218 |
| 219 TimeDelta previous_elapsed_profile_time; |
174 for (int i = 0; i < params_.bursts; ++i) { | 220 for (int i = 0; i < params_.bursts; ++i) { |
175 Profile profile; | 221 if (i != 0) { |
176 TimeDelta elapsed_profile_time; | 222 // Always wait, even if for 0 seconds, so we can observe a signal on |
177 if (CollectProfile(&profile, &elapsed_profile_time)) | 223 // stop_event_. |
178 profiles->push_back(profile); | 224 if (stop_event_.TimedWait( |
179 else | 225 std::max(params_.burst_interval - previous_elapsed_profile_time, |
| 226 TimeDelta()))) |
| 227 return; |
| 228 } |
| 229 |
| 230 CallStackProfile profile; |
| 231 if (!CollectProfile(&profile, &previous_elapsed_profile_time)) |
180 return; | 232 return; |
181 | 233 profiles->push_back(profile); |
182 if (stop_event_.TimedWait( | |
183 std::max(params_.burst_interval - elapsed_profile_time, | |
184 TimeDelta()))) | |
185 return; | |
186 } | 234 } |
187 } | 235 } |
188 | 236 |
189 void StackSamplingProfiler::SamplingThread::Stop() { | 237 void StackSamplingProfiler::SamplingThread::Stop() { |
190 stop_event_.Signal(); | 238 stop_event_.Signal(); |
191 } | 239 } |
192 | 240 |
193 void StackSamplingProfiler::SamplingThreadDeleter::operator()( | 241 // StackSamplingProfiler ------------------------------------------------------ |
194 SamplingThread* thread) const { | |
195 delete thread; | |
196 } | |
197 | |
198 StackSamplingProfiler::NativeStackSampler::NativeStackSampler() {} | |
199 | |
200 StackSamplingProfiler::NativeStackSampler::~NativeStackSampler() {} | |
201 | 242 |
202 StackSamplingProfiler::SamplingParams::SamplingParams() | 243 StackSamplingProfiler::SamplingParams::SamplingParams() |
203 : initial_delay(TimeDelta::FromMilliseconds(0)), | 244 : initial_delay(TimeDelta::FromMilliseconds(0)), |
204 bursts(1), | 245 bursts(1), |
205 burst_interval(TimeDelta::FromMilliseconds(10000)), | 246 burst_interval(TimeDelta::FromMilliseconds(10000)), |
206 samples_per_burst(300), | 247 samples_per_burst(300), |
207 sampling_interval(TimeDelta::FromMilliseconds(100)), | 248 sampling_interval(TimeDelta::FromMilliseconds(100)), |
208 preserve_sample_ordering(false) { | 249 preserve_sample_ordering(false), |
| 250 user_data(0) { |
209 } | 251 } |
210 | 252 |
211 StackSamplingProfiler::StackSamplingProfiler(PlatformThreadId thread_id, | 253 StackSamplingProfiler::StackSamplingProfiler(PlatformThreadId thread_id, |
212 const SamplingParams& params) | 254 const SamplingParams& params) |
213 : thread_id_(thread_id), params_(params) {} | 255 : thread_id_(thread_id), params_(params) {} |
214 | 256 |
215 StackSamplingProfiler::~StackSamplingProfiler() {} | 257 StackSamplingProfiler::StackSamplingProfiler(PlatformThreadId thread_id, |
| 258 const SamplingParams& params, |
| 259 CompletedCallback callback) |
| 260 : thread_id_(thread_id), params_(params), completed_callback_(callback) {} |
| 261 |
| 262 StackSamplingProfiler::~StackSamplingProfiler() { |
| 263 Stop(); |
| 264 if (!sampling_thread_handle_.is_null()) |
| 265 PlatformThread::Join(sampling_thread_handle_); |
| 266 } |
216 | 267 |
217 void StackSamplingProfiler::Start() { | 268 void StackSamplingProfiler::Start() { |
218 native_sampler_ = NativeStackSampler::Create(thread_id_); | 269 scoped_ptr<NativeStackSampler> native_sampler = |
219 if (!native_sampler_) | 270 NativeStackSampler::Create(thread_id_); |
| 271 if (!native_sampler) |
220 return; | 272 return; |
221 | 273 |
| 274 CompletedCallback callback = |
| 275 !completed_callback_.is_null() ? completed_callback_ : |
| 276 Bind(&DefaultProfileProcessor::ProcessProfiles, |
| 277 Unretained(DefaultProfileProcessor::GetInstance())); |
222 sampling_thread_.reset( | 278 sampling_thread_.reset( |
223 new SamplingThread( | 279 new SamplingThread(native_sampler.Pass(), params_, callback)); |
224 native_sampler_.Pass(), params_, | 280 if (!PlatformThread::Create(0, sampling_thread_.get(), |
225 (custom_completed_callback_.is_null() ? | 281 &sampling_thread_handle_)) |
226 Bind(&PendingProfiles::PutProfiles, | 282 sampling_thread_.reset(); |
227 Unretained(PendingProfiles::GetInstance())) : | |
228 custom_completed_callback_))); | |
229 if (!PlatformThread::CreateNonJoinable(0, sampling_thread_.get())) | |
230 LOG(ERROR) << "failed to create thread"; | |
231 } | 283 } |
232 | 284 |
233 void StackSamplingProfiler::Stop() { | 285 void StackSamplingProfiler::Stop() { |
234 if (sampling_thread_) | 286 if (sampling_thread_) |
235 sampling_thread_->Stop(); | 287 sampling_thread_->Stop(); |
236 } | 288 } |
237 | 289 |
238 // static | 290 // static |
239 void StackSamplingProfiler::GetPendingProfiles(std::vector<Profile>* profiles) { | 291 void StackSamplingProfiler::SetDefaultCompletedCallback( |
240 PendingProfiles::GetInstance()->GetProfiles(profiles); | 292 CompletedCallback callback) { |
| 293 DefaultProfileProcessor::GetInstance()->SetCompletedCallback(callback); |
241 } | 294 } |
242 | 295 |
243 void StackSamplingProfiler::SetCustomCompletedCallback( | 296 // StackSamplingProfiler::Frame global functions ------------------------------ |
244 Callback<void(const std::vector<Profile>&)> callback) { | |
245 custom_completed_callback_ = callback; | |
246 } | |
247 | 297 |
248 bool operator==(const StackSamplingProfiler::Frame &a, | 298 bool operator==(const StackSamplingProfiler::Frame &a, |
249 const StackSamplingProfiler::Frame &b) { | 299 const StackSamplingProfiler::Frame &b) { |
250 return a.instruction_pointer == b.instruction_pointer && | 300 return a.instruction_pointer == b.instruction_pointer && |
251 a.module_index == b.module_index; | 301 a.module_index == b.module_index; |
252 } | 302 } |
253 | 303 |
254 bool operator<(const StackSamplingProfiler::Frame &a, | 304 bool operator<(const StackSamplingProfiler::Frame &a, |
255 const StackSamplingProfiler::Frame &b) { | 305 const StackSamplingProfiler::Frame &b) { |
256 return (a.module_index < b.module_index) || | 306 return (a.module_index < b.module_index) || |
257 (a.module_index == b.module_index && | 307 (a.module_index == b.module_index && |
258 a.instruction_pointer < b.instruction_pointer); | 308 a.instruction_pointer < b.instruction_pointer); |
259 } | 309 } |
260 | 310 |
261 } // namespace base | 311 } // namespace base |
OLD | NEW |