blob: fd9bc6a9b54a1840354e2a7dea2fecdc33ff706b [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)
73#define ATRACE_TAG_LAST ATRACE_TAG_BIONIC
Alex Ray0a346432012-11-14 17:25:28 -080074
75// Reserved for initialization.
76#define ATRACE_TAG_NOT_READY (1LL<<63)
77
78#define ATRACE_TAG_VALID_MASK ((ATRACE_TAG_LAST - 1) | ATRACE_TAG_LAST)
79
80#ifndef ATRACE_TAG
81#define ATRACE_TAG ATRACE_TAG_NEVER
82#elif ATRACE_TAG > ATRACE_TAG_VALID_MASK
83#error ATRACE_TAG must be defined to be one of the tags defined in cutils/trace.h
84#endif
85
Jamie Gennis2b68e062013-05-07 15:09:17 -070086#ifdef HAVE_ANDROID_OS
Alex Ray0a346432012-11-14 17:25:28 -080087/**
88 * Maximum size of a message that can be logged to the trace buffer.
89 * Note this message includes a tag, the pid, and the string given as the name.
90 * Names should be kept short to get the most use of the trace buffer.
91 */
92#define ATRACE_MESSAGE_LENGTH 1024
93
94/**
95 * Opens the trace file for writing and reads the property for initial tags.
96 * The atrace.tags.enableflags property sets the tags to trace.
97 * This function should not be explicitly called, the first call to any normal
98 * trace function will cause it to be run safely.
99 */
100void atrace_setup();
101
102/**
Alex Raye7bb7bc2012-11-20 01:39:09 -0800103 * If tracing is ready, set atrace_enabled_tags to the system property
104 * debug.atrace.tags.enableflags. Can be used as a sysprop change callback.
105 */
106void atrace_update_tags();
107
108/**
Jamie Gennis774f9292013-02-25 18:15:40 -0800109 * Set whether the process is debuggable. By default the process is not
110 * considered debuggable. If the process is not debuggable then application-
111 * level tracing is not allowed unless the ro.debuggable system property is
112 * set to '1'.
113 */
114void atrace_set_debuggable(bool debuggable);
115
116/**
Jamie Gennisb13ea452013-04-15 18:50:22 -0700117 * Set whether tracing is enabled for the current process. This is used to
118 * prevent tracing within the Zygote process.
119 */
120void atrace_set_tracing_enabled(bool enabled);
121
122/**
Alex Ray0a346432012-11-14 17:25:28 -0800123 * Flag indicating whether setup has been completed, initialized to 0.
124 * Nonzero indicates setup has completed.
125 * Note: This does NOT indicate whether or not setup was successful.
126 */
Jamie Gennisb13ea452013-04-15 18:50:22 -0700127extern volatile int32_t atrace_is_ready;
Alex Ray0a346432012-11-14 17:25:28 -0800128
129/**
130 * Set of ATRACE_TAG flags to trace for, initialized to ATRACE_TAG_NOT_READY.
131 * A value of zero indicates setup has failed.
132 * Any other nonzero value indicates setup has succeeded, and tracing is on.
133 */
134extern uint64_t atrace_enabled_tags;
135
136/**
137 * Handle to the kernel's trace buffer, initialized to -1.
138 * Any other value indicates setup has succeeded, and is a valid fd for tracing.
139 */
140extern int atrace_marker_fd;
141
142/**
143 * atrace_init readies the process for tracing by opening the trace_marker file.
144 * Calling any trace function causes this to be run, so calling it is optional.
145 * This can be explicitly run to avoid setup delay on first trace function.
146 */
147#define ATRACE_INIT() atrace_init()
148static inline void atrace_init()
149{
150 if (CC_UNLIKELY(!android_atomic_acquire_load(&atrace_is_ready))) {
151 atrace_setup();
152 }
153}
154
155/**
156 * Get the mask of all tags currently enabled.
157 * It can be used as a guard condition around more expensive trace calculations.
158 * Every trace function calls this, which ensures atrace_init is run.
159 */
160#define ATRACE_GET_ENABLED_TAGS() atrace_get_enabled_tags()
161static inline uint64_t atrace_get_enabled_tags()
162{
163 atrace_init();
164 return atrace_enabled_tags;
165}
166
167/**
168 * Test if a given tag is currently enabled.
169 * Returns nonzero if the tag is enabled, otherwise zero.
170 * It can be used as a guard condition around more expensive trace calculations.
171 */
172#define ATRACE_ENABLED() atrace_is_tag_enabled(ATRACE_TAG)
173static inline uint64_t atrace_is_tag_enabled(uint64_t tag)
174{
175 return atrace_get_enabled_tags() & tag;
176}
177
178/**
179 * Trace the beginning of a context. name is used to identify the context.
180 * This is often used to time function execution.
181 */
182#define ATRACE_BEGIN(name) atrace_begin(ATRACE_TAG, name)
183static inline void atrace_begin(uint64_t tag, const char* name)
184{
185 if (CC_UNLIKELY(atrace_is_tag_enabled(tag))) {
186 char buf[ATRACE_MESSAGE_LENGTH];
187 size_t len;
188
189 len = snprintf(buf, ATRACE_MESSAGE_LENGTH, "B|%d|%s", getpid(), name);
190 write(atrace_marker_fd, buf, len);
191 }
192}
193
194/**
195 * Trace the end of a context.
196 * This should match up (and occur after) a corresponding ATRACE_BEGIN.
197 */
198#define ATRACE_END() atrace_end(ATRACE_TAG)
199static inline void atrace_end(uint64_t tag)
200{
201 if (CC_UNLIKELY(atrace_is_tag_enabled(tag))) {
202 char c = 'E';
203 write(atrace_marker_fd, &c, 1);
204 }
205}
206
207/**
Alex Ray66317772013-03-17 22:39:01 -0700208 * Trace the beginning of an asynchronous event. Unlike ATRACE_BEGIN/ATRACE_END
209 * contexts, asynchronous events do not need to be nested. The name describes
210 * the event, and the cookie provides a unique identifier for distinguishing
211 * simultaneous events. The name and cookie used to begin an event must be
212 * used to end it.
213 */
214#define ATRACE_ASYNC_BEGIN(name, cookie) \
215 atrace_async_begin(ATRACE_TAG, name, cookie)
216static inline void atrace_async_begin(uint64_t tag, const char* name,
217 int32_t cookie)
218{
219 if (CC_UNLIKELY(atrace_is_tag_enabled(tag))) {
220 char buf[ATRACE_MESSAGE_LENGTH];
221 size_t len;
222
Mark Salyzyna459d0b2014-03-20 13:02:57 -0700223 len = snprintf(buf, ATRACE_MESSAGE_LENGTH, "S|%d|%s|%" PRId32,
224 getpid(), name, cookie);
Alex Ray66317772013-03-17 22:39:01 -0700225 write(atrace_marker_fd, buf, len);
226 }
227}
228
229/**
230 * Trace the end of an asynchronous event.
231 * This should have a corresponding ATRACE_ASYNC_BEGIN.
232 */
233#define ATRACE_ASYNC_END(name, cookie) atrace_async_end(ATRACE_TAG, name, cookie)
234static inline void atrace_async_end(uint64_t tag, const char* name,
235 int32_t cookie)
236{
237 if (CC_UNLIKELY(atrace_is_tag_enabled(tag))) {
238 char buf[ATRACE_MESSAGE_LENGTH];
239 size_t len;
240
Mark Salyzyna459d0b2014-03-20 13:02:57 -0700241 len = snprintf(buf, ATRACE_MESSAGE_LENGTH, "F|%d|%s|%" PRId32,
242 getpid(), name, cookie);
Alex Ray66317772013-03-17 22:39:01 -0700243 write(atrace_marker_fd, buf, len);
244 }
245}
246
247
248/**
Alex Ray0a346432012-11-14 17:25:28 -0800249 * Traces an integer counter value. name is used to identify the counter.
250 * This can be used to track how a value changes over time.
251 */
252#define ATRACE_INT(name, value) atrace_int(ATRACE_TAG, name, value)
253static inline void atrace_int(uint64_t tag, const char* name, int32_t value)
254{
255 if (CC_UNLIKELY(atrace_is_tag_enabled(tag))) {
256 char buf[ATRACE_MESSAGE_LENGTH];
257 size_t len;
258
Mark Salyzyna459d0b2014-03-20 13:02:57 -0700259 len = snprintf(buf, ATRACE_MESSAGE_LENGTH, "C|%d|%s|%" PRId32,
Alex Ray0a346432012-11-14 17:25:28 -0800260 getpid(), name, value);
261 write(atrace_marker_fd, buf, len);
262 }
263}
264
Jamie Gennisf1921c72013-09-18 12:01:18 -0700265/**
266 * Traces a 64-bit integer counter value. name is used to identify the
267 * counter. This can be used to track how a value changes over time.
268 */
269#define ATRACE_INT64(name, value) atrace_int64(ATRACE_TAG, name, value)
270static inline void atrace_int64(uint64_t tag, const char* name, int64_t value)
271{
272 if (CC_UNLIKELY(atrace_is_tag_enabled(tag))) {
273 char buf[ATRACE_MESSAGE_LENGTH];
274 size_t len;
275
Mark Salyzyna459d0b2014-03-20 13:02:57 -0700276 len = snprintf(buf, ATRACE_MESSAGE_LENGTH, "C|%d|%s|%" PRId64,
Jamie Gennisf1921c72013-09-18 12:01:18 -0700277 getpid(), name, value);
278 write(atrace_marker_fd, buf, len);
279 }
280}
281
Jamie Gennis2b68e062013-05-07 15:09:17 -0700282#else // not HAVE_ANDROID_OS
283
284#define ATRACE_INIT()
285#define ATRACE_GET_ENABLED_TAGS()
Tobias Grosser9ede3322013-06-17 18:35:46 -0700286#define ATRACE_ENABLED() 0
Jamie Gennis2b68e062013-05-07 15:09:17 -0700287#define ATRACE_BEGIN(name)
288#define ATRACE_END()
289#define ATRACE_ASYNC_BEGIN(name, cookie)
290#define ATRACE_ASYNC_END(name, cookie)
291#define ATRACE_INT(name, value)
292
293#endif // not HAVE_ANDROID_OS
294
Alex Ray0a346432012-11-14 17:25:28 -0800295__END_DECLS
296
297#endif // _LIBS_CUTILS_TRACE_H