blob: 41bce009597e31eac3fc91f8b48efcd800391ca3 [file] [log] [blame]
Jamie Gennis2ccfe1a2012-02-23 11:28: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 ANDROID_TRACE_H
18#define ANDROID_TRACE_H
19
20#include <fcntl.h>
21#include <stdint.h>
22#include <stdio.h>
23#include <string.h>
24#include <sys/stat.h>
25#include <sys/types.h>
26#include <unistd.h>
27
28#include <cutils/compiler.h>
29#include <utils/threads.h>
30
31// The ATRACE_TAG macro can be defined before including this header to trace
32// using one of the tags defined below. It must be defined to one of the
33// following ATRACE_TAG_* macros. The trace tag is used to filter tracing in
34// userland to avoid some of the runtime cost of tracing when it is not desired.
35//
36// Defining ATRACE_TAG to be ATRACE_TAG_ALWAYS will result in the tracing always
37// being enabled - this should ONLY be done for debug code, as userland tracing
38// has a performance cost even when the trace is not being recorded. Defining
39// ATRACE_TAG to be ATRACE_TAG_NEVER or leaving ATRACE_TAG undefined will result
40// in the tracing always being disabled.
Jeff Brown59322a32012-03-09 14:46:01 -080041//
42// These tags must be kept in sync with frameworks/base/core/java/android/os/Trace.java.
Dianne Hackborn265743a2012-04-26 14:15:10 -070043#define ATRACE_TAG_NEVER 0 // The "never" tag is never enabled.
44#define ATRACE_TAG_ALWAYS (1<<0) // The "always" tag is always enabled.
45#define ATRACE_TAG_GRAPHICS (1<<1)
46#define ATRACE_TAG_INPUT (1<<2)
47#define ATRACE_TAG_VIEW (1<<3)
48#define ATRACE_TAG_WEBVIEW (1<<4)
49#define ATRACE_TAG_WINDOW_MANAGER (1<<5)
50#define ATRACE_TAG_ACTIVITY_MANAGER (1<<6)
Andy Stadler90dd9e12012-05-03 15:04:37 -070051#define ATRACE_TAG_SYNC_MANAGER (1<<7)
Glenn Kasten7e6e7e02012-05-07 11:08:31 -070052#define ATRACE_TAG_AUDIO (1<<8)
Jamie Gennis69fc3d42012-05-11 04:42:59 -070053#define ATRACE_TAG_VIDEO (1<<9)
Eino-Ville Talvala970701f2012-05-31 15:49:43 -070054#define ATRACE_TAG_CAMERA (1<<10)
55#define ATRACE_TAG_LAST ATRACE_TAG_CAMERA
Jamie Gennis2ccfe1a2012-02-23 11:28:28 -080056
Andy McFadden45ad8f42012-10-29 16:49:44 -070057#define ATRACE_TAG_NOT_READY (1LL<<63) // Reserved for use during init
58
Jeff Brown59322a32012-03-09 14:46:01 -080059#define ATRACE_TAG_VALID_MASK ((ATRACE_TAG_LAST - 1) | ATRACE_TAG_LAST)
Jamie Gennis2ccfe1a2012-02-23 11:28:28 -080060
61#ifndef ATRACE_TAG
62#define ATRACE_TAG ATRACE_TAG_NEVER
63#elif ATRACE_TAG > ATRACE_TAG_LAST
64#error ATRACE_TAG must be defined to be one of the tags defined in utils/Trace.h
65#endif
66
67// ATRACE_CALL traces the beginning and end of the current function. To trace
68// the correct start and end times this macro should be the first line of the
69// function body.
70#define ATRACE_CALL() android::ScopedTrace ___tracer(ATRACE_TAG, __FUNCTION__)
71
Romain Guy3fc49ad2012-10-18 16:16:10 -070072// ATRACE_NAME traces the beginning and end of the current function. To trace
73// the correct start and end times this macro should be the first line of the
74// function body.
75#define ATRACE_NAME(name) android::ScopedTrace ___tracer(ATRACE_TAG, name)
76
Jamie Gennis2ccfe1a2012-02-23 11:28:28 -080077// ATRACE_INT traces a named integer value. This can be used to track how the
78// value changes over time in a trace.
79#define ATRACE_INT(name, value) android::Tracer::traceCounter(ATRACE_TAG, name, value)
80
Jeff Brown59322a32012-03-09 14:46:01 -080081// ATRACE_ENABLED returns true if the trace tag is enabled. It can be used as a
82// guard condition around more expensive trace calculations.
83#define ATRACE_ENABLED() android::Tracer::isTagEnabled(ATRACE_TAG)
84
Jamie Gennis2ccfe1a2012-02-23 11:28:28 -080085namespace android {
86
87class Tracer {
88
89public:
90
Jeff Brown59322a32012-03-09 14:46:01 -080091 static uint64_t getEnabledTags() {
92 initIfNeeded();
93 return sEnabledTags;
94 }
95
96 static inline bool isTagEnabled(uint64_t tag) {
97 initIfNeeded();
98 return sEnabledTags & tag;
99 }
100
Jamie Gennis2ccfe1a2012-02-23 11:28:28 -0800101 static inline void traceCounter(uint64_t tag, const char* name,
102 int32_t value) {
Jeff Brown59322a32012-03-09 14:46:01 -0800103 if (CC_UNLIKELY(isTagEnabled(tag))) {
Jamie Gennis2ccfe1a2012-02-23 11:28:28 -0800104 char buf[1024];
105 snprintf(buf, 1024, "C|%d|%s|%d", getpid(), name, value);
Jeff Brown59322a32012-03-09 14:46:01 -0800106 write(sTraceFD, buf, strlen(buf));
Jamie Gennis2ccfe1a2012-02-23 11:28:28 -0800107 }
108 }
109
110 static inline void traceBegin(uint64_t tag, const char* name) {
Jeff Brown59322a32012-03-09 14:46:01 -0800111 if (CC_UNLIKELY(isTagEnabled(tag))) {
Jamie Gennis2ccfe1a2012-02-23 11:28:28 -0800112 char buf[1024];
113 size_t len = snprintf(buf, 1024, "B|%d|%s", getpid(), name);
Jeff Brown59322a32012-03-09 14:46:01 -0800114 write(sTraceFD, buf, len);
Jamie Gennis2ccfe1a2012-02-23 11:28:28 -0800115 }
116 }
117
118 static inline void traceEnd(uint64_t tag) {
Jeff Brown59322a32012-03-09 14:46:01 -0800119 if (CC_UNLIKELY(isTagEnabled(tag))) {
Jamie Gennis2ccfe1a2012-02-23 11:28:28 -0800120 char buf = 'E';
Jeff Brown59322a32012-03-09 14:46:01 -0800121 write(sTraceFD, &buf, 1);
Jamie Gennis2ccfe1a2012-02-23 11:28:28 -0800122 }
123 }
124
125private:
126
Jeff Brown59322a32012-03-09 14:46:01 -0800127 static inline void initIfNeeded() {
128 if (!android_atomic_acquire_load(&sIsReady)) {
129 init();
130 }
Jamie Gennis2ccfe1a2012-02-23 11:28:28 -0800131 }
132
Dianne Hackbornc1309d72012-05-08 18:54:22 -0700133 static void changeCallback();
134
Jamie Gennis2ccfe1a2012-02-23 11:28:28 -0800135 // init opens the trace marker file for writing and reads the
136 // atrace.tags.enableflags system property. It does this only the first
137 // time it is run, using sMutex for synchronization.
138 static void init();
139
Dianne Hackbornc1309d72012-05-08 18:54:22 -0700140 // retrieve the current value of the system property.
141 static void loadSystemProperty();
142
Jamie Gennis2ccfe1a2012-02-23 11:28:28 -0800143 // sIsReady is a boolean value indicating whether a call to init() has
144 // completed in this process. It is initialized to 0 and set to 1 when the
145 // first init() call completes. It is set to 1 even if a failure occurred
146 // in init (e.g. the trace marker file couldn't be opened).
147 //
148 // This should be checked by all tracing functions using an atomic acquire
149 // load operation before calling init(). This check avoids the need to lock
150 // a mutex each time a trace function gets called.
151 static volatile int32_t sIsReady;
152
153 // sTraceFD is the file descriptor used to write to the kernel's trace
154 // buffer. It is initialized to -1 and set to an open file descriptor in
155 // init() while a lock on sMutex is held.
156 //
157 // This should only be used by a trace function after init() has
158 // successfully completed.
159 static int sTraceFD;
160
161 // sEnabledTags is the set of tag bits for which tracing is currently
162 // enabled. It is initialized to 0 and set based on the
163 // atrace.tags.enableflags system property in init() while a lock on sMutex
164 // is held.
165 //
166 // This should only be used by a trace function after init() has
167 // successfully completed.
Jeff Brown59322a32012-03-09 14:46:01 -0800168 //
169 // This value is only ever non-zero when tracing is initialized and sTraceFD is not -1.
Jamie Gennis2ccfe1a2012-02-23 11:28:28 -0800170 static uint64_t sEnabledTags;
171
172 // sMutex is used to protect the execution of init().
173 static Mutex sMutex;
174};
175
176class ScopedTrace {
177
178public:
179 inline ScopedTrace(uint64_t tag, const char* name) :
180 mTag(tag) {
181 Tracer::traceBegin(mTag, name);
182 }
183
184 inline ~ScopedTrace() {
185 Tracer::traceEnd(mTag);
186 }
187
188private:
189
190 uint64_t mTag;
191};
192
193}; // namespace android
194
195#endif // ANDROID_TRACE_H