Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(124)

Side by Side Diff: content/browser/trace_controller_impl.cc

Issue 10837082: implement SetWatchEvent and WaitForEvent for trace-based-tests (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: cleaned up and fixed a bug Created 8 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "content/browser/trace_controller_impl.h" 5 #include "content/browser/trace_controller_impl.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/command_line.h" 8 #include "base/command_line.h"
9 #include "base/debug/trace_event.h" 9 #include "base/debug/trace_event.h"
10 #include "base/string_number_conversions.h" 10 #include "base/string_number_conversions.h"
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
44 TraceController* TraceController::GetInstance() { 44 TraceController* TraceController::GetInstance() {
45 return TraceControllerImpl::GetInstance(); 45 return TraceControllerImpl::GetInstance();
46 } 46 }
47 47
48 TraceControllerImpl::TraceControllerImpl() : 48 TraceControllerImpl::TraceControllerImpl() :
49 subscriber_(NULL), 49 subscriber_(NULL),
50 pending_end_ack_count_(0), 50 pending_end_ack_count_(0),
51 pending_bpf_ack_count_(0), 51 pending_bpf_ack_count_(0),
52 maximum_bpf_(0.0f), 52 maximum_bpf_(0.0f),
53 is_tracing_(false), 53 is_tracing_(false),
54 is_get_categories_(false) { 54 is_get_categories_(false),
55 TraceLog::GetInstance()->SetOutputCallback( 55 watch_num_occurrences_(0) {
56 base::Bind(&TraceControllerImpl::OnTraceDataCollected, 56 TraceLog::GetInstance()->SetNotificationCallback(
57 base::Bind(&TraceControllerImpl::OnTraceNotification,
57 base::Unretained(this))); 58 base::Unretained(this)));
58 } 59 }
59 60
60 TraceControllerImpl::~TraceControllerImpl() { 61 TraceControllerImpl::~TraceControllerImpl() {
61 if (TraceLog* trace_log = TraceLog::GetInstance()) 62 if (TraceLog* trace_log = TraceLog::GetInstance())
62 trace_log->SetOutputCallback(TraceLog::OutputCallback()); 63 trace_log->SetNotificationCallback(TraceLog::NotificationCallback());
63 } 64 }
64 65
65 TraceControllerImpl* TraceControllerImpl::GetInstance() { 66 TraceControllerImpl* TraceControllerImpl::GetInstance() {
66 return Singleton<TraceControllerImpl>::get(); 67 return Singleton<TraceControllerImpl>::get();
67 } 68 }
68 69
69 void TraceControllerImpl::InitStartupTracing(const CommandLine& command_line) { 70 void TraceControllerImpl::InitStartupTracing(const CommandLine& command_line) {
70 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 71 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
71 FilePath trace_file = command_line.GetSwitchValuePath( 72 FilePath trace_file = command_line.GetSwitchValuePath(
72 switches::kTraceStartupFile); 73 switches::kTraceStartupFile);
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after
203 } 204 }
204 205
205 // Message all child processes. 206 // Message all child processes.
206 for (FilterMap::iterator it = filters_.begin(); it != filters_.end(); ++it) { 207 for (FilterMap::iterator it = filters_.begin(); it != filters_.end(); ++it) {
207 it->get()->SendGetTraceBufferPercentFull(); 208 it->get()->SendGetTraceBufferPercentFull();
208 } 209 }
209 210
210 return true; 211 return true;
211 } 212 }
212 213
214 bool TraceControllerImpl::SetWatchEvent(TraceSubscriber* subscriber,
215 const char* category_name,
216 const char* event_name,
217 int num_occurrences) {
218 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
219 if (subscriber != subscriber_)
220 return false;
221
222 watch_num_occurrences_ = num_occurrences;
223 watch_category_ = category_name;
224 watch_name_ = event_name;
225
226 TraceLog::GetInstance()->SetWatchEvent(category_name, event_name,
227 num_occurrences);
228 for (FilterMap::iterator it = filters_.begin(); it != filters_.end(); ++it)
229 it->get()->SendSetWatchEvent(category_name, event_name, num_occurrences);
230
231 return true;
232 }
233
234 bool TraceControllerImpl::CancelWatchEvent(TraceSubscriber* subscriber) {
235 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
236 if (subscriber != subscriber_)
237 return false;
238
239 watch_num_occurrences_ = 0;
240 watch_category_.clear();
241 watch_name_.clear();
242
243 TraceLog::GetInstance()->CancelWatchEvent();
244 for (FilterMap::iterator it = filters_.begin(); it != filters_.end(); ++it)
245 it->get()->SendCancelWatchEvent();
246
247 return true;
248 }
249
213 void TraceControllerImpl::CancelSubscriber(TraceSubscriber* subscriber) { 250 void TraceControllerImpl::CancelSubscriber(TraceSubscriber* subscriber) {
214 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 251 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
215 252
216 if (subscriber == subscriber_) { 253 if (subscriber == subscriber_) {
217 subscriber_ = NULL; 254 subscriber_ = NULL;
218 // End tracing if necessary. 255 // End tracing if necessary.
219 if (is_tracing_ && pending_end_ack_count_ == 0) 256 if (is_tracing_ && pending_end_ack_count_ == 0)
220 EndTracingAsync(NULL); 257 EndTracingAsync(NULL);
221 } 258 }
222 } 259 }
223 260
224 void TraceControllerImpl::AddFilter(TraceMessageFilter* filter) { 261 void TraceControllerImpl::AddFilter(TraceMessageFilter* filter) {
225 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { 262 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) {
226 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, 263 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
227 base::Bind(&TraceControllerImpl::AddFilter, base::Unretained(this), 264 base::Bind(&TraceControllerImpl::AddFilter, base::Unretained(this),
228 make_scoped_refptr(filter))); 265 make_scoped_refptr(filter)));
229 return; 266 return;
230 } 267 }
231 268
232 filters_.insert(filter); 269 filters_.insert(filter);
233 if (is_tracing_enabled()) { 270 if (is_tracing_enabled()) {
234 filter->SendBeginTracing(included_categories_, excluded_categories_); 271 filter->SendBeginTracing(included_categories_, excluded_categories_);
272 if (watch_num_occurrences_ > 0) {
273 filter->SendSetWatchEvent(watch_category_, watch_name_,
274 watch_num_occurrences_);
275 }
235 } 276 }
236 } 277 }
237 278
238 void TraceControllerImpl::RemoveFilter(TraceMessageFilter* filter) { 279 void TraceControllerImpl::RemoveFilter(TraceMessageFilter* filter) {
239 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { 280 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) {
240 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, 281 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
241 base::Bind(&TraceControllerImpl::RemoveFilter, base::Unretained(this), 282 base::Bind(&TraceControllerImpl::RemoveFilter, base::Unretained(this),
242 make_scoped_refptr(filter))); 283 make_scoped_refptr(filter)));
243 return; 284 return;
244 } 285 }
(...skipping 26 matching lines...) Expand all
271 // Merge known_categories with known_categories_ 312 // Merge known_categories with known_categories_
272 known_categories_.insert(known_categories.begin(), known_categories.end()); 313 known_categories_.insert(known_categories.begin(), known_categories.end());
273 314
274 if (pending_end_ack_count_ == 0) 315 if (pending_end_ack_count_ == 0)
275 return; 316 return;
276 317
277 if (--pending_end_ack_count_ == 0) { 318 if (--pending_end_ack_count_ == 0) {
278 // All acks have been received. 319 // All acks have been received.
279 is_tracing_ = false; 320 is_tracing_ = false;
280 321
281 // Disable local trace. During this call, our OnTraceDataCollected will be 322 // Disable local trace.
323 TraceLog::GetInstance()->SetDisabled();
324
325 // During this call, our OnTraceDataCollected will be
282 // called with the last of the local trace data. Since we are on the UI 326 // called with the last of the local trace data. Since we are on the UI
283 // thread, the call to OnTraceDataCollected will be synchronous, so we can 327 // thread, the call to OnTraceDataCollected will be synchronous, so we can
284 // immediately call OnEndTracingComplete below. 328 // immediately call OnEndTracingComplete below.
285 TraceLog::GetInstance()->SetEnabled(false); 329 TraceLog::GetInstance()->Flush(
330 base::Bind(&TraceControllerImpl::OnTraceDataCollected,
331 base::Unretained(this)));
286 332
287 // Trigger callback if one is set. 333 // Trigger callback if one is set.
288 if (subscriber_) { 334 if (subscriber_) {
289 if (is_get_categories_) 335 if (is_get_categories_)
290 subscriber_->OnKnownCategoriesCollected(known_categories_); 336 subscriber_->OnKnownCategoriesCollected(known_categories_);
291 else 337 else
292 subscriber_->OnEndTracingComplete(); 338 subscriber_->OnEndTracingComplete();
293 // Clear subscriber so that others can use TraceController. 339 // Clear subscriber so that others can use TraceController.
294 subscriber_ = NULL; 340 subscriber_ = NULL;
295 } 341 }
(...skipping 21 matching lines...) Expand all
317 base::Bind(&TraceControllerImpl::OnTraceDataCollected, 363 base::Bind(&TraceControllerImpl::OnTraceDataCollected,
318 base::Unretained(this), events_str_ptr)); 364 base::Unretained(this), events_str_ptr));
319 return; 365 return;
320 } 366 }
321 367
322 // Drop trace events if we are just getting categories. 368 // Drop trace events if we are just getting categories.
323 if (subscriber_ && !is_get_categories_) 369 if (subscriber_ && !is_get_categories_)
324 subscriber_->OnTraceDataCollected(events_str_ptr); 370 subscriber_->OnTraceDataCollected(events_str_ptr);
325 } 371 }
326 372
327 void TraceControllerImpl::OnTraceBufferFull() { 373 void TraceControllerImpl::OnTraceNotification(int notification) {
328 // OnTraceBufferFull may be called from any browser thread, either by the 374 // OnTraceNotification may be called from any browser thread, either by the
329 // local event trace system or from child processes via TraceMessageFilter. 375 // local event trace system or from child processes via TraceMessageFilter.
330 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { 376 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) {
331 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, 377 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
332 base::Bind(&TraceControllerImpl::OnTraceBufferFull, 378 base::Bind(&TraceControllerImpl::OnTraceNotification,
333 base::Unretained(this))); 379 base::Unretained(this), notification));
334 return; 380 return;
335 } 381 }
336 382
337 // EndTracingAsync may return false if tracing is already in the process of 383 switch (static_cast<base::debug::TraceLog::Notification>(notification)) {
ccameron 2012/08/21 01:06:34 The definition of ...::Notification says that the
jbates 2012/08/23 22:45:28 Good catch, fixed this code to check for both flag
338 // being ended. That is ok. 384 case base::debug::TraceLog::TRACE_BUFFER_FULL:
339 EndTracingAsync(subscriber_); 385 // EndTracingAsync may return false if tracing is already in the process
386 // of being ended. That is ok.
387 EndTracingAsync(subscriber_);
388 break;
389 case base::debug::TraceLog::EVENT_WATCH_NOTIFICATION:
390 if (subscriber_)
391 subscriber_->OnEventWatchNotification();
392 break;
393 }
340 } 394 }
341 395
342 void TraceControllerImpl::OnTraceBufferPercentFullReply(float percent_full) { 396 void TraceControllerImpl::OnTraceBufferPercentFullReply(float percent_full) {
343 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { 397 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) {
344 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, 398 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
345 base::Bind(&TraceControllerImpl::OnTraceBufferPercentFullReply, 399 base::Bind(&TraceControllerImpl::OnTraceBufferPercentFullReply,
346 base::Unretained(this), percent_full)); 400 base::Unretained(this), percent_full));
347 return; 401 return;
348 } 402 }
349 403
(...skipping 12 matching lines...) Expand all
362 // The last ack represents local trace, so we need to ack it now. Note that 416 // The last ack represents local trace, so we need to ack it now. Note that
363 // this code only executes if there were child processes. 417 // this code only executes if there were child processes.
364 float bpf = TraceLog::GetInstance()->GetBufferPercentFull(); 418 float bpf = TraceLog::GetInstance()->GetBufferPercentFull();
365 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, 419 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
366 base::Bind(&TraceControllerImpl::OnTraceBufferPercentFullReply, 420 base::Bind(&TraceControllerImpl::OnTraceBufferPercentFullReply,
367 base::Unretained(this), bpf)); 421 base::Unretained(this), bpf));
368 } 422 }
369 } 423 }
370 424
371 } // namespace content 425 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698