blob: 27296f370f6c7e17dc2e43af9527f0a4483e887e [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>
Yabin Cuia8ac32c2015-04-15 14:50:27 -070021#include <stdatomic.h>
Mark Salyzyna459d0b2014-03-20 13:02:57 -070022#include <stdbool.h>
23#include <stdint.h>
Mark Salyzyn48878422014-05-22 16:08:52 -070024#include <stdio.h>
Alex Ray0a346432012-11-14 17:25:28 -080025#include <sys/cdefs.h>
26#include <sys/types.h>
Alex Ray0a346432012-11-14 17:25:28 -080027#include <unistd.h>
Alex Ray0a346432012-11-14 17:25:28 -080028
Mark Salyzyna459d0b2014-03-20 13:02:57 -070029#include <cutils/compiler.h>
Alex Ray448f76a2012-11-30 19:37:53 -080030
Alex Ray0a346432012-11-14 17:25:28 -080031__BEGIN_DECLS
32
33/**
34 * The ATRACE_TAG macro can be defined before including this header to trace
35 * using one of the tags defined below. It must be defined to one of the
36 * following ATRACE_TAG_* macros. The trace tag is used to filter tracing in
37 * userland to avoid some of the runtime cost of tracing when it is not desired.
38 *
39 * Defining ATRACE_TAG to be ATRACE_TAG_ALWAYS will result in the tracing always
40 * being enabled - this should ONLY be done for debug code, as userland tracing
41 * has a performance cost even when the trace is not being recorded. Defining
42 * ATRACE_TAG to be ATRACE_TAG_NEVER or leaving ATRACE_TAG undefined will result
43 * in the tracing always being disabled.
44 *
45 * ATRACE_TAG_HAL should be bitwise ORed with the relevant tags for tracing
46 * within a hardware module. For example a camera hardware module would set:
47 * #define ATRACE_TAG (ATRACE_TAG_CAMERA | ATRACE_TAG_HAL)
48 *
49 * Keep these in sync with frameworks/base/core/java/android/os/Trace.java.
50 */
51#define ATRACE_TAG_NEVER 0 // This tag is never enabled.
52#define ATRACE_TAG_ALWAYS (1<<0) // This tag is always enabled.
53#define ATRACE_TAG_GRAPHICS (1<<1)
54#define ATRACE_TAG_INPUT (1<<2)
55#define ATRACE_TAG_VIEW (1<<3)
56#define ATRACE_TAG_WEBVIEW (1<<4)
57#define ATRACE_TAG_WINDOW_MANAGER (1<<5)
58#define ATRACE_TAG_ACTIVITY_MANAGER (1<<6)
59#define ATRACE_TAG_SYNC_MANAGER (1<<7)
60#define ATRACE_TAG_AUDIO (1<<8)
61#define ATRACE_TAG_VIDEO (1<<9)
62#define ATRACE_TAG_CAMERA (1<<10)
63#define ATRACE_TAG_HAL (1<<11)
Jamie Gennis774f9292013-02-25 18:15:40 -080064#define ATRACE_TAG_APP (1<<12)
Dianne Hackborn24bc41b2013-04-12 14:51:43 -070065#define ATRACE_TAG_RESOURCES (1<<13)
Jamie Gennis2b68e062013-05-07 15:09:17 -070066#define ATRACE_TAG_DALVIK (1<<14)
Tim Murrayd8b11c12013-05-23 13:46:12 -070067#define ATRACE_TAG_RS (1<<15)
Brigid Smith8c82b352014-05-27 12:37:48 -070068#define ATRACE_TAG_BIONIC (1<<16)
Jeff Brownde4d9212014-08-14 19:23:30 -070069#define ATRACE_TAG_POWER (1<<17)
Todd Kennedyda106ad2015-08-03 09:58:01 -070070#define ATRACE_TAG_PACKAGE_MANAGER (1<<18)
Yasuhiro Matsuda715f4632015-07-01 01:54:49 +090071#define ATRACE_TAG_SYSTEM_SERVER (1<<19)
72#define ATRACE_TAG_LAST ATRACE_TAG_SYSTEM_SERVER
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
85/**
Alex Ray0a346432012-11-14 17:25:28 -080086 * Opens the trace file for writing and reads the property for initial tags.
87 * The atrace.tags.enableflags property sets the tags to trace.
88 * This function should not be explicitly called, the first call to any normal
89 * trace function will cause it to be run safely.
90 */
91void atrace_setup();
92
93/**
Alex Raye7bb7bc2012-11-20 01:39:09 -080094 * If tracing is ready, set atrace_enabled_tags to the system property
95 * debug.atrace.tags.enableflags. Can be used as a sysprop change callback.
96 */
97void atrace_update_tags();
98
99/**
Jamie Gennis774f9292013-02-25 18:15:40 -0800100 * Set whether the process is debuggable. By default the process is not
101 * considered debuggable. If the process is not debuggable then application-
102 * level tracing is not allowed unless the ro.debuggable system property is
103 * set to '1'.
104 */
105void atrace_set_debuggable(bool debuggable);
106
107/**
Jamie Gennisb13ea452013-04-15 18:50:22 -0700108 * Set whether tracing is enabled for the current process. This is used to
109 * prevent tracing within the Zygote process.
110 */
111void atrace_set_tracing_enabled(bool enabled);
112
113/**
Alex Ray0a346432012-11-14 17:25:28 -0800114 * Flag indicating whether setup has been completed, initialized to 0.
115 * Nonzero indicates setup has completed.
116 * Note: This does NOT indicate whether or not setup was successful.
117 */
Yabin Cuia8ac32c2015-04-15 14:50:27 -0700118extern atomic_bool atrace_is_ready;
Alex Ray0a346432012-11-14 17:25:28 -0800119
120/**
121 * Set of ATRACE_TAG flags to trace for, initialized to ATRACE_TAG_NOT_READY.
122 * A value of zero indicates setup has failed.
123 * Any other nonzero value indicates setup has succeeded, and tracing is on.
124 */
125extern uint64_t atrace_enabled_tags;
126
127/**
128 * Handle to the kernel's trace buffer, initialized to -1.
129 * Any other value indicates setup has succeeded, and is a valid fd for tracing.
130 */
131extern int atrace_marker_fd;
132
133/**
134 * atrace_init readies the process for tracing by opening the trace_marker file.
135 * Calling any trace function causes this to be run, so calling it is optional.
136 * This can be explicitly run to avoid setup delay on first trace function.
137 */
138#define ATRACE_INIT() atrace_init()
139static inline void atrace_init()
140{
Yabin Cuia8ac32c2015-04-15 14:50:27 -0700141 if (CC_UNLIKELY(!atomic_load_explicit(&atrace_is_ready, memory_order_acquire))) {
Alex Ray0a346432012-11-14 17:25:28 -0800142 atrace_setup();
143 }
144}
145
146/**
147 * Get the mask of all tags currently enabled.
148 * It can be used as a guard condition around more expensive trace calculations.
149 * Every trace function calls this, which ensures atrace_init is run.
150 */
151#define ATRACE_GET_ENABLED_TAGS() atrace_get_enabled_tags()
152static inline uint64_t atrace_get_enabled_tags()
153{
154 atrace_init();
155 return atrace_enabled_tags;
156}
157
158/**
159 * Test if a given tag is currently enabled.
160 * Returns nonzero if the tag is enabled, otherwise zero.
161 * It can be used as a guard condition around more expensive trace calculations.
162 */
163#define ATRACE_ENABLED() atrace_is_tag_enabled(ATRACE_TAG)
164static inline uint64_t atrace_is_tag_enabled(uint64_t tag)
165{
166 return atrace_get_enabled_tags() & tag;
167}
168
169/**
170 * Trace the beginning of a context. name is used to identify the context.
171 * This is often used to time function execution.
172 */
173#define ATRACE_BEGIN(name) atrace_begin(ATRACE_TAG, name)
174static inline void atrace_begin(uint64_t tag, const char* name)
175{
176 if (CC_UNLIKELY(atrace_is_tag_enabled(tag))) {
Chih-Hung Hsieh2d3150e2014-10-13 16:30:24 -0700177 void atrace_begin_body(const char*);
178 atrace_begin_body(name);
Alex Ray0a346432012-11-14 17:25:28 -0800179 }
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))) {
Chih-Hung Hsieh2d3150e2014-10-13 16:30:24 -0700208 void atrace_async_begin_body(const char*, int32_t);
209 atrace_async_begin_body(name, cookie);
Alex Ray66317772013-03-17 22:39:01 -0700210 }
211}
212
213/**
214 * Trace the end of an asynchronous event.
215 * This should have a corresponding ATRACE_ASYNC_BEGIN.
216 */
217#define ATRACE_ASYNC_END(name, cookie) atrace_async_end(ATRACE_TAG, name, cookie)
Chih-Hung Hsieh2d3150e2014-10-13 16:30:24 -0700218static inline void atrace_async_end(uint64_t tag, const char* name, int32_t cookie)
Alex Ray66317772013-03-17 22:39:01 -0700219{
220 if (CC_UNLIKELY(atrace_is_tag_enabled(tag))) {
Chih-Hung Hsieh2d3150e2014-10-13 16:30:24 -0700221 void atrace_async_end_body(const char*, int32_t);
222 atrace_async_end_body(name, cookie);
Alex Ray66317772013-03-17 22:39:01 -0700223 }
224}
225
Alex Ray66317772013-03-17 22:39:01 -0700226/**
Alex Ray0a346432012-11-14 17:25:28 -0800227 * Traces an integer counter value. name is used to identify the counter.
228 * This can be used to track how a value changes over time.
229 */
230#define ATRACE_INT(name, value) atrace_int(ATRACE_TAG, name, value)
231static inline void atrace_int(uint64_t tag, const char* name, int32_t value)
232{
233 if (CC_UNLIKELY(atrace_is_tag_enabled(tag))) {
Chih-Hung Hsieh2d3150e2014-10-13 16:30:24 -0700234 void atrace_int_body(const char*, int32_t);
235 atrace_int_body(name, value);
Alex Ray0a346432012-11-14 17:25:28 -0800236 }
237}
238
Jamie Gennisf1921c72013-09-18 12:01:18 -0700239/**
240 * Traces a 64-bit integer counter value. name is used to identify the
241 * counter. This can be used to track how a value changes over time.
242 */
243#define ATRACE_INT64(name, value) atrace_int64(ATRACE_TAG, name, value)
244static inline void atrace_int64(uint64_t tag, const char* name, int64_t value)
245{
246 if (CC_UNLIKELY(atrace_is_tag_enabled(tag))) {
Chih-Hung Hsieh2d3150e2014-10-13 16:30:24 -0700247 void atrace_int64_body(const char*, int64_t);
248 atrace_int64_body(name, value);
Jamie Gennisf1921c72013-09-18 12:01:18 -0700249 }
250}
251
Alex Ray0a346432012-11-14 17:25:28 -0800252__END_DECLS
253
254#endif // _LIBS_CUTILS_TRACE_H