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

Side by Side Diff: base/trace_event/trace_event.h

Issue 1340013002: Add thread-local allocation context for tracing (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Remove generation counter Created 5 years, 2 months 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 #ifndef BASE_TRACE_EVENT_TRACE_EVENT_H_ 5 #ifndef BASE_TRACE_EVENT_TRACE_EVENT_H_
6 #define BASE_TRACE_EVENT_TRACE_EVENT_H_ 6 #define BASE_TRACE_EVENT_TRACE_EVENT_H_
7 7
8 // This header file defines implementation details of how the trace macros in 8 // This header file defines implementation details of how the trace macros in
9 // trace_event_common.h collect and store trace events. Anything not 9 // trace_event_common.h collect and store trace events. Anything not
10 // implementation-specific should go in trace_macros_common.h instead of here. 10 // implementation-specific should go in trace_macros_common.h instead of here.
11 11
12 #include <string> 12 #include <string>
13 13
14 #include "base/atomicops.h" 14 #include "base/atomicops.h"
15 #include "base/time/time.h" 15 #include "base/time/time.h"
16 #include "base/trace_event/memory_profiler_allocation_context.h"
16 #include "base/trace_event/trace_event_common.h" 17 #include "base/trace_event/trace_event_common.h"
17 #include "base/trace_event/trace_event_memory.h" 18 #include "base/trace_event/trace_event_memory.h"
18 #include "base/trace_event/trace_event_system_stats_monitor.h" 19 #include "base/trace_event/trace_event_system_stats_monitor.h"
19 #include "base/trace_event/trace_log.h" 20 #include "base/trace_event/trace_log.h"
20 #include "build/build_config.h" 21 #include "build/build_config.h"
21 22
22 // By default, const char* argument values are assumed to have long-lived scope 23 // By default, const char* argument values are assumed to have long-lived scope
23 // and will not be copied. Use this macro to force a const char* to be copied. 24 // and will not be copied. Use this macro to force a const char* to be copied.
24 #define TRACE_STR_COPY(str) \ 25 #define TRACE_STR_COPY(str) \
25 trace_event_internal::TraceStringWithCopy(str) 26 trace_event_internal::TraceStringWithCopy(str)
(...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after
209 // Implementation detail: internal macro to create static category and add 210 // Implementation detail: internal macro to create static category and add
210 // event if the category is enabled. 211 // event if the category is enabled.
211 #define INTERNAL_TRACE_EVENT_ADD(phase, category_group, name, flags, ...) \ 212 #define INTERNAL_TRACE_EVENT_ADD(phase, category_group, name, flags, ...) \
212 do { \ 213 do { \
213 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category_group); \ 214 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category_group); \
214 if (INTERNAL_TRACE_EVENT_CATEGORY_GROUP_ENABLED_FOR_RECORDING_MODE()) { \ 215 if (INTERNAL_TRACE_EVENT_CATEGORY_GROUP_ENABLED_FOR_RECORDING_MODE()) { \
215 trace_event_internal::AddTraceEvent( \ 216 trace_event_internal::AddTraceEvent( \
216 phase, INTERNAL_TRACE_EVENT_UID(category_group_enabled), name, \ 217 phase, INTERNAL_TRACE_EVENT_UID(category_group_enabled), name, \
217 trace_event_internal::kNoId, flags, \ 218 trace_event_internal::kNoId, flags, \
218 trace_event_internal::kNoId, ##__VA_ARGS__); \ 219 trace_event_internal::kNoId, ##__VA_ARGS__); \
220 trace_event_internal::MaybeUpdatePseudoStack(phase, name); \
219 } \ 221 } \
220 } while (0) 222 } while (0)
221 223
222 // Implementation detail: internal macro to create static category and add begin 224 // Implementation detail: internal macro to create static category and add begin
223 // event if the category is enabled. Also adds the end event when the scope 225 // event if the category is enabled. Also adds the end event when the scope
224 // ends. 226 // ends.
225 #define INTERNAL_TRACE_EVENT_ADD_SCOPED(category_group, name, ...) \ 227 #define INTERNAL_TRACE_EVENT_ADD_SCOPED(category_group, name, ...) \
226 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category_group); \ 228 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category_group); \
227 trace_event_internal::ScopedTracer INTERNAL_TRACE_EVENT_UID(tracer); \ 229 trace_event_internal::ScopedTracer INTERNAL_TRACE_EVENT_UID(tracer); \
228 if (INTERNAL_TRACE_EVENT_CATEGORY_GROUP_ENABLED_FOR_RECORDING_MODE()) { \ 230 if (INTERNAL_TRACE_EVENT_CATEGORY_GROUP_ENABLED_FOR_RECORDING_MODE()) { \
(...skipping 20 matching lines...) Expand all
249 TRACE_EVENT_PHASE_COMPLETE, \ 251 TRACE_EVENT_PHASE_COMPLETE, \
250 INTERNAL_TRACE_EVENT_UID(category_group_enabled), name, \ 252 INTERNAL_TRACE_EVENT_UID(category_group_enabled), name, \
251 trace_event_internal::kNoId, trace_event_flags, \ 253 trace_event_internal::kNoId, trace_event_flags, \
252 trace_event_bind_id.data(), ##__VA_ARGS__); \ 254 trace_event_bind_id.data(), ##__VA_ARGS__); \
253 INTERNAL_TRACE_EVENT_UID(tracer).Initialize( \ 255 INTERNAL_TRACE_EVENT_UID(tracer).Initialize( \
254 INTERNAL_TRACE_EVENT_UID(category_group_enabled), name, h); \ 256 INTERNAL_TRACE_EVENT_UID(category_group_enabled), name, h); \
255 } 257 }
256 258
257 // Implementation detail: internal macro to create static category and add 259 // Implementation detail: internal macro to create static category and add
258 // event if the category is enabled. 260 // event if the category is enabled.
259 #define INTERNAL_TRACE_EVENT_ADD_WITH_ID(phase, category_group, name, id, \ 261 #define INTERNAL_TRACE_EVENT_ADD_WITH_ID(phase, category_group, name, id, \
dsinclair 2015/09/22 17:05:21 MaybeUpdatePsuedoStack?
Ruud van Asseldonk 2015/09/23 09:39:04 Done.
260 flags, ...) \ 262 flags, ...) \
261 do { \ 263 do { \
262 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category_group); \ 264 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category_group); \
263 if (INTERNAL_TRACE_EVENT_CATEGORY_GROUP_ENABLED_FOR_RECORDING_MODE()) { \ 265 if (INTERNAL_TRACE_EVENT_CATEGORY_GROUP_ENABLED_FOR_RECORDING_MODE()) { \
264 unsigned int trace_event_flags = flags | TRACE_EVENT_FLAG_HAS_ID; \ 266 unsigned int trace_event_flags = flags | TRACE_EVENT_FLAG_HAS_ID; \
265 trace_event_internal::TraceID trace_event_trace_id( \ 267 trace_event_internal::TraceID trace_event_trace_id( \
266 id, &trace_event_flags); \ 268 id, &trace_event_flags); \
267 trace_event_internal::AddTraceEvent( \ 269 trace_event_internal::AddTraceEvent( \
268 phase, INTERNAL_TRACE_EVENT_UID(category_group_enabled), \ 270 phase, INTERNAL_TRACE_EVENT_UID(category_group_enabled), \
269 name, trace_event_trace_id.data(), trace_event_flags, \ 271 name, trace_event_trace_id.data(), trace_event_flags, \
270 trace_event_internal::kNoId, ##__VA_ARGS__); \ 272 trace_event_internal::kNoId, ##__VA_ARGS__); \
271 } \ 273 } \
272 } while (0) 274 } while (0)
273 275
274 // Implementation detail: internal macro to create static category and add 276 // Implementation detail: internal macro to create static category and add
275 // event if the category is enabled. 277 // event if the category is enabled.
276 #define INTERNAL_TRACE_EVENT_ADD_WITH_ID_TID_AND_TIMESTAMP(phase, \ 278 #define INTERNAL_TRACE_EVENT_ADD_WITH_ID_TID_AND_TIMESTAMP(phase, \
dsinclair 2015/09/22 17:05:21 MaybeUpdatePseudOStack?
Ruud van Asseldonk 2015/09/23 09:39:03 Done.
277 category_group, name, id, thread_id, timestamp, flags, ...) \ 279 category_group, name, id, thread_id, timestamp, flags, ...) \
278 do { \ 280 do { \
279 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category_group); \ 281 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category_group); \
280 if (INTERNAL_TRACE_EVENT_CATEGORY_GROUP_ENABLED_FOR_RECORDING_MODE()) { \ 282 if (INTERNAL_TRACE_EVENT_CATEGORY_GROUP_ENABLED_FOR_RECORDING_MODE()) { \
281 unsigned int trace_event_flags = flags | TRACE_EVENT_FLAG_HAS_ID; \ 283 unsigned int trace_event_flags = flags | TRACE_EVENT_FLAG_HAS_ID; \
282 trace_event_internal::TraceID trace_event_trace_id( \ 284 trace_event_internal::TraceID trace_event_trace_id( \
283 id, &trace_event_flags); \ 285 id, &trace_event_flags); \
284 trace_event_internal::AddTraceEventWithThreadIdAndTimestamp( \ 286 trace_event_internal::AddTraceEventWithThreadIdAndTimestamp( \
285 phase, INTERNAL_TRACE_EVENT_UID(category_group_enabled), \ 287 phase, INTERNAL_TRACE_EVENT_UID(category_group_enabled), \
286 name, trace_event_trace_id.data(), trace_event_internal::kNoId, \ 288 name, trace_event_trace_id.data(), trace_event_internal::kNoId, \
(...skipping 420 matching lines...) Expand 10 before | Expand all | Expand 10 after
707 const char* arg1_name, 709 const char* arg1_name,
708 const ARG1_TYPE& arg1_val, 710 const ARG1_TYPE& arg1_val,
709 const char* arg2_name, 711 const char* arg2_name,
710 const ARG2_TYPE& arg2_val) { 712 const ARG2_TYPE& arg2_val) {
711 const int num_args = 2; 713 const int num_args = 2;
712 const char* arg_names[2] = { arg1_name, arg2_name }; 714 const char* arg_names[2] = { arg1_name, arg2_name };
713 unsigned char arg_types[2]; 715 unsigned char arg_types[2];
714 unsigned long long arg_values[2]; 716 unsigned long long arg_values[2];
715 SetTraceValue(arg1_val, &arg_types[0], &arg_values[0]); 717 SetTraceValue(arg1_val, &arg_types[0], &arg_values[0]);
716 SetTraceValue(arg2_val, &arg_types[1], &arg_values[1]); 718 SetTraceValue(arg2_val, &arg_types[1], &arg_values[1]);
717 return TRACE_EVENT_API_ADD_TRACE_EVENT_WITH_THREAD_ID_AND_TIMESTAMP( 719 return TRACE_EVENT_API_ADD_TRACE_EVENT_WITH_THREAD_ID_AND_TIMESTAMP(
dsinclair 2015/09/22 17:05:21 Should the MaybeStorePseudOStack happen in here? T
Ruud van Asseldonk 2015/09/23 09:39:03 Do you propose to remove the calls from the other
dsinclair 2015/09/23 19:35:21 Yes, removing from the macros and putting here was
718 phase, category_group_enabled, name, id, context_id, bind_id, thread_id, 720 phase, category_group_enabled, name, id, context_id, bind_id, thread_id,
719 timestamp, num_args, arg_names, arg_types, arg_values, NULL, flags); 721 timestamp, num_args, arg_names, arg_types, arg_values, NULL, flags);
720 } 722 }
721 723
722 template<class ARG1_TYPE, class ARG2_TYPE> 724 template<class ARG1_TYPE, class ARG2_TYPE>
723 static inline base::trace_event::TraceEventHandle AddTraceEvent( 725 static inline base::trace_event::TraceEventHandle AddTraceEvent(
724 char phase, 726 char phase,
725 const unsigned char* category_group_enabled, 727 const unsigned char* category_group_enabled,
726 const char* name, 728 const char* name,
727 unsigned long long id, 729 unsigned long long id,
(...skipping 15 matching lines...) Expand all
743 // Used by TRACE_EVENTx macros. Do not use directly. 745 // Used by TRACE_EVENTx macros. Do not use directly.
744 class TRACE_EVENT_API_CLASS_EXPORT ScopedTracer { 746 class TRACE_EVENT_API_CLASS_EXPORT ScopedTracer {
745 public: 747 public:
746 // Note: members of data_ intentionally left uninitialized. See Initialize. 748 // Note: members of data_ intentionally left uninitialized. See Initialize.
747 ScopedTracer() : p_data_(NULL) {} 749 ScopedTracer() : p_data_(NULL) {}
748 750
749 ~ScopedTracer() { 751 ~ScopedTracer() {
750 if (p_data_ && *data_.category_group_enabled) 752 if (p_data_ && *data_.category_group_enabled)
751 TRACE_EVENT_API_UPDATE_TRACE_EVENT_DURATION( 753 TRACE_EVENT_API_UPDATE_TRACE_EVENT_DURATION(
752 data_.category_group_enabled, data_.name, data_.event_handle); 754 data_.category_group_enabled, data_.name, data_.event_handle);
755
756 if (base::trace_event::AllocationContextTracker::capture_enabled())
757 base::trace_event::AllocationContextTracker::PopPseudoStackFrame();
753 } 758 }
754 759
755 void Initialize(const unsigned char* category_group_enabled, 760 void Initialize(const unsigned char* category_group_enabled,
756 const char* name, 761 const char* name,
757 base::trace_event::TraceEventHandle event_handle) { 762 base::trace_event::TraceEventHandle event_handle) {
758 data_.category_group_enabled = category_group_enabled; 763 data_.category_group_enabled = category_group_enabled;
759 data_.name = name; 764 data_.name = name;
760 data_.event_handle = event_handle; 765 data_.event_handle = event_handle;
761 p_data_ = &data_; 766 p_data_ = &data_;
767
768 if (base::trace_event::AllocationContextTracker::capture_enabled())
769 base::trace_event::AllocationContextTracker::PushPseudoStackFrame(name);
762 } 770 }
763 771
764 private: 772 private:
765 // This Data struct workaround is to avoid initializing all the members 773 // This Data struct workaround is to avoid initializing all the members
766 // in Data during construction of this object, since this object is always 774 // in Data during construction of this object, since this object is always
767 // constructed, even when tracing is disabled. If the members of Data were 775 // constructed, even when tracing is disabled. If the members of Data were
768 // members of this class instead, compiler warnings occur about potential 776 // members of this class instead, compiler warnings occur about potential
769 // uninitialized accesses. 777 // uninitialized accesses.
770 struct Data { 778 struct Data {
771 const unsigned char* category_group_enabled; 779 const unsigned char* category_group_enabled;
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
821 TRACE_EVENT_API_ATOMIC_STORE( 829 TRACE_EVENT_API_ATOMIC_STORE(
822 g_trace_state[BucketNumber], 830 g_trace_state[BucketNumber],
823 reinterpret_cast<TRACE_EVENT_API_ATOMIC_WORD>( 831 reinterpret_cast<TRACE_EVENT_API_ATOMIC_WORD>(
824 const_cast<char*>(category_and_name))); 832 const_cast<char*>(category_and_name)));
825 } 833 }
826 834
827 private: 835 private:
828 const char* previous_state_; 836 const char* previous_state_;
829 }; 837 };
830 838
839 static inline void MaybeUpdatePseudoStack(char phase, const char* name) {
840 if (!base::trace_event::AllocationContextTracker::capture_enabled())
841 return;
842
843 if (phase == TRACE_EVENT_PHASE_BEGIN)
844 base::trace_event::AllocationContextTracker::PushPseudoStackFrame(name);
845 else if (phase == TRACE_EVENT_PHASE_END)
846 base::trace_event::AllocationContextTracker::PopPseudoStackFrame();
847 }
848
831 } // namespace trace_event_internal 849 } // namespace trace_event_internal
832 850
833 namespace base { 851 namespace base {
834 namespace trace_event { 852 namespace trace_event {
835 853
836 template<typename IDType> class TraceScopedTrackableObject { 854 template<typename IDType> class TraceScopedTrackableObject {
837 public: 855 public:
838 TraceScopedTrackableObject(const char* category_group, const char* name, 856 TraceScopedTrackableObject(const char* category_group, const char* name,
839 IDType id) 857 IDType id)
840 : category_group_(category_group), 858 : category_group_(category_group),
(...skipping 15 matching lines...) Expand all
856 const char* name_; 874 const char* name_;
857 IDType id_; 875 IDType id_;
858 876
859 DISALLOW_COPY_AND_ASSIGN(TraceScopedTrackableObject); 877 DISALLOW_COPY_AND_ASSIGN(TraceScopedTrackableObject);
860 }; 878 };
861 879
862 } // namespace trace_event 880 } // namespace trace_event
863 } // namespace base 881 } // namespace base
864 882
865 #endif // BASE_TRACE_EVENT_TRACE_EVENT_H_ 883 #endif // BASE_TRACE_EVENT_TRACE_EVENT_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698