1 #ifndef ANDROID_PDX_TRACE_H_
2 #define ANDROID_PDX_TRACE_H_
6 #include <utils/Trace.h>
8 // Enables internal tracing in libpdx. This is disabled by default to avoid
9 // spamming the trace buffers during normal trace activities. libpdx must be
10 // built with this set to true to enable internal tracing.
11 #ifndef PDX_LIB_TRACE_ENABLED
12 #define PDX_LIB_TRACE_ENABLED false
18 // Utility to generate scoped tracers with arguments.
19 class ScopedTraceArgs {
21 template <typename... Args>
22 ScopedTraceArgs(uint64_t tag, const char* format, Args&&... args)
24 if (atrace_is_tag_enabled(tag_)) {
25 std::array<char, 1024> buffer;
26 snprintf(buffer.data(), buffer.size(), format,
27 std::forward<Args>(args)...);
28 atrace_begin(tag_, buffer.data());
32 ~ScopedTraceArgs() { atrace_end(tag_); }
37 ScopedTraceArgs(const ScopedTraceArgs&) = delete;
38 void operator=(const ScopedTraceArgs&) = delete;
41 // Utility to generate scoped tracers.
44 template <typename... Args>
45 ScopedTrace(uint64_t tag, bool enabled, const char* name)
46 : tag_{tag}, enabled_{enabled} {
48 atrace_begin(tag_, name);
60 ScopedTrace(const ScopedTrace&) = delete;
61 void operator=(const ScopedTrace&) = delete;
65 } // namespace android
67 // Macro to define a scoped tracer with arguments. Uses PASTE(x, y) macro
68 // defined in utils/Trace.h.
69 #define PDX_TRACE_FORMAT(format, ...) \
70 ::android::pdx::ScopedTraceArgs PASTE(__tracer, __LINE__) { \
71 ATRACE_TAG, format, ##__VA_ARGS__ \
74 // TODO(eieio): Rename this to PDX_LIB_TRACE_NAME() for internal use by libpdx
75 // and rename internal uses inside the library. This version is only enabled
76 // when PDX_LIB_TRACE_ENABLED is true.
77 #define PDX_TRACE_NAME(name) \
78 ::android::pdx::ScopedTrace PASTE(__tracer, __LINE__) { \
79 ATRACE_TAG, PDX_LIB_TRACE_ENABLED, name \
82 #endif // ANDROID_PDX_TRACE_H_