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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
|
// Copyright (c) 2012 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 "components/tracing/child_trace_message_filter.h"
#include "base/metrics/statistics_recorder.h"
#include "base/trace_event/trace_event.h"
#include "components/tracing/child_memory_dump_manager_delegate_impl.h"
#include "components/tracing/tracing_messages.h"
#include "ipc/ipc_channel.h"
using base::trace_event::TraceLog;
namespace tracing {
namespace {
const int kMinTimeBetweenHistogramChangesInSeconds = 10;
} // namespace
ChildTraceMessageFilter::ChildTraceMessageFilter(
base::SingleThreadTaskRunner* ipc_task_runner)
: sender_(NULL),
ipc_task_runner_(ipc_task_runner),
pending_memory_dump_guid_(0) {
}
void ChildTraceMessageFilter::OnFilterAdded(IPC::Sender* sender) {
sender_ = sender;
sender_->Send(new TracingHostMsg_ChildSupportsTracing());
ChildMemoryDumpManagerDelegateImpl::GetInstance()->SetChildTraceMessageFilter(
this);
}
void ChildTraceMessageFilter::OnFilterRemoved() {
ChildMemoryDumpManagerDelegateImpl::GetInstance()->SetChildTraceMessageFilter(
nullptr);
sender_ = NULL;
}
bool ChildTraceMessageFilter::OnMessageReceived(const IPC::Message& message) {
bool handled = true;
IPC_BEGIN_MESSAGE_MAP(ChildTraceMessageFilter, message)
IPC_MESSAGE_HANDLER(TracingMsg_BeginTracing, OnBeginTracing)
IPC_MESSAGE_HANDLER(TracingMsg_EndTracing, OnEndTracing)
IPC_MESSAGE_HANDLER(TracingMsg_CancelTracing, OnCancelTracing)
IPC_MESSAGE_HANDLER(TracingMsg_EnableMonitoring, OnEnableMonitoring)
IPC_MESSAGE_HANDLER(TracingMsg_DisableMonitoring, OnDisableMonitoring)
IPC_MESSAGE_HANDLER(TracingMsg_CaptureMonitoringSnapshot,
OnCaptureMonitoringSnapshot)
IPC_MESSAGE_HANDLER(TracingMsg_GetTraceLogStatus, OnGetTraceLogStatus)
IPC_MESSAGE_HANDLER(TracingMsg_SetWatchEvent, OnSetWatchEvent)
IPC_MESSAGE_HANDLER(TracingMsg_CancelWatchEvent, OnCancelWatchEvent)
IPC_MESSAGE_HANDLER(TracingMsg_ProcessMemoryDumpRequest,
OnProcessMemoryDumpRequest)
IPC_MESSAGE_HANDLER(TracingMsg_GlobalMemoryDumpResponse,
OnGlobalMemoryDumpResponse)
IPC_MESSAGE_HANDLER(TracingMsg_SetUMACallback, OnSetUMACallback)
IPC_MESSAGE_HANDLER(TracingMsg_ClearUMACallback, OnClearUMACallback)
IPC_MESSAGE_UNHANDLED(handled = false)
IPC_END_MESSAGE_MAP()
return handled;
}
ChildTraceMessageFilter::~ChildTraceMessageFilter() {}
void ChildTraceMessageFilter::OnBeginTracing(
const std::string& trace_config_str,
base::TraceTicks browser_time,
uint64 tracing_process_id) {
#if defined(__native_client__)
// NaCl and system times are offset by a bit, so subtract some time from
// the captured timestamps. The value might be off by a bit due to messaging
// latency.
base::TimeDelta time_offset = base::TraceTicks::Now() - browser_time;
TraceLog::GetInstance()->SetTimeOffset(time_offset);
#endif
ChildMemoryDumpManagerDelegateImpl::GetInstance()->set_tracing_process_id(
tracing_process_id);
TraceLog::GetInstance()->SetEnabled(
base::trace_event::TraceConfig(trace_config_str),
base::trace_event::TraceLog::RECORDING_MODE);
}
void ChildTraceMessageFilter::OnEndTracing() {
TraceLog::GetInstance()->SetDisabled();
// Flush will generate one or more callbacks to OnTraceDataCollected
// synchronously or asynchronously. EndTracingAck will be sent in the last
// OnTraceDataCollected. We are already on the IO thread, so the
// OnTraceDataCollected calls will not be deferred.
TraceLog::GetInstance()->Flush(
base::Bind(&ChildTraceMessageFilter::OnTraceDataCollected, this));
ChildMemoryDumpManagerDelegateImpl::GetInstance()->set_tracing_process_id(
base::trace_event::MemoryDumpManager::kInvalidTracingProcessId);
}
void ChildTraceMessageFilter::OnCancelTracing() {
TraceLog::GetInstance()->CancelTracing(
base::Bind(&ChildTraceMessageFilter::OnTraceDataCollected, this));
}
void ChildTraceMessageFilter::OnEnableMonitoring(
const std::string& trace_config_str,
base::TraceTicks browser_time) {
TraceLog::GetInstance()->SetEnabled(
base::trace_event::TraceConfig(trace_config_str),
base::trace_event::TraceLog::MONITORING_MODE);
}
void ChildTraceMessageFilter::OnDisableMonitoring() {
TraceLog::GetInstance()->SetDisabled();
}
void ChildTraceMessageFilter::OnCaptureMonitoringSnapshot() {
// Flush will generate one or more callbacks to
// OnMonitoringTraceDataCollected. It's important that the last
// OnMonitoringTraceDataCollected gets called before
// CaptureMonitoringSnapshotAck below. We are already on the IO thread,
// so the OnMonitoringTraceDataCollected calls will not be deferred.
TraceLog::GetInstance()->FlushButLeaveBufferIntact(
base::Bind(&ChildTraceMessageFilter::OnMonitoringTraceDataCollected,
this));
}
void ChildTraceMessageFilter::OnGetTraceLogStatus() {
sender_->Send(new TracingHostMsg_TraceLogStatusReply(
TraceLog::GetInstance()->GetStatus()));
}
void ChildTraceMessageFilter::OnSetWatchEvent(const std::string& category_name,
const std::string& event_name) {
TraceLog::GetInstance()->SetWatchEvent(
category_name, event_name,
base::Bind(&ChildTraceMessageFilter::OnWatchEventMatched, this));
}
void ChildTraceMessageFilter::OnCancelWatchEvent() {
TraceLog::GetInstance()->CancelWatchEvent();
}
void ChildTraceMessageFilter::OnWatchEventMatched() {
if (!ipc_task_runner_->BelongsToCurrentThread()) {
ipc_task_runner_->PostTask(
FROM_HERE,
base::Bind(&ChildTraceMessageFilter::OnWatchEventMatched, this));
return;
}
sender_->Send(new TracingHostMsg_WatchEventMatched);
}
void ChildTraceMessageFilter::OnTraceDataCollected(
const scoped_refptr<base::RefCountedString>& events_str_ptr,
bool has_more_events) {
if (!ipc_task_runner_->BelongsToCurrentThread()) {
ipc_task_runner_->PostTask(
FROM_HERE, base::Bind(&ChildTraceMessageFilter::OnTraceDataCollected,
this, events_str_ptr, has_more_events));
return;
}
if (events_str_ptr->data().size()) {
sender_->Send(new TracingHostMsg_TraceDataCollected(
events_str_ptr->data()));
}
if (!has_more_events) {
std::vector<std::string> category_groups;
TraceLog::GetInstance()->GetKnownCategoryGroups(&category_groups);
sender_->Send(new TracingHostMsg_EndTracingAck(category_groups));
}
}
void ChildTraceMessageFilter::OnMonitoringTraceDataCollected(
const scoped_refptr<base::RefCountedString>& events_str_ptr,
bool has_more_events) {
if (!ipc_task_runner_->BelongsToCurrentThread()) {
ipc_task_runner_->PostTask(
FROM_HERE,
base::Bind(&ChildTraceMessageFilter::OnMonitoringTraceDataCollected,
this, events_str_ptr, has_more_events));
return;
}
sender_->Send(new TracingHostMsg_MonitoringTraceDataCollected(
events_str_ptr->data()));
if (!has_more_events)
sender_->Send(new TracingHostMsg_CaptureMonitoringSnapshotAck());
}
// Sent by the Browser's MemoryDumpManager when coordinating a global dump.
void ChildTraceMessageFilter::OnProcessMemoryDumpRequest(
const base::trace_event::MemoryDumpRequestArgs& args) {
ChildMemoryDumpManagerDelegateImpl::GetInstance()->CreateProcessDump(
args,
base::Bind(&ChildTraceMessageFilter::OnProcessMemoryDumpDone, this));
}
void ChildTraceMessageFilter::OnProcessMemoryDumpDone(uint64 dump_guid,
bool success) {
sender_->Send(
new TracingHostMsg_ProcessMemoryDumpResponse(dump_guid, success));
}
// Initiates a dump request, asking the Browser's MemoryDumpManager to
// coordinate a global memory dump. The Browser's MDM will answer back with a
// MemoryDumpResponse when all the child processes (including this one) have
// dumped, or with a NACK (|success| == false) if the dump failed (e.g., due to
// a collision with a concurrent request from another child process).
void ChildTraceMessageFilter::SendGlobalMemoryDumpRequest(
const base::trace_event::MemoryDumpRequestArgs& args,
const base::trace_event::MemoryDumpCallback& callback) {
// If there is already another dump request pending from this child process,
// there is no point bothering the Browser's MemoryDumpManager.
if (pending_memory_dump_guid_) {
if (!callback.is_null())
callback.Run(args.dump_guid, false);
return;
}
pending_memory_dump_guid_ = args.dump_guid;
pending_memory_dump_callback_ = callback;
sender_->Send(new TracingHostMsg_GlobalMemoryDumpRequest(args));
}
// Sent by the Browser's MemoryDumpManager in response of a dump request
// initiated by this child process.
void ChildTraceMessageFilter::OnGlobalMemoryDumpResponse(uint64 dump_guid,
bool success) {
DCHECK_NE(0U, pending_memory_dump_guid_);
pending_memory_dump_guid_ = 0;
if (pending_memory_dump_callback_.is_null())
return;
pending_memory_dump_callback_.Run(dump_guid, success);
}
void ChildTraceMessageFilter::OnHistogramChanged(
const std::string& histogram_name,
base::Histogram::Sample reference_value,
base::Histogram::Sample actual_value) {
if (actual_value < reference_value)
return;
ipc_task_runner_->PostTask(
FROM_HERE, base::Bind(&ChildTraceMessageFilter::SendTriggerMessage, this,
histogram_name));
}
void ChildTraceMessageFilter::SendTriggerMessage(
const std::string& histogram_name) {
if (!histogram_last_changed_.is_null()) {
base::Time computed_next_allowed_time =
histogram_last_changed_ +
base::TimeDelta::FromSeconds(kMinTimeBetweenHistogramChangesInSeconds);
if (computed_next_allowed_time > base::Time::Now())
return;
}
histogram_last_changed_ = base::Time::Now();
if (sender_)
sender_->Send(new TracingHostMsg_TriggerBackgroundTrace(histogram_name));
}
void ChildTraceMessageFilter::OnSetUMACallback(
const std::string& histogram_name,
int histogram_value) {
histogram_last_changed_ = base::Time();
base::StatisticsRecorder::SetCallback(
histogram_name, base::Bind(&ChildTraceMessageFilter::OnHistogramChanged,
this, histogram_name, histogram_value));
}
void ChildTraceMessageFilter::OnClearUMACallback(
const std::string& histogram_name) {
histogram_last_changed_ = base::Time();
base::StatisticsRecorder::ClearCallback(histogram_name);
}
} // namespace tracing
|