blob: 38696625090f7fdb846ea3a4522f9bed2148d572 [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/**
Alex Raye7bb7bc2012-11-20 01:39:09 -080089 * If tracing is ready, set atrace_enabled_tags to the system property
90 * debug.atrace.tags.enableflags. Can be used as a sysprop change callback.
91 */
92void atrace_update_tags();
93
94/**
Alex Ray0a346432012-11-14 17:25:28 -080095 * Flag indicating whether setup has been completed, initialized to 0.
96 * Nonzero indicates setup has completed.
97 * Note: This does NOT indicate whether or not setup was successful.
98 */
99extern int32_t atrace_is_ready;
100
101/**
102 * Set of ATRACE_TAG flags to trace for, initialized to ATRACE_TAG_NOT_READY.
103 * A value of zero indicates setup has failed.
104 * Any other nonzero value indicates setup has succeeded, and tracing is on.
105 */
106extern uint64_t atrace_enabled_tags;
107
108/**
109 * Handle to the kernel's trace buffer, initialized to -1.
110 * Any other value indicates setup has succeeded, and is a valid fd for tracing.
111 */
112extern int atrace_marker_fd;
113
114/**
115 * atrace_init readies the process for tracing by opening the trace_marker file.
116 * Calling any trace function causes this to be run, so calling it is optional.
117 * This can be explicitly run to avoid setup delay on first trace function.
118 */
119#define ATRACE_INIT() atrace_init()
120static inline void atrace_init()
121{
122 if (CC_UNLIKELY(!android_atomic_acquire_load(&atrace_is_ready))) {
123 atrace_setup();
124 }
125}
126
127/**
128 * Get the mask of all tags currently enabled.
129 * It can be used as a guard condition around more expensive trace calculations.
130 * Every trace function calls this, which ensures atrace_init is run.
131 */
132#define ATRACE_GET_ENABLED_TAGS() atrace_get_enabled_tags()
133static inline uint64_t atrace_get_enabled_tags()
134{
135 atrace_init();
136 return atrace_enabled_tags;
137}
138
139/**
140 * Test if a given tag is currently enabled.
141 * Returns nonzero if the tag is enabled, otherwise zero.
142 * It can be used as a guard condition around more expensive trace calculations.
143 */
144#define ATRACE_ENABLED() atrace_is_tag_enabled(ATRACE_TAG)
145static inline uint64_t atrace_is_tag_enabled(uint64_t tag)
146{
147 return atrace_get_enabled_tags() & tag;
148}
149
150/**
151 * Trace the beginning of a context. name is used to identify the context.
152 * This is often used to time function execution.
153 */
154#define ATRACE_BEGIN(name) atrace_begin(ATRACE_TAG, name)
155static inline void atrace_begin(uint64_t tag, const char* name)
156{
157 if (CC_UNLIKELY(atrace_is_tag_enabled(tag))) {
158 char buf[ATRACE_MESSAGE_LENGTH];
159 size_t len;
160
161 len = snprintf(buf, ATRACE_MESSAGE_LENGTH, "B|%d|%s", getpid(), name);
162 write(atrace_marker_fd, buf, len);
163 }
164}
165
166/**
167 * Trace the end of a context.
168 * This should match up (and occur after) a corresponding ATRACE_BEGIN.
169 */
170#define ATRACE_END() atrace_end(ATRACE_TAG)
171static inline void atrace_end(uint64_t tag)
172{
173 if (CC_UNLIKELY(atrace_is_tag_enabled(tag))) {
174 char c = 'E';
175 write(atrace_marker_fd, &c, 1);
176 }
177}
178
179/**
180 * Traces an integer counter value. name is used to identify the counter.
181 * This can be used to track how a value changes over time.
182 */
183#define ATRACE_INT(name, value) atrace_int(ATRACE_TAG, name, value)
184static inline void atrace_int(uint64_t tag, const char* name, int32_t value)
185{
186 if (CC_UNLIKELY(atrace_is_tag_enabled(tag))) {
187 char buf[ATRACE_MESSAGE_LENGTH];
188 size_t len;
189
190 len = snprintf(buf, ATRACE_MESSAGE_LENGTH, "C|%d|%s|%d",
191 getpid(), name, value);
192 write(atrace_marker_fd, buf, len);
193 }
194}
195
196__END_DECLS
197
198#endif // _LIBS_CUTILS_TRACE_H