blob: dbd5e25fd4daeabc98a760c5600c3d946b359948 [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)
71#define ATRACE_TAG_LAST ATRACE_TAG_RS
Alex Ray0a346432012-11-14 17:25:28 -080072
73// Reserved for initialization.
74#define ATRACE_TAG_NOT_READY (1LL<<63)
75
76#define ATRACE_TAG_VALID_MASK ((ATRACE_TAG_LAST - 1) | ATRACE_TAG_LAST)
77
78#ifndef ATRACE_TAG
79#define ATRACE_TAG ATRACE_TAG_NEVER
80#elif ATRACE_TAG > ATRACE_TAG_VALID_MASK
81#error ATRACE_TAG must be defined to be one of the tags defined in cutils/trace.h
82#endif
83
Jamie Gennis2b68e062013-05-07 15:09:17 -070084#ifdef HAVE_ANDROID_OS
Alex Ray0a346432012-11-14 17:25:28 -080085/**
86 * Maximum size of a message that can be logged to the trace buffer.
87 * Note this message includes a tag, the pid, and the string given as the name.
88 * Names should be kept short to get the most use of the trace buffer.
89 */
90#define ATRACE_MESSAGE_LENGTH 1024
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 */
98void atrace_setup();
99
100/**
Alex Raye7bb7bc2012-11-20 01:39:09 -0800101 * 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 */
104void atrace_update_tags();
105
106/**
Jamie Gennis774f9292013-02-25 18:15:40 -0800107 * 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 */
112void atrace_set_debuggable(bool debuggable);
113
114/**
Jamie Gennisb13ea452013-04-15 18:50:22 -0700115 * Set whether tracing is enabled for the current process. This is used to
116 * prevent tracing within the Zygote process.
117 */
118void atrace_set_tracing_enabled(bool enabled);
119
120/**
Alex Ray0a346432012-11-14 17:25:28 -0800121 * Flag indicating whether setup has been completed, initialized to 0.
122 * Nonzero indicates setup has completed.
123 * Note: This does NOT indicate whether or not setup was successful.
124 */
Jamie Gennisb13ea452013-04-15 18:50:22 -0700125extern volatile int32_t atrace_is_ready;
Alex Ray0a346432012-11-14 17:25:28 -0800126
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 */
132extern 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 */
138extern 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()
146static inline void atrace_init()
147{
148 if (CC_UNLIKELY(!android_atomic_acquire_load(&atrace_is_ready))) {
149 atrace_setup();
150 }
151}
152
153/**
154 * Get the mask of all tags currently enabled.
155 * It can be used as a guard condition around more expensive trace calculations.
156 * Every trace function calls this, which ensures atrace_init is run.
157 */
158#define ATRACE_GET_ENABLED_TAGS() atrace_get_enabled_tags()
159static inline uint64_t atrace_get_enabled_tags()
160{
161 atrace_init();
162 return atrace_enabled_tags;
163}
164
165/**
166 * Test if a given tag is currently enabled.
167 * Returns nonzero if the tag is enabled, otherwise zero.
168 * It can be used as a guard condition around more expensive trace calculations.
169 */
170#define ATRACE_ENABLED() atrace_is_tag_enabled(ATRACE_TAG)
171static inline uint64_t atrace_is_tag_enabled(uint64_t tag)
172{
173 return atrace_get_enabled_tags() & tag;
174}
175
176/**
177 * Trace the beginning of a context. name is used to identify the context.
178 * This is often used to time function execution.
179 */
180#define ATRACE_BEGIN(name) atrace_begin(ATRACE_TAG, name)
181static inline void atrace_begin(uint64_t tag, const char* name)
182{
183 if (CC_UNLIKELY(atrace_is_tag_enabled(tag))) {
184 char buf[ATRACE_MESSAGE_LENGTH];
185 size_t len;
186
187 len = snprintf(buf, ATRACE_MESSAGE_LENGTH, "B|%d|%s", getpid(), name);
188 write(atrace_marker_fd, buf, len);
189 }
190}
191
192/**
193 * Trace the end of a context.
194 * This should match up (and occur after) a corresponding ATRACE_BEGIN.
195 */
196#define ATRACE_END() atrace_end(ATRACE_TAG)
197static inline void atrace_end(uint64_t tag)
198{
199 if (CC_UNLIKELY(atrace_is_tag_enabled(tag))) {
200 char c = 'E';
201 write(atrace_marker_fd, &c, 1);
202 }
203}
204
205/**
Alex Ray66317772013-03-17 22:39:01 -0700206 * Trace the beginning of an asynchronous event. Unlike ATRACE_BEGIN/ATRACE_END
207 * contexts, asynchronous events do not need to be nested. The name describes
208 * the event, and the cookie provides a unique identifier for distinguishing
209 * simultaneous events. The name and cookie used to begin an event must be
210 * used to end it.
211 */
212#define ATRACE_ASYNC_BEGIN(name, cookie) \
213 atrace_async_begin(ATRACE_TAG, name, cookie)
214static inline void atrace_async_begin(uint64_t tag, const char* name,
215 int32_t cookie)
216{
217 if (CC_UNLIKELY(atrace_is_tag_enabled(tag))) {
218 char buf[ATRACE_MESSAGE_LENGTH];
219 size_t len;
220
Mark Salyzyna459d0b2014-03-20 13:02:57 -0700221 len = snprintf(buf, ATRACE_MESSAGE_LENGTH, "S|%d|%s|%" PRId32,
222 getpid(), name, cookie);
Alex Ray66317772013-03-17 22:39:01 -0700223 write(atrace_marker_fd, buf, len);
224 }
225}
226
227/**
228 * Trace the end of an asynchronous event.
229 * This should have a corresponding ATRACE_ASYNC_BEGIN.
230 */
231#define ATRACE_ASYNC_END(name, cookie) atrace_async_end(ATRACE_TAG, name, cookie)
232static inline void atrace_async_end(uint64_t tag, const char* name,
233 int32_t cookie)
234{
235 if (CC_UNLIKELY(atrace_is_tag_enabled(tag))) {
236 char buf[ATRACE_MESSAGE_LENGTH];
237 size_t len;
238
Mark Salyzyna459d0b2014-03-20 13:02:57 -0700239 len = snprintf(buf, ATRACE_MESSAGE_LENGTH, "F|%d|%s|%" PRId32,
240 getpid(), name, cookie);
Alex Ray66317772013-03-17 22:39:01 -0700241 write(atrace_marker_fd, buf, len);
242 }
243}
244
245
246/**
Alex Ray0a346432012-11-14 17:25:28 -0800247 * Traces an integer counter value. name is used to identify the counter.
248 * This can be used to track how a value changes over time.
249 */
250#define ATRACE_INT(name, value) atrace_int(ATRACE_TAG, name, value)
251static inline void atrace_int(uint64_t tag, const char* name, int32_t value)
252{
253 if (CC_UNLIKELY(atrace_is_tag_enabled(tag))) {
254 char buf[ATRACE_MESSAGE_LENGTH];
255 size_t len;
256
Mark Salyzyna459d0b2014-03-20 13:02:57 -0700257 len = snprintf(buf, ATRACE_MESSAGE_LENGTH, "C|%d|%s|%" PRId32,
Alex Ray0a346432012-11-14 17:25:28 -0800258 getpid(), name, value);
259 write(atrace_marker_fd, buf, len);
260 }
261}
262
Jamie Gennisf1921c72013-09-18 12:01:18 -0700263/**
264 * Traces a 64-bit integer counter value. name is used to identify the
265 * counter. This can be used to track how a value changes over time.
266 */
267#define ATRACE_INT64(name, value) atrace_int64(ATRACE_TAG, name, value)
268static inline void atrace_int64(uint64_t tag, const char* name, int64_t value)
269{
270 if (CC_UNLIKELY(atrace_is_tag_enabled(tag))) {
271 char buf[ATRACE_MESSAGE_LENGTH];
272 size_t len;
273
Mark Salyzyna459d0b2014-03-20 13:02:57 -0700274 len = snprintf(buf, ATRACE_MESSAGE_LENGTH, "C|%d|%s|%" PRId64,
Jamie Gennisf1921c72013-09-18 12:01:18 -0700275 getpid(), name, value);
276 write(atrace_marker_fd, buf, len);
277 }
278}
279
Jamie Gennis2b68e062013-05-07 15:09:17 -0700280#else // not HAVE_ANDROID_OS
281
282#define ATRACE_INIT()
283#define ATRACE_GET_ENABLED_TAGS()
Tobias Grosser9ede3322013-06-17 18:35:46 -0700284#define ATRACE_ENABLED() 0
Jamie Gennis2b68e062013-05-07 15:09:17 -0700285#define ATRACE_BEGIN(name)
286#define ATRACE_END()
287#define ATRACE_ASYNC_BEGIN(name, cookie)
288#define ATRACE_ASYNC_END(name, cookie)
289#define ATRACE_INT(name, value)
290
291#endif // not HAVE_ANDROID_OS
292
Alex Ray0a346432012-11-14 17:25:28 -0800293__END_DECLS
294
295#endif // _LIBS_CUTILS_TRACE_H