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

Side by Side Diff: base/debug/trace_event_impl.cc

Issue 12302036: Add a mode flag to the tracing framework. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: base/ review cleanups. Created 7 years, 10 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
« no previous file with comments | « base/debug/trace_event_impl.h ('k') | base/debug/trace_event_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 "base/debug/trace_event_impl.h" 5 #include "base/debug/trace_event_impl.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/debug/leak_annotations.h" 10 #include "base/debug/leak_annotations.h"
11 #include "base/debug/trace_event.h" 11 #include "base/debug/trace_event.h"
12 #include "base/format_macros.h" 12 #include "base/format_macros.h"
13 #include "base/lazy_instance.h" 13 #include "base/lazy_instance.h"
14 #include "base/memory/singleton.h" 14 #include "base/memory/singleton.h"
15 #include "base/process_util.h" 15 #include "base/process_util.h"
16 #include "base/stl_util.h" 16 #include "base/stl_util.h"
17 #include "base/string_split.h"
17 #include "base/string_util.h" 18 #include "base/string_util.h"
18 #include "base/stringprintf.h" 19 #include "base/stringprintf.h"
19 #include "base/strings/string_tokenizer.h" 20 #include "base/strings/string_tokenizer.h"
20 #include "base/sys_info.h" 21 #include "base/sys_info.h"
21 #include "base/third_party/dynamic_annotations/dynamic_annotations.h" 22 #include "base/third_party/dynamic_annotations/dynamic_annotations.h"
22 #include "base/threading/platform_thread.h" 23 #include "base/threading/platform_thread.h"
23 #include "base/threading/thread_id_name_manager.h" 24 #include "base/threading/thread_id_name_manager.h"
24 #include "base/threading/thread_local.h" 25 #include "base/threading/thread_local.h"
25 #include "base/time.h" 26 #include "base/time.h"
26 #include "base/utf_string_conversions.h" 27 #include "base/utf_string_conversions.h"
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
65 const int g_category_categories_exhausted = 1; 66 const int g_category_categories_exhausted = 1;
66 const int g_category_metadata = 2; 67 const int g_category_metadata = 2;
67 int g_category_index = 3; // skip initial 3 categories 68 int g_category_index = 3; // skip initial 3 categories
68 69
69 // The name of the current thread. This is used to decide if the current 70 // The name of the current thread. This is used to decide if the current
70 // thread name has changed. We combine all the seen thread names into the 71 // thread name has changed. We combine all the seen thread names into the
71 // output name for the thread. 72 // output name for the thread.
72 LazyInstance<ThreadLocalPointer<const char> >::Leaky 73 LazyInstance<ThreadLocalPointer<const char> >::Leaky
73 g_current_thread_name = LAZY_INSTANCE_INITIALIZER; 74 g_current_thread_name = LAZY_INSTANCE_INITIALIZER;
74 75
76 const char kRecordUntilFull[] = "record-until-full";
77
75 } // namespace 78 } // namespace
76 79
77 //////////////////////////////////////////////////////////////////////////////// 80 ////////////////////////////////////////////////////////////////////////////////
78 // 81 //
79 // TraceEvent 82 // TraceEvent
80 // 83 //
81 //////////////////////////////////////////////////////////////////////////////// 84 ////////////////////////////////////////////////////////////////////////////////
82 85
83 namespace { 86 namespace {
84 87
(...skipping 256 matching lines...) Expand 10 before | Expand all | Expand 10 after
341 void TraceLog::NotificationHelper::SendNotificationIfAny() { 344 void TraceLog::NotificationHelper::SendNotificationIfAny() {
342 if (notification_) 345 if (notification_)
343 callback_copy_.Run(notification_); 346 callback_copy_.Run(notification_);
344 } 347 }
345 348
346 // static 349 // static
347 TraceLog* TraceLog::GetInstance() { 350 TraceLog* TraceLog::GetInstance() {
348 return Singleton<TraceLog, StaticMemorySingletonTraits<TraceLog> >::get(); 351 return Singleton<TraceLog, StaticMemorySingletonTraits<TraceLog> >::get();
349 } 352 }
350 353
354 // static
355 // Note, if you add more options here you also need to update:
356 // content/browser/devtools/devtools_tracing_handler:TraceOptionsFromString
357 TraceLog::Options TraceLog::TraceOptionsFromString(const std::string& options) {
358 std::vector<std::string> split;
359 base::SplitString(options, ',', &split);
360 int ret = 0;
361 for (std::vector<std::string>::iterator iter = split.begin();
362 iter != split.end();
363 ++iter) {
364 if (*iter == kRecordUntilFull) {
365 ret |= RECORD_UNTIL_FULL;
366 } else {
367 NOTREACHED(); // Unknown option provided.
368 }
369 }
370 if (!(ret & RECORD_UNTIL_FULL))
jar (doing other things) 2013/02/22 20:15:57 nit: This still reads super strangly. As written
dsinclair 2013/02/22 20:21:13 Done.
371 ret |= RECORD_UNTIL_FULL; // Default when no options are specified.
372
373 return static_cast<Options>(ret);
374 }
375
351 TraceLog::TraceLog() 376 TraceLog::TraceLog()
352 : enable_count_(0), 377 : enable_count_(0),
353 dispatching_to_observer_list_(false), 378 dispatching_to_observer_list_(false),
354 watch_category_(NULL) { 379 watch_category_(NULL),
380 trace_options_(RECORD_UNTIL_FULL) {
355 // Trace is enabled or disabled on one thread while other threads are 381 // Trace is enabled or disabled on one thread while other threads are
356 // accessing the enabled flag. We don't care whether edge-case events are 382 // accessing the enabled flag. We don't care whether edge-case events are
357 // traced or not, so we allow races on the enabled flag to keep the trace 383 // traced or not, so we allow races on the enabled flag to keep the trace
358 // macros fast. 384 // macros fast.
359 // TODO(jbates): ANNOTATE_BENIGN_RACE_SIZED crashes windows TSAN bots: 385 // TODO(jbates): ANNOTATE_BENIGN_RACE_SIZED crashes windows TSAN bots:
360 // ANNOTATE_BENIGN_RACE_SIZED(g_category_enabled, sizeof(g_category_enabled), 386 // ANNOTATE_BENIGN_RACE_SIZED(g_category_enabled, sizeof(g_category_enabled),
361 // "trace_event category enabled"); 387 // "trace_event category enabled");
362 for (int i = 0; i < TRACE_EVENT_MAX_CATEGORIES; ++i) { 388 for (int i = 0; i < TRACE_EVENT_MAX_CATEGORIES; ++i) {
363 ANNOTATE_BENIGN_RACE(&g_category_enabled[i], 389 ANNOTATE_BENIGN_RACE(&g_category_enabled[i],
364 "trace_event category enabled"); 390 "trace_event category enabled");
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after
471 return category_enabled; 497 return category_enabled;
472 } 498 }
473 499
474 void TraceLog::GetKnownCategories(std::vector<std::string>* categories) { 500 void TraceLog::GetKnownCategories(std::vector<std::string>* categories) {
475 AutoLock lock(lock_); 501 AutoLock lock(lock_);
476 for (int i = 0; i < g_category_index; i++) 502 for (int i = 0; i < g_category_index; i++)
477 categories->push_back(g_categories[i]); 503 categories->push_back(g_categories[i]);
478 } 504 }
479 505
480 void TraceLog::SetEnabled(const std::vector<std::string>& included_categories, 506 void TraceLog::SetEnabled(const std::vector<std::string>& included_categories,
481 const std::vector<std::string>& excluded_categories) { 507 const std::vector<std::string>& excluded_categories,
508 Options options) {
482 AutoLock lock(lock_); 509 AutoLock lock(lock_);
483 510
484 if (enable_count_++ > 0) { 511 if (enable_count_++ > 0) {
512 if (options != trace_options_) {
513 DLOG(ERROR) << "Attemting to re-enable tracing with a different "
514 << "set of options.";
515 }
516
485 // Tracing is already enabled, so just merge in enabled categories. 517 // Tracing is already enabled, so just merge in enabled categories.
486 // We only expand the set of enabled categories upon nested SetEnable(). 518 // We only expand the set of enabled categories upon nested SetEnable().
487 if (!included_categories_.empty() && !included_categories.empty()) { 519 if (!included_categories_.empty() && !included_categories.empty()) {
488 included_categories_.insert(included_categories_.end(), 520 included_categories_.insert(included_categories_.end(),
489 included_categories.begin(), 521 included_categories.begin(),
490 included_categories.end()); 522 included_categories.end());
491 EnableMatchingCategories(included_categories_, CATEGORY_ENABLED, 0); 523 EnableMatchingCategories(included_categories_, CATEGORY_ENABLED, 0);
492 } else { 524 } else {
493 // If either old or new included categories are empty, allow all events. 525 // If either old or new included categories are empty, allow all events.
494 included_categories_.clear(); 526 included_categories_.clear();
495 excluded_categories_.clear(); 527 excluded_categories_.clear();
496 EnableMatchingCategories(excluded_categories_, 0, CATEGORY_ENABLED); 528 EnableMatchingCategories(excluded_categories_, 0, CATEGORY_ENABLED);
497 } 529 }
498 return; 530 return;
499 } 531 }
532 trace_options_ = options;
500 533
501 if (dispatching_to_observer_list_) { 534 if (dispatching_to_observer_list_) {
502 DLOG(ERROR) << 535 DLOG(ERROR) <<
503 "Cannot manipulate TraceLog::Enabled state from an observer."; 536 "Cannot manipulate TraceLog::Enabled state from an observer.";
504 return; 537 return;
505 } 538 }
506 539
507 dispatching_to_observer_list_ = true; 540 dispatching_to_observer_list_ = true;
508 FOR_EACH_OBSERVER(EnabledStateChangedObserver, enabled_state_observer_list_, 541 FOR_EACH_OBSERVER(EnabledStateChangedObserver, enabled_state_observer_list_,
509 OnTraceLogWillEnable()); 542 OnTraceLogWillEnable());
510 dispatching_to_observer_list_ = false; 543 dispatching_to_observer_list_ = false;
511 544
512 logged_events_.reserve(1024); 545 logged_events_.reserve(1024);
513 included_categories_ = included_categories; 546 included_categories_ = included_categories;
514 excluded_categories_ = excluded_categories; 547 excluded_categories_ = excluded_categories;
515 // Note that if both included and excluded_categories are empty, the else 548 // Note that if both included and excluded_categories are empty, the else
516 // clause below excludes nothing, thereby enabling all categories. 549 // clause below excludes nothing, thereby enabling all categories.
517 if (!included_categories_.empty()) 550 if (!included_categories_.empty())
518 EnableMatchingCategories(included_categories_, CATEGORY_ENABLED, 0); 551 EnableMatchingCategories(included_categories_, CATEGORY_ENABLED, 0);
519 else 552 else
520 EnableMatchingCategories(excluded_categories_, 0, CATEGORY_ENABLED); 553 EnableMatchingCategories(excluded_categories_, 0, CATEGORY_ENABLED);
521 } 554 }
522 555
523 void TraceLog::SetEnabled(const std::string& categories) { 556 void TraceLog::SetEnabled(const std::string& categories, Options options) {
524 std::vector<std::string> included, excluded; 557 std::vector<std::string> included, excluded;
525 // Tokenize list of categories, delimited by ','. 558 // Tokenize list of categories, delimited by ','.
526 StringTokenizer tokens(categories, ","); 559 StringTokenizer tokens(categories, ",");
527 while (tokens.GetNext()) { 560 while (tokens.GetNext()) {
528 bool is_included = true; 561 bool is_included = true;
529 std::string category = tokens.token(); 562 std::string category = tokens.token();
530 // Excluded categories start with '-'. 563 // Excluded categories start with '-'.
531 if (category.at(0) == '-') { 564 if (category.at(0) == '-') {
532 // Remove '-' from category string. 565 // Remove '-' from category string.
533 category = category.substr(1); 566 category = category.substr(1);
534 is_included = false; 567 is_included = false;
535 } 568 }
536 if (is_included) 569 if (is_included)
537 included.push_back(category); 570 included.push_back(category);
538 else 571 else
539 excluded.push_back(category); 572 excluded.push_back(category);
540 } 573 }
541 SetEnabled(included, excluded); 574 SetEnabled(included, excluded, options);
542 } 575 }
543 576
544 void TraceLog::GetEnabledTraceCategories( 577 void TraceLog::GetEnabledTraceCategories(
545 std::vector<std::string>* included_out, 578 std::vector<std::string>* included_out,
546 std::vector<std::string>* excluded_out) { 579 std::vector<std::string>* excluded_out) {
547 AutoLock lock(lock_); 580 AutoLock lock(lock_);
548 if (enable_count_) { 581 if (enable_count_) {
549 *included_out = included_categories_; 582 *included_out = included_categories_;
550 *excluded_out = excluded_categories_; 583 *excluded_out = excluded_categories_;
551 } 584 }
(...skipping 18 matching lines...) Expand all
570 603
571 included_categories_.clear(); 604 included_categories_.clear();
572 excluded_categories_.clear(); 605 excluded_categories_.clear();
573 watch_category_ = NULL; 606 watch_category_ = NULL;
574 watch_event_name_ = ""; 607 watch_event_name_ = "";
575 for (int i = 0; i < g_category_index; i++) 608 for (int i = 0; i < g_category_index; i++)
576 g_category_enabled[i] = 0; 609 g_category_enabled[i] = 0;
577 AddThreadNameMetadataEvents(); 610 AddThreadNameMetadataEvents();
578 } 611 }
579 612
580 void TraceLog::SetEnabled(bool enabled) { 613 void TraceLog::SetEnabled(bool enabled, Options options) {
581 if (enabled) 614 if (enabled)
582 SetEnabled(std::vector<std::string>(), std::vector<std::string>()); 615 SetEnabled(std::vector<std::string>(), std::vector<std::string>(), options);
583 else 616 else
584 SetDisabled(); 617 SetDisabled();
585 } 618 }
586 619
587 void TraceLog::AddEnabledStateObserver(EnabledStateChangedObserver* listener) { 620 void TraceLog::AddEnabledStateObserver(EnabledStateChangedObserver* listener) {
588 enabled_state_observer_list_.AddObserver(listener); 621 enabled_state_observer_list_.AddObserver(listener);
589 } 622 }
590 623
591 void TraceLog::RemoveEnabledStateObserver( 624 void TraceLog::RemoveEnabledStateObserver(
592 EnabledStateChangedObserver* listener) { 625 EnabledStateChangedObserver* listener) {
(...skipping 269 matching lines...) Expand 10 before | Expand all | Expand 10 after
862 0, // num_args 895 0, // num_args
863 NULL, // arg_names 896 NULL, // arg_names
864 NULL, // arg_types 897 NULL, // arg_types
865 NULL, // arg_values 898 NULL, // arg_values
866 TRACE_EVENT_FLAG_NONE); // flags 899 TRACE_EVENT_FLAG_NONE); // flags
867 } 900 }
868 } 901 }
869 902
870 } // namespace trace_event_internal 903 } // namespace trace_event_internal
871 904
OLDNEW
« no previous file with comments | « base/debug/trace_event_impl.h ('k') | base/debug/trace_event_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698