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
3 // found in the LICENSE file.
5 #ifndef BASE_TRACE_EVENT_TRACE_EVENT_H_
6 #define BASE_TRACE_EVENT_TRACE_EVENT_H_
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
10 // implementation-specific should go in trace_macros_common.h instead of here.
14 #include "base/atomicops.h"
15 #include "base/time/time.h"
16 #include "base/trace_event/trace_event_common.h"
17 #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_log.h"
20 #include "build/build_config.h"
22 // 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 #define TRACE_STR_COPY(str) \
25 trace_event_internal::TraceStringWithCopy(str)
27 // By default, uint64 ID argument values are not mangled with the Process ID in
28 // TRACE_EVENT_ASYNC macros. Use this macro to force Process ID mangling.
29 #define TRACE_ID_MANGLE(id) \
30 trace_event_internal::TraceID::ForceMangle(id)
32 // By default, pointers are mangled with the Process ID in TRACE_EVENT_ASYNC
33 // macros. Use this macro to prevent Process ID mangling.
34 #define TRACE_ID_DONT_MANGLE(id) \
35 trace_event_internal::TraceID::DontMangle(id)
37 // Sets the current sample state to the given category and name (both must be
38 // constant strings). These states are intended for a sampling profiler.
39 // Implementation note: we store category and name together because we don't
40 // want the inconsistency/expense of storing two pointers.
41 // |thread_bucket| is [0..2] and is used to statically isolate samples in one
42 // thread from others.
43 #define TRACE_EVENT_SET_SAMPLING_STATE_FOR_BUCKET( \
44 bucket_number, category, name) \
45 trace_event_internal:: \
46 TraceEventSamplingStateScope<bucket_number>::Set(category "\0" name)
48 // Returns a current sampling state of the given bucket.
49 #define TRACE_EVENT_GET_SAMPLING_STATE_FOR_BUCKET(bucket_number) \
50 trace_event_internal::TraceEventSamplingStateScope<bucket_number>::Current()
52 // Creates a scope of a sampling state of the given bucket.
54 // { // The sampling state is set within this scope.
55 // TRACE_EVENT_SAMPLING_STATE_SCOPE_FOR_BUCKET(0, "category", "name");
58 #define TRACE_EVENT_SCOPED_SAMPLING_STATE_FOR_BUCKET( \
59 bucket_number, category, name) \
60 trace_event_internal::TraceEventSamplingStateScope<bucket_number> \
61 traceEventSamplingScope(category "\0" name);
63 #define TRACE_EVENT_API_CURRENT_THREAD_ID \
64 static_cast<int>(base::PlatformThread::CurrentId())
66 #define INTERNAL_TRACE_EVENT_CATEGORY_GROUP_ENABLED_FOR_RECORDING_MODE() \
67 UNLIKELY(*INTERNAL_TRACE_EVENT_UID(category_group_enabled) & \
68 (base::trace_event::TraceLog::ENABLED_FOR_RECORDING | \
69 base::trace_event::TraceLog::ENABLED_FOR_EVENT_CALLBACK | \
70 base::trace_event::TraceLog::ENABLED_FOR_ETW_EXPORT))
72 ////////////////////////////////////////////////////////////////////////////////
73 // Implementation specific tracing API definitions.
75 // Get a pointer to the enabled state of the given trace category. Only
76 // long-lived literal strings should be given as the category group. The
77 // returned pointer can be held permanently in a local static for example. If
78 // the unsigned char is non-zero, tracing is enabled. If tracing is enabled,
79 // TRACE_EVENT_API_ADD_TRACE_EVENT can be called. It's OK if tracing is disabled
80 // between the load of the tracing state and the call to
81 // TRACE_EVENT_API_ADD_TRACE_EVENT, because this flag only provides an early out
82 // for best performance when tracing is disabled.
83 // const unsigned char*
84 // TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED(const char* category_group)
85 #define TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED \
86 base::trace_event::TraceLog::GetCategoryGroupEnabled
88 // Get the number of times traces have been recorded. This is used to implement
89 // the TRACE_EVENT_IS_NEW_TRACE facility.
90 // unsigned int TRACE_EVENT_API_GET_NUM_TRACES_RECORDED()
91 #define TRACE_EVENT_API_GET_NUM_TRACES_RECORDED \
92 base::trace_event::TraceLog::GetInstance()->GetNumTracesRecorded
94 // Add a trace event to the platform tracing system.
95 // base::trace_event::TraceEventHandle TRACE_EVENT_API_ADD_TRACE_EVENT(
97 // const unsigned char* category_group_enabled,
99 // unsigned long long id,
100 // unsigned long long context_id,
102 // const char** arg_names,
103 // const unsigned char* arg_types,
104 // const unsigned long long* arg_values,
105 // unsigned int flags)
106 #define TRACE_EVENT_API_ADD_TRACE_EVENT \
107 base::trace_event::TraceLog::GetInstance()->AddTraceEvent
109 // Add a trace event to the platform tracing system.
110 // base::trace_event::TraceEventHandle
111 // TRACE_EVENT_API_ADD_TRACE_EVENT_WITH_CONTEXT_ID(
113 // const unsigned char* category_group_enabled,
115 // unsigned long long id,
116 // unsigned long long context_id,
118 // const char** arg_names,
119 // const unsigned char* arg_types,
120 // const unsigned long long* arg_values,
121 // unsigned int flags)
122 #define TRACE_EVENT_API_ADD_TRACE_EVENT_WITH_CONTEXT_ID \
123 base::trace_event::TraceLog::GetInstance()->AddTraceEventWithContextId
125 // Add a trace event to the platform tracing system.
126 // base::trace_event::TraceEventHandle
127 // TRACE_EVENT_API_ADD_TRACE_EVENT_WITH_TIMESTAMP(
129 // const unsigned char* category_group_enabled,
131 // unsigned long long id,
132 // unsigned long long context_id,
134 // const TraceTicks& timestamp,
136 // const char** arg_names,
137 // const unsigned char* arg_types,
138 // const unsigned long long* arg_values,
139 // unsigned int flags)
140 #define TRACE_EVENT_API_ADD_TRACE_EVENT_WITH_THREAD_ID_AND_TIMESTAMP \
141 base::trace_event::TraceLog::GetInstance() \
142 ->AddTraceEventWithThreadIdAndTimestamp
144 // Set the duration field of a COMPLETE trace event.
145 // void TRACE_EVENT_API_UPDATE_TRACE_EVENT_DURATION(
146 // const unsigned char* category_group_enabled,
148 // base::trace_event::TraceEventHandle id)
149 #define TRACE_EVENT_API_UPDATE_TRACE_EVENT_DURATION \
150 base::trace_event::TraceLog::GetInstance()->UpdateTraceEventDuration
152 // Defines atomic operations used internally by the tracing system.
153 #define TRACE_EVENT_API_ATOMIC_WORD base::subtle::AtomicWord
154 #define TRACE_EVENT_API_ATOMIC_LOAD(var) base::subtle::NoBarrier_Load(&(var))
155 #define TRACE_EVENT_API_ATOMIC_STORE(var, value) \
156 base::subtle::NoBarrier_Store(&(var), (value))
158 // Defines visibility for classes in trace_event.h
159 #define TRACE_EVENT_API_CLASS_EXPORT BASE_EXPORT
161 // The thread buckets for the sampling profiler.
162 TRACE_EVENT_API_CLASS_EXPORT
extern \
163 TRACE_EVENT_API_ATOMIC_WORD g_trace_state
[3];
165 #define TRACE_EVENT_API_THREAD_BUCKET(thread_bucket) \
166 g_trace_state[thread_bucket]
168 ////////////////////////////////////////////////////////////////////////////////
170 // Implementation detail: trace event macros create temporary variables
171 // to keep instrumentation overhead low. These macros give each temporary
172 // variable a unique name based on the line number to prevent name collisions.
173 #define INTERNAL_TRACE_EVENT_UID3(a,b) \
174 trace_event_unique_##a##b
175 #define INTERNAL_TRACE_EVENT_UID2(a,b) \
176 INTERNAL_TRACE_EVENT_UID3(a,b)
177 #define INTERNAL_TRACE_EVENT_UID(name_prefix) \
178 INTERNAL_TRACE_EVENT_UID2(name_prefix, __LINE__)
180 // Implementation detail: internal macro to create static category.
181 // No barriers are needed, because this code is designed to operate safely
182 // even when the unsigned char* points to garbage data (which may be the case
183 // on processors without cache coherency).
184 #define INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO_CUSTOM_VARIABLES( \
185 category_group, atomic, category_group_enabled) \
186 category_group_enabled = \
187 reinterpret_cast<const unsigned char*>(TRACE_EVENT_API_ATOMIC_LOAD( \
189 if (UNLIKELY(!category_group_enabled)) { \
190 category_group_enabled = \
191 TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED(category_group); \
192 TRACE_EVENT_API_ATOMIC_STORE(atomic, \
193 reinterpret_cast<TRACE_EVENT_API_ATOMIC_WORD>( \
194 category_group_enabled)); \
197 #define INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category_group) \
198 static TRACE_EVENT_API_ATOMIC_WORD INTERNAL_TRACE_EVENT_UID(atomic) = 0; \
199 const unsigned char* INTERNAL_TRACE_EVENT_UID(category_group_enabled); \
200 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO_CUSTOM_VARIABLES(category_group, \
201 INTERNAL_TRACE_EVENT_UID(atomic), \
202 INTERNAL_TRACE_EVENT_UID(category_group_enabled));
204 // Implementation detail: internal macro to create static category and add
205 // event if the category is enabled.
206 #define INTERNAL_TRACE_EVENT_ADD(phase, category_group, name, flags, ...) \
208 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category_group); \
209 if (INTERNAL_TRACE_EVENT_CATEGORY_GROUP_ENABLED_FOR_RECORDING_MODE()) { \
210 trace_event_internal::AddTraceEvent( \
211 phase, INTERNAL_TRACE_EVENT_UID(category_group_enabled), name, \
212 trace_event_internal::kNoId, flags, \
213 trace_event_internal::kNoId, ##__VA_ARGS__); \
217 // Implementation detail: internal macro to create static category and add begin
218 // event if the category is enabled. Also adds the end event when the scope
220 #define INTERNAL_TRACE_EVENT_ADD_SCOPED(category_group, name, ...) \
221 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category_group); \
222 trace_event_internal::ScopedTracer INTERNAL_TRACE_EVENT_UID(tracer); \
223 if (INTERNAL_TRACE_EVENT_CATEGORY_GROUP_ENABLED_FOR_RECORDING_MODE()) { \
224 base::trace_event::TraceEventHandle h = \
225 trace_event_internal::AddTraceEvent( \
226 TRACE_EVENT_PHASE_COMPLETE, \
227 INTERNAL_TRACE_EVENT_UID(category_group_enabled), name, \
228 trace_event_internal::kNoId, TRACE_EVENT_FLAG_NONE, \
229 trace_event_internal::kNoId, ##__VA_ARGS__); \
230 INTERNAL_TRACE_EVENT_UID(tracer).Initialize( \
231 INTERNAL_TRACE_EVENT_UID(category_group_enabled), name, h); \
234 #define INTERNAL_TRACE_EVENT_ADD_SCOPED_WITH_FLOW( \
235 category_group, name, bind_id, flow_flags, ...) \
236 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category_group); \
237 trace_event_internal::ScopedTracer INTERNAL_TRACE_EVENT_UID(tracer); \
238 if (INTERNAL_TRACE_EVENT_CATEGORY_GROUP_ENABLED_FOR_RECORDING_MODE()) { \
239 unsigned int trace_event_flags = flow_flags; \
240 trace_event_internal::TraceID trace_event_bind_id(bind_id, \
241 &trace_event_flags); \
242 base::trace_event::TraceEventHandle h = \
243 trace_event_internal::AddTraceEvent( \
244 TRACE_EVENT_PHASE_COMPLETE, \
245 INTERNAL_TRACE_EVENT_UID(category_group_enabled), name, \
246 trace_event_internal::kNoId, trace_event_flags, \
247 trace_event_bind_id.data(), ##__VA_ARGS__); \
248 INTERNAL_TRACE_EVENT_UID(tracer).Initialize( \
249 INTERNAL_TRACE_EVENT_UID(category_group_enabled), name, h); \
252 // Implementation detail: internal macro to create static category and add
253 // event if the category is enabled.
254 #define INTERNAL_TRACE_EVENT_ADD_WITH_ID(phase, category_group, name, id, \
257 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category_group); \
258 if (INTERNAL_TRACE_EVENT_CATEGORY_GROUP_ENABLED_FOR_RECORDING_MODE()) { \
259 unsigned int trace_event_flags = flags | TRACE_EVENT_FLAG_HAS_ID; \
260 trace_event_internal::TraceID trace_event_trace_id( \
261 id, &trace_event_flags); \
262 trace_event_internal::AddTraceEvent( \
263 phase, INTERNAL_TRACE_EVENT_UID(category_group_enabled), \
264 name, trace_event_trace_id.data(), trace_event_flags, \
265 trace_event_internal::kNoId, ##__VA_ARGS__); \
269 // Implementation detail: internal macro to create static category and add
270 // event if the category is enabled.
271 #define INTERNAL_TRACE_EVENT_ADD_WITH_ID_TID_AND_TIMESTAMP(phase, \
272 category_group, name, id, thread_id, timestamp, flags, ...) \
274 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category_group); \
275 if (INTERNAL_TRACE_EVENT_CATEGORY_GROUP_ENABLED_FOR_RECORDING_MODE()) { \
276 unsigned int trace_event_flags = flags | TRACE_EVENT_FLAG_HAS_ID; \
277 trace_event_internal::TraceID trace_event_trace_id( \
278 id, &trace_event_flags); \
279 trace_event_internal::AddTraceEventWithThreadIdAndTimestamp( \
280 phase, INTERNAL_TRACE_EVENT_UID(category_group_enabled), \
281 name, trace_event_trace_id.data(), trace_event_internal::kNoId, \
282 thread_id, base::TraceTicks::FromInternalValue(timestamp), \
283 trace_event_flags | TRACE_EVENT_FLAG_EXPLICIT_TIMESTAMP, \
284 trace_event_internal::kNoId, ##__VA_ARGS__); \
289 namespace trace_event_internal
{
291 // Specify these values when the corresponding argument of AddTraceEvent is not
293 const int kZeroNumArgs
= 0;
294 const unsigned long long kNoId
= 0;
296 // TraceID encapsulates an ID that can either be an integer or pointer. Pointers
297 // are by default mangled with the Process ID so that they are unlikely to
298 // collide when the same pointer is used on different processes.
303 explicit DontMangle(const void* id
)
304 : data_(static_cast<unsigned long long>(
305 reinterpret_cast<uintptr_t>(id
))) {}
306 explicit DontMangle(unsigned long long id
) : data_(id
) {}
307 explicit DontMangle(unsigned long id
) : data_(id
) {}
308 explicit DontMangle(unsigned int id
) : data_(id
) {}
309 explicit DontMangle(unsigned short id
) : data_(id
) {}
310 explicit DontMangle(unsigned char id
) : data_(id
) {}
311 explicit DontMangle(long long id
)
312 : data_(static_cast<unsigned long long>(id
)) {}
313 explicit DontMangle(long id
)
314 : data_(static_cast<unsigned long long>(id
)) {}
315 explicit DontMangle(int id
)
316 : data_(static_cast<unsigned long long>(id
)) {}
317 explicit DontMangle(short id
)
318 : data_(static_cast<unsigned long long>(id
)) {}
319 explicit DontMangle(signed char id
)
320 : data_(static_cast<unsigned long long>(id
)) {}
321 unsigned long long data() const { return data_
; }
323 unsigned long long data_
;
328 explicit ForceMangle(unsigned long long id
) : data_(id
) {}
329 explicit ForceMangle(unsigned long id
) : data_(id
) {}
330 explicit ForceMangle(unsigned int id
) : data_(id
) {}
331 explicit ForceMangle(unsigned short id
) : data_(id
) {}
332 explicit ForceMangle(unsigned char id
) : data_(id
) {}
333 explicit ForceMangle(long long id
)
334 : data_(static_cast<unsigned long long>(id
)) {}
335 explicit ForceMangle(long id
)
336 : data_(static_cast<unsigned long long>(id
)) {}
337 explicit ForceMangle(int id
)
338 : data_(static_cast<unsigned long long>(id
)) {}
339 explicit ForceMangle(short id
)
340 : data_(static_cast<unsigned long long>(id
)) {}
341 explicit ForceMangle(signed char id
)
342 : data_(static_cast<unsigned long long>(id
)) {}
343 unsigned long long data() const { return data_
; }
345 unsigned long long data_
;
347 TraceID(const void* id
, unsigned int* flags
)
348 : data_(static_cast<unsigned long long>(
349 reinterpret_cast<uintptr_t>(id
))) {
350 *flags
|= TRACE_EVENT_FLAG_MANGLE_ID
;
352 TraceID(ForceMangle id
, unsigned int* flags
) : data_(id
.data()) {
353 *flags
|= TRACE_EVENT_FLAG_MANGLE_ID
;
355 TraceID(DontMangle id
, unsigned int* flags
) : data_(id
.data()) {
357 TraceID(unsigned long long id
, unsigned int* flags
)
358 : data_(id
) { (void)flags
; }
359 TraceID(unsigned long id
, unsigned int* flags
)
360 : data_(id
) { (void)flags
; }
361 TraceID(unsigned int id
, unsigned int* flags
)
362 : data_(id
) { (void)flags
; }
363 TraceID(unsigned short id
, unsigned int* flags
)
364 : data_(id
) { (void)flags
; }
365 TraceID(unsigned char id
, unsigned int* flags
)
366 : data_(id
) { (void)flags
; }
367 TraceID(long long id
, unsigned int* flags
)
368 : data_(static_cast<unsigned long long>(id
)) { (void)flags
; }
369 TraceID(long id
, unsigned int* flags
)
370 : data_(static_cast<unsigned long long>(id
)) { (void)flags
; }
371 TraceID(int id
, unsigned int* flags
)
372 : data_(static_cast<unsigned long long>(id
)) { (void)flags
; }
373 TraceID(short id
, unsigned int* flags
)
374 : data_(static_cast<unsigned long long>(id
)) { (void)flags
; }
375 TraceID(signed char id
, unsigned int* flags
)
376 : data_(static_cast<unsigned long long>(id
)) { (void)flags
; }
378 unsigned long long data() const { return data_
; }
381 unsigned long long data_
;
384 // Simple union to store various types as unsigned long long.
385 union TraceValueUnion
{
387 unsigned long long as_uint
;
390 const void* as_pointer
;
391 const char* as_string
;
394 // Simple container for const char* that should be copied instead of retained.
395 class TraceStringWithCopy
{
397 explicit TraceStringWithCopy(const char* str
) : str_(str
) {}
398 const char* str() const { return str_
; }
403 // Define SetTraceValue for each allowed type. It stores the type and
404 // value in the return arguments. This allows this API to avoid declaring any
405 // structures so that it is portable to third_party libraries.
406 #define INTERNAL_DECLARE_SET_TRACE_VALUE(actual_type, \
410 static inline void SetTraceValue( \
412 unsigned char* type, \
413 unsigned long long* value) { \
414 TraceValueUnion type_value; \
415 type_value.union_member = arg_expression; \
416 *type = value_type_id; \
417 *value = type_value.as_uint; \
419 // Simpler form for int types that can be safely casted.
420 #define INTERNAL_DECLARE_SET_TRACE_VALUE_INT(actual_type, \
422 static inline void SetTraceValue( \
424 unsigned char* type, \
425 unsigned long long* value) { \
426 *type = value_type_id; \
427 *value = static_cast<unsigned long long>(arg); \
430 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(unsigned long long, TRACE_VALUE_TYPE_UINT
)
431 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(unsigned long, TRACE_VALUE_TYPE_UINT
)
432 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(unsigned int, TRACE_VALUE_TYPE_UINT
)
433 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(unsigned short, TRACE_VALUE_TYPE_UINT
)
434 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(unsigned char, TRACE_VALUE_TYPE_UINT
)
435 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(long long, TRACE_VALUE_TYPE_INT
)
436 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(long, TRACE_VALUE_TYPE_INT
)
437 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(int, TRACE_VALUE_TYPE_INT
)
438 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(short, TRACE_VALUE_TYPE_INT
)
439 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(signed char, TRACE_VALUE_TYPE_INT
)
440 INTERNAL_DECLARE_SET_TRACE_VALUE(bool, arg
, as_bool
, TRACE_VALUE_TYPE_BOOL
)
441 INTERNAL_DECLARE_SET_TRACE_VALUE(double, arg
, as_double
,
442 TRACE_VALUE_TYPE_DOUBLE
)
443 INTERNAL_DECLARE_SET_TRACE_VALUE(const void*, arg
, as_pointer
,
444 TRACE_VALUE_TYPE_POINTER
)
445 INTERNAL_DECLARE_SET_TRACE_VALUE(const char*, arg
, as_string
,
446 TRACE_VALUE_TYPE_STRING
)
447 INTERNAL_DECLARE_SET_TRACE_VALUE(const TraceStringWithCopy
&, arg
.str(),
448 as_string
, TRACE_VALUE_TYPE_COPY_STRING
)
450 #undef INTERNAL_DECLARE_SET_TRACE_VALUE
451 #undef INTERNAL_DECLARE_SET_TRACE_VALUE_INT
453 // std::string version of SetTraceValue so that trace arguments can be strings.
454 static inline void SetTraceValue(const std::string
& arg
,
456 unsigned long long* value
) {
457 TraceValueUnion type_value
;
458 type_value
.as_string
= arg
.c_str();
459 *type
= TRACE_VALUE_TYPE_COPY_STRING
;
460 *value
= type_value
.as_uint
;
463 // base::Time, base::TimeTicks, etc. versions of SetTraceValue to make it easier
464 // to trace these types.
465 static inline void SetTraceValue(const base::Time arg
,
467 unsigned long long* value
) {
468 *type
= TRACE_VALUE_TYPE_INT
;
469 *value
= arg
.ToInternalValue();
472 static inline void SetTraceValue(const base::TimeTicks arg
,
474 unsigned long long* value
) {
475 *type
= TRACE_VALUE_TYPE_INT
;
476 *value
= arg
.ToInternalValue();
479 static inline void SetTraceValue(const base::ThreadTicks arg
,
481 unsigned long long* value
) {
482 *type
= TRACE_VALUE_TYPE_INT
;
483 *value
= arg
.ToInternalValue();
486 static inline void SetTraceValue(const base::TraceTicks arg
,
488 unsigned long long* value
) {
489 *type
= TRACE_VALUE_TYPE_INT
;
490 *value
= arg
.ToInternalValue();
493 // These AddTraceEvent and AddTraceEventWithThreadIdAndTimestamp template
494 // functions are defined here instead of in the macro, because the arg_values
495 // could be temporary objects, such as std::string. In order to store
496 // pointers to the internal c_str and pass through to the tracing API,
497 // the arg_values must live throughout these procedures.
499 static inline base::trace_event::TraceEventHandle
500 AddTraceEventWithThreadIdAndTimestamp(
502 const unsigned char* category_group_enabled
,
504 unsigned long long id
,
505 unsigned long long context_id
,
507 const base::TraceTicks
& timestamp
,
509 unsigned long long bind_id
,
510 const char* arg1_name
,
511 const scoped_refptr
<base::trace_event::ConvertableToTraceFormat
>&
513 const int num_args
= 1;
514 unsigned char arg_types
[1] = { TRACE_VALUE_TYPE_CONVERTABLE
};
515 return TRACE_EVENT_API_ADD_TRACE_EVENT_WITH_THREAD_ID_AND_TIMESTAMP(
516 phase
, category_group_enabled
, name
, id
, context_id
, bind_id
, thread_id
,
517 timestamp
, num_args
, &arg1_name
, arg_types
, NULL
, &arg1_val
, flags
);
520 template<class ARG1_TYPE
>
521 static inline base::trace_event::TraceEventHandle
522 AddTraceEventWithThreadIdAndTimestamp(
524 const unsigned char* category_group_enabled
,
526 unsigned long long id
,
527 unsigned long long context_id
,
529 const base::TraceTicks
& timestamp
,
531 unsigned long long bind_id
,
532 const char* arg1_name
,
533 const ARG1_TYPE
& arg1_val
,
534 const char* arg2_name
,
535 const scoped_refptr
<base::trace_event::ConvertableToTraceFormat
>&
537 const int num_args
= 2;
538 const char* arg_names
[2] = { arg1_name
, arg2_name
};
540 unsigned char arg_types
[2];
541 unsigned long long arg_values
[2];
542 SetTraceValue(arg1_val
, &arg_types
[0], &arg_values
[0]);
543 arg_types
[1] = TRACE_VALUE_TYPE_CONVERTABLE
;
545 scoped_refptr
<base::trace_event::ConvertableToTraceFormat
>
546 convertable_values
[2];
547 convertable_values
[1] = arg2_val
;
549 return TRACE_EVENT_API_ADD_TRACE_EVENT_WITH_THREAD_ID_AND_TIMESTAMP(
550 phase
, category_group_enabled
, name
, id
, context_id
, bind_id
, thread_id
,
551 timestamp
, num_args
, arg_names
, arg_types
, arg_values
,
552 convertable_values
, flags
);
555 template<class ARG2_TYPE
>
556 static inline base::trace_event::TraceEventHandle
557 AddTraceEventWithThreadIdAndTimestamp(
559 const unsigned char* category_group_enabled
,
561 unsigned long long id
,
562 unsigned long long context_id
,
564 const base::TraceTicks
& timestamp
,
566 unsigned long long bind_id
,
567 const char* arg1_name
,
568 const scoped_refptr
<base::trace_event::ConvertableToTraceFormat
>& arg1_val
,
569 const char* arg2_name
,
570 const ARG2_TYPE
& arg2_val
) {
571 const int num_args
= 2;
572 const char* arg_names
[2] = { arg1_name
, arg2_name
};
574 unsigned char arg_types
[2];
575 unsigned long long arg_values
[2];
576 arg_types
[0] = TRACE_VALUE_TYPE_CONVERTABLE
;
578 SetTraceValue(arg2_val
, &arg_types
[1], &arg_values
[1]);
580 scoped_refptr
<base::trace_event::ConvertableToTraceFormat
>
581 convertable_values
[2];
582 convertable_values
[0] = arg1_val
;
584 return TRACE_EVENT_API_ADD_TRACE_EVENT_WITH_THREAD_ID_AND_TIMESTAMP(
585 phase
, category_group_enabled
, name
, id
, context_id
, bind_id
, thread_id
,
586 timestamp
, num_args
, arg_names
, arg_types
, arg_values
,
587 convertable_values
, flags
);
590 static inline base::trace_event::TraceEventHandle
591 AddTraceEventWithThreadIdAndTimestamp(
593 const unsigned char* category_group_enabled
,
595 unsigned long long id
,
596 unsigned long long context_id
,
598 const base::TraceTicks
& timestamp
,
600 unsigned long long bind_id
,
601 const char* arg1_name
,
602 const scoped_refptr
<base::trace_event::ConvertableToTraceFormat
>& arg1_val
,
603 const char* arg2_name
,
604 const scoped_refptr
<base::trace_event::ConvertableToTraceFormat
>&
606 const int num_args
= 2;
607 const char* arg_names
[2] = { arg1_name
, arg2_name
};
608 unsigned char arg_types
[2] =
609 { TRACE_VALUE_TYPE_CONVERTABLE
, TRACE_VALUE_TYPE_CONVERTABLE
};
610 scoped_refptr
<base::trace_event::ConvertableToTraceFormat
>
611 convertable_values
[2] = {arg1_val
, arg2_val
};
613 return TRACE_EVENT_API_ADD_TRACE_EVENT_WITH_THREAD_ID_AND_TIMESTAMP(
614 phase
, category_group_enabled
, name
, id
, context_id
, bind_id
, thread_id
,
615 timestamp
, num_args
, arg_names
, arg_types
, NULL
, convertable_values
,
619 static inline base::trace_event::TraceEventHandle
620 AddTraceEventWithThreadIdAndTimestamp(
622 const unsigned char* category_group_enabled
,
624 unsigned long long id
,
625 unsigned long long context_id
,
627 const base::TraceTicks
& timestamp
,
629 unsigned long long bind_id
) {
630 return TRACE_EVENT_API_ADD_TRACE_EVENT_WITH_THREAD_ID_AND_TIMESTAMP(
631 phase
, category_group_enabled
, name
, id
, context_id
, bind_id
, thread_id
,
632 timestamp
, kZeroNumArgs
, NULL
, NULL
, NULL
, NULL
, flags
);
635 static inline base::trace_event::TraceEventHandle
AddTraceEvent(
637 const unsigned char* category_group_enabled
,
639 unsigned long long id
,
641 unsigned long long bind_id
) {
642 const int thread_id
= static_cast<int>(base::PlatformThread::CurrentId());
643 const base::TraceTicks now
= base::TraceTicks::Now();
644 return AddTraceEventWithThreadIdAndTimestamp(phase
, category_group_enabled
,
645 name
, id
, kNoId
, thread_id
, now
,
649 template<class ARG1_TYPE
>
650 static inline base::trace_event::TraceEventHandle
651 AddTraceEventWithThreadIdAndTimestamp(
653 const unsigned char* category_group_enabled
,
655 unsigned long long id
,
656 unsigned long long context_id
,
658 const base::TraceTicks
& timestamp
,
660 unsigned long long bind_id
,
661 const char* arg1_name
,
662 const ARG1_TYPE
& arg1_val
) {
663 const int num_args
= 1;
664 unsigned char arg_types
[1];
665 unsigned long long arg_values
[1];
666 SetTraceValue(arg1_val
, &arg_types
[0], &arg_values
[0]);
667 return TRACE_EVENT_API_ADD_TRACE_EVENT_WITH_THREAD_ID_AND_TIMESTAMP(
668 phase
, category_group_enabled
, name
, id
, context_id
, bind_id
, thread_id
,
669 timestamp
, num_args
, &arg1_name
, arg_types
, arg_values
, NULL
, flags
);
672 template<class ARG1_TYPE
>
673 static inline base::trace_event::TraceEventHandle
AddTraceEvent(
675 const unsigned char* category_group_enabled
,
677 unsigned long long id
,
679 unsigned long long bind_id
,
680 const char* arg1_name
,
681 const ARG1_TYPE
& arg1_val
) {
682 int thread_id
= static_cast<int>(base::PlatformThread::CurrentId());
683 base::TraceTicks now
= base::TraceTicks::Now();
684 return AddTraceEventWithThreadIdAndTimestamp(phase
, category_group_enabled
,
685 name
, id
, kNoId
, thread_id
, now
,
687 arg1_name
, arg1_val
);
690 template<class ARG1_TYPE
, class ARG2_TYPE
>
691 static inline base::trace_event::TraceEventHandle
692 AddTraceEventWithThreadIdAndTimestamp(
694 const unsigned char* category_group_enabled
,
696 unsigned long long id
,
697 unsigned long long context_id
,
699 const base::TraceTicks
& timestamp
,
701 unsigned long long bind_id
,
702 const char* arg1_name
,
703 const ARG1_TYPE
& arg1_val
,
704 const char* arg2_name
,
705 const ARG2_TYPE
& arg2_val
) {
706 const int num_args
= 2;
707 const char* arg_names
[2] = { arg1_name
, arg2_name
};
708 unsigned char arg_types
[2];
709 unsigned long long arg_values
[2];
710 SetTraceValue(arg1_val
, &arg_types
[0], &arg_values
[0]);
711 SetTraceValue(arg2_val
, &arg_types
[1], &arg_values
[1]);
712 return TRACE_EVENT_API_ADD_TRACE_EVENT_WITH_THREAD_ID_AND_TIMESTAMP(
713 phase
, category_group_enabled
, name
, id
, context_id
, bind_id
, thread_id
,
714 timestamp
, num_args
, arg_names
, arg_types
, arg_values
, NULL
, flags
);
717 template<class ARG1_TYPE
, class ARG2_TYPE
>
718 static inline base::trace_event::TraceEventHandle
AddTraceEvent(
720 const unsigned char* category_group_enabled
,
722 unsigned long long id
,
724 unsigned long long bind_id
,
725 const char* arg1_name
,
726 const ARG1_TYPE
& arg1_val
,
727 const char* arg2_name
,
728 const ARG2_TYPE
& arg2_val
) {
729 int thread_id
= static_cast<int>(base::PlatformThread::CurrentId());
730 base::TraceTicks now
= base::TraceTicks::Now();
731 return AddTraceEventWithThreadIdAndTimestamp(phase
, category_group_enabled
,
732 name
, id
, kNoId
, thread_id
, now
,
735 arg2_name
, arg2_val
);
738 // Used by TRACE_EVENTx macros. Do not use directly.
739 class TRACE_EVENT_API_CLASS_EXPORT ScopedTracer
{
741 // Note: members of data_ intentionally left uninitialized. See Initialize.
742 ScopedTracer() : p_data_(NULL
) {}
745 if (p_data_
&& *data_
.category_group_enabled
)
746 TRACE_EVENT_API_UPDATE_TRACE_EVENT_DURATION(
747 data_
.category_group_enabled
, data_
.name
, data_
.event_handle
);
750 void Initialize(const unsigned char* category_group_enabled
,
752 base::trace_event::TraceEventHandle event_handle
) {
753 data_
.category_group_enabled
= category_group_enabled
;
755 data_
.event_handle
= event_handle
;
760 // This Data struct workaround is to avoid initializing all the members
761 // in Data during construction of this object, since this object is always
762 // constructed, even when tracing is disabled. If the members of Data were
763 // members of this class instead, compiler warnings occur about potential
764 // uninitialized accesses.
766 const unsigned char* category_group_enabled
;
768 base::trace_event::TraceEventHandle event_handle
;
774 // Used by TRACE_EVENT_BINARY_EFFICIENTx macro. Do not use directly.
775 class TRACE_EVENT_API_CLASS_EXPORT ScopedTraceBinaryEfficient
{
777 ScopedTraceBinaryEfficient(const char* category_group
, const char* name
);
778 ~ScopedTraceBinaryEfficient();
781 const unsigned char* category_group_enabled_
;
783 base::trace_event::TraceEventHandle event_handle_
;
786 // This macro generates less code then TRACE_EVENT0 but is also
787 // slower to execute when tracing is off. It should generally only be
788 // used with code that is seldom executed or conditionally executed
790 // For now the category_group must be "gpu".
791 #define TRACE_EVENT_BINARY_EFFICIENT0(category_group, name) \
792 trace_event_internal::ScopedTraceBinaryEfficient \
793 INTERNAL_TRACE_EVENT_UID(scoped_trace)(category_group, name);
795 // TraceEventSamplingStateScope records the current sampling state
796 // and sets a new sampling state. When the scope exists, it restores
797 // the sampling state having recorded.
798 template<size_t BucketNumber
>
799 class TraceEventSamplingStateScope
{
801 TraceEventSamplingStateScope(const char* category_and_name
) {
802 previous_state_
= TraceEventSamplingStateScope
<BucketNumber
>::Current();
803 TraceEventSamplingStateScope
<BucketNumber
>::Set(category_and_name
);
806 ~TraceEventSamplingStateScope() {
807 TraceEventSamplingStateScope
<BucketNumber
>::Set(previous_state_
);
810 static inline const char* Current() {
811 return reinterpret_cast<const char*>(TRACE_EVENT_API_ATOMIC_LOAD(
812 g_trace_state
[BucketNumber
]));
815 static inline void Set(const char* category_and_name
) {
816 TRACE_EVENT_API_ATOMIC_STORE(
817 g_trace_state
[BucketNumber
],
818 reinterpret_cast<TRACE_EVENT_API_ATOMIC_WORD
>(
819 const_cast<char*>(category_and_name
)));
823 const char* previous_state_
;
826 } // namespace trace_event_internal
829 namespace trace_event
{
831 template<typename IDType
> class TraceScopedTrackableObject
{
833 TraceScopedTrackableObject(const char* category_group
, const char* name
,
835 : category_group_(category_group
),
838 TRACE_EVENT_OBJECT_CREATED_WITH_ID(category_group_
, name_
, id_
);
841 template <typename ArgType
> void snapshot(ArgType snapshot
) {
842 TRACE_EVENT_OBJECT_SNAPSHOT_WITH_ID(category_group_
, name_
, id_
, snapshot
);
845 ~TraceScopedTrackableObject() {
846 TRACE_EVENT_OBJECT_DELETED_WITH_ID(category_group_
, name_
, id_
);
850 const char* category_group_
;
854 DISALLOW_COPY_AND_ASSIGN(TraceScopedTrackableObject
);
857 } // namespace trace_event
860 #endif // BASE_TRACE_EVENT_TRACE_EVENT_H_