widget/gonk/libui/cutils_trace.h

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /*
michael@0 2 * Copyright (C) 2012 The Android Open Source Project
michael@0 3 *
michael@0 4 * Licensed under the Apache License, Version 2.0 (the "License");
michael@0 5 * you may not use this file except in compliance with the License.
michael@0 6 * You may obtain a copy of the License at
michael@0 7 *
michael@0 8 * http://www.apache.org/licenses/LICENSE-2.0
michael@0 9 *
michael@0 10 * Unless required by applicable law or agreed to in writing, software
michael@0 11 * distributed under the License is distributed on an "AS IS" BASIS,
michael@0 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
michael@0 13 * See the License for the specific language governing permissions and
michael@0 14 * limitations under the License.
michael@0 15 */
michael@0 16
michael@0 17 #ifndef _LIBS_CUTILS_TRACE_H
michael@0 18 #define _LIBS_CUTILS_TRACE_H
michael@0 19
michael@0 20 #include <sys/cdefs.h>
michael@0 21 #include <sys/types.h>
michael@0 22 #include <stdint.h>
michael@0 23 #include <stdbool.h>
michael@0 24 #include <unistd.h>
michael@0 25 #include <cutils/compiler.h>
michael@0 26
michael@0 27 #ifdef ANDROID_SMP
michael@0 28 #include <cutils/atomic-inline.h>
michael@0 29 #else
michael@0 30 #include <cutils/atomic.h>
michael@0 31 #endif
michael@0 32
michael@0 33 __BEGIN_DECLS
michael@0 34
michael@0 35 /**
michael@0 36 * The ATRACE_TAG macro can be defined before including this header to trace
michael@0 37 * using one of the tags defined below. It must be defined to one of the
michael@0 38 * following ATRACE_TAG_* macros. The trace tag is used to filter tracing in
michael@0 39 * userland to avoid some of the runtime cost of tracing when it is not desired.
michael@0 40 *
michael@0 41 * Defining ATRACE_TAG to be ATRACE_TAG_ALWAYS will result in the tracing always
michael@0 42 * being enabled - this should ONLY be done for debug code, as userland tracing
michael@0 43 * has a performance cost even when the trace is not being recorded. Defining
michael@0 44 * ATRACE_TAG to be ATRACE_TAG_NEVER or leaving ATRACE_TAG undefined will result
michael@0 45 * in the tracing always being disabled.
michael@0 46 *
michael@0 47 * ATRACE_TAG_HAL should be bitwise ORed with the relevant tags for tracing
michael@0 48 * within a hardware module. For example a camera hardware module would set:
michael@0 49 * #define ATRACE_TAG (ATRACE_TAG_CAMERA | ATRACE_TAG_HAL)
michael@0 50 *
michael@0 51 * Keep these in sync with frameworks/base/core/java/android/os/Trace.java.
michael@0 52 */
michael@0 53 #define ATRACE_TAG_NEVER 0 // This tag is never enabled.
michael@0 54 #define ATRACE_TAG_ALWAYS (1<<0) // This tag is always enabled.
michael@0 55 #define ATRACE_TAG_GRAPHICS (1<<1)
michael@0 56 #define ATRACE_TAG_INPUT (1<<2)
michael@0 57 #define ATRACE_TAG_VIEW (1<<3)
michael@0 58 #define ATRACE_TAG_WEBVIEW (1<<4)
michael@0 59 #define ATRACE_TAG_WINDOW_MANAGER (1<<5)
michael@0 60 #define ATRACE_TAG_ACTIVITY_MANAGER (1<<6)
michael@0 61 #define ATRACE_TAG_SYNC_MANAGER (1<<7)
michael@0 62 #define ATRACE_TAG_AUDIO (1<<8)
michael@0 63 #define ATRACE_TAG_VIDEO (1<<9)
michael@0 64 #define ATRACE_TAG_CAMERA (1<<10)
michael@0 65 #define ATRACE_TAG_HAL (1<<11)
michael@0 66 #define ATRACE_TAG_APP (1<<12)
michael@0 67 #define ATRACE_TAG_RESOURCES (1<<13)
michael@0 68 #define ATRACE_TAG_DALVIK (1<<14)
michael@0 69 #define ATRACE_TAG_LAST ATRACE_TAG_DALVIK
michael@0 70
michael@0 71 // Reserved for initialization.
michael@0 72 #define ATRACE_TAG_NOT_READY (1LL<<63)
michael@0 73
michael@0 74 #define ATRACE_TAG_VALID_MASK ((ATRACE_TAG_LAST - 1) | ATRACE_TAG_LAST)
michael@0 75
michael@0 76 #ifndef ATRACE_TAG
michael@0 77 #define ATRACE_TAG ATRACE_TAG_NEVER
michael@0 78 #elif ATRACE_TAG > ATRACE_TAG_VALID_MASK
michael@0 79 #error ATRACE_TAG must be defined to be one of the tags defined in cutils/trace.h
michael@0 80 #endif
michael@0 81
michael@0 82 #ifdef HAVE_ANDROID_OS
michael@0 83 /**
michael@0 84 * Maximum size of a message that can be logged to the trace buffer.
michael@0 85 * Note this message includes a tag, the pid, and the string given as the name.
michael@0 86 * Names should be kept short to get the most use of the trace buffer.
michael@0 87 */
michael@0 88 #define ATRACE_MESSAGE_LENGTH 1024
michael@0 89
michael@0 90 /**
michael@0 91 * Opens the trace file for writing and reads the property for initial tags.
michael@0 92 * The atrace.tags.enableflags property sets the tags to trace.
michael@0 93 * This function should not be explicitly called, the first call to any normal
michael@0 94 * trace function will cause it to be run safely.
michael@0 95 */
michael@0 96 void atrace_setup();
michael@0 97
michael@0 98 /**
michael@0 99 * If tracing is ready, set atrace_enabled_tags to the system property
michael@0 100 * debug.atrace.tags.enableflags. Can be used as a sysprop change callback.
michael@0 101 */
michael@0 102 void atrace_update_tags();
michael@0 103
michael@0 104 /**
michael@0 105 * Set whether the process is debuggable. By default the process is not
michael@0 106 * considered debuggable. If the process is not debuggable then application-
michael@0 107 * level tracing is not allowed unless the ro.debuggable system property is
michael@0 108 * set to '1'.
michael@0 109 */
michael@0 110 void atrace_set_debuggable(bool debuggable);
michael@0 111
michael@0 112 /**
michael@0 113 * Set whether tracing is enabled for the current process. This is used to
michael@0 114 * prevent tracing within the Zygote process.
michael@0 115 */
michael@0 116 void atrace_set_tracing_enabled(bool enabled);
michael@0 117
michael@0 118 /**
michael@0 119 * Flag indicating whether setup has been completed, initialized to 0.
michael@0 120 * Nonzero indicates setup has completed.
michael@0 121 * Note: This does NOT indicate whether or not setup was successful.
michael@0 122 */
michael@0 123 extern volatile int32_t atrace_is_ready;
michael@0 124
michael@0 125 /**
michael@0 126 * Set of ATRACE_TAG flags to trace for, initialized to ATRACE_TAG_NOT_READY.
michael@0 127 * A value of zero indicates setup has failed.
michael@0 128 * Any other nonzero value indicates setup has succeeded, and tracing is on.
michael@0 129 */
michael@0 130 extern uint64_t atrace_enabled_tags;
michael@0 131
michael@0 132 /**
michael@0 133 * Handle to the kernel's trace buffer, initialized to -1.
michael@0 134 * Any other value indicates setup has succeeded, and is a valid fd for tracing.
michael@0 135 */
michael@0 136 extern int atrace_marker_fd;
michael@0 137
michael@0 138 /**
michael@0 139 * atrace_init readies the process for tracing by opening the trace_marker file.
michael@0 140 * Calling any trace function causes this to be run, so calling it is optional.
michael@0 141 * This can be explicitly run to avoid setup delay on first trace function.
michael@0 142 */
michael@0 143 #define ATRACE_INIT() atrace_init()
michael@0 144 static inline void atrace_init()
michael@0 145 {
michael@0 146 if (CC_UNLIKELY(!android_atomic_acquire_load(&atrace_is_ready))) {
michael@0 147 atrace_setup();
michael@0 148 }
michael@0 149 }
michael@0 150
michael@0 151 /**
michael@0 152 * Get the mask of all tags currently enabled.
michael@0 153 * It can be used as a guard condition around more expensive trace calculations.
michael@0 154 * Every trace function calls this, which ensures atrace_init is run.
michael@0 155 */
michael@0 156 #define ATRACE_GET_ENABLED_TAGS() atrace_get_enabled_tags()
michael@0 157 static inline uint64_t atrace_get_enabled_tags()
michael@0 158 {
michael@0 159 atrace_init();
michael@0 160 return atrace_enabled_tags;
michael@0 161 }
michael@0 162
michael@0 163 /**
michael@0 164 * Test if a given tag is currently enabled.
michael@0 165 * Returns nonzero if the tag is enabled, otherwise zero.
michael@0 166 * It can be used as a guard condition around more expensive trace calculations.
michael@0 167 */
michael@0 168 #define ATRACE_ENABLED() atrace_is_tag_enabled(ATRACE_TAG)
michael@0 169 static inline uint64_t atrace_is_tag_enabled(uint64_t tag)
michael@0 170 {
michael@0 171 return atrace_get_enabled_tags() & tag;
michael@0 172 }
michael@0 173
michael@0 174 /**
michael@0 175 * Trace the beginning of a context. name is used to identify the context.
michael@0 176 * This is often used to time function execution.
michael@0 177 */
michael@0 178 #define ATRACE_BEGIN(name) atrace_begin(ATRACE_TAG, name)
michael@0 179 static inline void atrace_begin(uint64_t tag, const char* name)
michael@0 180 {
michael@0 181 if (CC_UNLIKELY(atrace_is_tag_enabled(tag))) {
michael@0 182 char buf[ATRACE_MESSAGE_LENGTH];
michael@0 183 size_t len;
michael@0 184
michael@0 185 len = snprintf(buf, ATRACE_MESSAGE_LENGTH, "B|%d|%s", getpid(), name);
michael@0 186 write(atrace_marker_fd, buf, len);
michael@0 187 }
michael@0 188 }
michael@0 189
michael@0 190 /**
michael@0 191 * Trace the end of a context.
michael@0 192 * This should match up (and occur after) a corresponding ATRACE_BEGIN.
michael@0 193 */
michael@0 194 #define ATRACE_END() atrace_end(ATRACE_TAG)
michael@0 195 static inline void atrace_end(uint64_t tag)
michael@0 196 {
michael@0 197 if (CC_UNLIKELY(atrace_is_tag_enabled(tag))) {
michael@0 198 char c = 'E';
michael@0 199 write(atrace_marker_fd, &c, 1);
michael@0 200 }
michael@0 201 }
michael@0 202
michael@0 203 /**
michael@0 204 * Trace the beginning of an asynchronous event. Unlike ATRACE_BEGIN/ATRACE_END
michael@0 205 * contexts, asynchronous events do not need to be nested. The name describes
michael@0 206 * the event, and the cookie provides a unique identifier for distinguishing
michael@0 207 * simultaneous events. The name and cookie used to begin an event must be
michael@0 208 * used to end it.
michael@0 209 */
michael@0 210 #define ATRACE_ASYNC_BEGIN(name, cookie) \
michael@0 211 atrace_async_begin(ATRACE_TAG, name, cookie)
michael@0 212 static inline void atrace_async_begin(uint64_t tag, const char* name,
michael@0 213 int32_t cookie)
michael@0 214 {
michael@0 215 if (CC_UNLIKELY(atrace_is_tag_enabled(tag))) {
michael@0 216 char buf[ATRACE_MESSAGE_LENGTH];
michael@0 217 size_t len;
michael@0 218
michael@0 219 len = snprintf(buf, ATRACE_MESSAGE_LENGTH, "S|%d|%s|%d", getpid(),
michael@0 220 name, cookie);
michael@0 221 write(atrace_marker_fd, buf, len);
michael@0 222 }
michael@0 223 }
michael@0 224
michael@0 225 /**
michael@0 226 * Trace the end of an asynchronous event.
michael@0 227 * This should have a corresponding ATRACE_ASYNC_BEGIN.
michael@0 228 */
michael@0 229 #define ATRACE_ASYNC_END(name, cookie) atrace_async_end(ATRACE_TAG, name, cookie)
michael@0 230 static inline void atrace_async_end(uint64_t tag, const char* name,
michael@0 231 int32_t cookie)
michael@0 232 {
michael@0 233 if (CC_UNLIKELY(atrace_is_tag_enabled(tag))) {
michael@0 234 char buf[ATRACE_MESSAGE_LENGTH];
michael@0 235 size_t len;
michael@0 236
michael@0 237 len = snprintf(buf, ATRACE_MESSAGE_LENGTH, "F|%d|%s|%d", getpid(),
michael@0 238 name, cookie);
michael@0 239 write(atrace_marker_fd, buf, len);
michael@0 240 }
michael@0 241 }
michael@0 242
michael@0 243
michael@0 244 /**
michael@0 245 * Traces an integer counter value. name is used to identify the counter.
michael@0 246 * This can be used to track how a value changes over time.
michael@0 247 */
michael@0 248 #define ATRACE_INT(name, value) atrace_int(ATRACE_TAG, name, value)
michael@0 249 static inline void atrace_int(uint64_t tag, const char* name, int32_t value)
michael@0 250 {
michael@0 251 if (CC_UNLIKELY(atrace_is_tag_enabled(tag))) {
michael@0 252 char buf[ATRACE_MESSAGE_LENGTH];
michael@0 253 size_t len;
michael@0 254
michael@0 255 len = snprintf(buf, ATRACE_MESSAGE_LENGTH, "C|%d|%s|%d",
michael@0 256 getpid(), name, value);
michael@0 257 write(atrace_marker_fd, buf, len);
michael@0 258 }
michael@0 259 }
michael@0 260
michael@0 261 #else // not HAVE_ANDROID_OS
michael@0 262
michael@0 263 #define ATRACE_INIT()
michael@0 264 #define ATRACE_GET_ENABLED_TAGS()
michael@0 265 #define ATRACE_ENABLED()
michael@0 266 #define ATRACE_BEGIN(name)
michael@0 267 #define ATRACE_END()
michael@0 268 #define ATRACE_ASYNC_BEGIN(name, cookie)
michael@0 269 #define ATRACE_ASYNC_END(name, cookie)
michael@0 270 #define ATRACE_INT(name, value)
michael@0 271
michael@0 272 #endif // not HAVE_ANDROID_OS
michael@0 273
michael@0 274 __END_DECLS
michael@0 275
michael@0 276 #endif // _LIBS_CUTILS_TRACE_H

mercurial