blob: 7ae592a41fed1a9b2933dee57ea6c72c84d6ca48 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
|
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "services/tracing/public/cpp/traced_process_impl.h"
#include <utility>
#include "base/no_destructor.h"
#include "base/task/task_scheduler/task_scheduler.h"
#include "services/tracing/public/cpp/base_agent.h"
#include "services/tracing/public/cpp/perfetto/producer_client.h"
#include "services/tracing/public/cpp/trace_event_agent.h"
#include "services/tracing/public/mojom/constants.mojom.h"
namespace tracing {
// static
TracedProcessImpl* TracedProcessImpl::GetInstance() {
static base::NoDestructor<TracedProcessImpl> traced_process;
return traced_process.get();
}
TracedProcessImpl::TracedProcessImpl() : binding_(this) {
DETACH_FROM_SEQUENCE(sequence_checker_);
}
TracedProcessImpl::~TracedProcessImpl() = default;
// OnTracedProcessRequest can be called concurrently from
// multiple threads, as we get one call per service.
void TracedProcessImpl::OnTracedProcessRequest(
mojom::TracedProcessRequest request) {
if (task_runner_ && !task_runner_->RunsTasksInCurrentSequence()) {
task_runner_->PostTask(
FROM_HERE, base::BindOnce(&TracedProcessImpl::OnTracedProcessRequest,
base::Unretained(this), std::move(request)));
return;
}
// We only need one binding per process.
base::AutoLock lock(lock_);
if (binding_.is_bound()) {
return;
}
binding_.Bind(std::move(request));
}
// SetTaskRunner must be called before we start receiving
// any OnTracedProcessRequest calls.
void TracedProcessImpl::SetTaskRunner(
scoped_refptr<base::SequencedTaskRunner> task_runner) {
DCHECK(!binding_.is_bound());
DCHECK(!task_runner_);
task_runner_ = task_runner;
}
void TracedProcessImpl::RegisterAgent(BaseAgent* agent) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (agent_registry_) {
agent->Connect(agent_registry_.get());
}
agents_.insert(agent);
}
void TracedProcessImpl::UnregisterAgent(BaseAgent* agent) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
agents_.erase(agent);
}
void TracedProcessImpl::ConnectToTracingService(
mojom::ConnectToTracingRequestPtr request) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
// Tracing requires a running TaskScheduler; disable tracing
// for processes without it.
if (!base::TaskScheduler::GetInstance()) {
return;
}
// Ensure the TraceEventAgent has been created.
TraceEventAgent::GetInstance();
agent_registry_ =
tracing::mojom::AgentRegistryPtr(std::move(request->agent_registry));
agent_registry_.set_connection_error_handler(base::BindRepeating(
[](TracedProcessImpl* traced_process) {
// If the AgentRegistryPtr connection closes, the tracing service
// has gone down and we'll start accepting new connections from it
// again.
base::AutoLock lock(traced_process->lock_);
traced_process->agent_registry_.reset();
traced_process->binding_.Close();
},
base::Unretained(this)));
for (auto* agent : agents_) {
agent->Connect(agent_registry_.get());
}
ProducerClient::Get()->Connect(
tracing::mojom::PerfettoServicePtr(std::move(request->perfetto_service)));
}
void TracedProcessImpl::GetCategories(std::set<std::string>* category_set) {
for (auto* agent : agents_) {
agent->GetCategories(category_set);
}
}
} // namespace tracing
|