blob: fd24561f0a3bd8c8d46bc80572c2311280a69ada [file] [log] [blame]
Alex Ray0a346432012-11-14 17:25:28 -08001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17#ifndef _LIBS_CUTILS_TRACE_H
18#define _LIBS_CUTILS_TRACE_H
19
Mark Salyzyna459d0b2014-03-20 13:02:57 -070020#include <inttypes.h>
21#include <stdbool.h>
22#include <stdint.h>
Mark Salyzyn48878422014-05-22 16:08:52 -070023#include <stdio.h>
Alex Ray0a346432012-11-14 17:25:28 -080024#include <sys/cdefs.h>
25#include <sys/types.h>
Alex Ray0a346432012-11-14 17:25:28 -080026#include <unistd.h>
Alex Ray0a346432012-11-14 17:25:28 -080027
Mark Salyzyna459d0b2014-03-20 13:02:57 -070028#include <cutils/compiler.h>
Alex Ray448f76a2012-11-30 19:37:53 -080029#ifdef ANDROID_SMP
30#include <cutils/atomic-inline.h>
31#else
32#include <cutils/atomic.h>
33#endif
34
Alex Ray0a346432012-11-14 17:25:28 -080035__BEGIN_DECLS
36
37/**
38 * The ATRACE_TAG macro can be defined before including this header to trace
39 * using one of the tags defined below. It must be defined to one of the
40 * following ATRACE_TAG_* macros. The trace tag is used to filter tracing in
41 * userland to avoid some of the runtime cost of tracing when it is not desired.
42 *
43 * Defining ATRACE_TAG to be ATRACE_TAG_ALWAYS will result in the tracing always
44 * being enabled - this should ONLY be done for debug code, as userland tracing
45 * has a performance cost even when the trace is not being recorded. Defining
46 * ATRACE_TAG to be ATRACE_TAG_NEVER or leaving ATRACE_TAG undefined will result
47 * in the tracing always being disabled.
48 *
49 * ATRACE_TAG_HAL should be bitwise ORed with the relevant tags for tracing
50 * within a hardware module. For example a camera hardware module would set:
51 * #define ATRACE_TAG (ATRACE_TAG_CAMERA | ATRACE_TAG_HAL)
52 *
53 * Keep these in sync with frameworks/base/core/java/android/os/Trace.java.
54 */
55#define ATRACE_TAG_NEVER 0 // This tag is never enabled.
56#define ATRACE_TAG_ALWAYS (1<<0) // This tag is always enabled.
57#define ATRACE_TAG_GRAPHICS (1<<1)
58#define ATRACE_TAG_INPUT (1<<2)
59#define ATRACE_TAG_VIEW (1<<3)
60#define ATRACE_TAG_WEBVIEW (1<<4)
61#define ATRACE_TAG_WINDOW_MANAGER (1<<5)
62#define ATRACE_TAG_ACTIVITY_MANAGER (1<<6)
63#define ATRACE_TAG_SYNC_MANAGER (1<<7)
64#define ATRACE_TAG_AUDIO (1<<8)
65#define ATRACE_TAG_VIDEO (1<<9)
66#define ATRACE_TAG_CAMERA (1<<10)
67#define ATRACE_TAG_HAL (1<<11)
Jamie Gennis774f9292013-02-25 18:15:40 -080068#define ATRACE_TAG_APP (1<<12)
Dianne Hackborn24bc41b2013-04-12 14:51:43 -070069#define ATRACE_TAG_RESOURCES (1<<13)
Jamie Gennis2b68e062013-05-07 15:09:17 -070070#define ATRACE_TAG_DALVIK (1<<14)
Tim Murrayd8b11c12013-05-23 13:46:12 -070071#define ATRACE_TAG_RS (1<<15)
Brigid Smith8c82b352014-05-27 12:37:48 -070072#define ATRACE_TAG_BIONIC (1<<16)
Jeff Brownde4d9212014-08-14 19:23:30 -070073#define ATRACE_TAG_POWER (1<<17)
74#define ATRACE_TAG_LAST ATRACE_TAG_POWER
Alex Ray0a346432012-11-14 17:25:28 -080075
76// Reserved for initialization.
77#define ATRACE_TAG_NOT_READY (1LL<<63)
78
79#define ATRACE_TAG_VALID_MASK ((ATRACE_TAG_LAST - 1) | ATRACE_TAG_LAST)
80
81#ifndef ATRACE_TAG
82#define ATRACE_TAG ATRACE_TAG_NEVER
83#elif ATRACE_TAG > ATRACE_TAG_VALID_MASK
84#error ATRACE_TAG must be defined to be one of the tags defined in cutils/trace.h
85#endif
86
Jamie Gennis2b68e062013-05-07 15:09:17 -070087#ifdef HAVE_ANDROID_OS
Alex Ray0a346432012-11-14 17:25:28 -080088/**
89 * Maximum size of a message that can be logged to the trace buffer.
90 * Note this message includes a tag, the pid, and the string given as the name.
91 * Names should be kept short to get the most use of the trace buffer.
92 */
93#define ATRACE_MESSAGE_LENGTH 1024
94
95/**
96 * Opens the trace file for writing and reads the property for initial tags.
97 * The atrace.tags.enableflags property sets the tags to trace.
98 * This function should not be explicitly called, the first call to any normal
99 * trace function will cause it to be run safely.
100 */
101void atrace_setup();
102
103/**
Alex Raye7bb7bc2012-11-20 01:39:09 -0800104 * If tracing is ready, set atrace_enabled_tags to the system property
105 * debug.atrace.tags.enableflags. Can be used as a sysprop change callback.
106 */
107void atrace_update_tags();
108
109/**
Jamie Gennis774f9292013-02-25 18:15:40 -0800110 * Set whether the process is debuggable. By default the process is not
111 * considered debuggable. If the process is not debuggable then application-
112 * level tracing is not allowed unless the ro.debuggable system property is
113 * set to '1'.
114 */
115void atrace_set_debuggable(bool debuggable);
116
117/**
Jamie Gennisb13ea452013-04-15 18:50:22 -0700118 * Set whether tracing is enabled for the current process. This is used to
119 * prevent tracing within the Zygote process.
120 */
121void atrace_set_tracing_enabled(bool enabled);
122
123/**
Alex Ray0a346432012-11-14 17:25:28 -0800124 * Flag indicating whether setup has been completed, initialized to 0.
125 * Nonzero indicates setup has completed.
126 * Note: This does NOT indicate whether or not setup was successful.
127 */
Jamie Gennisb13ea452013-04-15 18:50:22 -0700128extern volatile int32_t atrace_is_ready;
Alex Ray0a346432012-11-14 17:25:28 -0800129
130/**
131 * Set of ATRACE_TAG flags to trace for, initialized to ATRACE_TAG_NOT_READY.
132 * A value of zero indicates setup has failed.
133 * Any other nonzero value indicates setup has succeeded, and tracing is on.
134 */
135extern uint64_t atrace_enabled_tags;
136
137/**
138 * Handle to the kernel's trace buffer, initialized to -1.
139 * Any other value indicates setup has succeeded, and is a valid fd for tracing.
140 */
141extern int atrace_marker_fd;
142
143/**
144 * atrace_init readies the process for tracing by opening the trace_marker file.
145 * Calling any trace function causes this to be run, so calling it is optional.
146 * This can be explicitly run to avoid setup delay on first trace function.
147 */
148#define ATRACE_INIT() atrace_init()
149static inline void atrace_init()
150{
151 if (CC_UNLIKELY(!android_atomic_acquire_load(&atrace_is_ready))) {
152 atrace_setup();
153 }
154}
155
156/**
157 * Get the mask of all tags currently enabled.
158 * It can be used as a guard condition around more expensive trace calculations.
159 * Every trace function calls this, which ensures atrace_init is run.
160 */
161#define ATRACE_GET_ENABLED_TAGS() atrace_get_enabled_tags()
162static inline uint64_t atrace_get_enabled_tags()
163{
164 atrace_init();
165 return atrace_enabled_tags;
166}
167
168/**
169 * Test if a given tag is currently enabled.
170 * Returns nonzero if the tag is enabled, otherwise zero.
171 * It can be used as a guard condition around more expensive trace calculations.
172 */
173#define ATRACE_ENABLED() atrace_is_tag_enabled(ATRACE_TAG)
174static inline uint64_t atrace_is_tag_enabled(uint64_t tag)
175{
176 return atrace_get_enabled_tags() & tag;
177}
178
179/**
180 * Trace the beginning of a context. name is used to identify the context.
181 * This is often used to time function execution.
182 */
183#define ATRACE_BEGIN(name) atrace_begin(ATRACE_TAG, name)
184static inline void atrace_begin(uint64_t tag, const char* name)
185{
186 if (CC_UNLIKELY(atrace_is_tag_enabled(tag))) {
187 char buf[ATRACE_MESSAGE_LENGTH];
188 size_t len;
189
190 len = snprintf(buf, ATRACE_MESSAGE_LENGTH, "B|%d|%s", getpid(), name);
191 write(atrace_marker_fd, buf, len);
192 }
193}
194
195/**
196 * Trace the end of a context.
197 * This should match up (and occur after) a corresponding ATRACE_BEGIN.
198 */
199#define ATRACE_END() atrace_end(ATRACE_TAG)
200static inline void atrace_end(uint64_t tag)
201{
202 if (CC_UNLIKELY(atrace_is_tag_enabled(tag))) {
203 char c = 'E';
204 write(atrace_marker_fd, &c, 1);
205 }
206}
207
208/**
Alex Ray66317772013-03-17 22:39:01 -0700209 * Trace the beginning of an asynchronous event. Unlike ATRACE_BEGIN/ATRACE_END
210 * contexts, asynchronous events do not need to be nested. The name describes
211 * the event, and the cookie provides a unique identifier for distinguishing
212 * simultaneous events. The name and cookie used to begin an event must be
213 * used to end it.
214 */
215#define ATRACE_ASYNC_BEGIN(name, cookie) \
216 atrace_async_begin(ATRACE_TAG, name, cookie)
217static inline void atrace_async_begin(uint64_t tag, const char* name,
218 int32_t cookie)
219{
220 if (CC_UNLIKELY(atrace_is_tag_enabled(tag))) {
221 char buf[ATRACE_MESSAGE_LENGTH];
222 size_t len;
223
Mark Salyzyna459d0b2014-03-20 13:02:57 -0700224 len = snprintf(buf, ATRACE_MESSAGE_LENGTH, "S|%d|%s|%" PRId32,
225 getpid(), name, cookie);
Alex Ray66317772013-03-17 22:39:01 -0700226 write(atrace_marker_fd, buf, len);
227 }
228}
229
230/**
231 * Trace the end of an asynchronous event.
232 * This should have a corresponding ATRACE_ASYNC_BEGIN.
233 */
234#define ATRACE_ASYNC_END(name, cookie) atrace_async_end(ATRACE_TAG, name, cookie)
235static inline void atrace_async_end(uint64_t tag, const char* name,
236 int32_t cookie)
237{
238 if (CC_UNLIKELY(atrace_is_tag_enabled(tag))) {
239 char buf[ATRACE_MESSAGE_LENGTH];
240 size_t len;
241
Mark Salyzyna459d0b2014-03-20 13:02:57 -0700242 len = snprintf(buf, ATRACE_MESSAGE_LENGTH, "F|%d|%s|%" PRId32,
243 getpid(), name, cookie);
Alex Ray66317772013-03-17 22:39:01 -0700244 write(atrace_marker_fd, buf, len);
245 }
246}
247
248
249/**
Alex Ray0a346432012-11-14 17:25:28 -0800250 * Traces an integer counter value. name is used to identify the counter.
251 * This can be used to track how a value changes over time.
252 */
253#define ATRACE_INT(name, value) atrace_int(ATRACE_TAG, name, value)
254static inline void atrace_int(uint64_t tag, const char* name, int32_t value)
255{
256 if (CC_UNLIKELY(atrace_is_tag_enabled(tag))) {
257 char buf[ATRACE_MESSAGE_LENGTH];
258 size_t len;
259
Mark Salyzyna459d0b2014-03-20 13:02:57 -0700260 len = snprintf(buf, ATRACE_MESSAGE_LENGTH, "C|%d|%s|%" PRId32,
Alex Ray0a346432012-11-14 17:25:28 -0800261 getpid(), name, value);
262 write(atrace_marker_fd, buf, len);
263 }
264}
265
Jamie Gennisf1921c72013-09-18 12:01:18 -0700266/**
267 * Traces a 64-bit integer counter value. name is used to identify the
268 * counter. This can be used to track how a value changes over time.
269 */
270#define ATRACE_INT64(name, value) atrace_int64(ATRACE_TAG, name, value)
271static inline void atrace_int64(uint64_t tag, const char* name, int64_t value)
272{
273 if (CC_UNLIKELY(atrace_is_tag_enabled(tag))) {
274 char buf[ATRACE_MESSAGE_LENGTH];
275 size_t len;
276
Mark Salyzyna459d0b2014-03-20 13:02:57 -0700277 len = snprintf(buf, ATRACE_MESSAGE_LENGTH, "C|%d|%s|%" PRId64,
Jamie Gennisf1921c72013-09-18 12:01:18 -0700278 getpid(), name, value);
279 write(atrace_marker_fd, buf, len);
280 }
281}
282
Jamie Gennis2b68e062013-05-07 15:09:17 -0700283#else // not HAVE_ANDROID_OS
284
285#define ATRACE_INIT()
286#define ATRACE_GET_ENABLED_TAGS()
Tobias Grosser9ede3322013-06-17 18:35:46 -0700287#define ATRACE_ENABLED() 0
Jamie Gennis2b68e062013-05-07 15:09:17 -0700288#define ATRACE_BEGIN(name)
289#define ATRACE_END()
290#define ATRACE_ASYNC_BEGIN(name, cookie)
291#define ATRACE_ASYNC_END(name, cookie)
292#define ATRACE_INT(name, value)
293
294#endif // not HAVE_ANDROID_OS
295
Alex Ray0a346432012-11-14 17:25:28 -0800296__END_DECLS
297
298#endif // _LIBS_CUTILS_TRACE_H