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 is designed to give you 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 // event to some other universe, you can copy-and-paste this file, |
8 // implement the TRACE_EVENT_API macros, and do any other necessary fixup for | 8 // implement the TRACE_EVENT_API macros, and do any other necessary fixup for |
9 // the target platform. The end result is that multiple libraries can funnel | 9 // the target platform. The end result is that multiple libraries can funnel |
10 // events through to a shared trace event collector. | 10 // events through to a shared trace event collector. |
(...skipping 274 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
285 category, name, TRACE_EVENT_FLAG_COPY) | 285 category, name, TRACE_EVENT_FLAG_COPY) |
286 #define TRACE_EVENT_COPY_END1(category, name, arg1_name, arg1_val) \ | 286 #define TRACE_EVENT_COPY_END1(category, name, arg1_name, arg1_val) \ |
287 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_END, \ | 287 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_END, \ |
288 category, name, TRACE_EVENT_FLAG_COPY, arg1_name, arg1_val) | 288 category, name, TRACE_EVENT_FLAG_COPY, arg1_name, arg1_val) |
289 #define TRACE_EVENT_COPY_END2(category, name, arg1_name, arg1_val, \ | 289 #define TRACE_EVENT_COPY_END2(category, name, arg1_name, arg1_val, \ |
290 arg2_name, arg2_val) \ | 290 arg2_name, arg2_val) \ |
291 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_END, \ | 291 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_END, \ |
292 category, name, TRACE_EVENT_FLAG_COPY, arg1_name, arg1_val, \ | 292 category, name, TRACE_EVENT_FLAG_COPY, arg1_name, arg1_val, \ |
293 arg2_name, arg2_val) | 293 arg2_name, arg2_val) |
294 | 294 |
295 // Time threshold event: | |
296 // Only record the event if the duration is greater than the specified | |
297 // threshold_us (time in microseconds). | |
298 // Records a pair of begin and end events called "name" for the current | |
299 // scope, with 0, 1 or 2 associated arguments. If the category is not | |
300 // enabled, then this does nothing. | |
301 // - category and name strings must have application lifetime (statics or | |
302 // literals). They may not include " chars. | |
303 #define TRACE_EVENT_IF_LONGER_THAN0(threshold_us, category, name) \ | |
304 INTERNAL_TRACE_EVENT_ADD_SCOPED_IF_LONGER_THAN(threshold_us, category, name) | |
305 #define TRACE_EVENT_IF_LONGER_THAN1( \ | |
306 threshold_us, category, name, arg1_name, arg1_val) \ | |
307 INTERNAL_TRACE_EVENT_ADD_SCOPED_IF_LONGER_THAN( \ | |
308 threshold_us, category, name, arg1_name, arg1_val) | |
309 #define TRACE_EVENT_IF_LONGER_THAN2( \ | |
310 threshold_us, category, name, arg1_name, arg1_val, arg2_name, arg2_val) \ | |
311 INTERNAL_TRACE_EVENT_ADD_SCOPED_IF_LONGER_THAN( \ | |
312 threshold_us, category, name, arg1_name, arg1_val, arg2_name, arg2_val) | |
313 | |
314 // Records the value of a counter called "name" immediately. Value | 295 // Records the value of a counter called "name" immediately. Value |
315 // must be representable as a 32 bit integer. | 296 // must be representable as a 32 bit integer. |
316 // - category and name strings must have application lifetime (statics or | 297 // - category and name strings must have application lifetime (statics or |
317 // literals). They may not include " chars. | 298 // literals). They may not include " chars. |
318 #define TRACE_COUNTER1(category, name, value) \ | 299 #define TRACE_COUNTER1(category, name, value) \ |
319 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_COUNTER, \ | 300 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_COUNTER, \ |
320 category, name, TRACE_EVENT_FLAG_NONE, \ | 301 category, name, TRACE_EVENT_FLAG_NONE, \ |
321 "value", static_cast<int>(value)) | 302 "value", static_cast<int>(value)) |
322 #define TRACE_COPY_COUNTER1(category, name, value) \ | 303 #define TRACE_COPY_COUNTER1(category, name, value) \ |
323 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_COUNTER, \ | 304 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_COUNTER, \ |
(...skipping 249 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
573 // unsigned char is non-zero, tracing is enabled. If tracing is enabled, | 554 // 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 | 555 // 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 | 556 // 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 | 557 // TRACE_EVENT_API_ADD_TRACE_EVENT, because this flag only provides an early out |
577 // for best performance when tracing is disabled. | 558 // for best performance when tracing is disabled. |
578 // const unsigned char* | 559 // const unsigned char* |
579 // TRACE_EVENT_API_GET_CATEGORY_ENABLED(const char* category_name) | 560 // TRACE_EVENT_API_GET_CATEGORY_ENABLED(const char* category_name) |
580 #define TRACE_EVENT_API_GET_CATEGORY_ENABLED \ | 561 #define TRACE_EVENT_API_GET_CATEGORY_ENABLED \ |
581 base::debug::TraceLog::GetCategoryEnabled | 562 base::debug::TraceLog::GetCategoryEnabled |
582 | 563 |
583 // Add a trace event to the platform tracing system. Returns thresholdBeginId | 564 // Add a trace event to the platform tracing system. |
584 // for use in a corresponding end TRACE_EVENT_API_ADD_TRACE_EVENT call. | 565 // void TRACE_EVENT_API_ADD_TRACE_EVENT( |
585 // int TRACE_EVENT_API_ADD_TRACE_EVENT( | |
586 // char phase, | 566 // char phase, |
587 // const unsigned char* category_enabled, | 567 // const unsigned char* category_enabled, |
588 // const char* name, | 568 // const char* name, |
589 // unsigned long long id, | 569 // unsigned long long id, |
590 // int num_args, | 570 // int num_args, |
591 // const char** arg_names, | 571 // const char** arg_names, |
592 // const unsigned char* arg_types, | 572 // const unsigned char* arg_types, |
593 // const unsigned long long* arg_values, | 573 // const unsigned long long* arg_values, |
594 // int threshold_begin_id, | |
595 // long long threshold, | |
596 // unsigned char flags) | 574 // unsigned char flags) |
597 #define TRACE_EVENT_API_ADD_TRACE_EVENT \ | 575 #define TRACE_EVENT_API_ADD_TRACE_EVENT \ |
598 base::debug::TraceLog::GetInstance()->AddTraceEvent | 576 base::debug::TraceLog::GetInstance()->AddTraceEvent |
599 | 577 |
600 //////////////////////////////////////////////////////////////////////////////// | 578 //////////////////////////////////////////////////////////////////////////////// |
601 | 579 |
602 // Implementation detail: trace event macros create temporary variables | 580 // Implementation detail: trace event macros create temporary variables |
603 // to keep instrumentation overhead low. These macros give each temporary | 581 // to keep instrumentation overhead low. These macros give each temporary |
604 // variable a unique name based on the line number to prevent name collissions. | 582 // variable a unique name based on the line number to prevent name collissions. |
605 #define INTERNAL_TRACE_EVENT_UID3(a,b) \ | 583 #define INTERNAL_TRACE_EVENT_UID3(a,b) \ |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
648 if (*INTERNAL_TRACE_EVENT_UID(catstatic)) { \ | 626 if (*INTERNAL_TRACE_EVENT_UID(catstatic)) { \ |
649 trace_event_internal::AddTraceEvent( \ | 627 trace_event_internal::AddTraceEvent( \ |
650 TRACE_EVENT_PHASE_BEGIN, \ | 628 TRACE_EVENT_PHASE_BEGIN, \ |
651 INTERNAL_TRACE_EVENT_UID(catstatic), \ | 629 INTERNAL_TRACE_EVENT_UID(catstatic), \ |
652 name, trace_event_internal::kNoEventId, \ | 630 name, trace_event_internal::kNoEventId, \ |
653 TRACE_EVENT_FLAG_NONE, ##__VA_ARGS__); \ | 631 TRACE_EVENT_FLAG_NONE, ##__VA_ARGS__); \ |
654 INTERNAL_TRACE_EVENT_UID(profileScope).Initialize( \ | 632 INTERNAL_TRACE_EVENT_UID(profileScope).Initialize( \ |
655 INTERNAL_TRACE_EVENT_UID(catstatic), name); \ | 633 INTERNAL_TRACE_EVENT_UID(catstatic), name); \ |
656 } | 634 } |
657 | 635 |
658 // Implementation detail: internal macro to create static category and add begin | |
659 // event if the category is enabled. Also adds the end event when the scope | |
660 // ends. If the elapsed time is < threshold time, the begin/end pair is erased. | |
661 #define INTERNAL_TRACE_EVENT_ADD_SCOPED_IF_LONGER_THAN(threshold, \ | |
662 category, name, ...) \ | |
663 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category); \ | |
664 trace_event_internal::TraceEndOnScopeCloseThreshold \ | |
665 INTERNAL_TRACE_EVENT_UID(profileScope); \ | |
666 if (*INTERNAL_TRACE_EVENT_UID(catstatic)) { \ | |
667 int INTERNAL_TRACE_EVENT_UID(begin_event_id) = \ | |
668 trace_event_internal::AddTraceEvent( \ | |
669 TRACE_EVENT_PHASE_BEGIN, \ | |
670 INTERNAL_TRACE_EVENT_UID(catstatic), \ | |
671 name, trace_event_internal::kNoEventId, \ | |
672 TRACE_EVENT_FLAG_NONE, ##__VA_ARGS__); \ | |
673 INTERNAL_TRACE_EVENT_UID(profileScope).Initialize( \ | |
674 INTERNAL_TRACE_EVENT_UID(catstatic), name, \ | |
675 INTERNAL_TRACE_EVENT_UID(begin_event_id), threshold); \ | |
676 } | |
677 | |
678 // Implementation detail: internal macro to create static category and add | 636 // Implementation detail: internal macro to create static category and add |
679 // event if the category is enabled. | 637 // event if the category is enabled. |
680 #define INTERNAL_TRACE_EVENT_ADD_WITH_ID(phase, category, name, id, flags, \ | 638 #define INTERNAL_TRACE_EVENT_ADD_WITH_ID(phase, category, name, id, flags, \ |
681 ...) \ | 639 ...) \ |
682 do { \ | 640 do { \ |
683 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category); \ | 641 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category); \ |
684 if (*INTERNAL_TRACE_EVENT_UID(catstatic)) { \ | 642 if (*INTERNAL_TRACE_EVENT_UID(catstatic)) { \ |
685 unsigned char trace_event_flags = flags | TRACE_EVENT_FLAG_HAS_ID; \ | 643 unsigned char trace_event_flags = flags | TRACE_EVENT_FLAG_HAS_ID; \ |
686 trace_event_internal::TraceID trace_event_trace_id( \ | 644 trace_event_internal::TraceID trace_event_trace_id( \ |
687 id, &trace_event_flags); \ | 645 id, &trace_event_flags); \ |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
723 #define TRACE_VALUE_TYPE_DOUBLE (static_cast<unsigned char>(4)) | 681 #define TRACE_VALUE_TYPE_DOUBLE (static_cast<unsigned char>(4)) |
724 #define TRACE_VALUE_TYPE_POINTER (static_cast<unsigned char>(5)) | 682 #define TRACE_VALUE_TYPE_POINTER (static_cast<unsigned char>(5)) |
725 #define TRACE_VALUE_TYPE_STRING (static_cast<unsigned char>(6)) | 683 #define TRACE_VALUE_TYPE_STRING (static_cast<unsigned char>(6)) |
726 #define TRACE_VALUE_TYPE_COPY_STRING (static_cast<unsigned char>(7)) | 684 #define TRACE_VALUE_TYPE_COPY_STRING (static_cast<unsigned char>(7)) |
727 | 685 |
728 namespace trace_event_internal { | 686 namespace trace_event_internal { |
729 | 687 |
730 // Specify these values when the corresponding argument of AddTraceEvent is not | 688 // Specify these values when the corresponding argument of AddTraceEvent is not |
731 // used. | 689 // used. |
732 const int kZeroNumArgs = 0; | 690 const int kZeroNumArgs = 0; |
733 const int kNoThreshholdBeginId = -1; | |
734 const long long kNoThresholdValue = 0; | |
735 const unsigned long long kNoEventId = 0; | 691 const unsigned long long kNoEventId = 0; |
736 | 692 |
737 // TraceID encapsulates an ID that can either be an integer or pointer. Pointers | 693 // TraceID encapsulates an ID that can either be an integer or pointer. Pointers |
738 // are mangled with the Process ID so that they are unlikely to collide when the | 694 // are mangled with the Process ID so that they are unlikely to collide when the |
739 // same pointer is used on different processes. | 695 // same pointer is used on different processes. |
740 class TraceID { | 696 class TraceID { |
741 public: | 697 public: |
742 class ForceMangle { | 698 class ForceMangle { |
743 public: | 699 public: |
744 explicit ForceMangle(unsigned long long id) : data_(id) {} | 700 explicit ForceMangle(unsigned long long id) : data_(id) {} |
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
872 *type = TRACE_VALUE_TYPE_COPY_STRING; | 828 *type = TRACE_VALUE_TYPE_COPY_STRING; |
873 *value = type_value.as_uint; | 829 *value = type_value.as_uint; |
874 } | 830 } |
875 | 831 |
876 // These AddTraceEvent template functions are defined here instead of in the | 832 // These AddTraceEvent template functions are defined here instead of in the |
877 // macro, because the arg_values could be temporary objects, such as | 833 // macro, because the arg_values could be temporary objects, such as |
878 // std::string. In order to store pointers to the internal c_str and pass | 834 // std::string. In order to store pointers to the internal c_str and pass |
879 // through to the tracing API, the arg_values must live throughout | 835 // through to the tracing API, the arg_values must live throughout |
880 // these procedures. | 836 // these procedures. |
881 | 837 |
882 static inline int AddTraceEvent(char phase, | 838 static inline void AddTraceEvent(char phase, |
883 const unsigned char* category_enabled, | 839 const unsigned char* category_enabled, |
884 const char* name, | 840 const char* name, |
885 unsigned long long id, | 841 unsigned long long id, |
886 unsigned char flags) { | 842 unsigned char flags) { |
887 return TRACE_EVENT_API_ADD_TRACE_EVENT( | 843 TRACE_EVENT_API_ADD_TRACE_EVENT( |
888 phase, category_enabled, name, id, | 844 phase, category_enabled, name, id, |
889 kZeroNumArgs, NULL, NULL, NULL, | 845 kZeroNumArgs, NULL, NULL, NULL, |
890 kNoThreshholdBeginId, kNoThresholdValue, flags); | 846 flags); |
891 } | 847 } |
892 | 848 |
893 template<class ARG1_TYPE> | 849 template<class ARG1_TYPE> |
894 static inline int AddTraceEvent(char phase, | 850 static inline void AddTraceEvent(char phase, |
895 const unsigned char* category_enabled, | 851 const unsigned char* category_enabled, |
896 const char* name, | 852 const char* name, |
897 unsigned long long id, | 853 unsigned long long id, |
898 unsigned char flags, | 854 unsigned char flags, |
899 const char* arg1_name, | 855 const char* arg1_name, |
900 const ARG1_TYPE& arg1_val) { | 856 const ARG1_TYPE& arg1_val) { |
901 const int num_args = 1; | 857 const int num_args = 1; |
902 unsigned char arg_types[1]; | 858 unsigned char arg_types[1]; |
903 unsigned long long arg_values[1]; | 859 unsigned long long arg_values[1]; |
904 SetTraceValue(arg1_val, &arg_types[0], &arg_values[0]); | 860 SetTraceValue(arg1_val, &arg_types[0], &arg_values[0]); |
905 return TRACE_EVENT_API_ADD_TRACE_EVENT( | 861 TRACE_EVENT_API_ADD_TRACE_EVENT( |
906 phase, category_enabled, name, id, | 862 phase, category_enabled, name, id, |
907 num_args, &arg1_name, arg_types, arg_values, | 863 num_args, &arg1_name, arg_types, arg_values, |
908 kNoThreshholdBeginId, kNoThresholdValue, flags); | 864 flags); |
909 } | 865 } |
910 | 866 |
911 template<class ARG1_TYPE, class ARG2_TYPE> | 867 template<class ARG1_TYPE, class ARG2_TYPE> |
912 static inline int AddTraceEvent(char phase, | 868 static inline void AddTraceEvent(char phase, |
913 const unsigned char* category_enabled, | 869 const unsigned char* category_enabled, |
914 const char* name, | 870 const char* name, |
915 unsigned long long id, | 871 unsigned long long id, |
916 unsigned char flags, | 872 unsigned char flags, |
917 const char* arg1_name, | 873 const char* arg1_name, |
918 const ARG1_TYPE& arg1_val, | 874 const ARG1_TYPE& arg1_val, |
919 const char* arg2_name, | 875 const char* arg2_name, |
920 const ARG2_TYPE& arg2_val) { | 876 const ARG2_TYPE& arg2_val) { |
921 const int num_args = 2; | 877 const int num_args = 2; |
922 const char* arg_names[2] = { arg1_name, arg2_name }; | 878 const char* arg_names[2] = { arg1_name, arg2_name }; |
923 unsigned char arg_types[2]; | 879 unsigned char arg_types[2]; |
924 unsigned long long arg_values[2]; | 880 unsigned long long arg_values[2]; |
925 SetTraceValue(arg1_val, &arg_types[0], &arg_values[0]); | 881 SetTraceValue(arg1_val, &arg_types[0], &arg_values[0]); |
926 SetTraceValue(arg2_val, &arg_types[1], &arg_values[1]); | 882 SetTraceValue(arg2_val, &arg_types[1], &arg_values[1]); |
927 return TRACE_EVENT_API_ADD_TRACE_EVENT( | 883 TRACE_EVENT_API_ADD_TRACE_EVENT( |
928 phase, category_enabled, name, id, | 884 phase, category_enabled, name, id, |
929 num_args, arg_names, arg_types, arg_values, | 885 num_args, arg_names, arg_types, arg_values, |
930 kNoThreshholdBeginId, kNoThresholdValue, flags); | 886 flags); |
931 } | 887 } |
932 | 888 |
933 // Used by TRACE_EVENTx macro. Do not use directly. | 889 // Used by TRACE_EVENTx macro. Do not use directly. |
934 class BASE_EXPORT TraceEndOnScopeClose { | 890 class BASE_EXPORT TraceEndOnScopeClose { |
935 public: | 891 public: |
936 // Note: members of data_ intentionally left uninitialized. See Initialize. | 892 // Note: members of data_ intentionally left uninitialized. See Initialize. |
937 TraceEndOnScopeClose() : p_data_(NULL) {} | 893 TraceEndOnScopeClose() : p_data_(NULL) {} |
938 ~TraceEndOnScopeClose() { | 894 ~TraceEndOnScopeClose() { |
939 if (p_data_) | 895 if (p_data_) |
940 AddEventIfEnabled(); | 896 AddEventIfEnabled(); |
(...skipping 12 matching lines...) Expand all Loading... |
953 // members of this class instead, compiler warnings occur about potential | 909 // members of this class instead, compiler warnings occur about potential |
954 // uninitialized accesses. | 910 // uninitialized accesses. |
955 struct Data { | 911 struct Data { |
956 const unsigned char* category_enabled; | 912 const unsigned char* category_enabled; |
957 const char* name; | 913 const char* name; |
958 }; | 914 }; |
959 Data* p_data_; | 915 Data* p_data_; |
960 Data data_; | 916 Data data_; |
961 }; | 917 }; |
962 | 918 |
963 // Used by TRACE_EVENTx macro. Do not use directly. | |
964 class BASE_EXPORT TraceEndOnScopeCloseThreshold { | |
965 public: | |
966 // Note: members of data_ intentionally left uninitialized. See Initialize. | |
967 TraceEndOnScopeCloseThreshold() : p_data_(NULL) {} | |
968 ~TraceEndOnScopeCloseThreshold() { | |
969 if (p_data_) | |
970 AddEventIfEnabled(); | |
971 } | |
972 | |
973 // Called by macros only when tracing is enabled at the point when the begin | |
974 // event is added. | |
975 void Initialize(const unsigned char* category_enabled, | |
976 const char* name, | |
977 int threshold_begin_id, | |
978 long long threshold); | |
979 | |
980 private: | |
981 // Add the end event if the category is still enabled. | |
982 void AddEventIfEnabled(); | |
983 | |
984 // This Data struct workaround is to avoid initializing all the members | |
985 // 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 | |
987 // members of this class instead, compiler warnings occur about potential | |
988 // uninitialized accesses. | |
989 struct Data { | |
990 long long threshold; | |
991 const unsigned char* category_enabled; | |
992 const char* name; | |
993 int threshold_begin_id; | |
994 }; | |
995 Data* p_data_; | |
996 Data data_; | |
997 }; | |
998 | 919 |
999 } // namespace trace_event_internal | 920 } // namespace trace_event_internal |
1000 | 921 |
1001 #endif // BASE_DEBUG_TRACE_EVENT_H_ | 922 #endif // BASE_DEBUG_TRACE_EVENT_H_ |
OLD | NEW |