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