1 // Copyright 2016 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "components/metrics/child_call_stack_profile_collector.h"
9 #include "base/check.h"
10 #include "base/functional/bind.h"
11 #include "base/synchronization/lock.h"
12 #include "base/task/single_thread_task_runner.h"
13 #include "base/time/time.h"
14 #include "third_party/metrics_proto/sampled_profile.pb.h"
18 ChildCallStackProfileCollector::ProfileState::ProfileState() = default;
19 ChildCallStackProfileCollector::ProfileState::ProfileState(ProfileState&&) =
22 ChildCallStackProfileCollector::ProfileState::ProfileState(
23 base::TimeTicks start_timestamp,
24 mojom::ProfileType profile_type,
25 std::string&& profile)
26 : start_timestamp(start_timestamp),
27 profile_type(profile_type),
28 profile(std::move(profile)) {}
30 ChildCallStackProfileCollector::ProfileState::~ProfileState() = default;
32 // Some versions of GCC need this for push_back to work with std::move.
33 ChildCallStackProfileCollector::ProfileState&
34 ChildCallStackProfileCollector::ProfileState::operator=(ProfileState&&) =
37 ChildCallStackProfileCollector::ChildCallStackProfileCollector() {}
39 ChildCallStackProfileCollector::~ChildCallStackProfileCollector() {}
41 void ChildCallStackProfileCollector::SetParentProfileCollector(
42 mojo::PendingRemote<metrics::mojom::CallStackProfileCollector>
44 base::AutoLock alock(lock_);
45 // This function should only invoked once, during the mode of operation when
46 // retaining profiles after construction.
47 DCHECK(retain_profiles_);
48 retain_profiles_ = false;
49 task_runner_ = base::SingleThreadTaskRunner::GetCurrentDefault();
50 // This should only be set one time per child process.
51 DCHECK(!parent_collector_);
52 // If |parent_collector| is mojo::NullRemote(), it skips Bind since
53 // mojo::Remote doesn't allow Bind with mojo::NullRemote().
54 if (parent_collector) {
55 parent_collector_.Bind(std::move(parent_collector));
56 if (parent_collector_) {
57 for (ProfileState& state : profiles_) {
58 mojom::SampledProfilePtr mojo_profile = mojom::SampledProfile::New();
59 mojo_profile->contents = std::move(state.profile);
60 parent_collector_->Collect(state.start_timestamp, state.profile_type,
61 std::move(mojo_profile));
68 void ChildCallStackProfileCollector::Collect(base::TimeTicks start_timestamp,
69 SampledProfile profile) {
70 base::AutoLock alock(lock_);
72 // The profiler thread does not have a task runner. Attempting to
73 // invoke Get() on it results in a DCHECK.
74 (!base::SingleThreadTaskRunner::HasCurrentDefault() ||
75 base::SingleThreadTaskRunner::GetCurrentDefault() != task_runner_)) {
76 // Post back to the thread that owns the the parent interface.
77 task_runner_->PostTask(
78 FROM_HERE, base::BindOnce(&ChildCallStackProfileCollector::Collect,
79 // This class has lazy instance lifetime.
80 base::Unretained(this), start_timestamp,
85 const mojom::ProfileType profile_type =
86 profile.trigger_event() == SampledProfile::PERIODIC_HEAP_COLLECTION
87 ? mojom::ProfileType::kHeap
88 : mojom::ProfileType::kCPU;
90 if (parent_collector_) {
91 mojom::SampledProfilePtr mojo_profile = mojom::SampledProfile::New();
92 profile.SerializeToString(&mojo_profile->contents);
93 parent_collector_->Collect(start_timestamp, profile_type,
94 std::move(mojo_profile));
98 if (retain_profiles_) {
99 std::string serialized_profile;
100 profile.SerializeToString(&serialized_profile);
101 profiles_.emplace_back(start_timestamp, profile_type,
102 std::move(serialized_profile));
106 } // namespace metrics