Remove APIPermission::kFileSystemWriteDirectory
[chromium-blink-merge.git] / base / trace_event / trace_event.h
blobc3b20fc58d0584bca3c578ff21e3c8868b8b53bb
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.
12 #include <string>
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");
56 // ...;
57 // }
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(
96 // char phase,
97 // const unsigned char* category_group_enabled,
98 // const char* name,
99 // unsigned long long id,
100 // int num_args,
101 // const char** arg_names,
102 // const unsigned char* arg_types,
103 // const unsigned long long* arg_values,
104 // const scoped_refptr<ConvertableToTraceFormat>*
105 // convertable_values,
106 // unsigned int flags)
107 #define TRACE_EVENT_API_ADD_TRACE_EVENT \
108 base::trace_event::TraceLog::GetInstance()->AddTraceEvent
110 // Add a trace event to the platform tracing system.
111 // base::trace_event::TraceEventHandle
112 // TRACE_EVENT_API_ADD_TRACE_EVENT_WITH_CONTEXT_ID(
113 // char phase,
114 // const unsigned char* category_group_enabled,
115 // const char* name,
116 // unsigned long long id,
117 // unsigned long long context_id,
118 // int num_args,
119 // const char** arg_names,
120 // const unsigned char* arg_types,
121 // const unsigned long long* arg_values,
122 // const scoped_refptr<ConvertableToTraceFormat>*
123 // convertable_values,
124 // unsigned int flags)
125 #define TRACE_EVENT_API_ADD_TRACE_EVENT_WITH_CONTEXT_ID \
126 base::trace_event::TraceLog::GetInstance()->AddTraceEventWithContextId
128 // Add a trace event to the platform tracing system.
129 // base::trace_event::TraceEventHandle
130 // TRACE_EVENT_API_ADD_TRACE_EVENT_WITH_TIMESTAMP(
131 // char phase,
132 // const unsigned char* category_group_enabled,
133 // const char* name,
134 // unsigned long long id,
135 // unsigned long long context_id,
136 // int thread_id,
137 // const TraceTicks& timestamp,
138 // int num_args,
139 // const char** arg_names,
140 // const unsigned char* arg_types,
141 // const unsigned long long* arg_values,
142 // const scoped_refptr<ConvertableToTraceFormat>*
143 // convertable_values,
144 // unsigned int flags)
145 #define TRACE_EVENT_API_ADD_TRACE_EVENT_WITH_THREAD_ID_AND_TIMESTAMP \
146 base::trace_event::TraceLog::GetInstance() \
147 ->AddTraceEventWithThreadIdAndTimestamp
149 // Set the duration field of a COMPLETE trace event.
150 // void TRACE_EVENT_API_UPDATE_TRACE_EVENT_DURATION(
151 // const unsigned char* category_group_enabled,
152 // const char* name,
153 // base::trace_event::TraceEventHandle id)
154 #define TRACE_EVENT_API_UPDATE_TRACE_EVENT_DURATION \
155 base::trace_event::TraceLog::GetInstance()->UpdateTraceEventDuration
157 // Defines atomic operations used internally by the tracing system.
158 #define TRACE_EVENT_API_ATOMIC_WORD base::subtle::AtomicWord
159 #define TRACE_EVENT_API_ATOMIC_LOAD(var) base::subtle::NoBarrier_Load(&(var))
160 #define TRACE_EVENT_API_ATOMIC_STORE(var, value) \
161 base::subtle::NoBarrier_Store(&(var), (value))
163 // Defines visibility for classes in trace_event.h
164 #define TRACE_EVENT_API_CLASS_EXPORT BASE_EXPORT
166 // The thread buckets for the sampling profiler.
167 TRACE_EVENT_API_CLASS_EXPORT extern \
168 TRACE_EVENT_API_ATOMIC_WORD g_trace_state[3];
170 #define TRACE_EVENT_API_THREAD_BUCKET(thread_bucket) \
171 g_trace_state[thread_bucket]
173 ////////////////////////////////////////////////////////////////////////////////
175 // Implementation detail: trace event macros create temporary variables
176 // to keep instrumentation overhead low. These macros give each temporary
177 // variable a unique name based on the line number to prevent name collisions.
178 #define INTERNAL_TRACE_EVENT_UID3(a,b) \
179 trace_event_unique_##a##b
180 #define INTERNAL_TRACE_EVENT_UID2(a,b) \
181 INTERNAL_TRACE_EVENT_UID3(a,b)
182 #define INTERNAL_TRACE_EVENT_UID(name_prefix) \
183 INTERNAL_TRACE_EVENT_UID2(name_prefix, __LINE__)
185 // Implementation detail: internal macro to create static category.
186 // No barriers are needed, because this code is designed to operate safely
187 // even when the unsigned char* points to garbage data (which may be the case
188 // on processors without cache coherency).
189 #define INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO_CUSTOM_VARIABLES( \
190 category_group, atomic, category_group_enabled) \
191 category_group_enabled = \
192 reinterpret_cast<const unsigned char*>(TRACE_EVENT_API_ATOMIC_LOAD( \
193 atomic)); \
194 if (UNLIKELY(!category_group_enabled)) { \
195 category_group_enabled = \
196 TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED(category_group); \
197 TRACE_EVENT_API_ATOMIC_STORE(atomic, \
198 reinterpret_cast<TRACE_EVENT_API_ATOMIC_WORD>( \
199 category_group_enabled)); \
202 #define INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category_group) \
203 static TRACE_EVENT_API_ATOMIC_WORD INTERNAL_TRACE_EVENT_UID(atomic) = 0; \
204 const unsigned char* INTERNAL_TRACE_EVENT_UID(category_group_enabled); \
205 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO_CUSTOM_VARIABLES(category_group, \
206 INTERNAL_TRACE_EVENT_UID(atomic), \
207 INTERNAL_TRACE_EVENT_UID(category_group_enabled));
209 // Implementation detail: internal macro to create static category and add
210 // event if the category is enabled.
211 #define INTERNAL_TRACE_EVENT_ADD(phase, category_group, name, flags, ...) \
212 do { \
213 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category_group); \
214 if (INTERNAL_TRACE_EVENT_CATEGORY_GROUP_ENABLED_FOR_RECORDING_MODE()) { \
215 trace_event_internal::AddTraceEvent( \
216 phase, INTERNAL_TRACE_EVENT_UID(category_group_enabled), name, \
217 trace_event_internal::kNoId, flags, \
218 trace_event_internal::kNoId, ##__VA_ARGS__); \
220 } while (0)
222 // 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
224 // ends.
225 #define INTERNAL_TRACE_EVENT_ADD_SCOPED(category_group, name, ...) \
226 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category_group); \
227 trace_event_internal::ScopedTracer INTERNAL_TRACE_EVENT_UID(tracer); \
228 if (INTERNAL_TRACE_EVENT_CATEGORY_GROUP_ENABLED_FOR_RECORDING_MODE()) { \
229 base::trace_event::TraceEventHandle h = \
230 trace_event_internal::AddTraceEvent( \
231 TRACE_EVENT_PHASE_COMPLETE, \
232 INTERNAL_TRACE_EVENT_UID(category_group_enabled), name, \
233 trace_event_internal::kNoId, TRACE_EVENT_FLAG_NONE, \
234 trace_event_internal::kNoId, ##__VA_ARGS__); \
235 INTERNAL_TRACE_EVENT_UID(tracer).Initialize( \
236 INTERNAL_TRACE_EVENT_UID(category_group_enabled), name, h); \
239 #define INTERNAL_TRACE_EVENT_ADD_SCOPED_WITH_FLOW( \
240 category_group, name, bind_id, flow_flags, ...) \
241 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category_group); \
242 trace_event_internal::ScopedTracer INTERNAL_TRACE_EVENT_UID(tracer); \
243 if (INTERNAL_TRACE_EVENT_CATEGORY_GROUP_ENABLED_FOR_RECORDING_MODE()) { \
244 unsigned int trace_event_flags = flow_flags; \
245 trace_event_internal::TraceID trace_event_bind_id(bind_id, \
246 &trace_event_flags); \
247 base::trace_event::TraceEventHandle h = \
248 trace_event_internal::AddTraceEvent( \
249 TRACE_EVENT_PHASE_COMPLETE, \
250 INTERNAL_TRACE_EVENT_UID(category_group_enabled), name, \
251 trace_event_internal::kNoId, trace_event_flags, \
252 trace_event_bind_id.data(), ##__VA_ARGS__); \
253 INTERNAL_TRACE_EVENT_UID(tracer).Initialize( \
254 INTERNAL_TRACE_EVENT_UID(category_group_enabled), name, h); \
257 // Implementation detail: internal macro to create static category and add
258 // event if the category is enabled.
259 #define INTERNAL_TRACE_EVENT_ADD_WITH_ID(phase, category_group, name, id, \
260 flags, ...) \
261 do { \
262 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category_group); \
263 if (INTERNAL_TRACE_EVENT_CATEGORY_GROUP_ENABLED_FOR_RECORDING_MODE()) { \
264 unsigned int trace_event_flags = flags | TRACE_EVENT_FLAG_HAS_ID; \
265 trace_event_internal::TraceID trace_event_trace_id( \
266 id, &trace_event_flags); \
267 trace_event_internal::AddTraceEvent( \
268 phase, INTERNAL_TRACE_EVENT_UID(category_group_enabled), \
269 name, trace_event_trace_id.data(), trace_event_flags, \
270 trace_event_internal::kNoId, ##__VA_ARGS__); \
272 } while (0)
274 // Implementation detail: internal macro to create static category and add
275 // event if the category is enabled.
276 #define INTERNAL_TRACE_EVENT_ADD_WITH_ID_TID_AND_TIMESTAMP(phase, \
277 category_group, name, id, thread_id, timestamp, flags, ...) \
278 do { \
279 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category_group); \
280 if (INTERNAL_TRACE_EVENT_CATEGORY_GROUP_ENABLED_FOR_RECORDING_MODE()) { \
281 unsigned int trace_event_flags = flags | TRACE_EVENT_FLAG_HAS_ID; \
282 trace_event_internal::TraceID trace_event_trace_id( \
283 id, &trace_event_flags); \
284 trace_event_internal::AddTraceEventWithThreadIdAndTimestamp( \
285 phase, INTERNAL_TRACE_EVENT_UID(category_group_enabled), \
286 name, trace_event_trace_id.data(), trace_event_internal::kNoId, \
287 thread_id, base::TraceTicks::FromInternalValue(timestamp), \
288 trace_event_flags | TRACE_EVENT_FLAG_EXPLICIT_TIMESTAMP, \
289 trace_event_internal::kNoId, ##__VA_ARGS__); \
291 } while (0)
294 namespace trace_event_internal {
296 // Specify these values when the corresponding argument of AddTraceEvent is not
297 // used.
298 const int kZeroNumArgs = 0;
299 const unsigned long long kNoId = 0;
301 // TraceID encapsulates an ID that can either be an integer or pointer. Pointers
302 // are by default mangled with the Process ID so that they are unlikely to
303 // collide when the same pointer is used on different processes.
304 class TraceID {
305 public:
306 class DontMangle {
307 public:
308 explicit DontMangle(const void* id)
309 : data_(static_cast<unsigned long long>(
310 reinterpret_cast<uintptr_t>(id))) {}
311 explicit DontMangle(unsigned long long id) : data_(id) {}
312 explicit DontMangle(unsigned long id) : data_(id) {}
313 explicit DontMangle(unsigned int id) : data_(id) {}
314 explicit DontMangle(unsigned short id) : data_(id) {}
315 explicit DontMangle(unsigned char id) : data_(id) {}
316 explicit DontMangle(long long id)
317 : data_(static_cast<unsigned long long>(id)) {}
318 explicit DontMangle(long id)
319 : data_(static_cast<unsigned long long>(id)) {}
320 explicit DontMangle(int id)
321 : data_(static_cast<unsigned long long>(id)) {}
322 explicit DontMangle(short id)
323 : data_(static_cast<unsigned long long>(id)) {}
324 explicit DontMangle(signed char id)
325 : data_(static_cast<unsigned long long>(id)) {}
326 unsigned long long data() const { return data_; }
327 private:
328 unsigned long long data_;
331 class ForceMangle {
332 public:
333 explicit ForceMangle(unsigned long long id) : data_(id) {}
334 explicit ForceMangle(unsigned long id) : data_(id) {}
335 explicit ForceMangle(unsigned int id) : data_(id) {}
336 explicit ForceMangle(unsigned short id) : data_(id) {}
337 explicit ForceMangle(unsigned char id) : data_(id) {}
338 explicit ForceMangle(long long id)
339 : data_(static_cast<unsigned long long>(id)) {}
340 explicit ForceMangle(long id)
341 : data_(static_cast<unsigned long long>(id)) {}
342 explicit ForceMangle(int id)
343 : data_(static_cast<unsigned long long>(id)) {}
344 explicit ForceMangle(short id)
345 : data_(static_cast<unsigned long long>(id)) {}
346 explicit ForceMangle(signed char id)
347 : data_(static_cast<unsigned long long>(id)) {}
348 unsigned long long data() const { return data_; }
349 private:
350 unsigned long long data_;
352 TraceID(const void* id, unsigned int* flags)
353 : data_(static_cast<unsigned long long>(
354 reinterpret_cast<uintptr_t>(id))) {
355 *flags |= TRACE_EVENT_FLAG_MANGLE_ID;
357 TraceID(ForceMangle id, unsigned int* flags) : data_(id.data()) {
358 *flags |= TRACE_EVENT_FLAG_MANGLE_ID;
360 TraceID(DontMangle id, unsigned int* flags) : data_(id.data()) {
362 TraceID(unsigned long long id, unsigned int* flags)
363 : data_(id) { (void)flags; }
364 TraceID(unsigned long id, unsigned int* flags)
365 : data_(id) { (void)flags; }
366 TraceID(unsigned int id, unsigned int* flags)
367 : data_(id) { (void)flags; }
368 TraceID(unsigned short id, unsigned int* flags)
369 : data_(id) { (void)flags; }
370 TraceID(unsigned char id, unsigned int* flags)
371 : data_(id) { (void)flags; }
372 TraceID(long long id, unsigned int* flags)
373 : data_(static_cast<unsigned long long>(id)) { (void)flags; }
374 TraceID(long id, unsigned int* flags)
375 : data_(static_cast<unsigned long long>(id)) { (void)flags; }
376 TraceID(int id, unsigned int* flags)
377 : data_(static_cast<unsigned long long>(id)) { (void)flags; }
378 TraceID(short id, unsigned int* flags)
379 : data_(static_cast<unsigned long long>(id)) { (void)flags; }
380 TraceID(signed char id, unsigned int* flags)
381 : data_(static_cast<unsigned long long>(id)) { (void)flags; }
383 unsigned long long data() const { return data_; }
385 private:
386 unsigned long long data_;
389 // Simple union to store various types as unsigned long long.
390 union TraceValueUnion {
391 bool as_bool;
392 unsigned long long as_uint;
393 long long as_int;
394 double as_double;
395 const void* as_pointer;
396 const char* as_string;
399 // Simple container for const char* that should be copied instead of retained.
400 class TraceStringWithCopy {
401 public:
402 explicit TraceStringWithCopy(const char* str) : str_(str) {}
403 const char* str() const { return str_; }
404 private:
405 const char* str_;
408 // Define SetTraceValue for each allowed type. It stores the type and
409 // value in the return arguments. This allows this API to avoid declaring any
410 // structures so that it is portable to third_party libraries.
411 #define INTERNAL_DECLARE_SET_TRACE_VALUE(actual_type, \
412 arg_expression, \
413 union_member, \
414 value_type_id) \
415 static inline void SetTraceValue( \
416 actual_type arg, \
417 unsigned char* type, \
418 unsigned long long* value) { \
419 TraceValueUnion type_value; \
420 type_value.union_member = arg_expression; \
421 *type = value_type_id; \
422 *value = type_value.as_uint; \
424 // Simpler form for int types that can be safely casted.
425 #define INTERNAL_DECLARE_SET_TRACE_VALUE_INT(actual_type, \
426 value_type_id) \
427 static inline void SetTraceValue( \
428 actual_type arg, \
429 unsigned char* type, \
430 unsigned long long* value) { \
431 *type = value_type_id; \
432 *value = static_cast<unsigned long long>(arg); \
435 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(unsigned long long, TRACE_VALUE_TYPE_UINT)
436 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(unsigned long, TRACE_VALUE_TYPE_UINT)
437 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(unsigned int, TRACE_VALUE_TYPE_UINT)
438 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(unsigned short, TRACE_VALUE_TYPE_UINT)
439 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(unsigned char, TRACE_VALUE_TYPE_UINT)
440 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(long long, TRACE_VALUE_TYPE_INT)
441 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(long, TRACE_VALUE_TYPE_INT)
442 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(int, TRACE_VALUE_TYPE_INT)
443 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(short, TRACE_VALUE_TYPE_INT)
444 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(signed char, TRACE_VALUE_TYPE_INT)
445 INTERNAL_DECLARE_SET_TRACE_VALUE(bool, arg, as_bool, TRACE_VALUE_TYPE_BOOL)
446 INTERNAL_DECLARE_SET_TRACE_VALUE(double, arg, as_double,
447 TRACE_VALUE_TYPE_DOUBLE)
448 INTERNAL_DECLARE_SET_TRACE_VALUE(const void*, arg, as_pointer,
449 TRACE_VALUE_TYPE_POINTER)
450 INTERNAL_DECLARE_SET_TRACE_VALUE(const char*, arg, as_string,
451 TRACE_VALUE_TYPE_STRING)
452 INTERNAL_DECLARE_SET_TRACE_VALUE(const TraceStringWithCopy&, arg.str(),
453 as_string, TRACE_VALUE_TYPE_COPY_STRING)
455 #undef INTERNAL_DECLARE_SET_TRACE_VALUE
456 #undef INTERNAL_DECLARE_SET_TRACE_VALUE_INT
458 // std::string version of SetTraceValue so that trace arguments can be strings.
459 static inline void SetTraceValue(const std::string& arg,
460 unsigned char* type,
461 unsigned long long* value) {
462 TraceValueUnion type_value;
463 type_value.as_string = arg.c_str();
464 *type = TRACE_VALUE_TYPE_COPY_STRING;
465 *value = type_value.as_uint;
468 // base::Time, base::TimeTicks, etc. versions of SetTraceValue to make it easier
469 // to trace these types.
470 static inline void SetTraceValue(const base::Time arg,
471 unsigned char* type,
472 unsigned long long* value) {
473 *type = TRACE_VALUE_TYPE_INT;
474 *value = arg.ToInternalValue();
477 static inline void SetTraceValue(const base::TimeTicks arg,
478 unsigned char* type,
479 unsigned long long* value) {
480 *type = TRACE_VALUE_TYPE_INT;
481 *value = arg.ToInternalValue();
484 static inline void SetTraceValue(const base::ThreadTicks arg,
485 unsigned char* type,
486 unsigned long long* value) {
487 *type = TRACE_VALUE_TYPE_INT;
488 *value = arg.ToInternalValue();
491 static inline void SetTraceValue(const base::TraceTicks arg,
492 unsigned char* type,
493 unsigned long long* value) {
494 *type = TRACE_VALUE_TYPE_INT;
495 *value = arg.ToInternalValue();
498 // These AddTraceEvent and AddTraceEventWithThreadIdAndTimestamp template
499 // functions are defined here instead of in the macro, because the arg_values
500 // could be temporary objects, such as std::string. In order to store
501 // pointers to the internal c_str and pass through to the tracing API,
502 // the arg_values must live throughout these procedures.
504 static inline base::trace_event::TraceEventHandle
505 AddTraceEventWithThreadIdAndTimestamp(
506 char phase,
507 const unsigned char* category_group_enabled,
508 const char* name,
509 unsigned long long id,
510 unsigned long long context_id,
511 int thread_id,
512 const base::TraceTicks& timestamp,
513 unsigned int flags,
514 unsigned long long bind_id,
515 const char* arg1_name,
516 const scoped_refptr<base::trace_event::ConvertableToTraceFormat>&
517 arg1_val) {
518 const int num_args = 1;
519 unsigned char arg_types[1] = { TRACE_VALUE_TYPE_CONVERTABLE };
520 return TRACE_EVENT_API_ADD_TRACE_EVENT_WITH_THREAD_ID_AND_TIMESTAMP(
521 phase, category_group_enabled, name, id, context_id, bind_id, thread_id,
522 timestamp, num_args, &arg1_name, arg_types, NULL, &arg1_val, flags);
525 template<class ARG1_TYPE>
526 static inline base::trace_event::TraceEventHandle
527 AddTraceEventWithThreadIdAndTimestamp(
528 char phase,
529 const unsigned char* category_group_enabled,
530 const char* name,
531 unsigned long long id,
532 unsigned long long context_id,
533 int thread_id,
534 const base::TraceTicks& timestamp,
535 unsigned int flags,
536 unsigned long long bind_id,
537 const char* arg1_name,
538 const ARG1_TYPE& arg1_val,
539 const char* arg2_name,
540 const scoped_refptr<base::trace_event::ConvertableToTraceFormat>&
541 arg2_val) {
542 const int num_args = 2;
543 const char* arg_names[2] = { arg1_name, arg2_name };
545 unsigned char arg_types[2];
546 unsigned long long arg_values[2];
547 SetTraceValue(arg1_val, &arg_types[0], &arg_values[0]);
548 arg_types[1] = TRACE_VALUE_TYPE_CONVERTABLE;
550 scoped_refptr<base::trace_event::ConvertableToTraceFormat>
551 convertable_values[2];
552 convertable_values[1] = arg2_val;
554 return TRACE_EVENT_API_ADD_TRACE_EVENT_WITH_THREAD_ID_AND_TIMESTAMP(
555 phase, category_group_enabled, name, id, context_id, bind_id, thread_id,
556 timestamp, num_args, arg_names, arg_types, arg_values,
557 convertable_values, flags);
560 template<class ARG2_TYPE>
561 static inline base::trace_event::TraceEventHandle
562 AddTraceEventWithThreadIdAndTimestamp(
563 char phase,
564 const unsigned char* category_group_enabled,
565 const char* name,
566 unsigned long long id,
567 unsigned long long context_id,
568 int thread_id,
569 const base::TraceTicks& timestamp,
570 unsigned int flags,
571 unsigned long long bind_id,
572 const char* arg1_name,
573 const scoped_refptr<base::trace_event::ConvertableToTraceFormat>& arg1_val,
574 const char* arg2_name,
575 const ARG2_TYPE& arg2_val) {
576 const int num_args = 2;
577 const char* arg_names[2] = { arg1_name, arg2_name };
579 unsigned char arg_types[2];
580 unsigned long long arg_values[2];
581 arg_types[0] = TRACE_VALUE_TYPE_CONVERTABLE;
582 arg_values[0] = 0;
583 SetTraceValue(arg2_val, &arg_types[1], &arg_values[1]);
585 scoped_refptr<base::trace_event::ConvertableToTraceFormat>
586 convertable_values[2];
587 convertable_values[0] = arg1_val;
589 return TRACE_EVENT_API_ADD_TRACE_EVENT_WITH_THREAD_ID_AND_TIMESTAMP(
590 phase, category_group_enabled, name, id, context_id, bind_id, thread_id,
591 timestamp, num_args, arg_names, arg_types, arg_values,
592 convertable_values, flags);
595 static inline base::trace_event::TraceEventHandle
596 AddTraceEventWithThreadIdAndTimestamp(
597 char phase,
598 const unsigned char* category_group_enabled,
599 const char* name,
600 unsigned long long id,
601 unsigned long long context_id,
602 int thread_id,
603 const base::TraceTicks& timestamp,
604 unsigned int flags,
605 unsigned long long bind_id,
606 const char* arg1_name,
607 const scoped_refptr<base::trace_event::ConvertableToTraceFormat>& arg1_val,
608 const char* arg2_name,
609 const scoped_refptr<base::trace_event::ConvertableToTraceFormat>&
610 arg2_val) {
611 const int num_args = 2;
612 const char* arg_names[2] = { arg1_name, arg2_name };
613 unsigned char arg_types[2] =
614 { TRACE_VALUE_TYPE_CONVERTABLE, TRACE_VALUE_TYPE_CONVERTABLE };
615 scoped_refptr<base::trace_event::ConvertableToTraceFormat>
616 convertable_values[2] = {arg1_val, arg2_val};
618 return TRACE_EVENT_API_ADD_TRACE_EVENT_WITH_THREAD_ID_AND_TIMESTAMP(
619 phase, category_group_enabled, name, id, context_id, bind_id, thread_id,
620 timestamp, num_args, arg_names, arg_types, NULL, convertable_values,
621 flags);
624 static inline base::trace_event::TraceEventHandle
625 AddTraceEventWithThreadIdAndTimestamp(
626 char phase,
627 const unsigned char* category_group_enabled,
628 const char* name,
629 unsigned long long id,
630 unsigned long long context_id,
631 int thread_id,
632 const base::TraceTicks& timestamp,
633 unsigned int flags,
634 unsigned long long bind_id) {
635 return TRACE_EVENT_API_ADD_TRACE_EVENT_WITH_THREAD_ID_AND_TIMESTAMP(
636 phase, category_group_enabled, name, id, context_id, bind_id, thread_id,
637 timestamp, kZeroNumArgs, NULL, NULL, NULL, NULL, flags);
640 static inline base::trace_event::TraceEventHandle AddTraceEvent(
641 char phase,
642 const unsigned char* category_group_enabled,
643 const char* name,
644 unsigned long long id,
645 unsigned int flags,
646 unsigned long long bind_id) {
647 const int thread_id = static_cast<int>(base::PlatformThread::CurrentId());
648 const base::TraceTicks now = base::TraceTicks::Now();
649 return AddTraceEventWithThreadIdAndTimestamp(phase, category_group_enabled,
650 name, id, kNoId, thread_id, now,
651 flags, bind_id);
654 template<class ARG1_TYPE>
655 static inline base::trace_event::TraceEventHandle
656 AddTraceEventWithThreadIdAndTimestamp(
657 char phase,
658 const unsigned char* category_group_enabled,
659 const char* name,
660 unsigned long long id,
661 unsigned long long context_id,
662 int thread_id,
663 const base::TraceTicks& timestamp,
664 unsigned int flags,
665 unsigned long long bind_id,
666 const char* arg1_name,
667 const ARG1_TYPE& arg1_val) {
668 const int num_args = 1;
669 unsigned char arg_types[1];
670 unsigned long long arg_values[1];
671 SetTraceValue(arg1_val, &arg_types[0], &arg_values[0]);
672 return TRACE_EVENT_API_ADD_TRACE_EVENT_WITH_THREAD_ID_AND_TIMESTAMP(
673 phase, category_group_enabled, name, id, context_id, bind_id, thread_id,
674 timestamp, num_args, &arg1_name, arg_types, arg_values, NULL, flags);
677 template<class ARG1_TYPE>
678 static inline base::trace_event::TraceEventHandle AddTraceEvent(
679 char phase,
680 const unsigned char* category_group_enabled,
681 const char* name,
682 unsigned long long id,
683 unsigned int flags,
684 unsigned long long bind_id,
685 const char* arg1_name,
686 const ARG1_TYPE& arg1_val) {
687 int thread_id = static_cast<int>(base::PlatformThread::CurrentId());
688 base::TraceTicks now = base::TraceTicks::Now();
689 return AddTraceEventWithThreadIdAndTimestamp(phase, category_group_enabled,
690 name, id, kNoId, thread_id, now,
691 flags, bind_id,
692 arg1_name, arg1_val);
695 template<class ARG1_TYPE, class ARG2_TYPE>
696 static inline base::trace_event::TraceEventHandle
697 AddTraceEventWithThreadIdAndTimestamp(
698 char phase,
699 const unsigned char* category_group_enabled,
700 const char* name,
701 unsigned long long id,
702 unsigned long long context_id,
703 int thread_id,
704 const base::TraceTicks& timestamp,
705 unsigned int flags,
706 unsigned long long bind_id,
707 const char* arg1_name,
708 const ARG1_TYPE& arg1_val,
709 const char* arg2_name,
710 const ARG2_TYPE& arg2_val) {
711 const int num_args = 2;
712 const char* arg_names[2] = { arg1_name, arg2_name };
713 unsigned char arg_types[2];
714 unsigned long long arg_values[2];
715 SetTraceValue(arg1_val, &arg_types[0], &arg_values[0]);
716 SetTraceValue(arg2_val, &arg_types[1], &arg_values[1]);
717 return TRACE_EVENT_API_ADD_TRACE_EVENT_WITH_THREAD_ID_AND_TIMESTAMP(
718 phase, category_group_enabled, name, id, context_id, bind_id, thread_id,
719 timestamp, num_args, arg_names, arg_types, arg_values, NULL, flags);
722 template<class ARG1_TYPE, class ARG2_TYPE>
723 static inline base::trace_event::TraceEventHandle AddTraceEvent(
724 char phase,
725 const unsigned char* category_group_enabled,
726 const char* name,
727 unsigned long long id,
728 unsigned int flags,
729 unsigned long long bind_id,
730 const char* arg1_name,
731 const ARG1_TYPE& arg1_val,
732 const char* arg2_name,
733 const ARG2_TYPE& arg2_val) {
734 int thread_id = static_cast<int>(base::PlatformThread::CurrentId());
735 base::TraceTicks now = base::TraceTicks::Now();
736 return AddTraceEventWithThreadIdAndTimestamp(phase, category_group_enabled,
737 name, id, kNoId, thread_id, now,
738 flags, bind_id,
739 arg1_name, arg1_val,
740 arg2_name, arg2_val);
743 // Used by TRACE_EVENTx macros. Do not use directly.
744 class TRACE_EVENT_API_CLASS_EXPORT ScopedTracer {
745 public:
746 // Note: members of data_ intentionally left uninitialized. See Initialize.
747 ScopedTracer() : p_data_(NULL) {}
749 ~ScopedTracer() {
750 if (p_data_ && *data_.category_group_enabled)
751 TRACE_EVENT_API_UPDATE_TRACE_EVENT_DURATION(
752 data_.category_group_enabled, data_.name, data_.event_handle);
755 void Initialize(const unsigned char* category_group_enabled,
756 const char* name,
757 base::trace_event::TraceEventHandle event_handle) {
758 data_.category_group_enabled = category_group_enabled;
759 data_.name = name;
760 data_.event_handle = event_handle;
761 p_data_ = &data_;
764 private:
765 // This Data struct workaround is to avoid initializing all the members
766 // 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
768 // members of this class instead, compiler warnings occur about potential
769 // uninitialized accesses.
770 struct Data {
771 const unsigned char* category_group_enabled;
772 const char* name;
773 base::trace_event::TraceEventHandle event_handle;
775 Data* p_data_;
776 Data data_;
779 // Used by TRACE_EVENT_BINARY_EFFICIENTx macro. Do not use directly.
780 class TRACE_EVENT_API_CLASS_EXPORT ScopedTraceBinaryEfficient {
781 public:
782 ScopedTraceBinaryEfficient(const char* category_group, const char* name);
783 ~ScopedTraceBinaryEfficient();
785 private:
786 const unsigned char* category_group_enabled_;
787 const char* name_;
788 base::trace_event::TraceEventHandle event_handle_;
791 // This macro generates less code then TRACE_EVENT0 but is also
792 // slower to execute when tracing is off. It should generally only be
793 // used with code that is seldom executed or conditionally executed
794 // when debugging.
795 // For now the category_group must be "gpu".
796 #define TRACE_EVENT_BINARY_EFFICIENT0(category_group, name) \
797 trace_event_internal::ScopedTraceBinaryEfficient \
798 INTERNAL_TRACE_EVENT_UID(scoped_trace)(category_group, name);
800 // TraceEventSamplingStateScope records the current sampling state
801 // and sets a new sampling state. When the scope exists, it restores
802 // the sampling state having recorded.
803 template<size_t BucketNumber>
804 class TraceEventSamplingStateScope {
805 public:
806 TraceEventSamplingStateScope(const char* category_and_name) {
807 previous_state_ = TraceEventSamplingStateScope<BucketNumber>::Current();
808 TraceEventSamplingStateScope<BucketNumber>::Set(category_and_name);
811 ~TraceEventSamplingStateScope() {
812 TraceEventSamplingStateScope<BucketNumber>::Set(previous_state_);
815 static inline const char* Current() {
816 return reinterpret_cast<const char*>(TRACE_EVENT_API_ATOMIC_LOAD(
817 g_trace_state[BucketNumber]));
820 static inline void Set(const char* category_and_name) {
821 TRACE_EVENT_API_ATOMIC_STORE(
822 g_trace_state[BucketNumber],
823 reinterpret_cast<TRACE_EVENT_API_ATOMIC_WORD>(
824 const_cast<char*>(category_and_name)));
827 private:
828 const char* previous_state_;
831 } // namespace trace_event_internal
833 namespace base {
834 namespace trace_event {
836 template<typename IDType> class TraceScopedTrackableObject {
837 public:
838 TraceScopedTrackableObject(const char* category_group, const char* name,
839 IDType id)
840 : category_group_(category_group),
841 name_(name),
842 id_(id) {
843 TRACE_EVENT_OBJECT_CREATED_WITH_ID(category_group_, name_, id_);
846 template <typename ArgType> void snapshot(ArgType snapshot) {
847 TRACE_EVENT_OBJECT_SNAPSHOT_WITH_ID(category_group_, name_, id_, snapshot);
850 ~TraceScopedTrackableObject() {
851 TRACE_EVENT_OBJECT_DELETED_WITH_ID(category_group_, name_, id_);
854 private:
855 const char* category_group_;
856 const char* name_;
857 IDType id_;
859 DISALLOW_COPY_AND_ASSIGN(TraceScopedTrackableObject);
862 } // namespace trace_event
863 } // namespace base
865 #endif // BASE_TRACE_EVENT_TRACE_EVENT_H_