blob: 15f8fb9f369d6d9735c2913571838348128e65d2 [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
20#include <sys/cdefs.h>
21#include <sys/types.h>
22#include <stdint.h>
Jamie Gennis774f9292013-02-25 18:15:40 -080023#include <stdbool.h>
Alex Ray0a346432012-11-14 17:25:28 -080024#include <unistd.h>
Alex Ray0a346432012-11-14 17:25:28 -080025#include <cutils/compiler.h>
26
Alex Ray448f76a2012-11-30 19:37:53 -080027#ifdef ANDROID_SMP
28#include <cutils/atomic-inline.h>
29#else
30#include <cutils/atomic.h>
31#endif
32
Alex Ray0a346432012-11-14 17:25:28 -080033__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)
Jamie Gennis774f9292013-02-25 18:15:40 -080066#define ATRACE_TAG_APP (1<<12)
Dianne Hackborn24bc41b2013-04-12 14:51:43 -070067#define ATRACE_TAG_RESOURCES (1<<13)
68#define ATRACE_TAG_LAST ATRACE_TAG_RESOURCES
Alex Ray0a346432012-11-14 17:25:28 -080069
70// Reserved for initialization.
71#define ATRACE_TAG_NOT_READY (1LL<<63)
72
73#define ATRACE_TAG_VALID_MASK ((ATRACE_TAG_LAST - 1) | ATRACE_TAG_LAST)
74
75#ifndef ATRACE_TAG
76#define ATRACE_TAG ATRACE_TAG_NEVER
77#elif ATRACE_TAG > ATRACE_TAG_VALID_MASK
78#error ATRACE_TAG must be defined to be one of the tags defined in cutils/trace.h
79#endif
80
81/**
82 * Maximum size of a message that can be logged to the trace buffer.
83 * Note this message includes a tag, the pid, and the string given as the name.
84 * Names should be kept short to get the most use of the trace buffer.
85 */
86#define ATRACE_MESSAGE_LENGTH 1024
87
88/**
89 * Opens the trace file for writing and reads the property for initial tags.
90 * The atrace.tags.enableflags property sets the tags to trace.
91 * This function should not be explicitly called, the first call to any normal
92 * trace function will cause it to be run safely.
93 */
94void atrace_setup();
95
96/**
Alex Raye7bb7bc2012-11-20 01:39:09 -080097 * If tracing is ready, set atrace_enabled_tags to the system property
98 * debug.atrace.tags.enableflags. Can be used as a sysprop change callback.
99 */
100void atrace_update_tags();
101
102/**
Jamie Gennis774f9292013-02-25 18:15:40 -0800103 * Set whether the process is debuggable. By default the process is not
104 * considered debuggable. If the process is not debuggable then application-
105 * level tracing is not allowed unless the ro.debuggable system property is
106 * set to '1'.
107 */
108void atrace_set_debuggable(bool debuggable);
109
110/**
Alex Ray0a346432012-11-14 17:25:28 -0800111 * Flag indicating whether setup has been completed, initialized to 0.
112 * Nonzero indicates setup has completed.
113 * Note: This does NOT indicate whether or not setup was successful.
114 */
115extern int32_t atrace_is_ready;
116
117/**
118 * Set of ATRACE_TAG flags to trace for, initialized to ATRACE_TAG_NOT_READY.
119 * A value of zero indicates setup has failed.
120 * Any other nonzero value indicates setup has succeeded, and tracing is on.
121 */
122extern uint64_t atrace_enabled_tags;
123
124/**
125 * Handle to the kernel's trace buffer, initialized to -1.
126 * Any other value indicates setup has succeeded, and is a valid fd for tracing.
127 */
128extern int atrace_marker_fd;
129
130/**
131 * atrace_init readies the process for tracing by opening the trace_marker file.
132 * Calling any trace function causes this to be run, so calling it is optional.
133 * This can be explicitly run to avoid setup delay on first trace function.
134 */
135#define ATRACE_INIT() atrace_init()
136static inline void atrace_init()
137{
138 if (CC_UNLIKELY(!android_atomic_acquire_load(&atrace_is_ready))) {
139 atrace_setup();
140 }
141}
142
143/**
144 * Get the mask of all tags currently enabled.
145 * It can be used as a guard condition around more expensive trace calculations.
146 * Every trace function calls this, which ensures atrace_init is run.
147 */
148#define ATRACE_GET_ENABLED_TAGS() atrace_get_enabled_tags()
149static inline uint64_t atrace_get_enabled_tags()
150{
151 atrace_init();
152 return atrace_enabled_tags;
153}
154
155/**
156 * Test if a given tag is currently enabled.
157 * Returns nonzero if the tag is enabled, otherwise zero.
158 * It can be used as a guard condition around more expensive trace calculations.
159 */
160#define ATRACE_ENABLED() atrace_is_tag_enabled(ATRACE_TAG)
161static inline uint64_t atrace_is_tag_enabled(uint64_t tag)
162{
163 return atrace_get_enabled_tags() & tag;
164}
165
166/**
167 * Trace the beginning of a context. name is used to identify the context.
168 * This is often used to time function execution.
169 */
170#define ATRACE_BEGIN(name) atrace_begin(ATRACE_TAG, name)
171static inline void atrace_begin(uint64_t tag, const char* name)
172{
173 if (CC_UNLIKELY(atrace_is_tag_enabled(tag))) {
174 char buf[ATRACE_MESSAGE_LENGTH];
175 size_t len;
176
177 len = snprintf(buf, ATRACE_MESSAGE_LENGTH, "B|%d|%s", getpid(), name);
178 write(atrace_marker_fd, buf, len);
179 }
180}
181
182/**
183 * Trace the end of a context.
184 * This should match up (and occur after) a corresponding ATRACE_BEGIN.
185 */
186#define ATRACE_END() atrace_end(ATRACE_TAG)
187static inline void atrace_end(uint64_t tag)
188{
189 if (CC_UNLIKELY(atrace_is_tag_enabled(tag))) {
190 char c = 'E';
191 write(atrace_marker_fd, &c, 1);
192 }
193}
194
195/**
Alex Ray66317772013-03-17 22:39:01 -0700196 * Trace the beginning of an asynchronous event. Unlike ATRACE_BEGIN/ATRACE_END
197 * contexts, asynchronous events do not need to be nested. The name describes
198 * the event, and the cookie provides a unique identifier for distinguishing
199 * simultaneous events. The name and cookie used to begin an event must be
200 * used to end it.
201 */
202#define ATRACE_ASYNC_BEGIN(name, cookie) \
203 atrace_async_begin(ATRACE_TAG, name, cookie)
204static inline void atrace_async_begin(uint64_t tag, const char* name,
205 int32_t cookie)
206{
207 if (CC_UNLIKELY(atrace_is_tag_enabled(tag))) {
208 char buf[ATRACE_MESSAGE_LENGTH];
209 size_t len;
210
211 len = snprintf(buf, ATRACE_MESSAGE_LENGTH, "S|%d|%s|%d", getpid(),
212 name, cookie);
213 write(atrace_marker_fd, buf, len);
214 }
215}
216
217/**
218 * Trace the end of an asynchronous event.
219 * This should have a corresponding ATRACE_ASYNC_BEGIN.
220 */
221#define ATRACE_ASYNC_END(name, cookie) atrace_async_end(ATRACE_TAG, name, cookie)
222static inline void atrace_async_end(uint64_t tag, const char* name,
223 int32_t cookie)
224{
225 if (CC_UNLIKELY(atrace_is_tag_enabled(tag))) {
226 char buf[ATRACE_MESSAGE_LENGTH];
227 size_t len;
228
229 len = snprintf(buf, ATRACE_MESSAGE_LENGTH, "F|%d|%s|%d", getpid(),
230 name, cookie);
231 write(atrace_marker_fd, buf, len);
232 }
233}
234
235
236/**
Alex Ray0a346432012-11-14 17:25:28 -0800237 * Traces an integer counter value. name is used to identify the counter.
238 * This can be used to track how a value changes over time.
239 */
240#define ATRACE_INT(name, value) atrace_int(ATRACE_TAG, name, value)
241static inline void atrace_int(uint64_t tag, const char* name, int32_t value)
242{
243 if (CC_UNLIKELY(atrace_is_tag_enabled(tag))) {
244 char buf[ATRACE_MESSAGE_LENGTH];
245 size_t len;
246
247 len = snprintf(buf, ATRACE_MESSAGE_LENGTH, "C|%d|%s|%d",
248 getpid(), name, value);
249 write(atrace_marker_fd, buf, len);
250 }
251}
252
253__END_DECLS
254
255#endif // _LIBS_CUTILS_TRACE_H