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