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