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 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 38 const StackSamplingProfiler::CompletedCallback& callback); | 38 const StackSamplingProfiler::CompletedCallback& callback); |
| 39 | 39 |
| 40 private: | 40 private: |
| 41 AsyncRunner(); | 41 AsyncRunner(); |
| 42 | 42 |
| 43 // Runs the callback and deletes the AsyncRunner instance. | 43 // Runs the callback and deletes the AsyncRunner instance. |
| 44 static void RunCallbackAndDeleteInstance( | 44 static void RunCallbackAndDeleteInstance( |
| 45 std::unique_ptr<AsyncRunner> object_to_be_deleted, | 45 std::unique_ptr<AsyncRunner> object_to_be_deleted, |
| 46 const StackSamplingProfiler::CompletedCallback& callback, | 46 const StackSamplingProfiler::CompletedCallback& callback, |
| 47 scoped_refptr<SingleThreadTaskRunner> task_runner, | 47 scoped_refptr<SingleThreadTaskRunner> task_runner, |
| 48 const StackSamplingProfiler::CallStackProfiles& profiles); | 48 StackSamplingProfiler::CallStackProfiles profiles); |
| 49 | 49 |
| 50 std::unique_ptr<StackSamplingProfiler> profiler_; | 50 std::unique_ptr<StackSamplingProfiler> profiler_; |
| 51 | 51 |
| 52 DISALLOW_COPY_AND_ASSIGN(AsyncRunner); | 52 DISALLOW_COPY_AND_ASSIGN(AsyncRunner); |
| 53 }; | 53 }; |
| 54 | 54 |
| 55 // static | 55 // static |
| 56 void AsyncRunner::Run( | 56 void AsyncRunner::Run( |
| 57 PlatformThreadId thread_id, | 57 PlatformThreadId thread_id, |
| 58 const StackSamplingProfiler::SamplingParams& params, | 58 const StackSamplingProfiler::SamplingParams& params, |
| 59 const StackSamplingProfiler::CompletedCallback &callback) { | 59 const StackSamplingProfiler::CompletedCallback &callback) { |
| 60 std::unique_ptr<AsyncRunner> runner(new AsyncRunner); | 60 std::unique_ptr<AsyncRunner> runner(new AsyncRunner); |
| 61 AsyncRunner* temp_ptr = runner.get(); | 61 AsyncRunner* temp_ptr = runner.get(); |
| 62 temp_ptr->profiler_.reset( | 62 temp_ptr->profiler_.reset( |
| 63 new StackSamplingProfiler(thread_id, params, | 63 new StackSamplingProfiler(thread_id, params, |
| 64 Bind(&AsyncRunner::RunCallbackAndDeleteInstance, | 64 Bind(&AsyncRunner::RunCallbackAndDeleteInstance, |
| 65 Passed(&runner), callback, | 65 Passed(&runner), callback, |
| 66 ThreadTaskRunnerHandle::Get()))); | 66 ThreadTaskRunnerHandle::Get()))); |
| 67 // The callback won't be called until after Start(), so temp_ptr will still | 67 // The callback won't be called until after Start(), so temp_ptr will still |
| 68 // be valid here. | 68 // be valid here. |
| 69 temp_ptr->profiler_->Start(); | 69 temp_ptr->profiler_->Start(); |
| 70 } | 70 } |
| 71 | 71 |
| 72 AsyncRunner::AsyncRunner() {} | 72 AsyncRunner::AsyncRunner() {} |
| 73 | 73 |
| 74 void AsyncRunner::RunCallbackAndDeleteInstance( | 74 void AsyncRunner::RunCallbackAndDeleteInstance( |
| 75 std::unique_ptr<AsyncRunner> object_to_be_deleted, | 75 std::unique_ptr<AsyncRunner> object_to_be_deleted, |
| 76 const StackSamplingProfiler::CompletedCallback& callback, | 76 const StackSamplingProfiler::CompletedCallback& callback, |
| 77 scoped_refptr<SingleThreadTaskRunner> task_runner, | 77 scoped_refptr<SingleThreadTaskRunner> task_runner, |
| 78 const StackSamplingProfiler::CallStackProfiles& profiles) { | 78 StackSamplingProfiler::CallStackProfiles profiles) { |
| 79 callback.Run(profiles); | 79 callback.Run(std::move(profiles)); |
| 80 // Delete the instance on the original calling thread. | 80 // Delete the instance on the original calling thread. |
| 81 task_runner->DeleteSoon(FROM_HERE, object_to_be_deleted.release()); | 81 task_runner->DeleteSoon(FROM_HERE, object_to_be_deleted.release()); |
| 82 } | 82 } |
| 83 | 83 |
| 84 } // namespace | 84 } // namespace |
| 85 | 85 |
| 86 // StackSamplingProfiler::Module ---------------------------------------------- | 86 // StackSamplingProfiler::Module ---------------------------------------------- |
| 87 | 87 |
| 88 StackSamplingProfiler::Module::Module() : base_address(0u) {} | 88 StackSamplingProfiler::Module::Module() : base_address(0u) {} |
| 89 StackSamplingProfiler::Module::Module(uintptr_t base_address, | 89 StackSamplingProfiler::Module::Module(uintptr_t base_address, |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 103 | 103 |
| 104 StackSamplingProfiler::Frame::Frame() | 104 StackSamplingProfiler::Frame::Frame() |
| 105 : instruction_pointer(0), module_index(kUnknownModuleIndex) { | 105 : instruction_pointer(0), module_index(kUnknownModuleIndex) { |
| 106 } | 106 } |
| 107 | 107 |
| 108 // StackSamplingProfiler::CallStackProfile ------------------------------------ | 108 // StackSamplingProfiler::CallStackProfile ------------------------------------ |
| 109 | 109 |
| 110 StackSamplingProfiler::CallStackProfile::CallStackProfile() {} | 110 StackSamplingProfiler::CallStackProfile::CallStackProfile() {} |
| 111 | 111 |
| 112 StackSamplingProfiler::CallStackProfile::CallStackProfile( | 112 StackSamplingProfiler::CallStackProfile::CallStackProfile( |
| 113 const CallStackProfile& other) = default; | 113 const CallStackProfile& other) = default; |
|
Mike Wittman
2016/10/21 18:05:29
nit: function order (this should be last)
bcwhite
2016/10/24 13:17:45
Done.
| |
| 114 | 114 |
| 115 StackSamplingProfiler::CallStackProfile::CallStackProfile( | |
| 116 CallStackProfile&& other) = default; | |
| 117 | |
| 115 StackSamplingProfiler::CallStackProfile::~CallStackProfile() {} | 118 StackSamplingProfiler::CallStackProfile::~CallStackProfile() {} |
| 116 | 119 |
| 120 StackSamplingProfiler::CallStackProfile& | |
| 121 StackSamplingProfiler::CallStackProfile::operator=(CallStackProfile&& other) = | |
| 122 default; | |
| 123 | |
| 124 StackSamplingProfiler::CallStackProfile | |
| 125 StackSamplingProfiler::CallStackProfile::CopyForTesting() const { | |
| 126 return CallStackProfile(*this); | |
| 127 } | |
| 128 | |
| 117 // StackSamplingProfiler::SamplingThread -------------------------------------- | 129 // StackSamplingProfiler::SamplingThread -------------------------------------- |
| 118 | 130 |
| 119 StackSamplingProfiler::SamplingThread::SamplingThread( | 131 StackSamplingProfiler::SamplingThread::SamplingThread( |
| 120 std::unique_ptr<NativeStackSampler> native_sampler, | 132 std::unique_ptr<NativeStackSampler> native_sampler, |
| 121 const SamplingParams& params, | 133 const SamplingParams& params, |
| 122 const CompletedCallback& completed_callback) | 134 const CompletedCallback& completed_callback) |
| 123 : native_sampler_(std::move(native_sampler)), | 135 : native_sampler_(std::move(native_sampler)), |
| 124 params_(params), | 136 params_(params), |
| 125 stop_event_(WaitableEvent::ResetPolicy::AUTOMATIC, | 137 stop_event_(WaitableEvent::ResetPolicy::AUTOMATIC, |
| 126 WaitableEvent::InitialState::NOT_SIGNALED), | 138 WaitableEvent::InitialState::NOT_SIGNALED), |
| 127 completed_callback_(completed_callback) {} | 139 completed_callback_(completed_callback) {} |
| 128 | 140 |
| 129 StackSamplingProfiler::SamplingThread::~SamplingThread() {} | 141 StackSamplingProfiler::SamplingThread::~SamplingThread() {} |
| 130 | 142 |
| 131 void StackSamplingProfiler::SamplingThread::ThreadMain() { | 143 void StackSamplingProfiler::SamplingThread::ThreadMain() { |
| 132 PlatformThread::SetName("Chrome_SamplingProfilerThread"); | 144 PlatformThread::SetName("Chrome_SamplingProfilerThread"); |
| 133 | 145 |
| 134 // For now, just ignore any requests to profile while another profiler is | 146 // For now, just ignore any requests to profile while another profiler is |
| 135 // working. | 147 // working. |
| 136 if (!concurrent_profiling_lock.Get().Try()) | 148 if (!concurrent_profiling_lock.Get().Try()) |
| 137 return; | 149 return; |
| 138 | 150 |
| 139 CallStackProfiles profiles; | 151 CallStackProfiles profiles; |
| 140 CollectProfiles(&profiles); | 152 CollectProfiles(&profiles); |
| 141 concurrent_profiling_lock.Get().Release(); | 153 concurrent_profiling_lock.Get().Release(); |
| 142 completed_callback_.Run(profiles); | 154 completed_callback_.Run(std::move(profiles)); |
| 143 } | 155 } |
| 144 | 156 |
| 145 // Depending on how long the sampling takes and the length of the sampling | 157 // Depending on how long the sampling takes and the length of the sampling |
| 146 // interval, a burst of samples could take arbitrarily longer than | 158 // interval, a burst of samples could take arbitrarily longer than |
| 147 // samples_per_burst * sampling_interval. In this case, we (somewhat | 159 // samples_per_burst * sampling_interval. In this case, we (somewhat |
| 148 // arbitrarily) honor the number of samples requested rather than strictly | 160 // arbitrarily) honor the number of samples requested rather than strictly |
| 149 // adhering to the sampling intervals. Once we have established users for the | 161 // adhering to the sampling intervals. Once we have established users for the |
| 150 // StackSamplingProfiler and the collected data to judge, we may go the other | 162 // StackSamplingProfiler and the collected data to judge, we may go the other |
| 151 // way or make this behavior configurable. | 163 // way or make this behavior configurable. |
| 152 void StackSamplingProfiler::SamplingThread::CollectProfile( | 164 void StackSamplingProfiler::SamplingThread::CollectProfile( |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 196 if (stop_event_.TimedWait( | 208 if (stop_event_.TimedWait( |
| 197 std::max(params_.burst_interval - previous_elapsed_profile_time, | 209 std::max(params_.burst_interval - previous_elapsed_profile_time, |
| 198 TimeDelta()))) | 210 TimeDelta()))) |
| 199 return; | 211 return; |
| 200 } | 212 } |
| 201 | 213 |
| 202 CallStackProfile profile; | 214 CallStackProfile profile; |
| 203 bool was_stopped = false; | 215 bool was_stopped = false; |
| 204 CollectProfile(&profile, &previous_elapsed_profile_time, &was_stopped); | 216 CollectProfile(&profile, &previous_elapsed_profile_time, &was_stopped); |
| 205 if (!profile.samples.empty()) | 217 if (!profile.samples.empty()) |
| 206 profiles->push_back(profile); | 218 profiles->push_back(std::move(profile)); |
| 207 | 219 |
| 208 if (was_stopped) | 220 if (was_stopped) |
| 209 return; | 221 return; |
| 210 } | 222 } |
| 211 } | 223 } |
| 212 | 224 |
| 213 void StackSamplingProfiler::SamplingThread::Stop() { | 225 void StackSamplingProfiler::SamplingThread::Stop() { |
| 214 stop_event_.Signal(); | 226 stop_event_.Signal(); |
| 215 } | 227 } |
| 216 | 228 |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 290 } | 302 } |
| 291 | 303 |
| 292 bool operator<(const StackSamplingProfiler::Frame &a, | 304 bool operator<(const StackSamplingProfiler::Frame &a, |
| 293 const StackSamplingProfiler::Frame &b) { | 305 const StackSamplingProfiler::Frame &b) { |
| 294 return (a.module_index < b.module_index) || | 306 return (a.module_index < b.module_index) || |
| 295 (a.module_index == b.module_index && | 307 (a.module_index == b.module_index && |
| 296 a.instruction_pointer < b.instruction_pointer); | 308 a.instruction_pointer < b.instruction_pointer); |
| 297 } | 309 } |
| 298 | 310 |
| 299 } // namespace base | 311 } // namespace base |
| OLD | NEW |