OLD | NEW |
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 // This header is designed to give you trace_event macros without specifying | 5 // This header file defines the set of trace_event macros without specifying |
6 // how the events actually get collected and stored. If you need to expose trace | 6 // how the events actually get collected and stored. If you need to expose trace |
7 // event to some other universe, you can copy-and-paste this file, | 7 // events to some other universe, you can copy-and-paste this file as well as |
8 // implement the TRACE_EVENT_API macros, and do any other necessary fixup for | 8 // trace_event.h, modifying the macros contained there as necessary for the |
9 // the target platform. The end result is that multiple libraries can funnel | 9 // target platform. The end result is that multiple libraries can funnel events |
10 // events through to a shared trace event collector. | 10 // through to a shared trace event collector. |
11 | 11 |
12 // Trace events are for tracking application performance and resource usage. | 12 // Trace events are for tracking application performance and resource usage. |
13 // Macros are provided to track: | 13 // Macros are provided to track: |
14 // Begin and end of function calls | 14 // Begin and end of function calls |
15 // Counters | 15 // Counters |
16 // | 16 // |
17 // Events are issued against categories. Whereas LOG's | 17 // Events are issued against categories. Whereas LOG's |
18 // categories are statically defined, TRACE categories are created | 18 // categories are statically defined, TRACE categories are created |
19 // implicitly with a string. For example: | 19 // implicitly with a string. For example: |
20 // TRACE_EVENT_INSTANT0("MY_SUBSYSTEM", "SomeImportantEvent") | 20 // TRACE_EVENT_INSTANT0("MY_SUBSYSTEM", "SomeImportantEvent") |
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
141 // the time of the system being enabled or disabled. This is acceptable as | 141 // the time of the system being enabled or disabled. This is acceptable as |
142 // we tolerate some data loss while the system is being enabled/disabled and | 142 // we tolerate some data loss while the system is being enabled/disabled and |
143 // because AddTraceEvent is threadsafe internally and checks the enabled state | 143 // because AddTraceEvent is threadsafe internally and checks the enabled state |
144 // again under lock. | 144 // again under lock. |
145 // | 145 // |
146 // Without the use of these static category pointers and enabled flags all | 146 // Without the use of these static category pointers and enabled flags all |
147 // trace points would carry a significant performance cost of aquiring a lock | 147 // trace points would carry a significant performance cost of aquiring a lock |
148 // and resolving the category. | 148 // and resolving the category. |
149 | 149 |
150 | 150 |
151 #ifndef BASE_DEBUG_TRACE_EVENT_H_ | 151 #ifndef BASE_DEBUG_TRACE_EVENT_INTERNAL_H_ |
152 #define BASE_DEBUG_TRACE_EVENT_H_ | 152 #define BASE_DEBUG_TRACE_EVENT_INTERNAL_H_ |
153 | 153 |
154 #include <string> | 154 #include <string> |
155 | 155 |
156 #include "base/atomicops.h" | |
157 #include "base/debug/trace_event_impl.h" | |
158 #include "build/build_config.h" | |
159 | |
160 // By default, const char* argument values are assumed to have long-lived scope | 156 // By default, const char* argument values are assumed to have long-lived scope |
161 // and will not be copied. Use this macro to force a const char* to be copied. | 157 // and will not be copied. Use this macro to force a const char* to be copied. |
162 #define TRACE_STR_COPY(str) \ | 158 #define TRACE_STR_COPY(str) \ |
163 trace_event_internal::TraceStringWithCopy(str) | 159 trace_event_internal::TraceStringWithCopy(str) |
164 | 160 |
165 // By default, uint64 ID argument values are not mangled with the Process ID in | 161 // By default, uint64 ID argument values are not mangled with the Process ID in |
166 // TRACE_EVENT_ASYNC macros. Use this macro to force Process ID mangling. | 162 // TRACE_EVENT_ASYNC macros. Use this macro to force Process ID mangling. |
167 #define TRACE_ID_MANGLE(id) \ | 163 #define TRACE_ID_MANGLE(id) \ |
168 trace_event_internal::TraceID::ForceMangle(id) | 164 trace_event_internal::TraceID::ForceMangle(id) |
169 | 165 |
(...skipping 368 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
538 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_END, \ | 534 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_END, \ |
539 category, name, id, TRACE_EVENT_FLAG_COPY, \ | 535 category, name, id, TRACE_EVENT_FLAG_COPY, \ |
540 arg1_name, arg1_val) | 536 arg1_name, arg1_val) |
541 #define TRACE_EVENT_COPY_FLOW_END2(category, name, id, arg1_name, arg1_val, \ | 537 #define TRACE_EVENT_COPY_FLOW_END2(category, name, id, arg1_name, arg1_val, \ |
542 arg2_name, arg2_val) \ | 538 arg2_name, arg2_val) \ |
543 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_END, \ | 539 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_END, \ |
544 category, name, id, TRACE_EVENT_FLAG_COPY, \ | 540 category, name, id, TRACE_EVENT_FLAG_COPY, \ |
545 arg1_name, arg1_val, arg2_name, arg2_val) | 541 arg1_name, arg1_val, arg2_name, arg2_val) |
546 | 542 |
547 | 543 |
548 //////////////////////////////////////////////////////////////////////////////// | |
549 // Implementation specific tracing API definitions. | |
550 | |
551 // Get a pointer to the enabled state of the given trace category. Only | |
552 // long-lived literal strings should be given as the category name. The returned | |
553 // pointer can be held permanently in a local static for example. If the | |
554 // unsigned char is non-zero, tracing is enabled. If tracing is enabled, | |
555 // TRACE_EVENT_API_ADD_TRACE_EVENT can be called. It's OK if tracing is disabled | |
556 // between the load of the tracing state and the call to | |
557 // TRACE_EVENT_API_ADD_TRACE_EVENT, because this flag only provides an early out | |
558 // for best performance when tracing is disabled. | |
559 // const unsigned char* | |
560 // TRACE_EVENT_API_GET_CATEGORY_ENABLED(const char* category_name) | |
561 #define TRACE_EVENT_API_GET_CATEGORY_ENABLED \ | |
562 base::debug::TraceLog::GetCategoryEnabled | |
563 | |
564 // Add a trace event to the platform tracing system. | |
565 // void TRACE_EVENT_API_ADD_TRACE_EVENT( | |
566 // char phase, | |
567 // const unsigned char* category_enabled, | |
568 // const char* name, | |
569 // unsigned long long id, | |
570 // int num_args, | |
571 // const char** arg_names, | |
572 // const unsigned char* arg_types, | |
573 // const unsigned long long* arg_values, | |
574 // unsigned char flags) | |
575 #define TRACE_EVENT_API_ADD_TRACE_EVENT \ | |
576 base::debug::TraceLog::GetInstance()->AddTraceEvent | |
577 | |
578 //////////////////////////////////////////////////////////////////////////////// | |
579 | |
580 // Implementation detail: trace event macros create temporary variables | 544 // Implementation detail: trace event macros create temporary variables |
581 // to keep instrumentation overhead low. These macros give each temporary | 545 // to keep instrumentation overhead low. These macros give each temporary |
582 // variable a unique name based on the line number to prevent name collissions. | 546 // variable a unique name based on the line number to prevent name collissions. |
583 #define INTERNAL_TRACE_EVENT_UID3(a,b) \ | 547 #define INTERNAL_TRACE_EVENT_UID3(a,b) \ |
584 trace_event_unique_##a##b | 548 trace_event_unique_##a##b |
585 #define INTERNAL_TRACE_EVENT_UID2(a,b) \ | 549 #define INTERNAL_TRACE_EVENT_UID2(a,b) \ |
586 INTERNAL_TRACE_EVENT_UID3(a,b) | 550 INTERNAL_TRACE_EVENT_UID3(a,b) |
587 #define INTERNAL_TRACE_EVENT_UID(name_prefix) \ | 551 #define INTERNAL_TRACE_EVENT_UID(name_prefix) \ |
588 INTERNAL_TRACE_EVENT_UID2(name_prefix, __LINE__) | 552 INTERNAL_TRACE_EVENT_UID2(name_prefix, __LINE__) |
589 | 553 |
590 // Implementation detail: internal macro to create static category. | 554 // Implementation detail: internal macro to create static category. |
591 // No barriers are needed, because this code is designed to operate safely | 555 // No barriers are needed, because this code is designed to operate safely |
592 // even when the unsigned char* points to garbage data (which may be the case | 556 // even when the unsigned char* points to garbage data (which may be the case |
593 // on processors without cache coherency). | 557 // on processors without cache coherency). |
594 #define INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category) \ | 558 #define INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category) \ |
595 static base::subtle::AtomicWord INTERNAL_TRACE_EVENT_UID(atomic) = 0; \ | 559 static TRACE_EVENT_API_ATOMIC_WORD INTERNAL_TRACE_EVENT_UID(atomic) = 0; \ |
596 const uint8* INTERNAL_TRACE_EVENT_UID(catstatic) = \ | 560 const unsigned char* INTERNAL_TRACE_EVENT_UID(catstatic) = \ |
597 reinterpret_cast<const uint8*>( \ | 561 reinterpret_cast<const unsigned char*>(TRACE_EVENT_API_ATOMIC_LOAD( \ |
598 base::subtle::NoBarrier_Load(&INTERNAL_TRACE_EVENT_UID(atomic))); \ | 562 INTERNAL_TRACE_EVENT_UID(atomic))); \ |
599 if (!INTERNAL_TRACE_EVENT_UID(catstatic)) { \ | 563 if (!INTERNAL_TRACE_EVENT_UID(catstatic)) { \ |
600 INTERNAL_TRACE_EVENT_UID(catstatic) = \ | 564 INTERNAL_TRACE_EVENT_UID(catstatic) = \ |
601 TRACE_EVENT_API_GET_CATEGORY_ENABLED(category); \ | 565 TRACE_EVENT_API_GET_CATEGORY_ENABLED(category); \ |
602 base::subtle::NoBarrier_Store(&INTERNAL_TRACE_EVENT_UID(atomic), \ | 566 TRACE_EVENT_API_ATOMIC_STORE(INTERNAL_TRACE_EVENT_UID(atomic), \ |
603 reinterpret_cast<base::subtle::AtomicWord>( \ | 567 reinterpret_cast<TRACE_EVENT_API_ATOMIC_WORD>( \ |
604 INTERNAL_TRACE_EVENT_UID(catstatic))); \ | 568 INTERNAL_TRACE_EVENT_UID(catstatic))); \ |
605 } | 569 } |
606 | 570 |
607 // Implementation detail: internal macro to create static category and add | 571 // Implementation detail: internal macro to create static category and add |
608 // event if the category is enabled. | 572 // event if the category is enabled. |
609 #define INTERNAL_TRACE_EVENT_ADD(phase, category, name, flags, ...) \ | 573 #define INTERNAL_TRACE_EVENT_ADD(phase, category, name, flags, ...) \ |
610 do { \ | 574 do { \ |
611 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category); \ | 575 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category); \ |
612 if (*INTERNAL_TRACE_EVENT_UID(catstatic)) { \ | 576 if (*INTERNAL_TRACE_EVENT_UID(catstatic)) { \ |
613 trace_event_internal::AddTraceEvent( \ | 577 trace_event_internal::AddTraceEvent( \ |
(...skipping 221 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
835 // through to the tracing API, the arg_values must live throughout | 799 // through to the tracing API, the arg_values must live throughout |
836 // these procedures. | 800 // these procedures. |
837 | 801 |
838 static inline void AddTraceEvent(char phase, | 802 static inline void AddTraceEvent(char phase, |
839 const unsigned char* category_enabled, | 803 const unsigned char* category_enabled, |
840 const char* name, | 804 const char* name, |
841 unsigned long long id, | 805 unsigned long long id, |
842 unsigned char flags) { | 806 unsigned char flags) { |
843 TRACE_EVENT_API_ADD_TRACE_EVENT( | 807 TRACE_EVENT_API_ADD_TRACE_EVENT( |
844 phase, category_enabled, name, id, | 808 phase, category_enabled, name, id, |
845 kZeroNumArgs, NULL, NULL, NULL, | 809 kZeroNumArgs, NULL, NULL, NULL, flags); |
846 flags); | |
847 } | 810 } |
848 | 811 |
849 template<class ARG1_TYPE> | 812 template<class ARG1_TYPE> |
850 static inline void AddTraceEvent(char phase, | 813 static inline void AddTraceEvent(char phase, |
851 const unsigned char* category_enabled, | 814 const unsigned char* category_enabled, |
852 const char* name, | 815 const char* name, |
853 unsigned long long id, | 816 unsigned long long id, |
854 unsigned char flags, | 817 unsigned char flags, |
855 const char* arg1_name, | 818 const char* arg1_name, |
856 const ARG1_TYPE& arg1_val) { | 819 const ARG1_TYPE& arg1_val) { |
857 const int num_args = 1; | 820 const int num_args = 1; |
858 unsigned char arg_types[1]; | 821 unsigned char arg_types[1]; |
859 unsigned long long arg_values[1]; | 822 unsigned long long arg_values[1]; |
860 SetTraceValue(arg1_val, &arg_types[0], &arg_values[0]); | 823 SetTraceValue(arg1_val, &arg_types[0], &arg_values[0]); |
861 TRACE_EVENT_API_ADD_TRACE_EVENT( | 824 TRACE_EVENT_API_ADD_TRACE_EVENT( |
862 phase, category_enabled, name, id, | 825 phase, category_enabled, name, id, |
863 num_args, &arg1_name, arg_types, arg_values, | 826 num_args, &arg1_name, arg_types, arg_values, flags); |
864 flags); | |
865 } | 827 } |
866 | 828 |
867 template<class ARG1_TYPE, class ARG2_TYPE> | 829 template<class ARG1_TYPE, class ARG2_TYPE> |
868 static inline void AddTraceEvent(char phase, | 830 static inline void AddTraceEvent(char phase, |
869 const unsigned char* category_enabled, | 831 const unsigned char* category_enabled, |
870 const char* name, | 832 const char* name, |
871 unsigned long long id, | 833 unsigned long long id, |
872 unsigned char flags, | 834 unsigned char flags, |
873 const char* arg1_name, | 835 const char* arg1_name, |
874 const ARG1_TYPE& arg1_val, | 836 const ARG1_TYPE& arg1_val, |
875 const char* arg2_name, | 837 const char* arg2_name, |
876 const ARG2_TYPE& arg2_val) { | 838 const ARG2_TYPE& arg2_val) { |
877 const int num_args = 2; | 839 const int num_args = 2; |
878 const char* arg_names[2] = { arg1_name, arg2_name }; | 840 const char* arg_names[2] = { arg1_name, arg2_name }; |
879 unsigned char arg_types[2]; | 841 unsigned char arg_types[2]; |
880 unsigned long long arg_values[2]; | 842 unsigned long long arg_values[2]; |
881 SetTraceValue(arg1_val, &arg_types[0], &arg_values[0]); | 843 SetTraceValue(arg1_val, &arg_types[0], &arg_values[0]); |
882 SetTraceValue(arg2_val, &arg_types[1], &arg_values[1]); | 844 SetTraceValue(arg2_val, &arg_types[1], &arg_values[1]); |
883 TRACE_EVENT_API_ADD_TRACE_EVENT( | 845 TRACE_EVENT_API_ADD_TRACE_EVENT( |
884 phase, category_enabled, name, id, | 846 phase, category_enabled, name, id, |
885 num_args, arg_names, arg_types, arg_values, | 847 num_args, arg_names, arg_types, arg_values, flags); |
886 flags); | |
887 } | 848 } |
888 | 849 |
889 // Used by TRACE_EVENTx macro. Do not use directly. | 850 // Used by TRACE_EVENTx macro. Do not use directly. |
890 class BASE_EXPORT TraceEndOnScopeClose { | 851 class TRACE_EVENT_API_CLASS_EXPORT TraceEndOnScopeClose { |
891 public: | 852 public: |
892 // Note: members of data_ intentionally left uninitialized. See Initialize. | 853 // Note: members of data_ intentionally left uninitialized. See Initialize. |
893 TraceEndOnScopeClose() : p_data_(NULL) {} | 854 TraceEndOnScopeClose() : p_data_(NULL) {} |
894 ~TraceEndOnScopeClose() { | 855 ~TraceEndOnScopeClose() { |
895 if (p_data_) | 856 if (p_data_) |
896 AddEventIfEnabled(); | 857 AddEventIfEnabled(); |
897 } | 858 } |
898 | 859 |
899 void Initialize(const unsigned char* category_enabled, | 860 void Initialize(const unsigned char* category_enabled, |
900 const char* name); | 861 const char* name) { |
| 862 data_.category_enabled = category_enabled; |
| 863 data_.name = name; |
| 864 p_data_ = &data_; |
| 865 } |
| 866 |
901 | 867 |
902 private: | 868 private: |
903 // Add the end event if the category is still enabled. | 869 // Add the end event if the category is still enabled. |
904 void AddEventIfEnabled(); | 870 void AddEventIfEnabled() { |
| 871 // Only called when p_data_ is non-null. |
| 872 if (*p_data_->category_enabled) { |
| 873 TRACE_EVENT_API_ADD_TRACE_EVENT( |
| 874 TRACE_EVENT_PHASE_END, |
| 875 p_data_->category_enabled, |
| 876 p_data_->name, kNoEventId, |
| 877 kZeroNumArgs, NULL, NULL, NULL, |
| 878 TRACE_EVENT_FLAG_NONE); |
| 879 } |
| 880 } |
905 | 881 |
906 // This Data struct workaround is to avoid initializing all the members | 882 // This Data struct workaround is to avoid initializing all the members |
907 // in Data during construction of this object, since this object is always | 883 // in Data during construction of this object, since this object is always |
908 // constructed, even when tracing is disabled. If the members of Data were | 884 // constructed, even when tracing is disabled. If the members of Data were |
909 // members of this class instead, compiler warnings occur about potential | 885 // members of this class instead, compiler warnings occur about potential |
910 // uninitialized accesses. | 886 // uninitialized accesses. |
911 struct Data { | 887 struct Data { |
912 const unsigned char* category_enabled; | 888 const unsigned char* category_enabled; |
913 const char* name; | 889 const char* name; |
914 }; | 890 }; |
915 Data* p_data_; | 891 Data* p_data_; |
916 Data data_; | 892 Data data_; |
917 }; | 893 }; |
918 | 894 |
919 | |
920 } // namespace trace_event_internal | 895 } // namespace trace_event_internal |
921 | 896 |
922 #endif // BASE_DEBUG_TRACE_EVENT_H_ | 897 #endif // BASE_DEBUG_TRACE_EVENT_INTERNAL_H_ |
OLD | NEW |