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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
|
// 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 "content/browser/trace_controller_impl.h"
#include "base/bind.h"
#include "base/command_line.h"
#include "base/debug/trace_event.h"
#include "base/string_number_conversions.h"
#include "content/browser/trace_message_filter.h"
#include "content/browser/trace_subscriber_stdio.h"
#include "content/common/child_process_messages.h"
#include "content/public/browser/browser_message_filter.h"
#include "content/public/common/content_switches.h"
using base::debug::TraceLog;
namespace content {
namespace {
base::LazyInstance<TraceControllerImpl>::Leaky g_controller =
LAZY_INSTANCE_INITIALIZER;
class AutoStopTraceSubscriberStdio : public TraceSubscriberStdio {
public:
AutoStopTraceSubscriberStdio(const FilePath& file_path)
: TraceSubscriberStdio(file_path) {}
static void EndStartupTrace(TraceSubscriberStdio* subscriber) {
if (!TraceControllerImpl::GetInstance()->EndTracingAsync(subscriber))
delete subscriber;
// else, the tracing will end asynchronously in OnEndTracingComplete().
}
virtual void OnEndTracingComplete() {
TraceSubscriberStdio::OnEndTracingComplete();
delete this;
// TODO(joth): this would be the time to automatically open up
// chrome://tracing/ and load up the trace data collected.
}
};
} // namespace
TraceController* TraceController::GetInstance() {
return TraceControllerImpl::GetInstance();
}
TraceControllerImpl::TraceControllerImpl() :
subscriber_(NULL),
pending_end_ack_count_(0),
pending_bpf_ack_count_(0),
maximum_bpf_(0.0f),
is_tracing_(false),
is_get_categories_(false) {
TraceLog::GetInstance()->SetNotificationCallback(
base::Bind(&TraceControllerImpl::OnTraceNotification,
base::Unretained(this)));
}
TraceControllerImpl::~TraceControllerImpl() {
// No need to SetNotificationCallback(nil) on the TraceLog since this is a
// Leaky instance.
NOTREACHED();
}
TraceControllerImpl* TraceControllerImpl::GetInstance() {
return g_controller.Pointer();
}
void TraceControllerImpl::InitStartupTracing(const CommandLine& command_line) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
FilePath trace_file = command_line.GetSwitchValuePath(
switches::kTraceStartupFile);
// trace_file = "none" means that startup events will show up for the next
// begin/end tracing (via about:tracing or AutomationProxy::BeginTracing/
// EndTracing, for example).
if (trace_file == FilePath().AppendASCII("none"))
return;
if (trace_file.empty()) {
// Default to saving the startup trace into the current dir.
trace_file = FilePath().AppendASCII("chrometrace.log");
}
scoped_ptr<AutoStopTraceSubscriberStdio> subscriber(
new AutoStopTraceSubscriberStdio(trace_file));
DCHECK(can_begin_tracing(subscriber.get()));
std::string delay_str = command_line.GetSwitchValueASCII(
switches::kTraceStartupDuration);
int delay_secs = 5;
if (!delay_str.empty() && !base::StringToInt(delay_str, &delay_secs)) {
DLOG(WARNING) << "Could not parse --" << switches::kTraceStartupDuration
<< "=" << delay_str << " defaulting to 5 (secs)";
delay_secs = 5;
}
OnTracingBegan(subscriber.get());
BrowserThread::PostDelayedTask(
BrowserThread::UI,
FROM_HERE,
base::Bind(&AutoStopTraceSubscriberStdio::EndStartupTrace,
base::Unretained(subscriber.release())),
base::TimeDelta::FromSeconds(delay_secs));
}
bool TraceControllerImpl::GetKnownCategoriesAsync(TraceSubscriber* subscriber) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
// Known categories come back from child processes with the EndTracingAck
// message. So to get known categories, just begin and end tracing immediately
// afterwards. This will ping all the child processes for categories.
is_get_categories_ = true;
bool success = BeginTracing(subscriber, "*") &&
EndTracingAsync(subscriber);
is_get_categories_ = success;
return success;
}
bool TraceControllerImpl::BeginTracing(
TraceSubscriber* subscriber,
const std::vector<std::string>& included_categories,
const std::vector<std::string>& excluded_categories) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
if (!can_begin_tracing(subscriber))
return false;
// Enable tracing
TraceLog::GetInstance()->SetEnabled(included_categories, excluded_categories);
OnTracingBegan(subscriber);
return true;
}
bool TraceControllerImpl::BeginTracing(TraceSubscriber* subscriber,
const std::string& categories) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
if (!can_begin_tracing(subscriber))
return false;
// Enable tracing
TraceLog::GetInstance()->SetEnabled(categories);
OnTracingBegan(subscriber);
return true;
}
bool TraceControllerImpl::EndTracingAsync(TraceSubscriber* subscriber) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
if (!can_end_tracing() || subscriber != subscriber_)
return false;
// There could be a case where there are no child processes and filters_
// is empty. In that case we can immediately tell the subscriber that tracing
// has ended. To avoid recursive calls back to the subscriber, we will just
// use the existing asynchronous OnEndTracingAck code.
// Count myself (local trace) in pending_end_ack_count_, acked below.
pending_end_ack_count_ = filters_.size() + 1;
// Handle special case of zero child processes.
if (pending_end_ack_count_ == 1) {
// Ack asynchronously now, because we don't have any children to wait for.
std::vector<std::string> categories;
TraceLog::GetInstance()->GetKnownCategories(&categories);
BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
base::Bind(&TraceControllerImpl::OnEndTracingAck,
base::Unretained(this), categories));
}
// Notify all child processes.
for (FilterMap::iterator it = filters_.begin(); it != filters_.end(); ++it) {
it->get()->SendEndTracing();
}
return true;
}
bool TraceControllerImpl::GetTraceBufferPercentFullAsync(
TraceSubscriber* subscriber) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
if (!can_get_buffer_percent_full() || subscriber != subscriber_)
return false;
maximum_bpf_ = 0.0f;
pending_bpf_ack_count_ = filters_.size() + 1;
// Handle special case of zero child processes.
if (pending_bpf_ack_count_ == 1) {
// Ack asynchronously now, because we don't have any children to wait for.
float bpf = TraceLog::GetInstance()->GetBufferPercentFull();
BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
base::Bind(&TraceControllerImpl::OnTraceBufferPercentFullReply,
base::Unretained(this), bpf));
}
// Message all child processes.
for (FilterMap::iterator it = filters_.begin(); it != filters_.end(); ++it) {
it->get()->SendGetTraceBufferPercentFull();
}
return true;
}
bool TraceControllerImpl::SetWatchEvent(TraceSubscriber* subscriber,
const std::string& category_name,
const std::string& event_name) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
if (subscriber != subscriber_)
return false;
watch_category_ = category_name;
watch_name_ = event_name;
TraceLog::GetInstance()->SetWatchEvent(category_name, event_name);
for (FilterMap::iterator it = filters_.begin(); it != filters_.end(); ++it)
it->get()->SendSetWatchEvent(category_name, event_name);
return true;
}
bool TraceControllerImpl::CancelWatchEvent(TraceSubscriber* subscriber) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
if (subscriber != subscriber_)
return false;
watch_category_.clear();
watch_name_.clear();
TraceLog::GetInstance()->CancelWatchEvent();
for (FilterMap::iterator it = filters_.begin(); it != filters_.end(); ++it)
it->get()->SendCancelWatchEvent();
return true;
}
void TraceControllerImpl::CancelSubscriber(TraceSubscriber* subscriber) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
if (subscriber == subscriber_) {
subscriber_ = NULL;
// End tracing if necessary.
if (is_tracing_ && pending_end_ack_count_ == 0)
EndTracingAsync(NULL);
}
}
void TraceControllerImpl::AddFilter(TraceMessageFilter* filter) {
if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) {
BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
base::Bind(&TraceControllerImpl::AddFilter, base::Unretained(this),
make_scoped_refptr(filter)));
return;
}
filters_.insert(filter);
if (is_tracing_enabled()) {
filter->SendBeginTracing(included_categories_, excluded_categories_);
if (!watch_category_.empty())
filter->SendSetWatchEvent(watch_category_, watch_name_);
}
}
void TraceControllerImpl::RemoveFilter(TraceMessageFilter* filter) {
if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) {
BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
base::Bind(&TraceControllerImpl::RemoveFilter, base::Unretained(this),
make_scoped_refptr(filter)));
return;
}
filters_.erase(filter);
}
void TraceControllerImpl::OnTracingBegan(TraceSubscriber* subscriber) {
is_tracing_ = true;
subscriber_ = subscriber;
TraceLog::GetInstance()->GetEnabledTraceCategories(&included_categories_,
&excluded_categories_);
// Notify all child processes.
for (FilterMap::iterator it = filters_.begin(); it != filters_.end(); ++it) {
it->get()->SendBeginTracing(included_categories_, excluded_categories_);
}
}
void TraceControllerImpl::OnEndTracingAck(
const std::vector<std::string>& known_categories) {
if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) {
BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
base::Bind(&TraceControllerImpl::OnEndTracingAck,
base::Unretained(this), known_categories));
return;
}
// Merge known_categories with known_categories_
known_categories_.insert(known_categories.begin(), known_categories.end());
if (pending_end_ack_count_ == 0)
return;
if (--pending_end_ack_count_ == 0) {
// All acks have been received.
is_tracing_ = false;
// Disable local trace.
TraceLog::GetInstance()->SetDisabled();
// During this call, our OnTraceDataCollected will be
// called with the last of the local trace data. Since we are on the UI
// thread, the call to OnTraceDataCollected will be synchronous, so we can
// immediately call OnEndTracingComplete below.
TraceLog::GetInstance()->Flush(
base::Bind(&TraceControllerImpl::OnTraceDataCollected,
base::Unretained(this)));
// Trigger callback if one is set.
if (subscriber_) {
if (is_get_categories_)
subscriber_->OnKnownCategoriesCollected(known_categories_);
else
subscriber_->OnEndTracingComplete();
// Clear subscriber so that others can use TraceController.
subscriber_ = NULL;
}
is_get_categories_ = false;
}
if (pending_end_ack_count_ == 1) {
// The last ack represents local trace, so we need to ack it now. Note that
// this code only executes if there were child processes.
std::vector<std::string> categories;
TraceLog::GetInstance()->GetKnownCategories(&categories);
BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
base::Bind(&TraceControllerImpl::OnEndTracingAck,
base::Unretained(this), categories));
}
}
void TraceControllerImpl::OnTraceDataCollected(
const scoped_refptr<base::RefCountedString>& events_str_ptr) {
// OnTraceDataCollected may be called from any browser thread, either by the
// local event trace system or from child processes via TraceMessageFilter.
if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) {
BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
base::Bind(&TraceControllerImpl::OnTraceDataCollected,
base::Unretained(this), events_str_ptr));
return;
}
// Drop trace events if we are just getting categories.
if (subscriber_ && !is_get_categories_)
subscriber_->OnTraceDataCollected(events_str_ptr);
}
void TraceControllerImpl::OnTraceNotification(int notification) {
// OnTraceNotification may be called from any browser thread, either by the
// local event trace system or from child processes via TraceMessageFilter.
if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) {
BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
base::Bind(&TraceControllerImpl::OnTraceNotification,
base::Unretained(this), notification));
return;
}
if (notification & base::debug::TraceLog::TRACE_BUFFER_FULL) {
// EndTracingAsync may return false if tracing is already in the process
// of being ended. That is ok.
EndTracingAsync(subscriber_);
}
if (notification & base::debug::TraceLog::EVENT_WATCH_NOTIFICATION) {
if (subscriber_)
subscriber_->OnEventWatchNotification();
}
}
void TraceControllerImpl::OnTraceBufferPercentFullReply(float percent_full) {
if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) {
BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
base::Bind(&TraceControllerImpl::OnTraceBufferPercentFullReply,
base::Unretained(this), percent_full));
return;
}
if (pending_bpf_ack_count_ == 0)
return;
maximum_bpf_ = (maximum_bpf_ > percent_full)? maximum_bpf_ : percent_full;
if (--pending_bpf_ack_count_ == 0) {
// Trigger callback if one is set.
if (subscriber_)
subscriber_->OnTraceBufferPercentFullReply(maximum_bpf_);
}
if (pending_bpf_ack_count_ == 1) {
// The last ack represents local trace, so we need to ack it now. Note that
// this code only executes if there were child processes.
float bpf = TraceLog::GetInstance()->GetBufferPercentFull();
BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
base::Bind(&TraceControllerImpl::OnTraceBufferPercentFullReply,
base::Unretained(this), bpf));
}
}
} // namespace content
|