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/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; | 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); | |
|
Peter Kasting
2015/03/30 23:07:34
FWIW, I don't love using "Move" in the function na
Mike Wittman
2015/03/31 01:06:36
Used GetAndClearPendingProfiles().
| |
| 35 | 37 |
| 36 private: | 38 private: |
| 39 PendingProfiles(); | |
| 40 friend struct DefaultSingletonTraits<PendingProfiles>; | |
|
Peter Kasting
2015/03/30 23:07:34
Nit: The Google style guide is unclear here, but c
Mike Wittman
2015/03/31 01:06:36
Done.
| |
| 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 |
| 81 class StackSamplingProfiler::SamplingThread : public PlatformThread::Delegate { | 94 // StackSamplingProfiler::SamplingThread -------------------------------------- |
| 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 | |
| 92 // Implementation of PlatformThread::Delegate: | |
| 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 | 95 |
| 116 StackSamplingProfiler::SamplingThread::SamplingThread( | 96 StackSamplingProfiler::SamplingThread::SamplingThread( |
| 117 scoped_ptr<NativeStackSampler> native_sampler, | 97 scoped_ptr<NativeStackSampler> native_sampler, |
| 118 const SamplingParams& params, | 98 const SamplingParams& params, |
| 119 Callback<void(const std::vector<Profile>&)> completed_callback) | 99 CompletedCallback completed_callback) |
| 120 : native_sampler_(native_sampler.Pass()), | 100 : native_sampler_(native_sampler.Pass()), |
| 121 params_(params), | 101 params_(params), |
| 122 stop_event_(false, false), | 102 stop_event_(false, false), |
| 123 completed_callback_(completed_callback) { | 103 completed_callback_(completed_callback) { |
| 124 } | 104 } |
| 125 | 105 |
| 126 StackSamplingProfiler::SamplingThread::~SamplingThread() {} | 106 StackSamplingProfiler::SamplingThread::~SamplingThread() {} |
| 127 | 107 |
| 128 void StackSamplingProfiler::SamplingThread::ThreadMain() { | 108 void StackSamplingProfiler::SamplingThread::ThreadMain() { |
| 129 PlatformThread::SetName("Chrome_SamplingProfilerThread"); | 109 PlatformThread::SetName("Chrome_SamplingProfilerThread"); |
| 130 | 110 |
| 131 std::vector<Profile> profiles; | 111 std::vector<Profile> profiles; |
| 132 CollectProfiles(&profiles); | 112 CollectProfiles(&profiles); |
| 133 completed_callback_.Run(profiles); | 113 completed_callback_.Run(profiles); |
| 134 } | 114 } |
| 135 | 115 |
| 116 // Depending on how long the sampling takes and the length of the sampling | |
| 117 // interval, a burst of samples could take arbitrarily longer than | |
| 118 // samples_per_burst * sampling_interval. In this case, we (somewhat | |
| 119 // arbitrarily) honor the number of samples requested rather than strictly | |
| 120 // adhereing to the sampling intervals. Once we have established users for the | |
|
Peter Kasting
2015/03/30 23:07:34
Nit: adhering
Mike Wittman
2015/03/31 01:06:36
Done.
| |
| 121 // StackSamplingProfiler and the collected data to judge, we may go the other | |
| 122 // way or make this behavior configurable. | |
| 136 bool StackSamplingProfiler::SamplingThread::CollectProfile( | 123 bool StackSamplingProfiler::SamplingThread::CollectProfile( |
| 137 Profile* profile, | 124 Profile* profile, |
| 138 TimeDelta* elapsed_time) { | 125 TimeDelta* elapsed_time) { |
| 139 ElapsedTimer profile_timer; | 126 ElapsedTimer profile_timer; |
| 140 Profile current_profile; | 127 Profile current_profile; |
| 141 native_sampler_->ProfileRecordingStarting(¤t_profile); | 128 native_sampler_->ProfileRecordingStarting(¤t_profile); |
| 142 current_profile.sampling_period = params_.sampling_interval; | 129 current_profile.sampling_period = params_.sampling_interval; |
| 143 bool stopped_early = false; | 130 bool burst_completed = true; |
| 131 TimeDelta previous_elapsed_sample_time; | |
| 144 for (int i = 0; i < params_.samples_per_burst; ++i) { | 132 for (int i = 0; i < params_.samples_per_burst; ++i) { |
| 133 if (i != 0) { | |
| 134 // Always wait, even if for 0 seconds, so we can observe a signal on | |
| 135 // stop_event_. | |
| 136 if (stop_event_.TimedWait( | |
| 137 std::max(params_.sampling_interval - previous_elapsed_sample_time, | |
| 138 TimeDelta()))) { | |
| 139 burst_completed = false; | |
| 140 break; | |
| 141 } | |
| 142 } | |
| 145 ElapsedTimer sample_timer; | 143 ElapsedTimer sample_timer; |
| 146 current_profile.samples.push_back(Sample()); | 144 current_profile.samples.push_back(Sample()); |
| 147 native_sampler_->RecordStackSample(¤t_profile.samples.back()); | 145 native_sampler_->RecordStackSample(¤t_profile.samples.back()); |
| 148 TimeDelta elapsed_sample_time = sample_timer.Elapsed(); | 146 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 } | 147 } |
| 158 | 148 |
| 159 *elapsed_time = profile_timer.Elapsed(); | 149 *elapsed_time = profile_timer.Elapsed(); |
| 160 current_profile.profile_duration = *elapsed_time; | 150 current_profile.profile_duration = *elapsed_time; |
| 161 native_sampler_->ProfileRecordingStopped(); | 151 native_sampler_->ProfileRecordingStopped(); |
| 162 | 152 |
| 163 if (!stopped_early) | 153 if (burst_completed) |
| 164 *profile = current_profile; | 154 *profile = current_profile; |
| 165 | 155 |
| 166 return !stopped_early; | 156 return burst_completed; |
| 167 } | 157 } |
| 168 | 158 |
| 159 // In an analogous manner to CollectProfiles and samples exceeding the expected | |
|
Peter Kasting
2015/03/30 23:07:34
Nit: CollectProfile()?
Mike Wittman
2015/03/31 01:06:36
Done.
| |
| 160 // total sampling time, bursts may also exceed the burst_interval. We adopt the | |
| 161 // same wait-and-see approach here. | |
| 169 void StackSamplingProfiler::SamplingThread::CollectProfiles( | 162 void StackSamplingProfiler::SamplingThread::CollectProfiles( |
| 170 std::vector<Profile>* profiles) { | 163 std::vector<Profile>* profiles) { |
| 171 if (stop_event_.TimedWait(params_.initial_delay)) | 164 if (stop_event_.TimedWait(params_.initial_delay)) |
| 172 return; | 165 return; |
| 173 | 166 |
| 167 TimeDelta previous_elapsed_profile_time; | |
| 174 for (int i = 0; i < params_.bursts; ++i) { | 168 for (int i = 0; i < params_.bursts; ++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_.burst_interval - previous_elapsed_profile_time, | |
| 174 TimeDelta()))) | |
| 175 return; | |
| 176 } | |
| 177 | |
| 175 Profile profile; | 178 Profile profile; |
| 176 TimeDelta elapsed_profile_time; | 179 TimeDelta elapsed_profile_time; |
|
Peter Kasting
2015/03/30 23:07:34
This variable is dead.
Mike Wittman
2015/03/31 01:06:36
Removed.
| |
| 177 if (CollectProfile(&profile, &elapsed_profile_time)) | 180 if (!CollectProfile(&profile, &previous_elapsed_profile_time)) |
| 178 profiles->push_back(profile); | |
| 179 else | |
| 180 return; | 181 return; |
| 181 | 182 profiles->push_back(profile); |
| 182 if (stop_event_.TimedWait( | |
| 183 std::max(params_.burst_interval - elapsed_profile_time, | |
| 184 TimeDelta()))) | |
| 185 return; | |
| 186 } | 183 } |
| 187 } | 184 } |
| 188 | 185 |
| 189 void StackSamplingProfiler::SamplingThread::Stop() { | 186 void StackSamplingProfiler::SamplingThread::Stop() { |
| 190 stop_event_.Signal(); | 187 stop_event_.Signal(); |
| 191 } | 188 } |
| 192 | 189 |
| 193 void StackSamplingProfiler::SamplingThreadDeleter::operator()( | 190 // StackSamplingProfiler ------------------------------------------------------ |
| 194 SamplingThread* thread) const { | |
| 195 delete thread; | |
| 196 } | |
| 197 | |
| 198 StackSamplingProfiler::NativeStackSampler::NativeStackSampler() {} | |
| 199 | |
| 200 StackSamplingProfiler::NativeStackSampler::~NativeStackSampler() {} | |
| 201 | 191 |
| 202 StackSamplingProfiler::SamplingParams::SamplingParams() | 192 StackSamplingProfiler::SamplingParams::SamplingParams() |
| 203 : initial_delay(TimeDelta::FromMilliseconds(0)), | 193 : initial_delay(TimeDelta::FromMilliseconds(0)), |
| 204 bursts(1), | 194 bursts(1), |
| 205 burst_interval(TimeDelta::FromMilliseconds(10000)), | 195 burst_interval(TimeDelta::FromMilliseconds(10000)), |
| 206 samples_per_burst(300), | 196 samples_per_burst(300), |
| 207 sampling_interval(TimeDelta::FromMilliseconds(100)), | 197 sampling_interval(TimeDelta::FromMilliseconds(100)), |
| 208 preserve_sample_ordering(false) { | 198 preserve_sample_ordering(false) { |
| 209 } | 199 } |
| 210 | 200 |
| 211 StackSamplingProfiler::StackSamplingProfiler(PlatformThreadId thread_id, | 201 StackSamplingProfiler::StackSamplingProfiler(PlatformThreadId thread_id, |
| 212 const SamplingParams& params) | 202 const SamplingParams& params) |
| 213 : thread_id_(thread_id), params_(params) {} | 203 : thread_id_(thread_id), params_(params) {} |
| 214 | 204 |
| 215 StackSamplingProfiler::~StackSamplingProfiler() {} | 205 StackSamplingProfiler::~StackSamplingProfiler() {} |
| 216 | 206 |
| 217 void StackSamplingProfiler::Start() { | 207 void StackSamplingProfiler::Start() { |
| 218 native_sampler_ = NativeStackSampler::Create(thread_id_); | 208 scoped_ptr<NativeStackSampler> native_sampler = |
| 219 if (!native_sampler_) | 209 NativeStackSampler::Create(thread_id_); |
| 210 if (!native_sampler) | |
| 220 return; | 211 return; |
| 221 | 212 |
| 222 sampling_thread_.reset( | 213 sampling_thread_.reset( |
| 223 new SamplingThread( | 214 new SamplingThread( |
| 224 native_sampler_.Pass(), params_, | 215 native_sampler.Pass(), params_, |
| 225 (custom_completed_callback_.is_null() ? | 216 (custom_completed_callback_.is_null() ? |
| 226 Bind(&PendingProfiles::PutProfiles, | 217 Bind(&PendingProfiles::AppendProfiles, |
| 227 Unretained(PendingProfiles::GetInstance())) : | 218 Unretained(PendingProfiles::GetInstance())) : |
| 228 custom_completed_callback_))); | 219 custom_completed_callback_))); |
| 229 if (!PlatformThread::CreateNonJoinable(0, sampling_thread_.get())) | 220 if (!PlatformThread::CreateNonJoinable(0, sampling_thread_.get())) |
| 230 LOG(ERROR) << "failed to create thread"; | 221 sampling_thread_.reset(); |
| 231 } | 222 } |
| 232 | 223 |
| 233 void StackSamplingProfiler::Stop() { | 224 void StackSamplingProfiler::Stop() { |
| 234 if (sampling_thread_) | 225 if (sampling_thread_) |
| 235 sampling_thread_->Stop(); | 226 sampling_thread_->Stop(); |
| 236 } | 227 } |
| 237 | 228 |
| 238 // static | 229 // static |
| 239 void StackSamplingProfiler::GetPendingProfiles(std::vector<Profile>* profiles) { | 230 void StackSamplingProfiler::GetPendingProfiles(std::vector<Profile>* profiles) { |
| 240 PendingProfiles::GetInstance()->GetProfiles(profiles); | 231 PendingProfiles::GetInstance()->MoveProfiles(profiles); |
| 241 } | 232 } |
| 242 | 233 |
| 243 void StackSamplingProfiler::SetCustomCompletedCallback( | 234 // StackSamplingProfiler::Frame global functions ------------------------------ |
| 244 Callback<void(const std::vector<Profile>&)> callback) { | |
| 245 custom_completed_callback_ = callback; | |
| 246 } | |
| 247 | 235 |
| 248 bool operator==(const StackSamplingProfiler::Frame &a, | 236 bool operator==(const StackSamplingProfiler::Frame &a, |
| 249 const StackSamplingProfiler::Frame &b) { | 237 const StackSamplingProfiler::Frame &b) { |
| 250 return a.instruction_pointer == b.instruction_pointer && | 238 return a.instruction_pointer == b.instruction_pointer && |
| 251 a.module_index == b.module_index; | 239 a.module_index == b.module_index; |
| 252 } | 240 } |
| 253 | 241 |
| 254 bool operator<(const StackSamplingProfiler::Frame &a, | 242 bool operator<(const StackSamplingProfiler::Frame &a, |
| 255 const StackSamplingProfiler::Frame &b) { | 243 const StackSamplingProfiler::Frame &b) { |
| 256 return (a.module_index < b.module_index) || | 244 return (a.module_index < b.module_index) || |
| 257 (a.module_index == b.module_index && | 245 (a.module_index == b.module_index && |
| 258 a.instruction_pointer < b.instruction_pointer); | 246 a.instruction_pointer < b.instruction_pointer); |
| 259 } | 247 } |
| 260 | 248 |
| 261 } // namespace base | 249 } // namespace base |
| 250 | |
| OLD | NEW |