2
* Copyright (C) 2012 The Android Open Source Project
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
8
* http://www.apache.org/licenses/LICENSE-2.0
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
17
#ifndef _LIBS_CUTILS_TRACE_H
18
#define _LIBS_CUTILS_TRACE_H
21
#include <stdatomic.h>
25
#include <sys/cdefs.h>
26
#include <sys/types.h>
28
#include <cutils/compiler.h>
33
* The ATRACE_TAG macro can be defined before including this header to trace
34
* using one of the tags defined below. It must be defined to one of the
35
* following ATRACE_TAG_* macros. The trace tag is used to filter tracing in
36
* userland to avoid some of the runtime cost of tracing when it is not desired.
38
* Defining ATRACE_TAG to be ATRACE_TAG_ALWAYS will result in the tracing always
39
* being enabled - this should ONLY be done for debug code, as userland tracing
40
* has a performance cost even when the trace is not being recorded. Defining
41
* ATRACE_TAG to be ATRACE_TAG_NEVER or leaving ATRACE_TAG undefined will result
42
* in the tracing always being disabled.
44
* ATRACE_TAG_HAL should be bitwise ORed with the relevant tags for tracing
45
* within a hardware module. For example a camera hardware module would set:
46
* #define ATRACE_TAG (ATRACE_TAG_CAMERA | ATRACE_TAG_HAL)
48
* Keep these in sync with frameworks/base/core/java/android/os/Trace.java.
50
#define ATRACE_TAG_NEVER 0 // This tag is never enabled.
51
#define ATRACE_TAG_ALWAYS (1<<0) // This tag is always enabled.
52
#define ATRACE_TAG_GRAPHICS (1<<1)
53
#define ATRACE_TAG_INPUT (1<<2)
54
#define ATRACE_TAG_VIEW (1<<3)
55
#define ATRACE_TAG_WEBVIEW (1<<4)
56
#define ATRACE_TAG_WINDOW_MANAGER (1<<5)
57
#define ATRACE_TAG_ACTIVITY_MANAGER (1<<6)
58
#define ATRACE_TAG_SYNC_MANAGER (1<<7)
59
#define ATRACE_TAG_AUDIO (1<<8)
60
#define ATRACE_TAG_VIDEO (1<<9)
61
#define ATRACE_TAG_CAMERA (1<<10)
62
#define ATRACE_TAG_HAL (1<<11)
63
#define ATRACE_TAG_APP (1<<12)
64
#define ATRACE_TAG_RESOURCES (1<<13)
65
#define ATRACE_TAG_DALVIK (1<<14)
66
#define ATRACE_TAG_RS (1<<15)
67
#define ATRACE_TAG_BIONIC (1<<16)
68
#define ATRACE_TAG_POWER (1<<17)
69
#define ATRACE_TAG_PACKAGE_MANAGER (1<<18)
70
#define ATRACE_TAG_SYSTEM_SERVER (1<<19)
71
#define ATRACE_TAG_DATABASE (1<<20)
72
#define ATRACE_TAG_NETWORK (1<<21)
73
#define ATRACE_TAG_ADB (1<<22)
74
#define ATRACE_TAG_VIBRATOR (1<<23)
75
#define ATRACE_TAG_AIDL (1<<24)
76
#define ATRACE_TAG_NNAPI (1<<25)
77
#define ATRACE_TAG_RRO (1<<26)
78
#define ATRACE_TAG_LAST ATRACE_TAG_RRO
80
// Reserved for initialization.
81
#define ATRACE_TAG_NOT_READY (1ULL<<63)
83
#define ATRACE_TAG_VALID_MASK ((ATRACE_TAG_LAST - 1) | ATRACE_TAG_LAST)
86
#define ATRACE_TAG ATRACE_TAG_NEVER
87
#elif ATRACE_TAG > ATRACE_TAG_VALID_MASK
88
#error ATRACE_TAG must be defined to be one of the tags defined in cutils/trace.h
92
* Opens the trace file for writing and reads the property for initial tags.
93
* The atrace.tags.enableflags property sets the tags to trace.
94
* This function should not be explicitly called, the first call to any normal
95
* trace function will cause it to be run safely.
100
* If tracing is ready, set atrace_enabled_tags to the system property
101
* debug.atrace.tags.enableflags. Can be used as a sysprop change callback.
103
void atrace_update_tags();
106
* Set whether tracing is enabled for the current process. This is used to
107
* prevent tracing within the Zygote process.
109
void atrace_set_tracing_enabled(bool enabled);
112
* This is always set to false. This forces code that uses an old version
113
* of this header to always call into atrace_setup, in which we call
114
* atrace_init unconditionally.
116
extern atomic_bool atrace_is_ready;
119
* Set of ATRACE_TAG flags to trace for, initialized to ATRACE_TAG_NOT_READY.
120
* A value of zero indicates setup has failed.
121
* Any other nonzero value indicates setup has succeeded, and tracing is on.
123
extern uint64_t atrace_enabled_tags;
126
* Handle to the kernel's trace buffer, initialized to -1.
127
* Any other value indicates setup has succeeded, and is a valid fd for tracing.
129
extern int atrace_marker_fd;
132
* atrace_init readies the process for tracing by opening the trace_marker file.
133
* Calling any trace function causes this to be run, so calling it is optional.
134
* This can be explicitly run to avoid setup delay on first trace function.
136
#define ATRACE_INIT() atrace_init()
137
#define ATRACE_GET_ENABLED_TAGS() atrace_get_enabled_tags()
140
uint64_t atrace_get_enabled_tags();
143
* Test if a given tag is currently enabled.
144
* Returns nonzero if the tag is enabled, otherwise zero.
145
* It can be used as a guard condition around more expensive trace calculations.
147
#define ATRACE_ENABLED() atrace_is_tag_enabled(ATRACE_TAG)
148
static inline uint64_t atrace_is_tag_enabled(uint64_t tag)
150
return atrace_get_enabled_tags() & tag;
154
* Trace the beginning of a context. name is used to identify the context.
155
* This is often used to time function execution.
157
#define ATRACE_BEGIN(name) atrace_begin(ATRACE_TAG, name)
158
static inline void atrace_begin(uint64_t tag, const char* name)
160
if (CC_UNLIKELY(atrace_is_tag_enabled(tag))) {
161
void atrace_begin_body(const char*);
162
atrace_begin_body(name);
167
* Trace the end of a context.
168
* This should match up (and occur after) a corresponding ATRACE_BEGIN.
170
#define ATRACE_END() atrace_end(ATRACE_TAG)
171
static inline void atrace_end(uint64_t tag)
173
if (CC_UNLIKELY(atrace_is_tag_enabled(tag))) {
174
void atrace_end_body();
180
* Trace the beginning of an asynchronous event. Unlike ATRACE_BEGIN/ATRACE_END
181
* contexts, asynchronous events do not need to be nested. The name describes
182
* the event, and the cookie provides a unique identifier for distinguishing
183
* simultaneous events. The name and cookie used to begin an event must be
186
#define ATRACE_ASYNC_BEGIN(name, cookie) \
187
atrace_async_begin(ATRACE_TAG, name, cookie)
188
static inline void atrace_async_begin(uint64_t tag, const char* name,
191
if (CC_UNLIKELY(atrace_is_tag_enabled(tag))) {
192
void atrace_async_begin_body(const char*, int32_t);
193
atrace_async_begin_body(name, cookie);
198
* Trace the end of an asynchronous event.
199
* This should have a corresponding ATRACE_ASYNC_BEGIN.
201
#define ATRACE_ASYNC_END(name, cookie) atrace_async_end(ATRACE_TAG, name, cookie)
202
static inline void atrace_async_end(uint64_t tag, const char* name, int32_t cookie)
204
if (CC_UNLIKELY(atrace_is_tag_enabled(tag))) {
205
void atrace_async_end_body(const char*, int32_t);
206
atrace_async_end_body(name, cookie);
211
* Traces an integer counter value. name is used to identify the counter.
212
* This can be used to track how a value changes over time.
214
#define ATRACE_INT(name, value) atrace_int(ATRACE_TAG, name, value)
215
static inline void atrace_int(uint64_t tag, const char* name, int32_t value)
217
if (CC_UNLIKELY(atrace_is_tag_enabled(tag))) {
218
void atrace_int_body(const char*, int32_t);
219
atrace_int_body(name, value);
224
* Traces a 64-bit integer counter value. name is used to identify the
225
* counter. This can be used to track how a value changes over time.
227
#define ATRACE_INT64(name, value) atrace_int64(ATRACE_TAG, name, value)
228
static inline void atrace_int64(uint64_t tag, const char* name, int64_t value)
230
if (CC_UNLIKELY(atrace_is_tag_enabled(tag))) {
231
void atrace_int64_body(const char*, int64_t);
232
atrace_int64_body(name, value);
238
#endif // _LIBS_CUTILS_TRACE_H