Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "base/profiler/stack_sampling_profiler.h" | |
| 6 | |
| 7 #include <algorithm> | |
| 8 | |
| 9 #include "base/bind.h" | |
| 10 #include "base/callback.h" | |
| 11 #include "base/memory/singleton.h" | |
| 12 #include "base/synchronization/lock.h" | |
| 13 #include "base/synchronization/waitable_event.h" | |
| 14 #include "base/timer/elapsed_timer.h" | |
| 15 | |
| 16 template <typename T> struct DefaultSingletonTraits; | |
| 17 | |
| 18 namespace base { | |
| 19 | |
| 20 namespace { | |
| 21 | |
| 22 // Thread-safe singleton class that stores collected profiles waiting to be | |
| 23 // processed. | |
| 24 class PendingProfiles { | |
| 25 public: | |
| 26 PendingProfiles(); | |
| 27 ~PendingProfiles(); | |
| 28 | |
| 29 static PendingProfiles* GetInstance(); | |
| 30 | |
| 31 // Appends |profiles|. This function is thread safe. | |
| 32 void PutProfiles(const std::vector<StackSamplingProfiler::Profile>& profiles); | |
| 33 // Gets the pending profiles into *|profiles|. This function is thread safe. | |
| 34 void GetProfiles(std::vector<StackSamplingProfiler::Profile>* profiles); | |
| 35 | |
| 36 private: | |
| 37 Lock profiles_lock_; | |
| 38 std::vector<StackSamplingProfiler::Profile> profiles_; | |
| 39 | |
| 40 DISALLOW_COPY_AND_ASSIGN(PendingProfiles); | |
| 41 }; | |
| 42 | |
| 43 PendingProfiles::PendingProfiles() {} | |
| 44 | |
| 45 PendingProfiles::~PendingProfiles() {} | |
| 46 | |
| 47 // static | |
| 48 PendingProfiles* PendingProfiles::GetInstance() { | |
| 49 return Singleton<PendingProfiles>::get(); | |
| 50 } | |
| 51 | |
| 52 void PendingProfiles::PutProfiles( | |
| 53 const std::vector<StackSamplingProfiler::Profile>& profiles) { | |
| 54 AutoLock scoped_lock(profiles_lock_); | |
| 55 profiles_.insert(profiles_.end(), profiles.begin(), profiles.end()); | |
| 56 } | |
| 57 | |
| 58 void PendingProfiles::GetProfiles( | |
| 59 std::vector<StackSamplingProfiler::Profile>* profiles) { | |
| 60 profiles->clear(); | |
| 61 | |
| 62 AutoLock scoped_lock(profiles_lock_); | |
| 63 profiles_.swap(*profiles); | |
| 64 } | |
| 65 } // namespace | |
| 66 | |
| 67 StackSamplingProfiler::Module::Module() : base_address(nullptr) {} | |
| 68 | |
| 69 StackSamplingProfiler::Module::~Module() {} | |
| 70 | |
| 71 StackSamplingProfiler::Frame::Frame() | |
| 72 : instruction_pointer(nullptr), | |
| 73 module_index(-1) {} | |
| 74 | |
| 75 StackSamplingProfiler::Frame::~Frame() {} | |
| 76 | |
| 77 StackSamplingProfiler::Profile::Profile() : preserve_sample_ordering(false) {} | |
| 78 | |
| 79 StackSamplingProfiler::Profile::~Profile() {} | |
| 80 | |
| 81 class StackSamplingProfiler::SamplingThread : public PlatformThread::Delegate { | |
| 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 | |
| 116 StackSamplingProfiler::SamplingThread::SamplingThread( | |
| 117 scoped_ptr<NativeStackSampler> native_sampler, | |
| 118 const SamplingParams& params, | |
| 119 Callback<void(const std::vector<Profile>&)> completed_callback) | |
| 120 : native_sampler_(native_sampler.Pass()), | |
| 121 params_(params), | |
| 122 stop_event_(false, false), | |
| 123 completed_callback_(completed_callback) { | |
| 124 } | |
| 125 | |
| 126 StackSamplingProfiler::SamplingThread::~SamplingThread() {} | |
| 127 | |
| 128 void StackSamplingProfiler::SamplingThread::ThreadMain() { | |
| 129 PlatformThread::SetName("Chrome_SamplingProfilerThread"); | |
| 130 | |
| 131 std::vector<Profile> profiles; | |
| 132 CollectProfiles(&profiles); | |
| 133 completed_callback_.Run(profiles); | |
| 134 } | |
| 135 | |
| 136 bool StackSamplingProfiler::SamplingThread::CollectProfile( | |
| 137 Profile* profile, | |
| 138 TimeDelta* elapsed_time) { | |
| 139 ElapsedTimer profile_timer; | |
| 140 Profile current_profile; | |
| 141 native_sampler_->ProfileRecordingStarting(¤t_profile); | |
| 142 current_profile.sampling_period = params_.sampling_interval; | |
| 143 bool stopped_early = false; | |
| 144 for (int i = 0; i < params_.samples_per_burst; ++i) { | |
| 145 ElapsedTimer sample_timer; | |
| 146 current_profile.samples.push_back(Sample()); | |
| 147 native_sampler_->RecordStackSample(¤t_profile.samples.back()); | |
| 148 TimeDelta 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 } | |
| 158 | |
| 159 *elapsed_time = profile_timer.Elapsed(); | |
| 160 current_profile.profile_duration = *elapsed_time; | |
| 161 native_sampler_->ProfileRecordingStopped(); | |
| 162 | |
| 163 if (!stopped_early) | |
| 164 *profile = current_profile; | |
| 165 | |
| 166 return !stopped_early; | |
| 167 } | |
| 168 | |
| 169 void StackSamplingProfiler::SamplingThread::CollectProfiles( | |
| 170 std::vector<Profile>* profiles) { | |
| 171 if (stop_event_.TimedWait(params_.initial_delay)) | |
| 172 return; | |
| 173 | |
| 174 for (int i = 0; i < params_.bursts; ++i) { | |
| 175 Profile profile; | |
| 176 TimeDelta elapsed_profile_time; | |
| 177 if (CollectProfile(&profile, &elapsed_profile_time)) | |
| 178 profiles->push_back(profile); | |
| 179 else | |
| 180 return; | |
| 181 | |
| 182 if (stop_event_.TimedWait( | |
| 183 std::max(params_.burst_interval - elapsed_profile_time, | |
| 184 TimeDelta()))) | |
| 185 return; | |
| 186 } | |
| 187 } | |
| 188 | |
| 189 void StackSamplingProfiler::SamplingThread::Stop() { | |
| 190 stop_event_.Signal(); | |
| 191 } | |
| 192 | |
| 193 void StackSamplingProfiler::SamplingThreadDeleter::operator()( | |
| 194 SamplingThread* thread) const { | |
| 195 delete thread; | |
|
cpu_(ooo_6.6-7.5)
2015/03/19 02:49:04
why do we need a special deleter?
Mike Wittman
2015/03/19 20:27:16
To keep the definition of SamplingThread out of th
| |
| 196 } | |
| 197 | |
| 198 StackSamplingProfiler::NativeStackSampler::NativeStackSampler() {} | |
| 199 | |
| 200 StackSamplingProfiler::NativeStackSampler::~NativeStackSampler() {} | |
| 201 | |
| 202 StackSamplingProfiler::SamplingParams::SamplingParams() | |
| 203 : initial_delay(TimeDelta::FromMilliseconds(0)), bursts(1), | |
|
cpu_(ooo_6.6-7.5)
2015/03/19 02:49:04
bursts() in next line
Mike Wittman
2015/03/19 20:27:17
Done.
| |
| 204 burst_interval(TimeDelta::FromMilliseconds(10000)), | |
| 205 samples_per_burst(300), | |
| 206 sampling_interval(TimeDelta::FromMilliseconds(100)), | |
| 207 preserve_sample_ordering(false) { | |
| 208 } | |
| 209 | |
| 210 StackSamplingProfiler::StackSamplingProfiler(PlatformThreadId thread_id, | |
| 211 const SamplingParams& params) | |
| 212 : thread_id_(thread_id), params_(params) {} | |
| 213 | |
| 214 StackSamplingProfiler::~StackSamplingProfiler() {} | |
| 215 | |
| 216 void StackSamplingProfiler::Start() { | |
| 217 native_sampler_ = NativeStackSampler::Create(thread_id_); | |
| 218 if (!native_sampler_) | |
| 219 return; | |
| 220 | |
| 221 sampling_thread_.reset( | |
| 222 new SamplingThread( | |
| 223 native_sampler_.Pass(), params_, | |
| 224 (custom_completed_callback_.is_null() ? | |
| 225 Bind(&PendingProfiles::PutProfiles, | |
| 226 Unretained(PendingProfiles::GetInstance())) : | |
| 227 custom_completed_callback_))); | |
| 228 if (!PlatformThread::CreateNonJoinable(0, sampling_thread_.get())) | |
| 229 LOG(ERROR) << "failed to create thread"; | |
| 230 } | |
| 231 | |
| 232 void StackSamplingProfiler::Stop() { | |
| 233 if (sampling_thread_) | |
| 234 sampling_thread_->Stop(); | |
| 235 } | |
| 236 | |
| 237 // static | |
| 238 void StackSamplingProfiler::GetPendingProfiles(std::vector<Profile>* profiles) { | |
| 239 PendingProfiles::GetInstance()->GetProfiles(profiles); | |
| 240 } | |
| 241 | |
| 242 void StackSamplingProfiler::SetCustomCompletedCallback( | |
| 243 Callback<void(const std::vector<Profile>&)> callback) { | |
| 244 custom_completed_callback_ = callback; | |
| 245 } | |
| 246 | |
| 247 bool operator==(const StackSamplingProfiler::Frame &a, | |
| 248 const StackSamplingProfiler::Frame &b) { | |
| 249 return a.instruction_pointer == b.instruction_pointer && | |
| 250 a.module_index == b.module_index; | |
| 251 } | |
| 252 | |
| 253 bool operator<(const StackSamplingProfiler::Frame &a, | |
| 254 const StackSamplingProfiler::Frame &b) { | |
| 255 return (a.module_index < b.module_index) || | |
| 256 (a.module_index == b.module_index && | |
| 257 a.instruction_pointer < b.instruction_pointer); | |
| 258 } | |
| 259 | |
| 260 } // namespace base | |
| OLD | NEW |