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

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

Issue 11464005: Add trace event Pepper API (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Enable NaCl compilation Created 8 years 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
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 // 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
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 387 matching lines...) Expand 10 before | Expand all | Expand 10 after
557 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_END, \ 553 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_END, \
558 category, name, id, TRACE_EVENT_FLAG_COPY, \ 554 category, name, id, TRACE_EVENT_FLAG_COPY, \
559 arg1_name, arg1_val) 555 arg1_name, arg1_val)
560 #define TRACE_EVENT_COPY_FLOW_END2(category, name, id, arg1_name, arg1_val, \ 556 #define TRACE_EVENT_COPY_FLOW_END2(category, name, id, arg1_name, arg1_val, \
561 arg2_name, arg2_val) \ 557 arg2_name, arg2_val) \
562 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_END, \ 558 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_END, \
563 category, name, id, TRACE_EVENT_FLAG_COPY, \ 559 category, name, id, TRACE_EVENT_FLAG_COPY, \
564 arg1_name, arg1_val, arg2_name, arg2_val) 560 arg1_name, arg1_val, arg2_name, arg2_val)
565 561
566 562
567 ////////////////////////////////////////////////////////////////////////////////
568 // Implementation specific tracing API definitions.
569
570 // Get a pointer to the enabled state of the given trace category. Only
571 // long-lived literal strings should be given as the category name. The returned
572 // pointer can be held permanently in a local static for example. If the
573 // unsigned char is non-zero, tracing is enabled. If tracing is enabled,
574 // TRACE_EVENT_API_ADD_TRACE_EVENT can be called. It's OK if tracing is disabled
575 // between the load of the tracing state and the call to
576 // TRACE_EVENT_API_ADD_TRACE_EVENT, because this flag only provides an early out
577 // for best performance when tracing is disabled.
578 // const unsigned char*
579 // TRACE_EVENT_API_GET_CATEGORY_ENABLED(const char* category_name)
580 #define TRACE_EVENT_API_GET_CATEGORY_ENABLED \
581 base::debug::TraceLog::GetCategoryEnabled
582
583 // Add a trace event to the platform tracing system. Returns thresholdBeginId
584 // for use in a corresponding end TRACE_EVENT_API_ADD_TRACE_EVENT call.
585 // int TRACE_EVENT_API_ADD_TRACE_EVENT(
586 // char phase,
587 // const unsigned char* category_enabled,
588 // const char* name,
589 // unsigned long long id,
590 // int num_args,
591 // const char** arg_names,
592 // const unsigned char* arg_types,
593 // const unsigned long long* arg_values,
594 // int threshold_begin_id,
595 // long long threshold,
596 // unsigned char flags)
597 #define TRACE_EVENT_API_ADD_TRACE_EVENT \
598 base::debug::TraceLog::GetInstance()->AddTraceEvent
599
600 ////////////////////////////////////////////////////////////////////////////////
601
602 // Implementation detail: trace event macros create temporary variables 563 // Implementation detail: trace event macros create temporary variables
603 // to keep instrumentation overhead low. These macros give each temporary 564 // to keep instrumentation overhead low. These macros give each temporary
604 // variable a unique name based on the line number to prevent name collissions. 565 // variable a unique name based on the line number to prevent name collissions.
605 #define INTERNAL_TRACE_EVENT_UID3(a,b) \ 566 #define INTERNAL_TRACE_EVENT_UID3(a,b) \
606 trace_event_unique_##a##b 567 trace_event_unique_##a##b
607 #define INTERNAL_TRACE_EVENT_UID2(a,b) \ 568 #define INTERNAL_TRACE_EVENT_UID2(a,b) \
608 INTERNAL_TRACE_EVENT_UID3(a,b) 569 INTERNAL_TRACE_EVENT_UID3(a,b)
609 #define INTERNAL_TRACE_EVENT_UID(name_prefix) \ 570 #define INTERNAL_TRACE_EVENT_UID(name_prefix) \
610 INTERNAL_TRACE_EVENT_UID2(name_prefix, __LINE__) 571 INTERNAL_TRACE_EVENT_UID2(name_prefix, __LINE__)
611 572
612 // Implementation detail: internal macro to create static category. 573 // Implementation detail: internal macro to create static category.
613 // No barriers are needed, because this code is designed to operate safely 574 // No barriers are needed, because this code is designed to operate safely
614 // even when the unsigned char* points to garbage data (which may be the case 575 // even when the unsigned char* points to garbage data (which may be the case
615 // on processors without cache coherency). 576 // on processors without cache coherency).
616 #define INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category) \ 577 #define INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category) \
617 static base::subtle::AtomicWord INTERNAL_TRACE_EVENT_UID(atomic) = 0; \ 578 static TRACE_EVENT_API_ATOMIC_WORD INTERNAL_TRACE_EVENT_UID(atomic) = 0; \
618 const uint8* INTERNAL_TRACE_EVENT_UID(catstatic) = \ 579 const unsigned char* INTERNAL_TRACE_EVENT_UID(catstatic) = \
619 reinterpret_cast<const uint8*>( \ 580 reinterpret_cast<const unsigned char*>(TRACE_EVENT_API_ATOMIC_LOAD( \
620 base::subtle::NoBarrier_Load(&INTERNAL_TRACE_EVENT_UID(atomic))); \ 581 INTERNAL_TRACE_EVENT_UID(atomic))); \
621 if (!INTERNAL_TRACE_EVENT_UID(catstatic)) { \ 582 if (!INTERNAL_TRACE_EVENT_UID(catstatic)) { \
622 INTERNAL_TRACE_EVENT_UID(catstatic) = \ 583 INTERNAL_TRACE_EVENT_UID(catstatic) = \
623 TRACE_EVENT_API_GET_CATEGORY_ENABLED(category); \ 584 TRACE_EVENT_API_GET_CATEGORY_ENABLED(category); \
624 base::subtle::NoBarrier_Store(&INTERNAL_TRACE_EVENT_UID(atomic), \ 585 TRACE_EVENT_API_ATOMIC_STORE(INTERNAL_TRACE_EVENT_UID(atomic), \
625 reinterpret_cast<base::subtle::AtomicWord>( \ 586 reinterpret_cast<TRACE_EVENT_API_ATOMIC_WORD>( \
626 INTERNAL_TRACE_EVENT_UID(catstatic))); \ 587 INTERNAL_TRACE_EVENT_UID(catstatic))); \
627 } 588 }
628 589
629 // Implementation detail: internal macro to create static category and add 590 // Implementation detail: internal macro to create static category and add
630 // event if the category is enabled. 591 // event if the category is enabled.
631 #define INTERNAL_TRACE_EVENT_ADD(phase, category, name, flags, ...) \ 592 #define INTERNAL_TRACE_EVENT_ADD(phase, category, name, flags, ...) \
632 do { \ 593 do { \
633 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category); \ 594 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category); \
634 if (*INTERNAL_TRACE_EVENT_UID(catstatic)) { \ 595 if (*INTERNAL_TRACE_EVENT_UID(catstatic)) { \
635 trace_event_internal::AddTraceEvent( \ 596 trace_event_internal::AddTraceEvent( \
(...skipping 288 matching lines...) Expand 10 before | Expand all | Expand 10 after
924 unsigned long long arg_values[2]; 885 unsigned long long arg_values[2];
925 SetTraceValue(arg1_val, &arg_types[0], &arg_values[0]); 886 SetTraceValue(arg1_val, &arg_types[0], &arg_values[0]);
926 SetTraceValue(arg2_val, &arg_types[1], &arg_values[1]); 887 SetTraceValue(arg2_val, &arg_types[1], &arg_values[1]);
927 return TRACE_EVENT_API_ADD_TRACE_EVENT( 888 return TRACE_EVENT_API_ADD_TRACE_EVENT(
928 phase, category_enabled, name, id, 889 phase, category_enabled, name, id,
929 num_args, arg_names, arg_types, arg_values, 890 num_args, arg_names, arg_types, arg_values,
930 kNoThreshholdBeginId, kNoThresholdValue, flags); 891 kNoThreshholdBeginId, kNoThresholdValue, flags);
931 } 892 }
932 893
933 // Used by TRACE_EVENTx macro. Do not use directly. 894 // Used by TRACE_EVENTx macro. Do not use directly.
934 class BASE_EXPORT TraceEndOnScopeClose { 895 class TRACE_EVENT_API_CLASS_EXPORT TraceEndOnScopeClose {
935 public: 896 public:
936 // Note: members of data_ intentionally left uninitialized. See Initialize. 897 // Note: members of data_ intentionally left uninitialized. See Initialize.
937 TraceEndOnScopeClose() : p_data_(NULL) {} 898 TraceEndOnScopeClose() : p_data_(NULL) {}
938 ~TraceEndOnScopeClose() { 899 ~TraceEndOnScopeClose() {
939 if (p_data_) 900 if (p_data_)
940 AddEventIfEnabled(); 901 AddEventIfEnabled();
941 } 902 }
942 903
943 void Initialize(const unsigned char* category_enabled, 904 void Initialize(const unsigned char* category_enabled,
944 const char* name); 905 const char* name) {
906 data_.category_enabled = category_enabled;
907 data_.name = name;
908 p_data_ = &data_;
909 }
910
945 911
946 private: 912 private:
947 // Add the end event if the category is still enabled. 913 // Add the end event if the category is still enabled.
948 void AddEventIfEnabled(); 914 void AddEventIfEnabled() {
915 // Only called when p_data_ is non-null.
916 if (*p_data_->category_enabled) {
917 TRACE_EVENT_API_ADD_TRACE_EVENT(
jbauman 2012/12/14 00:36:07 Hopefully this won't increase the chrome binary si
elijahtaylor1 2012/12/18 20:40:31 Good point, I just measured and it looks like this
918 TRACE_EVENT_PHASE_END,
919 p_data_->category_enabled,
920 p_data_->name, kNoEventId,
921 kZeroNumArgs, NULL, NULL, NULL,
922 kNoThreshholdBeginId, kNoThresholdValue, TRACE_EVENT_FLAG_NONE);
923 }
924 }
949 925
950 // This Data struct workaround is to avoid initializing all the members 926 // This Data struct workaround is to avoid initializing all the members
951 // in Data during construction of this object, since this object is always 927 // in Data during construction of this object, since this object is always
952 // constructed, even when tracing is disabled. If the members of Data were 928 // constructed, even when tracing is disabled. If the members of Data were
953 // members of this class instead, compiler warnings occur about potential 929 // members of this class instead, compiler warnings occur about potential
954 // uninitialized accesses. 930 // uninitialized accesses.
955 struct Data { 931 struct Data {
956 const unsigned char* category_enabled; 932 const unsigned char* category_enabled;
957 const char* name; 933 const char* name;
958 }; 934 };
959 Data* p_data_; 935 Data* p_data_;
960 Data data_; 936 Data data_;
961 }; 937 };
962 938
963 // Used by TRACE_EVENTx macro. Do not use directly. 939 // Used by TRACE_EVENTx macro. Do not use directly.
964 class BASE_EXPORT TraceEndOnScopeCloseThreshold { 940 class TRACE_EVENT_API_CLASS_EXPORT TraceEndOnScopeCloseThreshold {
965 public: 941 public:
966 // Note: members of data_ intentionally left uninitialized. See Initialize. 942 // Note: members of data_ intentionally left uninitialized. See Initialize.
967 TraceEndOnScopeCloseThreshold() : p_data_(NULL) {} 943 TraceEndOnScopeCloseThreshold() : p_data_(NULL) {}
968 ~TraceEndOnScopeCloseThreshold() { 944 ~TraceEndOnScopeCloseThreshold() {
969 if (p_data_) 945 if (p_data_)
970 AddEventIfEnabled(); 946 AddEventIfEnabled();
971 } 947 }
972 948
973 // Called by macros only when tracing is enabled at the point when the begin 949 // Called by macros only when tracing is enabled at the point when the begin
974 // event is added. 950 // event is added.
975 void Initialize(const unsigned char* category_enabled, 951 void Initialize(const unsigned char* category_enabled,
976 const char* name, 952 const char* name,
977 int threshold_begin_id, 953 int threshold_begin_id,
978 long long threshold); 954 long long threshold) {
955 data_.category_enabled = category_enabled;
956 data_.name = name;
957 data_.threshold_begin_id = threshold_begin_id;
958 data_.threshold = threshold;
959 p_data_ = &data_;
960 }
979 961
980 private: 962 private:
981 // Add the end event if the category is still enabled. 963 // Add the end event if the category is still enabled.
982 void AddEventIfEnabled(); 964 void AddEventIfEnabled() {
965 // Only called when p_data_ is non-null.
966 if (*p_data_->category_enabled) {
967 TRACE_EVENT_API_ADD_TRACE_EVENT(
968 TRACE_EVENT_PHASE_END,
969 p_data_->category_enabled,
970 p_data_->name, kNoEventId,
971 kZeroNumArgs, NULL, NULL, NULL,
972 p_data_->threshold_begin_id, p_data_->threshold,
973 TRACE_EVENT_FLAG_NONE);
974 }
975 }
983 976
984 // This Data struct workaround is to avoid initializing all the members 977 // This Data struct workaround is to avoid initializing all the members
985 // in Data during construction of this object, since this object is always 978 // in Data during construction of this object, since this object is always
986 // constructed, even when tracing is disabled. If the members of Data were 979 // constructed, even when tracing is disabled. If the members of Data were
987 // members of this class instead, compiler warnings occur about potential 980 // members of this class instead, compiler warnings occur about potential
988 // uninitialized accesses. 981 // uninitialized accesses.
989 struct Data { 982 struct Data {
990 long long threshold; 983 long long threshold;
991 const unsigned char* category_enabled; 984 const unsigned char* category_enabled;
992 const char* name; 985 const char* name;
993 int threshold_begin_id; 986 int threshold_begin_id;
994 }; 987 };
995 Data* p_data_; 988 Data* p_data_;
996 Data data_; 989 Data data_;
997 }; 990 };
998 991
999 } // namespace trace_event_internal 992 } // namespace trace_event_internal
1000 993
1001 #endif // BASE_DEBUG_TRACE_EVENT_H_ 994 #endif // BASE_DEBUG_TRACE_EVENT_INTERNAL_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698