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