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 |
| 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, |
| 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 |
| 10 // events through to a shared trace event collector. |
| 11 |
5 // Trace events are for tracking application performance and resource usage. | 12 // Trace events are for tracking application performance and resource usage. |
6 // Macros are provided to track: | 13 // Macros are provided to track: |
7 // Begin and end of function calls | 14 // Begin and end of function calls |
8 // Counters | 15 // Counters |
9 // | 16 // |
10 // Events are issued against categories. Whereas LOG's | 17 // Events are issued against categories. Whereas LOG's |
11 // categories are statically defined, TRACE categories are created | 18 // categories are statically defined, TRACE categories are created |
12 // implicitly with a string. For example: | 19 // implicitly with a string. For example: |
13 // TRACE_EVENT_INSTANT0("MY_SUBSYSTEM", "SomeImportantEvent") | 20 // TRACE_EVENT_INSTANT0("MY_SUBSYSTEM", "SomeImportantEvent") |
14 // | 21 // |
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
140 // and resolving the category. | 147 // and resolving the category. |
141 // | 148 // |
142 // ANNOTATE_BENIGN_RACE is used to suppress the warning on the static category | 149 // ANNOTATE_BENIGN_RACE is used to suppress the warning on the static category |
143 // pointers. | 150 // pointers. |
144 | 151 |
145 | 152 |
146 #ifndef BASE_DEBUG_TRACE_EVENT_H_ | 153 #ifndef BASE_DEBUG_TRACE_EVENT_H_ |
147 #define BASE_DEBUG_TRACE_EVENT_H_ | 154 #define BASE_DEBUG_TRACE_EVENT_H_ |
148 #pragma once | 155 #pragma once |
149 | 156 |
| 157 #include <string> |
| 158 |
150 #include "build/build_config.h" | 159 #include "build/build_config.h" |
151 | 160 #include "base/debug/trace_event_impl.h" |
152 #include <string> | |
153 #include <vector> | |
154 | |
155 #include "base/callback.h" | |
156 #include "base/hash_tables.h" | |
157 #include "base/memory/ref_counted_memory.h" | |
158 #include "base/string_util.h" | |
159 #include "base/synchronization/lock.h" | |
160 #include "base/third_party/dynamic_annotations/dynamic_annotations.h" | |
161 #include "base/timer.h" | |
162 | |
163 //////////////////////////////////////////////////////////////////////////////// | |
164 // TODO(jbates): split this into a separate header. | |
165 // This header is designed to be give you trace_event macros without specifying | |
166 // how the events actually get collected and stored. If you need to expose trace | |
167 // event to some other universe, you can copy-and-paste this file and just | |
168 // implement these API macros. | |
169 | |
170 // unsigned char* | |
171 // TRACE_EVENT_API_GET_CATEGORY_ENABLED(const char* category_name) | |
172 #define TRACE_EVENT_API_GET_CATEGORY_ENABLED \ | |
173 base::debug::TraceLog::GetCategoryEnabled | |
174 | |
175 // Returns the threshold_begin_id used by TRACE_IF_LONGER_THAN macros. | |
176 // int TRACE_EVENT_API_ADD_TRACE_EVENT( | |
177 // char phase, | |
178 // const unsigned char* category_enabled, | |
179 // const char* name, | |
180 // unsigned long long id, | |
181 // int num_args, | |
182 // const char** arg_names, | |
183 // const unsigned char* arg_types, | |
184 // const unsigned long long* arg_values, | |
185 // int threshold_begin_id, | |
186 // long long threshold, | |
187 // unsigned char flags) | |
188 #define TRACE_EVENT_API_ADD_TRACE_EVENT \ | |
189 base::debug::TraceLog::GetInstance()->AddTraceEvent | |
190 | |
191 // void TRACE_EVENT_API_ADD_COUNTER_EVENT( | |
192 // const unsigned char* category_enabled, | |
193 // const char* name, | |
194 // unsigned long long id, | |
195 // const char* arg1_name, int arg1_val, | |
196 // const char* arg2_name, int arg2_val, | |
197 // unsigned char flags) | |
198 #define TRACE_EVENT_API_ADD_COUNTER_EVENT \ | |
199 base::debug::TraceLog::GetInstance()->AddCounterEvent | |
200 | |
201 // Mangle |pointer| with a process ID hash so that if |pointer| occurs on more | |
202 // than one process, it will not collide in the trace data. | |
203 // unsigned long long TRACE_EVENT_API_GET_ID_FROM_POINTER(void* pointer) | |
204 #define TRACE_EVENT_API_GET_ID_FROM_POINTER \ | |
205 base::debug::TraceLog::GetInstance()->GetInterProcessID | |
206 | |
207 //////////////////////////////////////////////////////////////////////////////// | |
208 | 161 |
209 // By default, const char* argument values are assumed to have long-lived scope | 162 // By default, const char* argument values are assumed to have long-lived scope |
210 // and will not be copied. Use this macro to force a const char* to be copied. | 163 // and will not be copied. Use this macro to force a const char* to be copied. |
211 #define TRACE_STR_COPY(str) \ | 164 #define TRACE_STR_COPY(str) \ |
212 trace_event_internal::TraceStringWithCopy(str) | 165 trace_event_internal::TraceStringWithCopy(str) |
213 | 166 |
214 // Older style trace macros with explicit id and extra data | |
215 // Only these macros result in publishing data to ETW as currently implemented. | |
216 #define TRACE_EVENT_BEGIN_ETW(name, id, extra) \ | |
217 base::debug::TraceLog::AddTraceEventEtw( \ | |
218 TRACE_EVENT_PHASE_BEGIN, \ | |
219 name, reinterpret_cast<const void*>(id), extra) | |
220 | |
221 #define TRACE_EVENT_END_ETW(name, id, extra) \ | |
222 base::debug::TraceLog::AddTraceEventEtw( \ | |
223 TRACE_EVENT_PHASE_END, \ | |
224 name, reinterpret_cast<const void*>(id), extra) | |
225 | |
226 #define TRACE_EVENT_INSTANT_ETW(name, id, extra) \ | |
227 base::debug::TraceLog::AddTraceEventEtw( \ | |
228 TRACE_EVENT_PHASE_INSTANT, \ | |
229 name, reinterpret_cast<const void*>(id), extra) | |
230 | |
231 // Records a pair of begin and end events called "name" for the current | 167 // Records a pair of begin and end events called "name" for the current |
232 // scope, with 0, 1 or 2 associated arguments. If the category is not | 168 // scope, with 0, 1 or 2 associated arguments. If the category is not |
233 // enabled, then this does nothing. | 169 // enabled, then this does nothing. |
234 // - category and name strings must have application lifetime (statics or | 170 // - category and name strings must have application lifetime (statics or |
235 // literals). They may not include " chars. | 171 // literals). They may not include " chars. |
236 #define TRACE_EVENT0(category, name) \ | 172 #define TRACE_EVENT0(category, name) \ |
237 INTERNAL_TRACE_EVENT_ADD_SCOPED(category, name) | 173 INTERNAL_TRACE_EVENT_ADD_SCOPED(category, name) |
238 #define TRACE_EVENT1(category, name, arg1_name, arg1_val) \ | 174 #define TRACE_EVENT1(category, name, arg1_name, arg1_val) \ |
239 INTERNAL_TRACE_EVENT_ADD_SCOPED(category, name, arg1_name, arg1_val) | 175 INTERNAL_TRACE_EVENT_ADD_SCOPED(category, name, arg1_name, arg1_val) |
240 #define TRACE_EVENT2(category, name, arg1_name, arg1_val, arg2_name, arg2_val) \ | 176 #define TRACE_EVENT2(category, name, arg1_name, arg1_val, arg2_name, arg2_val) \ |
(...skipping 247 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
488 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FINISH, \ | 424 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FINISH, \ |
489 category, name, id, TRACE_EVENT_FLAG_HAS_ID | TRACE_EVENT_FLAG_COPY, \ | 425 category, name, id, TRACE_EVENT_FLAG_HAS_ID | TRACE_EVENT_FLAG_COPY, \ |
490 arg1_name, arg1_val) | 426 arg1_name, arg1_val) |
491 #define TRACE_EVENT_COPY_FINISH2(category, name, id, arg1_name, arg1_val, \ | 427 #define TRACE_EVENT_COPY_FINISH2(category, name, id, arg1_name, arg1_val, \ |
492 arg2_name, arg2_val) \ | 428 arg2_name, arg2_val) \ |
493 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FINISH, \ | 429 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FINISH, \ |
494 category, name, id, TRACE_EVENT_FLAG_HAS_ID | TRACE_EVENT_FLAG_COPY, \ | 430 category, name, id, TRACE_EVENT_FLAG_HAS_ID | TRACE_EVENT_FLAG_COPY, \ |
495 arg1_name, arg1_val, arg2_name, arg2_val) | 431 arg1_name, arg1_val, arg2_name, arg2_val) |
496 | 432 |
497 | 433 |
| 434 //////////////////////////////////////////////////////////////////////////////// |
| 435 // Implementation specific tracing API definitions. |
| 436 |
| 437 // const unsigned char* |
| 438 // TRACE_EVENT_API_GET_CATEGORY_ENABLED(const char* category_name) |
| 439 #define TRACE_EVENT_API_GET_CATEGORY_ENABLED \ |
| 440 base::debug::TraceLog::GetCategoryEnabled |
| 441 |
| 442 // Returns the threshold_begin_id used by TRACE_IF_LONGER_THAN macros. |
| 443 // int TRACE_EVENT_API_ADD_TRACE_EVENT( |
| 444 // char phase, |
| 445 // const unsigned char* category_enabled, |
| 446 // const char* name, |
| 447 // unsigned long long id, |
| 448 // int num_args, |
| 449 // const char** arg_names, |
| 450 // const unsigned char* arg_types, |
| 451 // const unsigned long long* arg_values, |
| 452 // int threshold_begin_id, |
| 453 // long long threshold, |
| 454 // unsigned char flags) |
| 455 #define TRACE_EVENT_API_ADD_TRACE_EVENT \ |
| 456 base::debug::TraceLog::GetInstance()->AddTraceEvent |
| 457 |
| 458 // void TRACE_EVENT_API_ADD_COUNTER_EVENT( |
| 459 // const unsigned char* category_enabled, |
| 460 // const char* name, |
| 461 // unsigned long long id, |
| 462 // const char* arg1_name, int arg1_val, |
| 463 // const char* arg2_name, int arg2_val, |
| 464 // unsigned char flags) |
| 465 #define TRACE_EVENT_API_ADD_COUNTER_EVENT \ |
| 466 base::debug::TraceLog::GetInstance()->AddCounterEvent |
| 467 |
| 468 // Mangle |pointer| with a process ID hash so that if |pointer| occurs on more |
| 469 // than one process, it will not collide in the trace data. |
| 470 // unsigned long long TRACE_EVENT_API_GET_ID_FROM_POINTER(void* pointer) |
| 471 #define TRACE_EVENT_API_GET_ID_FROM_POINTER \ |
| 472 base::debug::TraceLog::GetInstance()->GetInterProcessID |
| 473 |
| 474 //////////////////////////////////////////////////////////////////////////////// |
| 475 |
498 // Implementation detail: trace event macros create temporary variables | 476 // Implementation detail: trace event macros create temporary variables |
499 // to keep instrumentation overhead low. These macros give each temporary | 477 // to keep instrumentation overhead low. These macros give each temporary |
500 // variable a unique name based on the line number to prevent name collissions. | 478 // variable a unique name based on the line number to prevent name collissions. |
501 #define INTERNAL_TRACE_EVENT_UID3(a,b) \ | 479 #define INTERNAL_TRACE_EVENT_UID3(a,b) \ |
502 trace_event_unique_##a##b | 480 trace_event_unique_##a##b |
503 #define INTERNAL_TRACE_EVENT_UID2(a,b) \ | 481 #define INTERNAL_TRACE_EVENT_UID2(a,b) \ |
504 INTERNAL_TRACE_EVENT_UID3(a,b) | 482 INTERNAL_TRACE_EVENT_UID3(a,b) |
505 #define INTERNAL_TRACE_EVENT_UID(name_prefix) \ | 483 #define INTERNAL_TRACE_EVENT_UID(name_prefix) \ |
506 INTERNAL_TRACE_EVENT_UID2(name_prefix, __LINE__) | 484 INTERNAL_TRACE_EVENT_UID2(name_prefix, __LINE__) |
507 | 485 |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
579 #define INTERNAL_TRACE_EVENT_ADD_WITH_ID(phase, category, name, id, flags, \ | 557 #define INTERNAL_TRACE_EVENT_ADD_WITH_ID(phase, category, name, id, flags, \ |
580 ...) \ | 558 ...) \ |
581 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category); \ | 559 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category); \ |
582 if (*INTERNAL_TRACE_EVENT_UID(catstatic)) { \ | 560 if (*INTERNAL_TRACE_EVENT_UID(catstatic)) { \ |
583 trace_event_internal::AddTraceEvent( \ | 561 trace_event_internal::AddTraceEvent( \ |
584 phase, INTERNAL_TRACE_EVENT_UID(catstatic), \ | 562 phase, INTERNAL_TRACE_EVENT_UID(catstatic), \ |
585 name, trace_event_internal::TraceID(id).data(), flags, \ | 563 name, trace_event_internal::TraceID(id).data(), flags, \ |
586 ##__VA_ARGS__); \ | 564 ##__VA_ARGS__); \ |
587 } | 565 } |
588 | 566 |
589 template <typename Type> | |
590 struct StaticMemorySingletonTraits; | |
591 | |
592 namespace base { | |
593 | |
594 class RefCountedString; | |
595 | |
596 namespace debug { | |
597 | |
598 const int kTraceMaxNumArgs = 2; | |
599 | |
600 // Notes regarding the following definitions: | 567 // Notes regarding the following definitions: |
601 // New values can be added and propagated to third party libraries, but existing | 568 // New values can be added and propagated to third party libraries, but existing |
602 // definitions must never be changed, because third party libraries may use old | 569 // definitions must never be changed, because third party libraries may use old |
603 // definitions. | 570 // definitions. |
604 | 571 |
605 // Phase indicates the nature of an event entry. E.g. part of a begin/end pair. | 572 // Phase indicates the nature of an event entry. E.g. part of a begin/end pair. |
606 #define TRACE_EVENT_PHASE_BEGIN ('B') | 573 #define TRACE_EVENT_PHASE_BEGIN ('B') |
607 #define TRACE_EVENT_PHASE_END ('E') | 574 #define TRACE_EVENT_PHASE_END ('E') |
608 #define TRACE_EVENT_PHASE_INSTANT ('I') | 575 #define TRACE_EVENT_PHASE_INSTANT ('I') |
609 #define TRACE_EVENT_PHASE_START ('S') | 576 #define TRACE_EVENT_PHASE_START ('S') |
610 #define TRACE_EVENT_PHASE_FINISH ('F') | 577 #define TRACE_EVENT_PHASE_FINISH ('F') |
611 #define TRACE_EVENT_PHASE_METADATA ('M') | 578 #define TRACE_EVENT_PHASE_METADATA ('M') |
612 #define TRACE_EVENT_PHASE_COUNTER ('C') | 579 #define TRACE_EVENT_PHASE_COUNTER ('C') |
613 | 580 |
614 // Flags for changing the behavior of TRACE_EVENT_API_ADD_TRACE_EVENT. | 581 // Flags for changing the behavior of TRACE_EVENT_API_ADD_TRACE_EVENT. |
615 #define TRACE_EVENT_FLAG_NONE (static_cast<unsigned char>(0)) | 582 #define TRACE_EVENT_FLAG_NONE (static_cast<unsigned char>(0)) |
616 #define TRACE_EVENT_FLAG_COPY (static_cast<unsigned char>(1<<0)) | 583 #define TRACE_EVENT_FLAG_COPY (static_cast<unsigned char>(1<<0)) |
617 #define TRACE_EVENT_FLAG_HAS_ID (static_cast<unsigned char>(1<<1)) | 584 #define TRACE_EVENT_FLAG_HAS_ID (static_cast<unsigned char>(1<<1)) |
618 | 585 |
619 // Type values for identifying types in the TraceValue union. | 586 // Type values for identifying types in the TraceValue union. |
620 #define TRACE_VALUE_TYPE_BOOL (static_cast<unsigned char>(1)) | 587 #define TRACE_VALUE_TYPE_BOOL (static_cast<unsigned char>(1)) |
621 #define TRACE_VALUE_TYPE_UINT (static_cast<unsigned char>(2)) | 588 #define TRACE_VALUE_TYPE_UINT (static_cast<unsigned char>(2)) |
622 #define TRACE_VALUE_TYPE_INT (static_cast<unsigned char>(3)) | 589 #define TRACE_VALUE_TYPE_INT (static_cast<unsigned char>(3)) |
623 #define TRACE_VALUE_TYPE_DOUBLE (static_cast<unsigned char>(4)) | 590 #define TRACE_VALUE_TYPE_DOUBLE (static_cast<unsigned char>(4)) |
624 #define TRACE_VALUE_TYPE_POINTER (static_cast<unsigned char>(5)) | 591 #define TRACE_VALUE_TYPE_POINTER (static_cast<unsigned char>(5)) |
625 #define TRACE_VALUE_TYPE_STRING (static_cast<unsigned char>(6)) | 592 #define TRACE_VALUE_TYPE_STRING (static_cast<unsigned char>(6)) |
626 #define TRACE_VALUE_TYPE_COPY_STRING (static_cast<unsigned char>(7)) | 593 #define TRACE_VALUE_TYPE_COPY_STRING (static_cast<unsigned char>(7)) |
627 | 594 |
628 // Output records are "Events" and can be obtained via the | |
629 // OutputCallback whenever the tracing system decides to flush. This | |
630 // can happen at any time, on any thread, or you can programatically | |
631 // force it to happen. | |
632 class BASE_EXPORT TraceEvent { | |
633 public: | |
634 union TraceValue { | |
635 bool as_bool; | |
636 unsigned long long as_uint; | |
637 long long as_int; | |
638 double as_double; | |
639 const void* as_pointer; | |
640 const char* as_string; | |
641 }; | |
642 | |
643 TraceEvent(); | |
644 TraceEvent(int thread_id, | |
645 TimeTicks timestamp, | |
646 char phase, | |
647 const unsigned char* category_enabled, | |
648 const char* name, | |
649 unsigned long long id, | |
650 int num_args, | |
651 const char** arg_names, | |
652 const unsigned char* arg_types, | |
653 const unsigned long long* arg_values, | |
654 unsigned char flags); | |
655 ~TraceEvent(); | |
656 | |
657 // Serialize event data to JSON | |
658 static void AppendEventsAsJSON(const std::vector<TraceEvent>& events, | |
659 size_t start, | |
660 size_t count, | |
661 std::string* out); | |
662 void AppendAsJSON(std::string* out) const; | |
663 | |
664 TimeTicks timestamp() const { return timestamp_; } | |
665 | |
666 // Exposed for unittesting: | |
667 | |
668 const base::RefCountedString* parameter_copy_storage() const { | |
669 return parameter_copy_storage_.get(); | |
670 } | |
671 | |
672 const char* name() const { return name_; } | |
673 | |
674 private: | |
675 // Note: these are ordered by size (largest first) for optimal packing. | |
676 TimeTicks timestamp_; | |
677 // id_ can be used to store phase-specific data. | |
678 unsigned long long id_; | |
679 TraceValue arg_values_[kTraceMaxNumArgs]; | |
680 const char* arg_names_[kTraceMaxNumArgs]; | |
681 const unsigned char* category_enabled_; | |
682 const char* name_; | |
683 scoped_refptr<base::RefCountedString> parameter_copy_storage_; | |
684 int thread_id_; | |
685 char phase_; | |
686 unsigned char flags_; | |
687 unsigned char arg_types_[kTraceMaxNumArgs]; | |
688 }; | |
689 | |
690 | |
691 // TraceResultBuffer collects and converts trace fragments returned by TraceLog | |
692 // to JSON output. | |
693 class BASE_EXPORT TraceResultBuffer { | |
694 public: | |
695 typedef base::Callback<void(const std::string&)> OutputCallback; | |
696 | |
697 // If you don't need to stream JSON chunks out efficiently, and just want to | |
698 // get a complete JSON string after calling Finish, use this struct to collect | |
699 // JSON trace output. | |
700 struct BASE_EXPORT SimpleOutput { | |
701 OutputCallback GetCallback(); | |
702 void Append(const std::string& json_string); | |
703 | |
704 // Do what you want with the json_output_ string after calling | |
705 // TraceResultBuffer::Finish. | |
706 std::string json_output; | |
707 }; | |
708 | |
709 TraceResultBuffer(); | |
710 ~TraceResultBuffer(); | |
711 | |
712 // Set callback. The callback will be called during Start with the initial | |
713 // JSON output and during AddFragment and Finish with following JSON output | |
714 // chunks. The callback target must live past the last calls to | |
715 // TraceResultBuffer::Start/AddFragment/Finish. | |
716 void SetOutputCallback(const OutputCallback& json_chunk_callback); | |
717 | |
718 // Start JSON output. This resets all internal state, so you can reuse | |
719 // the TraceResultBuffer by calling Start. | |
720 void Start(); | |
721 | |
722 // Call AddFragment 0 or more times to add trace fragments from TraceLog. | |
723 void AddFragment(const std::string& trace_fragment); | |
724 | |
725 // When all fragments have been added, call Finish to complete the JSON | |
726 // formatted output. | |
727 void Finish(); | |
728 | |
729 private: | |
730 OutputCallback output_callback_; | |
731 bool append_comma_; | |
732 }; | |
733 | |
734 | |
735 class BASE_EXPORT TraceLog { | |
736 public: | |
737 static TraceLog* GetInstance(); | |
738 | |
739 // Get set of known categories. This can change as new code paths are reached. | |
740 // The known categories are inserted into |categories|. | |
741 void GetKnownCategories(std::vector<std::string>* categories); | |
742 | |
743 // Enable tracing for provided list of categories. If tracing is already | |
744 // enabled, this method does nothing -- changing categories during trace is | |
745 // not supported. | |
746 // If both included_categories and excluded_categories are empty, | |
747 // all categories are traced. | |
748 // Else if included_categories is non-empty, only those are traced. | |
749 // Else if excluded_categories is non-empty, everything but those are traced. | |
750 // Wildcards * and ? are supported (see MatchPattern in string_util.h). | |
751 void SetEnabled(const std::vector<std::string>& included_categories, | |
752 const std::vector<std::string>& excluded_categories); | |
753 | |
754 // |categories| is a comma-delimited list of category wildcards. | |
755 // A category can have an optional '-' prefix to make it an excluded category. | |
756 // All the same rules apply above, so for example, having both included and | |
757 // excluded categories in the same list would not be supported. | |
758 // | |
759 // Example: SetEnabled("test_MyTest*"); | |
760 // Example: SetEnabled("test_MyTest*,test_OtherStuff"); | |
761 // Example: SetEnabled("-excluded_category1,-excluded_category2"); | |
762 void SetEnabled(const std::string& categories); | |
763 | |
764 // Retieves the categories set via a prior call to SetEnabled(). Only | |
765 // meaningful if |IsEnabled()| is true. | |
766 void GetEnabledTraceCategories(std::vector<std::string>* included_out, | |
767 std::vector<std::string>* excluded_out); | |
768 | |
769 // Disable tracing for all categories. | |
770 void SetDisabled(); | |
771 // Helper method to enable/disable tracing for all categories. | |
772 void SetEnabled(bool enabled); | |
773 bool IsEnabled() { return enabled_; } | |
774 | |
775 float GetBufferPercentFull() const; | |
776 | |
777 // When enough events are collected, they are handed (in bulk) to | |
778 // the output callback. If no callback is set, the output will be | |
779 // silently dropped. The callback must be thread safe. The string format is | |
780 // undefined. Use TraceResultBuffer to convert one or more trace strings to | |
781 // JSON. | |
782 typedef RefCountedData<std::string> RefCountedString; | |
783 typedef base::Callback<void(const scoped_refptr<RefCountedString>&)> | |
784 OutputCallback; | |
785 void SetOutputCallback(const OutputCallback& cb); | |
786 | |
787 // The trace buffer does not flush dynamically, so when it fills up, | |
788 // subsequent trace events will be dropped. This callback is generated when | |
789 // the trace buffer is full. The callback must be thread safe. | |
790 typedef base::Callback<void(void)> BufferFullCallback; | |
791 void SetBufferFullCallback(const BufferFullCallback& cb); | |
792 | |
793 // Flushes all logged data to the callback. | |
794 void Flush(); | |
795 | |
796 // Called by TRACE_EVENT* macros, don't call this directly. | |
797 static const unsigned char* GetCategoryEnabled(const char* name); | |
798 static const char* GetCategoryName(const unsigned char* category_enabled); | |
799 | |
800 // Called by TRACE_EVENT* macros, don't call this directly. | |
801 // Returns the index in the internal vector of the event if it was added, or | |
802 // -1 if the event was not added. | |
803 // On end events, the return value of the begin event can be specified along | |
804 // with a threshold in microseconds. If the elapsed time between begin and end | |
805 // is less than the threshold, the begin/end event pair is dropped. | |
806 // If |copy| is set, |name|, |arg_name1| and |arg_name2| will be deep copied | |
807 // into the event; see "Memory scoping note" and TRACE_EVENT_COPY_XXX above. | |
808 int AddTraceEvent(char phase, | |
809 const unsigned char* category_enabled, | |
810 const char* name, | |
811 unsigned long long id, | |
812 int num_args, | |
813 const char** arg_names, | |
814 const unsigned char* arg_types, | |
815 const unsigned long long* arg_values, | |
816 int threshold_begin_id, | |
817 long long threshold, | |
818 unsigned char flags); | |
819 static void AddTraceEventEtw(char phase, | |
820 const char* name, | |
821 const void* id, | |
822 const char* extra); | |
823 static void AddTraceEventEtw(char phase, | |
824 const char* name, | |
825 const void* id, | |
826 const std::string& extra); | |
827 | |
828 // A wrapper around AddTraceEvent used by TRACE_COUNTERx macros | |
829 // that allows only integer values for the counters. | |
830 void AddCounterEvent(const unsigned char* category_enabled, | |
831 const char* name, | |
832 unsigned long long id, | |
833 const char* arg1_name, int arg1_val, | |
834 const char* arg2_name, int arg2_val, | |
835 unsigned char flags); | |
836 | |
837 // Mangle |ptr| with a hash based on the process ID so that if |ptr| occurs on | |
838 // more than one process, it will not collide. | |
839 unsigned long long GetInterProcessID(void* ptr) const { | |
840 return static_cast<unsigned long long>(reinterpret_cast<uintptr_t>(ptr)) ^ | |
841 process_id_hash_; | |
842 } | |
843 | |
844 int process_id() const { return process_id_; } | |
845 | |
846 // Exposed for unittesting: | |
847 | |
848 // Allows deleting our singleton instance. | |
849 static void DeleteForTesting(); | |
850 | |
851 // Allows resurrecting our singleton instance post-AtExit processing. | |
852 static void Resurrect(); | |
853 | |
854 // Allow tests to inspect TraceEvents. | |
855 size_t GetEventsSize() const { return logged_events_.size(); } | |
856 const TraceEvent& GetEventAt(size_t index) const { | |
857 DCHECK(index < logged_events_.size()); | |
858 return logged_events_[index]; | |
859 } | |
860 | |
861 void SetProcessID(int process_id); | |
862 | |
863 private: | |
864 // This allows constructor and destructor to be private and usable only | |
865 // by the Singleton class. | |
866 friend struct StaticMemorySingletonTraits<TraceLog>; | |
867 | |
868 TraceLog(); | |
869 ~TraceLog(); | |
870 const unsigned char* GetCategoryEnabledInternal(const char* name); | |
871 void AddThreadNameMetadataEvents(); | |
872 void AddClockSyncMetadataEvents(); | |
873 | |
874 // TODO(nduca): switch to per-thread trace buffers to reduce thread | |
875 // synchronization. | |
876 Lock lock_; | |
877 bool enabled_; | |
878 OutputCallback output_callback_; | |
879 BufferFullCallback buffer_full_callback_; | |
880 std::vector<TraceEvent> logged_events_; | |
881 std::vector<std::string> included_categories_; | |
882 std::vector<std::string> excluded_categories_; | |
883 | |
884 base::hash_map<int, std::string> thread_names_; | |
885 | |
886 // XORed with TraceID to make it unlikely to collide with other processes. | |
887 unsigned long long process_id_hash_; | |
888 | |
889 int process_id_; | |
890 | |
891 DISALLOW_COPY_AND_ASSIGN(TraceLog); | |
892 }; | |
893 | |
894 } // namespace debug | |
895 } // namespace base | |
896 | |
897 namespace trace_event_internal { | 595 namespace trace_event_internal { |
898 | 596 |
899 // Specify these values when the corresponding argument of AddTraceEvent is not | 597 // Specify these values when the corresponding argument of AddTraceEvent is not |
900 // used. | 598 // used. |
901 const int kZeroNumArgs = 0; | 599 const int kZeroNumArgs = 0; |
902 const int kNoThreshholdBeginId = -1; | 600 const int kNoThreshholdBeginId = -1; |
903 const long long kNoThresholdValue = 0; | 601 const long long kNoThresholdValue = 0; |
904 const unsigned long long kNoEventId = 0; | 602 const unsigned long long kNoEventId = 0; |
905 | 603 |
906 // TraceID encapsulates an ID that can either be an integer or pointer. Pointers | 604 // TraceID encapsulates an ID that can either be an integer or pointer. Pointers |
(...skipping 214 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1121 const char* name; | 819 const char* name; |
1122 int threshold_begin_id; | 820 int threshold_begin_id; |
1123 }; | 821 }; |
1124 Data* p_data_; | 822 Data* p_data_; |
1125 Data data_; | 823 Data data_; |
1126 }; | 824 }; |
1127 | 825 |
1128 } // namespace trace_event_internal | 826 } // namespace trace_event_internal |
1129 | 827 |
1130 #endif // BASE_DEBUG_TRACE_EVENT_H_ | 828 #endif // BASE_DEBUG_TRACE_EVENT_H_ |
OLD | NEW |