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

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

Issue 66193005: Independently enable recording and event callback (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 1 month 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 5
6 #ifndef BASE_DEBUG_TRACE_EVENT_IMPL_H_ 6 #ifndef BASE_DEBUG_TRACE_EVENT_IMPL_H_
7 #define BASE_DEBUG_TRACE_EVENT_IMPL_H_ 7 #define BASE_DEBUG_TRACE_EVENT_IMPL_H_
8 8
9 #include <stack> 9 #include <stack>
10 #include <string> 10 #include <string>
(...skipping 363 matching lines...) Expand 10 before | Expand all | Expand 10 after
374 // Enable the sampling profiler in the recording mode. 374 // Enable the sampling profiler in the recording mode.
375 ENABLE_SAMPLING = 1 << 2, 375 ENABLE_SAMPLING = 1 << 2,
376 376
377 // Enable the sampling profiler in the monitoring mode. 377 // Enable the sampling profiler in the monitoring mode.
378 MONITOR_SAMPLING = 1 << 3, 378 MONITOR_SAMPLING = 1 << 3,
379 379
380 // Echo to console. Events are discarded. 380 // Echo to console. Events are discarded.
381 ECHO_TO_CONSOLE = 1 << 4, 381 ECHO_TO_CONSOLE = 1 << 4,
382 }; 382 };
383 383
384 // The pointer returned from GetCategoryGroupEnabledInternal() points to a
385 // value with zero or more of the following bits. Used in this class only.
386 // The TRACE_EVENT macros should only use the value as a bool.
387 enum CategoryGroupEnabledFlags {
388 // Normal enabled flag for category groups enabled by SetEnabled().
389 ENABLED_FOR_RECORDING = 1 << 0,
390 // Category group enabled by SetEventCallbackEnabled().
391 ENABLED_FOR_EVENT_CALLBACK = 1 << 1,
392 };
393
384 static TraceLog* GetInstance(); 394 static TraceLog* GetInstance();
385 395
386 // Convert the given string to trace options. Defaults to RECORD_UNTIL_FULL if 396 // Convert the given string to trace options. Defaults to RECORD_UNTIL_FULL if
387 // the string does not provide valid options. 397 // the string does not provide valid options.
388 static Options TraceOptionsFromString(const std::string& str); 398 static Options TraceOptionsFromString(const std::string& str);
389 399
390 // Get set of known category groups. This can change as new code paths are 400 // Get set of known category groups. This can change as new code paths are
391 // reached. The known category groups are inserted into |category_groups|. 401 // reached. The known category groups are inserted into |category_groups|.
392 void GetKnownCategoryGroups(std::vector<std::string>* category_groups); 402 void GetKnownCategoryGroups(std::vector<std::string>* category_groups);
393 403
394 // Retrieves the current CategoryFilter. 404 // Retrieves a copy (for thread-safety) of the current CategoryFilter.
395 const CategoryFilter& GetCurrentCategoryFilter(); 405 CategoryFilter GetCurrentCategoryFilter();
396 406
397 Options trace_options() const { 407 Options trace_options() const {
398 return static_cast<Options>(subtle::NoBarrier_Load(&trace_options_)); 408 return static_cast<Options>(subtle::NoBarrier_Load(&trace_options_));
399 } 409 }
400 410
401 // Enables tracing. See CategoryFilter comments for details 411 // Enables normal tracing (recording trace events in the trace buffer).
402 // on how to control what categories will be traced. 412 // See CategoryFilter comments for details on how to control what categories
413 // will be traced. If tracing has already been enabled, |category_filter| will
414 // be merged into the current category filter.
403 void SetEnabled(const CategoryFilter& category_filter, Options options); 415 void SetEnabled(const CategoryFilter& category_filter, Options options);
404 416
405 // Disables tracing for all categories. 417 // Disables normal tracing for all categories.
406 void SetDisabled(); 418 void SetDisabled();
407 bool IsEnabled() { return !!enable_count_; } 419
420 bool IsEnabled() { return enabled_; }
408 421
409 // The number of times we have begun recording traces. If tracing is off, 422 // The number of times we have begun recording traces. If tracing is off,
410 // returns -1. If tracing is on, then it returns the number of times we have 423 // returns -1. If tracing is on, then it returns the number of times we have
411 // recorded a trace. By watching for this number to increment, you can 424 // recorded a trace. By watching for this number to increment, you can
412 // passively discover when a new trace has begun. This is then used to 425 // passively discover when a new trace has begun. This is then used to
413 // implement the TRACE_EVENT_IS_NEW_TRACE() primitive. 426 // implement the TRACE_EVENT_IS_NEW_TRACE() primitive.
414 int GetNumTracesRecorded(); 427 int GetNumTracesRecorded();
415 428
416 #if defined(OS_ANDROID) 429 #if defined(OS_ANDROID)
417 void StartATrace(); 430 void StartATrace();
(...skipping 24 matching lines...) Expand all
442 // time and from any thread. WARNING: It is possible for the previously set 455 // time and from any thread. WARNING: It is possible for the previously set
443 // callback to be called during OR AFTER a call to SetNotificationCallback. 456 // callback to be called during OR AFTER a call to SetNotificationCallback.
444 // Therefore, the target of the callback must either be a global function, 457 // Therefore, the target of the callback must either be a global function,
445 // ref-counted object or a LazyInstance with Leaky traits (or equivalent). 458 // ref-counted object or a LazyInstance with Leaky traits (or equivalent).
446 typedef base::Callback<void(int)> NotificationCallback; 459 typedef base::Callback<void(int)> NotificationCallback;
447 void SetNotificationCallback(const NotificationCallback& cb); 460 void SetNotificationCallback(const NotificationCallback& cb);
448 461
449 // Not using base::Callback because of its limited by 7 parameters. 462 // Not using base::Callback because of its limited by 7 parameters.
450 // Also, using primitive type allows directly passing callback from WebCore. 463 // Also, using primitive type allows directly passing callback from WebCore.
451 // WARNING: It is possible for the previously set callback to be called 464 // WARNING: It is possible for the previously set callback to be called
452 // after a call to SetEventCallback() that replaces or clears the callback. 465 // after a call to SetEventCallbackEnabled() that replaces or a call to
466 // SetEventCallbackDisabled() that disables the callback.
453 // This callback may be invoked on any thread. 467 // This callback may be invoked on any thread.
454 // TODO(wangxianzhu): For now for TRACE_EVENT_PHASE_COMPLETE events, the 468 // For TRACE_EVENT_PHASE_COMPLETE events, the/ client will still receive pairs
caseq 2013/11/19 18:32:29 nit: the/ client => the client
Xianzhu 2013/11/19 18:39:21 Done.
455 // client will still receive pairs of TRACE_EVENT_PHASE_BEGIN and 469 // of TRACE_EVENT_PHASE_BEGIN and TRACE_EVENT_PHASE_END events to keep the
456 // TRACE_EVENT_PHASE_END events. Should send TRACE_EVENT_PHASE_COMPLETE 470 // interface simple.
457 // directly to clients if it is beneficial and feasible.
458 typedef void (*EventCallback)(TimeTicks timestamp, 471 typedef void (*EventCallback)(TimeTicks timestamp,
459 char phase, 472 char phase,
460 const unsigned char* category_group_enabled, 473 const unsigned char* category_group_enabled,
461 const char* name, 474 const char* name,
462 unsigned long long id, 475 unsigned long long id,
463 int num_args, 476 int num_args,
464 const char* const arg_names[], 477 const char* const arg_names[],
465 const unsigned char arg_types[], 478 const unsigned char arg_types[],
466 const unsigned long long arg_values[], 479 const unsigned long long arg_values[],
467 unsigned char flags); 480 unsigned char flags);
468 void SetEventCallback(EventCallback cb); 481
482 // Enable tracing for EventCallback.
483 void SetEventCallbackEnabled(const CategoryFilter& category_filter,
484 EventCallback cb);
485 void SetEventCallbackDisabled();
469 486
470 // Flush all collected events to the given output callback. The callback will 487 // Flush all collected events to the given output callback. The callback will
471 // be called one or more times either synchronously or asynchronously from 488 // be called one or more times either synchronously or asynchronously from
472 // the current thread with IPC-bite-size chunks. The string format is 489 // the current thread with IPC-bite-size chunks. The string format is
473 // undefined. Use TraceResultBuffer to convert one or more trace strings to 490 // undefined. Use TraceResultBuffer to convert one or more trace strings to
474 // JSON. The callback can be null if the caller doesn't want any data. 491 // JSON. The callback can be null if the caller doesn't want any data.
475 // Due to the implementation of thread-local buffers, flush can't be 492 // Due to the implementation of thread-local buffers, flush can't be
476 // done when tracing is enabled. If called when tracing is enabled, the 493 // done when tracing is enabled. If called when tracing is enabled, the
477 // callback will be called directly with (empty_string, false) to indicate 494 // callback will be called directly with (empty_string, false) to indicate
478 // the end of this unsuccessful flush. 495 // the end of this unsuccessful flush.
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
517 unsigned char flags); 534 unsigned char flags);
518 static void AddTraceEventEtw(char phase, 535 static void AddTraceEventEtw(char phase,
519 const char* category_group, 536 const char* category_group,
520 const void* id, 537 const void* id,
521 const char* extra); 538 const char* extra);
522 static void AddTraceEventEtw(char phase, 539 static void AddTraceEventEtw(char phase,
523 const char* category_group, 540 const char* category_group,
524 const void* id, 541 const void* id,
525 const std::string& extra); 542 const std::string& extra);
526 543
527 void UpdateTraceEventDuration(TraceEventHandle handle); 544 void UpdateTraceEventDuration(const unsigned char* category_group_enabled,
545 const char* name,
546 TraceEventHandle handle);
528 547
529 // For every matching event, a notification will be fired. NOTE: the 548 // For every matching event, a notification will be fired. NOTE: the
530 // notification will fire for each matching event that has already occurred 549 // notification will fire for each matching event that has already occurred
531 // since tracing was started (including before tracing if the process was 550 // since tracing was started (including before tracing if the process was
532 // started with tracing turned on). 551 // started with tracing turned on).
533 void SetWatchEvent(const std::string& category_name, 552 void SetWatchEvent(const std::string& category_name,
534 const std::string& event_name); 553 const std::string& event_name);
535 // Cancel the watch event. If tracing is enabled, this may race with the 554 // Cancel the watch event. If tracing is enabled, this may race with the
536 // watch event notification firing. 555 // watch event notification firing.
537 void CancelWatchEvent(); 556 void CancelWatchEvent();
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
585 TraceBufferRingBufferGetReturnChunk); 604 TraceBufferRingBufferGetReturnChunk);
586 FRIEND_TEST_ALL_PREFIXES(TraceEventTestFixture, 605 FRIEND_TEST_ALL_PREFIXES(TraceEventTestFixture,
587 TraceBufferRingBufferHalfIteration); 606 TraceBufferRingBufferHalfIteration);
588 FRIEND_TEST_ALL_PREFIXES(TraceEventTestFixture, 607 FRIEND_TEST_ALL_PREFIXES(TraceEventTestFixture,
589 TraceBufferRingBufferFullIteration); 608 TraceBufferRingBufferFullIteration);
590 609
591 // This allows constructor and destructor to be private and usable only 610 // This allows constructor and destructor to be private and usable only
592 // by the Singleton class. 611 // by the Singleton class.
593 friend struct DefaultSingletonTraits<TraceLog>; 612 friend struct DefaultSingletonTraits<TraceLog>;
594 613
595 // Enable/disable each category group based on the current enable_count_ 614 // Enable/disable each category group based on the current enabled_,
596 // and category_filter_. Disable the category group if enabled_count_ is 0, or 615 // category_filter_, event_callback_ and event_callback_category_filter_.
597 // if the category group contains a category that matches an included category 616 // Enable the category group if enabled_ is true and category_filter_ matches
598 // pattern, that category group will be enabled. 617 // the category group, or event_callback_ is not null and
599 // On Android, ATRACE_ENABLED flag will be applied if atrace is started. 618 // event_callback_category_filter_ matches the category group.
600 void UpdateCategoryGroupEnabledFlags(); 619 void UpdateCategoryGroupEnabledFlags();
601 void UpdateCategoryGroupEnabledFlag(int category_index); 620 void UpdateCategoryGroupEnabledFlag(int category_index);
602 621
603 // Helper class for managing notification_thread_count_ and running 622 // Helper class for managing notification_thread_count_ and running
604 // notification callbacks. This is very similar to a reader-writer lock, but 623 // notification callbacks. This is very similar to a reader-writer lock, but
605 // shares the lock with TraceLog and manages the notification flags. 624 // shares the lock with TraceLog and manages the notification flags.
606 class NotificationHelper { 625 class NotificationHelper {
607 public: 626 public:
608 inline explicit NotificationHelper(TraceLog* trace_log); 627 inline explicit NotificationHelper(TraceLog* trace_log);
609 inline ~NotificationHelper(); 628 inline ~NotificationHelper();
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
666 TimeTicks OffsetNow() const { 685 TimeTicks OffsetNow() const {
667 return OffsetTimestamp(TimeTicks::NowFromSystemTraceTime()); 686 return OffsetTimestamp(TimeTicks::NowFromSystemTraceTime());
668 } 687 }
669 TimeTicks OffsetTimestamp(const TimeTicks& timestamp) const { 688 TimeTicks OffsetTimestamp(const TimeTicks& timestamp) const {
670 return timestamp - time_offset_; 689 return timestamp - time_offset_;
671 } 690 }
672 691
673 // This lock protects TraceLog member accesses from arbitrary threads. 692 // This lock protects TraceLog member accesses from arbitrary threads.
674 Lock lock_; 693 Lock lock_;
675 int locked_line_; 694 int locked_line_;
676 int enable_count_; 695 bool enabled_;
677 int num_traces_recorded_; 696 int num_traces_recorded_;
678 subtle::AtomicWord /* bool */ buffer_is_full_; 697 subtle::AtomicWord /* bool */ buffer_is_full_;
679 NotificationCallback notification_callback_; 698 NotificationCallback notification_callback_;
680 scoped_ptr<TraceBuffer> logged_events_; 699 scoped_ptr<TraceBuffer> logged_events_;
681 subtle::AtomicWord /* EventCallback */ event_callback_; 700 subtle::AtomicWord /* EventCallback */ event_callback_;
682 bool dispatching_to_observer_list_; 701 bool dispatching_to_observer_list_;
683 std::vector<EnabledStateObserver*> enabled_state_observer_list_; 702 std::vector<EnabledStateObserver*> enabled_state_observer_list_;
684 703
685 std::string process_name_; 704 std::string process_name_;
686 base::hash_map<int, std::string> process_labels_; 705 base::hash_map<int, std::string> process_labels_;
(...skipping 16 matching lines...) Expand all
703 subtle::AtomicWord /* const unsigned char* */ watch_category_; 722 subtle::AtomicWord /* const unsigned char* */ watch_category_;
704 std::string watch_event_name_; 723 std::string watch_event_name_;
705 724
706 subtle::AtomicWord /* Options */ trace_options_; 725 subtle::AtomicWord /* Options */ trace_options_;
707 726
708 // Sampling thread handles. 727 // Sampling thread handles.
709 scoped_ptr<TraceSamplingThread> sampling_thread_; 728 scoped_ptr<TraceSamplingThread> sampling_thread_;
710 PlatformThreadHandle sampling_thread_handle_; 729 PlatformThreadHandle sampling_thread_handle_;
711 730
712 CategoryFilter category_filter_; 731 CategoryFilter category_filter_;
732 CategoryFilter event_callback_category_filter_;
713 733
714 ThreadLocalPointer<ThreadLocalEventBuffer> thread_local_event_buffer_; 734 ThreadLocalPointer<ThreadLocalEventBuffer> thread_local_event_buffer_;
715 ThreadLocalBoolean thread_blocks_message_loop_; 735 ThreadLocalBoolean thread_blocks_message_loop_;
716 736
717 // Contains the message loops of threads that have had at least one event 737 // Contains the message loops of threads that have had at least one event
718 // added into the local event buffer. Not using MessageLoopProxy because we 738 // added into the local event buffer. Not using MessageLoopProxy because we
719 // need to know the life time of the message loops. 739 // need to know the life time of the message loops.
720 hash_set<MessageLoop*> thread_message_loops_; 740 hash_set<MessageLoop*> thread_message_loops_;
721 741
722 // For events which can't be added into the thread local buffer, e.g. events 742 // For events which can't be added into the thread local buffer, e.g. events
723 // from threads without a message loop. 743 // from threads without a message loop.
724 scoped_ptr<TraceBufferChunk> thread_shared_chunk_; 744 scoped_ptr<TraceBufferChunk> thread_shared_chunk_;
725 size_t thread_shared_chunk_index_; 745 size_t thread_shared_chunk_index_;
726 746
727 // Set when asynchronous Flush is in progress. 747 // Set when asynchronous Flush is in progress.
728 OutputCallback flush_output_callback_; 748 OutputCallback flush_output_callback_;
729 scoped_refptr<MessageLoopProxy> flush_message_loop_proxy_; 749 scoped_refptr<MessageLoopProxy> flush_message_loop_proxy_;
730 subtle::AtomicWord generation_; 750 subtle::AtomicWord generation_;
731 751
732 DISALLOW_COPY_AND_ASSIGN(TraceLog); 752 DISALLOW_COPY_AND_ASSIGN(TraceLog);
733 }; 753 };
734 754
735 } // namespace debug 755 } // namespace debug
736 } // namespace base 756 } // namespace base
737 757
738 #endif // BASE_DEBUG_TRACE_EVENT_IMPL_H_ 758 #endif // BASE_DEBUG_TRACE_EVENT_IMPL_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698