| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "components/metrics/child_call_stack_profile_collector.h" | 5 #include "components/metrics/child_call_stack_profile_collector.h" |
| 6 | 6 |
| 7 #include <utility> |
| 8 |
| 7 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/bind_helpers.h" |
| 8 #include "base/logging.h" | 11 #include "base/logging.h" |
| 9 #include "base/synchronization/lock.h" | 12 #include "base/synchronization/lock.h" |
| 10 #include "base/threading/thread_task_runner_handle.h" | 13 #include "base/threading/thread_task_runner_handle.h" |
| 11 #include "services/shell/public/cpp/interface_provider.h" | 14 #include "services/shell/public/cpp/interface_provider.h" |
| 12 | 15 |
| 13 namespace metrics { | 16 namespace metrics { |
| 14 | 17 |
| 15 ChildCallStackProfileCollector::ProfilesState::ProfilesState() = default; | 18 ChildCallStackProfileCollector::ProfilesState::ProfilesState() = default; |
| 16 ChildCallStackProfileCollector::ProfilesState::ProfilesState( | 19 ChildCallStackProfileCollector::ProfilesState::ProfilesState(ProfilesState&&) = |
| 17 const ProfilesState&) = default; | 20 default; |
| 18 | 21 |
| 19 ChildCallStackProfileCollector::ProfilesState::ProfilesState( | 22 ChildCallStackProfileCollector::ProfilesState::ProfilesState( |
| 20 const CallStackProfileParams& params, | 23 const CallStackProfileParams& params, |
| 21 base::TimeTicks start_timestamp, | 24 base::TimeTicks start_timestamp, |
| 22 const base::StackSamplingProfiler::CallStackProfiles& profiles) | 25 base::StackSamplingProfiler::CallStackProfiles profiles) |
| 23 : params(params), start_timestamp(start_timestamp), profiles(profiles) {} | 26 : params(params), |
| 27 start_timestamp(start_timestamp), |
| 28 profiles(std::move(profiles)) {} |
| 24 | 29 |
| 25 ChildCallStackProfileCollector::ProfilesState::~ProfilesState() = default; | 30 ChildCallStackProfileCollector::ProfilesState::~ProfilesState() = default; |
| 26 | 31 |
| 32 // Some versions of GCC need this for push_back to work with std::move. |
| 33 ChildCallStackProfileCollector::ProfilesState& |
| 34 ChildCallStackProfileCollector::ProfilesState::operator=(ProfilesState&&) = |
| 35 default; |
| 36 |
| 27 ChildCallStackProfileCollector::ChildCallStackProfileCollector() {} | 37 ChildCallStackProfileCollector::ChildCallStackProfileCollector() {} |
| 28 | 38 |
| 29 ChildCallStackProfileCollector::~ChildCallStackProfileCollector() {} | 39 ChildCallStackProfileCollector::~ChildCallStackProfileCollector() {} |
| 30 | 40 |
| 31 base::StackSamplingProfiler::CompletedCallback | 41 base::StackSamplingProfiler::CompletedCallback |
| 32 ChildCallStackProfileCollector::GetProfilerCallback( | 42 ChildCallStackProfileCollector::GetProfilerCallback( |
| 33 const CallStackProfileParams& params) { | 43 const CallStackProfileParams& params) { |
| 34 return base::Bind(&ChildCallStackProfileCollector::Collect, | 44 return base::Bind(&ChildCallStackProfileCollector::Collect, |
| 35 // This class has lazy instance lifetime. | 45 // This class has lazy instance lifetime. |
| 36 base::Unretained(this), params, | 46 base::Unretained(this), params, |
| 37 base::TimeTicks::Now()); | 47 base::TimeTicks::Now()); |
| 38 } | 48 } |
| 39 | 49 |
| 40 void ChildCallStackProfileCollector::SetParentProfileCollector( | 50 void ChildCallStackProfileCollector::SetParentProfileCollector( |
| 41 metrics::mojom::CallStackProfileCollectorPtr parent_collector) { | 51 metrics::mojom::CallStackProfileCollectorPtr parent_collector) { |
| 42 base::AutoLock alock(lock_); | 52 base::AutoLock alock(lock_); |
| 43 // This function should only invoked once, during the mode of operation when | 53 // This function should only invoked once, during the mode of operation when |
| 44 // retaining profiles after construction. | 54 // retaining profiles after construction. |
| 45 DCHECK(retain_profiles_); | 55 DCHECK(retain_profiles_); |
| 46 retain_profiles_ = false; | 56 retain_profiles_ = false; |
| 47 task_runner_ = base::ThreadTaskRunnerHandle::Get(); | 57 task_runner_ = base::ThreadTaskRunnerHandle::Get(); |
| 48 parent_collector_ = std::move(parent_collector); | 58 parent_collector_ = std::move(parent_collector); |
| 49 if (parent_collector_) { | 59 if (parent_collector_) { |
| 50 for (const ProfilesState& state : profiles_) { | 60 for (ProfilesState& state : profiles_) { |
| 51 parent_collector_->Collect(state.params, state.start_timestamp, | 61 parent_collector_->Collect(state.params, state.start_timestamp, |
| 52 state.profiles); | 62 std::move(state.profiles)); |
| 53 } | 63 } |
| 54 } | 64 } |
| 55 profiles_.clear(); | 65 profiles_.clear(); |
| 56 } | 66 } |
| 57 | 67 |
| 58 void ChildCallStackProfileCollector::Collect( | 68 void ChildCallStackProfileCollector::Collect( |
| 59 const CallStackProfileParams& params, | 69 const CallStackProfileParams& params, |
| 60 base::TimeTicks start_timestamp, | 70 base::TimeTicks start_timestamp, |
| 61 const std::vector<CallStackProfile>& profiles) { | 71 std::vector<CallStackProfile> profiles) { |
| 62 base::AutoLock alock(lock_); | 72 base::AutoLock alock(lock_); |
| 63 if (task_runner_ && | 73 if (task_runner_ && |
| 64 // The profiler thread does not have a task runner. Attempting to | 74 // The profiler thread does not have a task runner. Attempting to |
| 65 // invoke Get() on it results in a DCHECK. | 75 // invoke Get() on it results in a DCHECK. |
| 66 (!base::ThreadTaskRunnerHandle::IsSet() || | 76 (!base::ThreadTaskRunnerHandle::IsSet() || |
| 67 base::ThreadTaskRunnerHandle::Get() != task_runner_)) { | 77 base::ThreadTaskRunnerHandle::Get() != task_runner_)) { |
| 68 // Post back to the thread that owns the the parent interface. | 78 // Post back to the thread that owns the the parent interface. |
| 69 task_runner_->PostTask(FROM_HERE, base::Bind( | 79 task_runner_->PostTask(FROM_HERE, base::Bind( |
| 70 &ChildCallStackProfileCollector::Collect, | 80 &ChildCallStackProfileCollector::Collect, |
| 71 // This class has lazy instance lifetime. | 81 // This class has lazy instance lifetime. |
| 72 base::Unretained(this), | 82 base::Unretained(this), |
| 73 params, | 83 params, |
| 74 start_timestamp, | 84 start_timestamp, |
| 75 profiles)); | 85 base::Passed(std::move(profiles)))); |
| 76 return; | 86 return; |
| 77 } | 87 } |
| 78 | 88 |
| 79 if (parent_collector_) { | 89 if (parent_collector_) { |
| 80 parent_collector_->Collect(params, start_timestamp, profiles); | 90 parent_collector_->Collect(params, start_timestamp, std::move(profiles)); |
| 81 } else if (retain_profiles_) { | 91 } else if (retain_profiles_) { |
| 82 profiles_.push_back(ProfilesState(params, start_timestamp, profiles)); | 92 profiles_.push_back( |
| 93 ProfilesState(params, start_timestamp, std::move(profiles))); |
| 83 } | 94 } |
| 84 } | 95 } |
| 85 | 96 |
| 86 } // namespace metrics | 97 } // namespace metrics |
| OLD | NEW |