michael@0: /* michael@0: * Copyright (C) 2012 The Android Open Source Project michael@0: * michael@0: * Licensed under the Apache License, Version 2.0 (the "License"); michael@0: * you may not use this file except in compliance with the License. michael@0: * You may obtain a copy of the License at michael@0: * michael@0: * http://www.apache.org/licenses/LICENSE-2.0 michael@0: * michael@0: * Unless required by applicable law or agreed to in writing, software michael@0: * distributed under the License is distributed on an "AS IS" BASIS, michael@0: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. michael@0: * See the License for the specific language governing permissions and michael@0: * limitations under the License. michael@0: */ michael@0: michael@0: #ifndef _LIBS_CUTILS_TRACE_H michael@0: #define _LIBS_CUTILS_TRACE_H michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: #ifdef ANDROID_SMP michael@0: #include michael@0: #else michael@0: #include michael@0: #endif michael@0: michael@0: __BEGIN_DECLS michael@0: michael@0: /** michael@0: * The ATRACE_TAG macro can be defined before including this header to trace michael@0: * using one of the tags defined below. It must be defined to one of the michael@0: * following ATRACE_TAG_* macros. The trace tag is used to filter tracing in michael@0: * userland to avoid some of the runtime cost of tracing when it is not desired. michael@0: * michael@0: * Defining ATRACE_TAG to be ATRACE_TAG_ALWAYS will result in the tracing always michael@0: * being enabled - this should ONLY be done for debug code, as userland tracing michael@0: * has a performance cost even when the trace is not being recorded. Defining michael@0: * ATRACE_TAG to be ATRACE_TAG_NEVER or leaving ATRACE_TAG undefined will result michael@0: * in the tracing always being disabled. michael@0: * michael@0: * ATRACE_TAG_HAL should be bitwise ORed with the relevant tags for tracing michael@0: * within a hardware module. For example a camera hardware module would set: michael@0: * #define ATRACE_TAG (ATRACE_TAG_CAMERA | ATRACE_TAG_HAL) michael@0: * michael@0: * Keep these in sync with frameworks/base/core/java/android/os/Trace.java. michael@0: */ michael@0: #define ATRACE_TAG_NEVER 0 // This tag is never enabled. michael@0: #define ATRACE_TAG_ALWAYS (1<<0) // This tag is always enabled. michael@0: #define ATRACE_TAG_GRAPHICS (1<<1) michael@0: #define ATRACE_TAG_INPUT (1<<2) michael@0: #define ATRACE_TAG_VIEW (1<<3) michael@0: #define ATRACE_TAG_WEBVIEW (1<<4) michael@0: #define ATRACE_TAG_WINDOW_MANAGER (1<<5) michael@0: #define ATRACE_TAG_ACTIVITY_MANAGER (1<<6) michael@0: #define ATRACE_TAG_SYNC_MANAGER (1<<7) michael@0: #define ATRACE_TAG_AUDIO (1<<8) michael@0: #define ATRACE_TAG_VIDEO (1<<9) michael@0: #define ATRACE_TAG_CAMERA (1<<10) michael@0: #define ATRACE_TAG_HAL (1<<11) michael@0: #define ATRACE_TAG_APP (1<<12) michael@0: #define ATRACE_TAG_RESOURCES (1<<13) michael@0: #define ATRACE_TAG_DALVIK (1<<14) michael@0: #define ATRACE_TAG_LAST ATRACE_TAG_DALVIK michael@0: michael@0: // Reserved for initialization. michael@0: #define ATRACE_TAG_NOT_READY (1LL<<63) michael@0: michael@0: #define ATRACE_TAG_VALID_MASK ((ATRACE_TAG_LAST - 1) | ATRACE_TAG_LAST) michael@0: michael@0: #ifndef ATRACE_TAG michael@0: #define ATRACE_TAG ATRACE_TAG_NEVER michael@0: #elif ATRACE_TAG > ATRACE_TAG_VALID_MASK michael@0: #error ATRACE_TAG must be defined to be one of the tags defined in cutils/trace.h michael@0: #endif michael@0: michael@0: #ifdef HAVE_ANDROID_OS michael@0: /** michael@0: * Maximum size of a message that can be logged to the trace buffer. michael@0: * Note this message includes a tag, the pid, and the string given as the name. michael@0: * Names should be kept short to get the most use of the trace buffer. michael@0: */ michael@0: #define ATRACE_MESSAGE_LENGTH 1024 michael@0: michael@0: /** michael@0: * Opens the trace file for writing and reads the property for initial tags. michael@0: * The atrace.tags.enableflags property sets the tags to trace. michael@0: * This function should not be explicitly called, the first call to any normal michael@0: * trace function will cause it to be run safely. michael@0: */ michael@0: void atrace_setup(); michael@0: michael@0: /** michael@0: * If tracing is ready, set atrace_enabled_tags to the system property michael@0: * debug.atrace.tags.enableflags. Can be used as a sysprop change callback. michael@0: */ michael@0: void atrace_update_tags(); michael@0: michael@0: /** michael@0: * Set whether the process is debuggable. By default the process is not michael@0: * considered debuggable. If the process is not debuggable then application- michael@0: * level tracing is not allowed unless the ro.debuggable system property is michael@0: * set to '1'. michael@0: */ michael@0: void atrace_set_debuggable(bool debuggable); michael@0: michael@0: /** michael@0: * Set whether tracing is enabled for the current process. This is used to michael@0: * prevent tracing within the Zygote process. michael@0: */ michael@0: void atrace_set_tracing_enabled(bool enabled); michael@0: michael@0: /** michael@0: * Flag indicating whether setup has been completed, initialized to 0. michael@0: * Nonzero indicates setup has completed. michael@0: * Note: This does NOT indicate whether or not setup was successful. michael@0: */ michael@0: extern volatile int32_t atrace_is_ready; michael@0: michael@0: /** michael@0: * Set of ATRACE_TAG flags to trace for, initialized to ATRACE_TAG_NOT_READY. michael@0: * A value of zero indicates setup has failed. michael@0: * Any other nonzero value indicates setup has succeeded, and tracing is on. michael@0: */ michael@0: extern uint64_t atrace_enabled_tags; michael@0: michael@0: /** michael@0: * Handle to the kernel's trace buffer, initialized to -1. michael@0: * Any other value indicates setup has succeeded, and is a valid fd for tracing. michael@0: */ michael@0: extern int atrace_marker_fd; michael@0: michael@0: /** michael@0: * atrace_init readies the process for tracing by opening the trace_marker file. michael@0: * Calling any trace function causes this to be run, so calling it is optional. michael@0: * This can be explicitly run to avoid setup delay on first trace function. michael@0: */ michael@0: #define ATRACE_INIT() atrace_init() michael@0: static inline void atrace_init() michael@0: { michael@0: if (CC_UNLIKELY(!android_atomic_acquire_load(&atrace_is_ready))) { michael@0: atrace_setup(); michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * Get the mask of all tags currently enabled. michael@0: * It can be used as a guard condition around more expensive trace calculations. michael@0: * Every trace function calls this, which ensures atrace_init is run. michael@0: */ michael@0: #define ATRACE_GET_ENABLED_TAGS() atrace_get_enabled_tags() michael@0: static inline uint64_t atrace_get_enabled_tags() michael@0: { michael@0: atrace_init(); michael@0: return atrace_enabled_tags; michael@0: } michael@0: michael@0: /** michael@0: * Test if a given tag is currently enabled. michael@0: * Returns nonzero if the tag is enabled, otherwise zero. michael@0: * It can be used as a guard condition around more expensive trace calculations. michael@0: */ michael@0: #define ATRACE_ENABLED() atrace_is_tag_enabled(ATRACE_TAG) michael@0: static inline uint64_t atrace_is_tag_enabled(uint64_t tag) michael@0: { michael@0: return atrace_get_enabled_tags() & tag; michael@0: } michael@0: michael@0: /** michael@0: * Trace the beginning of a context. name is used to identify the context. michael@0: * This is often used to time function execution. michael@0: */ michael@0: #define ATRACE_BEGIN(name) atrace_begin(ATRACE_TAG, name) michael@0: static inline void atrace_begin(uint64_t tag, const char* name) michael@0: { michael@0: if (CC_UNLIKELY(atrace_is_tag_enabled(tag))) { michael@0: char buf[ATRACE_MESSAGE_LENGTH]; michael@0: size_t len; michael@0: michael@0: len = snprintf(buf, ATRACE_MESSAGE_LENGTH, "B|%d|%s", getpid(), name); michael@0: write(atrace_marker_fd, buf, len); michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * Trace the end of a context. michael@0: * This should match up (and occur after) a corresponding ATRACE_BEGIN. michael@0: */ michael@0: #define ATRACE_END() atrace_end(ATRACE_TAG) michael@0: static inline void atrace_end(uint64_t tag) michael@0: { michael@0: if (CC_UNLIKELY(atrace_is_tag_enabled(tag))) { michael@0: char c = 'E'; michael@0: write(atrace_marker_fd, &c, 1); michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * Trace the beginning of an asynchronous event. Unlike ATRACE_BEGIN/ATRACE_END michael@0: * contexts, asynchronous events do not need to be nested. The name describes michael@0: * the event, and the cookie provides a unique identifier for distinguishing michael@0: * simultaneous events. The name and cookie used to begin an event must be michael@0: * used to end it. michael@0: */ michael@0: #define ATRACE_ASYNC_BEGIN(name, cookie) \ michael@0: atrace_async_begin(ATRACE_TAG, name, cookie) michael@0: static inline void atrace_async_begin(uint64_t tag, const char* name, michael@0: int32_t cookie) michael@0: { michael@0: if (CC_UNLIKELY(atrace_is_tag_enabled(tag))) { michael@0: char buf[ATRACE_MESSAGE_LENGTH]; michael@0: size_t len; michael@0: michael@0: len = snprintf(buf, ATRACE_MESSAGE_LENGTH, "S|%d|%s|%d", getpid(), michael@0: name, cookie); michael@0: write(atrace_marker_fd, buf, len); michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * Trace the end of an asynchronous event. michael@0: * This should have a corresponding ATRACE_ASYNC_BEGIN. michael@0: */ michael@0: #define ATRACE_ASYNC_END(name, cookie) atrace_async_end(ATRACE_TAG, name, cookie) michael@0: static inline void atrace_async_end(uint64_t tag, const char* name, michael@0: int32_t cookie) michael@0: { michael@0: if (CC_UNLIKELY(atrace_is_tag_enabled(tag))) { michael@0: char buf[ATRACE_MESSAGE_LENGTH]; michael@0: size_t len; michael@0: michael@0: len = snprintf(buf, ATRACE_MESSAGE_LENGTH, "F|%d|%s|%d", getpid(), michael@0: name, cookie); michael@0: write(atrace_marker_fd, buf, len); michael@0: } michael@0: } michael@0: michael@0: michael@0: /** michael@0: * Traces an integer counter value. name is used to identify the counter. michael@0: * This can be used to track how a value changes over time. michael@0: */ michael@0: #define ATRACE_INT(name, value) atrace_int(ATRACE_TAG, name, value) michael@0: static inline void atrace_int(uint64_t tag, const char* name, int32_t value) michael@0: { michael@0: if (CC_UNLIKELY(atrace_is_tag_enabled(tag))) { michael@0: char buf[ATRACE_MESSAGE_LENGTH]; michael@0: size_t len; michael@0: michael@0: len = snprintf(buf, ATRACE_MESSAGE_LENGTH, "C|%d|%s|%d", michael@0: getpid(), name, value); michael@0: write(atrace_marker_fd, buf, len); michael@0: } michael@0: } michael@0: michael@0: #else // not HAVE_ANDROID_OS michael@0: michael@0: #define ATRACE_INIT() michael@0: #define ATRACE_GET_ENABLED_TAGS() michael@0: #define ATRACE_ENABLED() michael@0: #define ATRACE_BEGIN(name) michael@0: #define ATRACE_END() michael@0: #define ATRACE_ASYNC_BEGIN(name, cookie) michael@0: #define ATRACE_ASYNC_END(name, cookie) michael@0: #define ATRACE_INT(name, value) michael@0: michael@0: #endif // not HAVE_ANDROID_OS michael@0: michael@0: __END_DECLS michael@0: michael@0: #endif // _LIBS_CUTILS_TRACE_H