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