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

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

Issue 1837013003: [tracing] Add support for profiling trace events (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@ipc_trace
Patch Set: nits. Created 4 years, 8 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_event_common.h instead of here. 10 // implementation-specific should go in trace_event_common.h instead of here.
11 11
12 #include <stddef.h> 12 #include <stddef.h>
13 #include <stdint.h> 13 #include <stdint.h>
14 14
15 #include <string> 15 #include <string>
16 16
17 #include "base/atomicops.h" 17 #include "base/atomicops.h"
18 #include "base/macros.h" 18 #include "base/macros.h"
19 #include "base/time/time.h" 19 #include "base/time/time.h"
20 #include "base/trace_event/common/trace_event_common.h" 20 #include "base/trace_event/common/trace_event_common.h"
21 #include "base/trace_event/heap_profiler_allocation_context_tracker.h"
21 #include "base/trace_event/trace_event_system_stats_monitor.h" 22 #include "base/trace_event/trace_event_system_stats_monitor.h"
22 #include "base/trace_event/trace_log.h" 23 #include "base/trace_event/trace_log.h"
23 #include "build/build_config.h" 24 #include "build/build_config.h"
24 25
25 // By default, const char* argument values are assumed to have long-lived scope 26 // By default, const char* argument values are assumed to have long-lived scope
26 // and will not be copied. Use this macro to force a const char* to be copied. 27 // and will not be copied. Use this macro to force a const char* to be copied.
27 #define TRACE_STR_COPY(str) \ 28 #define TRACE_STR_COPY(str) \
28 trace_event_internal::TraceStringWithCopy(str) 29 trace_event_internal::TraceStringWithCopy(str)
29 30
30 // By default, uint64_t ID argument values are not mangled with the Process ID 31 // By default, uint64_t ID argument values are not mangled with the Process ID
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
64 // ...; 65 // ...;
65 // } 66 // }
66 #define TRACE_EVENT_SCOPED_SAMPLING_STATE_FOR_BUCKET( \ 67 #define TRACE_EVENT_SCOPED_SAMPLING_STATE_FOR_BUCKET( \
67 bucket_number, category, name) \ 68 bucket_number, category, name) \
68 trace_event_internal::TraceEventSamplingStateScope<bucket_number> \ 69 trace_event_internal::TraceEventSamplingStateScope<bucket_number> \
69 traceEventSamplingScope(category "\0" name); 70 traceEventSamplingScope(category "\0" name);
70 71
71 #define TRACE_EVENT_API_CURRENT_THREAD_ID \ 72 #define TRACE_EVENT_API_CURRENT_THREAD_ID \
72 static_cast<int>(base::PlatformThread::CurrentId()) 73 static_cast<int>(base::PlatformThread::CurrentId())
73 74
75 #define INTERNAL_TRACE_EVENT_CATEGORY_GROUP_ENABLED_FOR_TRACING() \
76 UNLIKELY(*INTERNAL_TRACE_EVENT_UID(category_group_enabled) & \
77 (base::trace_event::TraceLog::ENABLED_FOR_HEAP_PROFILING | \
78 base::trace_event::TraceLog::ENABLED_FOR_NON_PROFILING_MODE))
79
74 #define INTERNAL_TRACE_EVENT_CATEGORY_GROUP_ENABLED_FOR_RECORDING_MODE() \ 80 #define INTERNAL_TRACE_EVENT_CATEGORY_GROUP_ENABLED_FOR_RECORDING_MODE() \
75 UNLIKELY(*INTERNAL_TRACE_EVENT_UID(category_group_enabled) & \ 81 UNLIKELY(*INTERNAL_TRACE_EVENT_UID(category_group_enabled) & \
76 (base::trace_event::TraceLog::ENABLED_FOR_RECORDING | \ 82 (base::trace_event::TraceLog::ENABLED_FOR_NON_PROFILING_MODE))
77 base::trace_event::TraceLog::ENABLED_FOR_EVENT_CALLBACK | \ 83
78 base::trace_event::TraceLog::ENABLED_FOR_ETW_EXPORT)) 84 #define INTERNAL_TRACE_EVENT_CATEGORY_GROUP_ENABLED_FOR_HEAP_PROFILING() \
85 UNLIKELY(*INTERNAL_TRACE_EVENT_UID(category_group_enabled) & \
86 base::trace_event::TraceLog::ENABLED_FOR_HEAP_PROFILING)
79 87
80 //////////////////////////////////////////////////////////////////////////////// 88 ////////////////////////////////////////////////////////////////////////////////
81 // Implementation specific tracing API definitions. 89 // Implementation specific tracing API definitions.
82 90
83 // Get a pointer to the enabled state of the given trace category. Only 91 // Get a pointer to the enabled state of the given trace category. Only
84 // long-lived literal strings should be given as the category group. The 92 // long-lived literal strings should be given as the category group. The
85 // returned pointer can be held permanently in a local static for example. If 93 // returned pointer can be held permanently in a local static for example. If
86 // the unsigned char is non-zero, tracing is enabled. If tracing is enabled, 94 // the unsigned char is non-zero, tracing is enabled. If tracing is enabled,
87 // TRACE_EVENT_API_ADD_TRACE_EVENT can be called. It's OK if tracing is disabled 95 // TRACE_EVENT_API_ADD_TRACE_EVENT can be called. It's OK if tracing is disabled
88 // between the load of the tracing state and the call to 96 // between the load of the tracing state and the call to
(...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after
256 phase, INTERNAL_TRACE_EVENT_UID(category_group_enabled), name, \ 264 phase, INTERNAL_TRACE_EVENT_UID(category_group_enabled), name, \
257 trace_event_internal::kGlobalScope, trace_event_internal::kNoId, \ 265 trace_event_internal::kGlobalScope, trace_event_internal::kNoId, \
258 flags, trace_event_internal::kNoId, ##__VA_ARGS__); \ 266 flags, trace_event_internal::kNoId, ##__VA_ARGS__); \
259 } \ 267 } \
260 } while (0) 268 } while (0)
261 269
262 // Implementation detail: internal macro to create static category and add begin 270 // Implementation detail: internal macro to create static category and add begin
263 // event if the category is enabled. Also adds the end event when the scope 271 // event if the category is enabled. Also adds the end event when the scope
264 // ends. 272 // ends.
265 #define INTERNAL_TRACE_EVENT_ADD_SCOPED(category_group, name, ...) \ 273 #define INTERNAL_TRACE_EVENT_ADD_SCOPED(category_group, name, ...) \
266 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category_group); \ 274 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category_group); \
267 trace_event_internal::ScopedTracer INTERNAL_TRACE_EVENT_UID(tracer); \ 275 trace_event_internal::ScopedTracer INTERNAL_TRACE_EVENT_UID(tracer); \
276 if (INTERNAL_TRACE_EVENT_CATEGORY_GROUP_ENABLED_FOR_TRACING()) { \
268 if (INTERNAL_TRACE_EVENT_CATEGORY_GROUP_ENABLED_FOR_RECORDING_MODE()) { \ 277 if (INTERNAL_TRACE_EVENT_CATEGORY_GROUP_ENABLED_FOR_RECORDING_MODE()) { \
269 base::trace_event::TraceEventHandle h = \ 278 base::trace_event::TraceEventHandle h = \
270 trace_event_internal::AddTraceEvent( \ 279 trace_event_internal::AddTraceEvent( \
271 TRACE_EVENT_PHASE_COMPLETE, \ 280 TRACE_EVENT_PHASE_COMPLETE, \
272 INTERNAL_TRACE_EVENT_UID(category_group_enabled), name, \ 281 INTERNAL_TRACE_EVENT_UID(category_group_enabled), name, \
273 trace_event_internal::kGlobalScope, trace_event_internal::kNoId, \ 282 trace_event_internal::kGlobalScope, trace_event_internal::kNoId, \
274 TRACE_EVENT_FLAG_NONE, trace_event_internal::kNoId, \ 283 TRACE_EVENT_FLAG_NONE, trace_event_internal::kNoId, \
275 ##__VA_ARGS__); \ 284 ##__VA_ARGS__); \
276 INTERNAL_TRACE_EVENT_UID(tracer).Initialize( \ 285 INTERNAL_TRACE_EVENT_UID(tracer) \
277 INTERNAL_TRACE_EVENT_UID(category_group_enabled), name, h); \ 286 .Initialize(INTERNAL_TRACE_EVENT_UID(category_group_enabled), name, \
278 } 287 h); \
288 } \
289 if (INTERNAL_TRACE_EVENT_CATEGORY_GROUP_ENABLED_FOR_HEAP_PROFILING()) { \
290 INTERNAL_TRACE_EVENT_UID(tracer) \
291 .InitializeForHeapProfiling(name, TRACE_EVENT_FLAG_NONE); \
292 } \
293 }
279 294
280 #define INTERNAL_TRACE_EVENT_ADD_SCOPED_WITH_FLOW( \ 295 #define INTERNAL_TRACE_EVENT_ADD_SCOPED_WITH_FLOW(category_group, name, \
281 category_group, name, bind_id, flow_flags, ...) \ 296 bind_id, flow_flags, ...) \
282 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category_group); \ 297 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category_group); \
283 trace_event_internal::ScopedTracer INTERNAL_TRACE_EVENT_UID(tracer); \ 298 trace_event_internal::ScopedTracer INTERNAL_TRACE_EVENT_UID(tracer); \
284 if (INTERNAL_TRACE_EVENT_CATEGORY_GROUP_ENABLED_FOR_RECORDING_MODE()) { \ 299 if (INTERNAL_TRACE_EVENT_CATEGORY_GROUP_ENABLED_FOR_TRACING()) { \
285 unsigned int trace_event_flags = flow_flags; \ 300 if (INTERNAL_TRACE_EVENT_CATEGORY_GROUP_ENABLED_FOR_RECORDING_MODE()) { \
286 trace_event_internal::TraceID trace_event_bind_id(bind_id, \ 301 unsigned int trace_event_flags = flow_flags; \
287 &trace_event_flags); \ 302 trace_event_internal::TraceID trace_event_bind_id(bind_id, \
288 base::trace_event::TraceEventHandle h = \ 303 &trace_event_flags); \
289 trace_event_internal::AddTraceEvent( \ 304 base::trace_event::TraceEventHandle h = \
290 TRACE_EVENT_PHASE_COMPLETE, \ 305 trace_event_internal::AddTraceEvent( \
291 INTERNAL_TRACE_EVENT_UID(category_group_enabled), name, \ 306 TRACE_EVENT_PHASE_COMPLETE, \
292 trace_event_internal::kGlobalScope, trace_event_internal::kNoId, \ 307 INTERNAL_TRACE_EVENT_UID(category_group_enabled), name, \
293 trace_event_flags, trace_event_bind_id.raw_id(), ##__VA_ARGS__); \ 308 trace_event_internal::kGlobalScope, trace_event_internal::kNoId, \
294 INTERNAL_TRACE_EVENT_UID(tracer).Initialize( \ 309 trace_event_flags, trace_event_bind_id.raw_id(), ##__VA_ARGS__); \
295 INTERNAL_TRACE_EVENT_UID(category_group_enabled), name, h); \ 310 INTERNAL_TRACE_EVENT_UID(tracer) \
311 .Initialize(INTERNAL_TRACE_EVENT_UID(category_group_enabled), name, \
312 h); \
313 } \
314 if (INTERNAL_TRACE_EVENT_CATEGORY_GROUP_ENABLED_FOR_HEAP_PROFILING()) { \
315 INTERNAL_TRACE_EVENT_UID(tracer) \
316 .InitializeForHeapProfiling(name, flow_flags); \
317 } \
296 } 318 }
297 319
298 // Implementation detail: internal macro to create static category and add 320 // Implementation detail: internal macro to create static category and add
299 // event if the category is enabled. 321 // event if the category is enabled.
300 #define INTERNAL_TRACE_EVENT_ADD_WITH_ID(phase, category_group, name, id, \ 322 #define INTERNAL_TRACE_EVENT_ADD_WITH_ID(phase, category_group, name, id, \
301 flags, ...) \ 323 flags, ...) \
302 do { \ 324 do { \
303 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category_group); \ 325 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category_group); \
304 if (INTERNAL_TRACE_EVENT_CATEGORY_GROUP_ENABLED_FOR_RECORDING_MODE()) { \ 326 if (INTERNAL_TRACE_EVENT_CATEGORY_GROUP_ENABLED_FOR_RECORDING_MODE()) { \
305 unsigned int trace_event_flags = flags | TRACE_EVENT_FLAG_HAS_ID; \ 327 unsigned int trace_event_flags = flags | TRACE_EVENT_FLAG_HAS_ID; \
(...skipping 651 matching lines...) Expand 10 before | Expand all | Expand 10 after
957 arg_values, nullptr, TRACE_EVENT_FLAG_NONE); 979 arg_values, nullptr, TRACE_EVENT_FLAG_NONE);
958 } 980 }
959 981
960 // Used by TRACE_EVENTx macros. Do not use directly. 982 // Used by TRACE_EVENTx macros. Do not use directly.
961 class TRACE_EVENT_API_CLASS_EXPORT ScopedTracer { 983 class TRACE_EVENT_API_CLASS_EXPORT ScopedTracer {
962 public: 984 public:
963 // Note: members of data_ intentionally left uninitialized. See Initialize. 985 // Note: members of data_ intentionally left uninitialized. See Initialize.
964 ScopedTracer() : p_data_(NULL) {} 986 ScopedTracer() : p_data_(NULL) {}
965 987
966 ~ScopedTracer() { 988 ~ScopedTracer() {
967 if (p_data_ && *data_.category_group_enabled) 989 if (p_data_ && *data_.category_group_enabled) {
968 TRACE_EVENT_API_UPDATE_TRACE_EVENT_DURATION( 990 TRACE_EVENT_API_UPDATE_TRACE_EVENT_DURATION(
969 data_.category_group_enabled, data_.name, data_.event_handle); 991 data_.category_group_enabled, data_.name, data_.event_handle);
992 }
993 // TODO(ssid): Pop the frame from the pseudo stack if initialized for
994 // profiling is true, after adding profiling mode in skia and Blink
995 // crbug.com/598426.
970 } 996 }
971 997
972 void Initialize(const unsigned char* category_group_enabled, 998 void Initialize(const unsigned char* category_group_enabled,
973 const char* name, 999 const char* name,
974 base::trace_event::TraceEventHandle event_handle) { 1000 base::trace_event::TraceEventHandle event_handle) {
975 data_.category_group_enabled = category_group_enabled; 1001 data_.category_group_enabled = category_group_enabled;
976 data_.name = name; 1002 data_.name = name;
977 data_.event_handle = event_handle; 1003 data_.event_handle = event_handle;
978 p_data_ = &data_; 1004 p_data_ = &data_;
979 } 1005 }
980 1006
1007 void InitializeForHeapProfiling(const char* name, unsigned int flags) {
1008 if (!(flags & TRACE_EVENT_FLAG_COPY)) {
1009 data_.initialized_for_profiling = true;
1010 data_.name = name;
1011 // TODO(ssid): Add the frame to the pseudo stack after adding profiling
1012 // mode in skia and Blink crbug.com/598426.
1013 }
1014 }
1015
981 private: 1016 private:
982 // This Data struct workaround is to avoid initializing all the members 1017 // This Data struct workaround is to avoid initializing all the members
983 // in Data during construction of this object, since this object is always 1018 // in Data during construction of this object, since this object is always
984 // constructed, even when tracing is disabled. If the members of Data were 1019 // constructed, even when tracing is disabled. If the members of Data were
985 // members of this class instead, compiler warnings occur about potential 1020 // members of this class instead, compiler warnings occur about potential
986 // uninitialized accesses. 1021 // uninitialized accesses.
987 struct Data { 1022 struct Data {
988 const unsigned char* category_group_enabled; 1023 const unsigned char* category_group_enabled;
989 const char* name; 1024 const char* name;
990 base::trace_event::TraceEventHandle event_handle; 1025 base::trace_event::TraceEventHandle event_handle;
1026 bool initialized_for_profiling;
991 }; 1027 };
992 Data* p_data_; 1028 Data* p_data_;
993 Data data_; 1029 Data data_;
994 }; 1030 };
995 1031
996 // Used by TRACE_EVENT_BINARY_EFFICIENTx macro. Do not use directly. 1032 // Used by TRACE_EVENT_BINARY_EFFICIENTx macro. Do not use directly.
997 class TRACE_EVENT_API_CLASS_EXPORT ScopedTraceBinaryEfficient { 1033 class TRACE_EVENT_API_CLASS_EXPORT ScopedTraceBinaryEfficient {
998 public: 1034 public:
999 ScopedTraceBinaryEfficient(const char* category_group, const char* name); 1035 ScopedTraceBinaryEfficient(const char* category_group, const char* name);
1000 ~ScopedTraceBinaryEfficient(); 1036 ~ScopedTraceBinaryEfficient();
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
1073 const char* name_; 1109 const char* name_;
1074 IDType id_; 1110 IDType id_;
1075 1111
1076 DISALLOW_COPY_AND_ASSIGN(TraceScopedTrackableObject); 1112 DISALLOW_COPY_AND_ASSIGN(TraceScopedTrackableObject);
1077 }; 1113 };
1078 1114
1079 } // namespace trace_event 1115 } // namespace trace_event
1080 } // namespace base 1116 } // namespace base
1081 1117
1082 #endif // BASE_TRACE_EVENT_TRACE_EVENT_H_ 1118 #endif // BASE_TRACE_EVENT_TRACE_EVENT_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698