blob: 1db39038bc7e94f21855d7356ca14affa1fb95e1 [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>
23#include <unistd.h>
Alex Ray0a346432012-11-14 17:25:28 -080024#include <cutils/compiler.h>
25
Alex Ray448f76a2012-11-30 19:37:53 -080026#ifdef ANDROID_SMP
27#include <cutils/atomic-inline.h>
28#else
29#include <cutils/atomic.h>
30#endif
31
Alex Ray0a346432012-11-14 17:25:28 -080032__BEGIN_DECLS
33
34/**
35 * The ATRACE_TAG macro can be defined before including this header to trace
36 * using one of the tags defined below. It must be defined to one of the
37 * following ATRACE_TAG_* macros. The trace tag is used to filter tracing in
38 * userland to avoid some of the runtime cost of tracing when it is not desired.
39 *
40 * Defining ATRACE_TAG to be ATRACE_TAG_ALWAYS will result in the tracing always
41 * being enabled - this should ONLY be done for debug code, as userland tracing
42 * has a performance cost even when the trace is not being recorded. Defining
43 * ATRACE_TAG to be ATRACE_TAG_NEVER or leaving ATRACE_TAG undefined will result
44 * in the tracing always being disabled.
45 *
46 * ATRACE_TAG_HAL should be bitwise ORed with the relevant tags for tracing
47 * within a hardware module. For example a camera hardware module would set:
48 * #define ATRACE_TAG (ATRACE_TAG_CAMERA | ATRACE_TAG_HAL)
49 *
50 * Keep these in sync with frameworks/base/core/java/android/os/Trace.java.
51 */
52#define ATRACE_TAG_NEVER 0 // This tag is never enabled.
53#define ATRACE_TAG_ALWAYS (1<<0) // This tag is always enabled.
54#define ATRACE_TAG_GRAPHICS (1<<1)
55#define ATRACE_TAG_INPUT (1<<2)
56#define ATRACE_TAG_VIEW (1<<3)
57#define ATRACE_TAG_WEBVIEW (1<<4)
58#define ATRACE_TAG_WINDOW_MANAGER (1<<5)
59#define ATRACE_TAG_ACTIVITY_MANAGER (1<<6)
60#define ATRACE_TAG_SYNC_MANAGER (1<<7)
61#define ATRACE_TAG_AUDIO (1<<8)
62#define ATRACE_TAG_VIDEO (1<<9)
63#define ATRACE_TAG_CAMERA (1<<10)
64#define ATRACE_TAG_HAL (1<<11)
65#define ATRACE_TAG_LAST ATRACE_TAG_HAL
66
67// Reserved for initialization.
68#define ATRACE_TAG_NOT_READY (1LL<<63)
69
70#define ATRACE_TAG_VALID_MASK ((ATRACE_TAG_LAST - 1) | ATRACE_TAG_LAST)
71
72#ifndef ATRACE_TAG
73#define ATRACE_TAG ATRACE_TAG_NEVER
74#elif ATRACE_TAG > ATRACE_TAG_VALID_MASK
75#error ATRACE_TAG must be defined to be one of the tags defined in cutils/trace.h
76#endif
77
78/**
79 * Maximum size of a message that can be logged to the trace buffer.
80 * Note this message includes a tag, the pid, and the string given as the name.
81 * Names should be kept short to get the most use of the trace buffer.
82 */
83#define ATRACE_MESSAGE_LENGTH 1024
84
85/**
86 * 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/**
Alex Ray0a346432012-11-14 17:25:28 -0800100 * Flag indicating whether setup has been completed, initialized to 0.
101 * Nonzero indicates setup has completed.
102 * Note: This does NOT indicate whether or not setup was successful.
103 */
104extern int32_t atrace_is_ready;
105
106/**
107 * Set of ATRACE_TAG flags to trace for, initialized to ATRACE_TAG_NOT_READY.
108 * A value of zero indicates setup has failed.
109 * Any other nonzero value indicates setup has succeeded, and tracing is on.
110 */
111extern uint64_t atrace_enabled_tags;
112
113/**
114 * Handle to the kernel's trace buffer, initialized to -1.
115 * Any other value indicates setup has succeeded, and is a valid fd for tracing.
116 */
117extern int atrace_marker_fd;
118
119/**
120 * atrace_init readies the process for tracing by opening the trace_marker file.
121 * Calling any trace function causes this to be run, so calling it is optional.
122 * This can be explicitly run to avoid setup delay on first trace function.
123 */
124#define ATRACE_INIT() atrace_init()
125static inline void atrace_init()
126{
127 if (CC_UNLIKELY(!android_atomic_acquire_load(&atrace_is_ready))) {
128 atrace_setup();
129 }
130}
131
132/**
133 * Get the mask of all tags currently enabled.
134 * It can be used as a guard condition around more expensive trace calculations.
135 * Every trace function calls this, which ensures atrace_init is run.
136 */
137#define ATRACE_GET_ENABLED_TAGS() atrace_get_enabled_tags()
138static inline uint64_t atrace_get_enabled_tags()
139{
140 atrace_init();
141 return atrace_enabled_tags;
142}
143
144/**
145 * Test if a given tag is currently enabled.
146 * Returns nonzero if the tag is enabled, otherwise zero.
147 * It can be used as a guard condition around more expensive trace calculations.
148 */
149#define ATRACE_ENABLED() atrace_is_tag_enabled(ATRACE_TAG)
150static inline uint64_t atrace_is_tag_enabled(uint64_t tag)
151{
152 return atrace_get_enabled_tags() & tag;
153}
154
155/**
156 * Trace the beginning of a context. name is used to identify the context.
157 * This is often used to time function execution.
158 */
159#define ATRACE_BEGIN(name) atrace_begin(ATRACE_TAG, name)
160static inline void atrace_begin(uint64_t tag, const char* name)
161{
162 if (CC_UNLIKELY(atrace_is_tag_enabled(tag))) {
163 char buf[ATRACE_MESSAGE_LENGTH];
164 size_t len;
165
166 len = snprintf(buf, ATRACE_MESSAGE_LENGTH, "B|%d|%s", getpid(), name);
167 write(atrace_marker_fd, buf, len);
168 }
169}
170
171/**
172 * Trace the end of a context.
173 * This should match up (and occur after) a corresponding ATRACE_BEGIN.
174 */
175#define ATRACE_END() atrace_end(ATRACE_TAG)
176static inline void atrace_end(uint64_t tag)
177{
178 if (CC_UNLIKELY(atrace_is_tag_enabled(tag))) {
179 char c = 'E';
180 write(atrace_marker_fd, &c, 1);
181 }
182}
183
184/**
185 * Traces an integer counter value. name is used to identify the counter.
186 * This can be used to track how a value changes over time.
187 */
188#define ATRACE_INT(name, value) atrace_int(ATRACE_TAG, name, value)
189static inline void atrace_int(uint64_t tag, const char* name, int32_t value)
190{
191 if (CC_UNLIKELY(atrace_is_tag_enabled(tag))) {
192 char buf[ATRACE_MESSAGE_LENGTH];
193 size_t len;
194
195 len = snprintf(buf, ATRACE_MESSAGE_LENGTH, "C|%d|%s|%d",
196 getpid(), name, value);
197 write(atrace_marker_fd, buf, len);
198 }
199}
200
201__END_DECLS
202
203#endif // _LIBS_CUTILS_TRACE_H