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