widget/gonk/libui/cutils_trace.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/widget/gonk/libui/cutils_trace.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,276 @@
     1.4 +/*
     1.5 + * Copyright (C) 2012 The Android Open Source Project
     1.6 + *
     1.7 + * Licensed under the Apache License, Version 2.0 (the "License");
     1.8 + * you may not use this file except in compliance with the License.
     1.9 + * You may obtain a copy of the License at
    1.10 + *
    1.11 + *      http://www.apache.org/licenses/LICENSE-2.0
    1.12 + *
    1.13 + * Unless required by applicable law or agreed to in writing, software
    1.14 + * distributed under the License is distributed on an "AS IS" BASIS,
    1.15 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    1.16 + * See the License for the specific language governing permissions and
    1.17 + * limitations under the License.
    1.18 + */
    1.19 +
    1.20 +#ifndef _LIBS_CUTILS_TRACE_H
    1.21 +#define _LIBS_CUTILS_TRACE_H
    1.22 +
    1.23 +#include <sys/cdefs.h>
    1.24 +#include <sys/types.h>
    1.25 +#include <stdint.h>
    1.26 +#include <stdbool.h>
    1.27 +#include <unistd.h>
    1.28 +#include <cutils/compiler.h>
    1.29 +
    1.30 +#ifdef ANDROID_SMP
    1.31 +#include <cutils/atomic-inline.h>
    1.32 +#else
    1.33 +#include <cutils/atomic.h>
    1.34 +#endif
    1.35 +
    1.36 +__BEGIN_DECLS
    1.37 +
    1.38 +/**
    1.39 + * The ATRACE_TAG macro can be defined before including this header to trace
    1.40 + * using one of the tags defined below.  It must be defined to one of the
    1.41 + * following ATRACE_TAG_* macros.  The trace tag is used to filter tracing in
    1.42 + * userland to avoid some of the runtime cost of tracing when it is not desired.
    1.43 + *
    1.44 + * Defining ATRACE_TAG to be ATRACE_TAG_ALWAYS will result in the tracing always
    1.45 + * being enabled - this should ONLY be done for debug code, as userland tracing
    1.46 + * has a performance cost even when the trace is not being recorded.  Defining
    1.47 + * ATRACE_TAG to be ATRACE_TAG_NEVER or leaving ATRACE_TAG undefined will result
    1.48 + * in the tracing always being disabled.
    1.49 + *
    1.50 + * ATRACE_TAG_HAL should be bitwise ORed with the relevant tags for tracing
    1.51 + * within a hardware module.  For example a camera hardware module would set:
    1.52 + * #define ATRACE_TAG  (ATRACE_TAG_CAMERA | ATRACE_TAG_HAL)
    1.53 + *
    1.54 + * Keep these in sync with frameworks/base/core/java/android/os/Trace.java.
    1.55 + */
    1.56 +#define ATRACE_TAG_NEVER            0       // This tag is never enabled.
    1.57 +#define ATRACE_TAG_ALWAYS           (1<<0)  // This tag is always enabled.
    1.58 +#define ATRACE_TAG_GRAPHICS         (1<<1)
    1.59 +#define ATRACE_TAG_INPUT            (1<<2)
    1.60 +#define ATRACE_TAG_VIEW             (1<<3)
    1.61 +#define ATRACE_TAG_WEBVIEW          (1<<4)
    1.62 +#define ATRACE_TAG_WINDOW_MANAGER   (1<<5)
    1.63 +#define ATRACE_TAG_ACTIVITY_MANAGER (1<<6)
    1.64 +#define ATRACE_TAG_SYNC_MANAGER     (1<<7)
    1.65 +#define ATRACE_TAG_AUDIO            (1<<8)
    1.66 +#define ATRACE_TAG_VIDEO            (1<<9)
    1.67 +#define ATRACE_TAG_CAMERA           (1<<10)
    1.68 +#define ATRACE_TAG_HAL              (1<<11)
    1.69 +#define ATRACE_TAG_APP              (1<<12)
    1.70 +#define ATRACE_TAG_RESOURCES        (1<<13)
    1.71 +#define ATRACE_TAG_DALVIK           (1<<14)
    1.72 +#define ATRACE_TAG_LAST             ATRACE_TAG_DALVIK
    1.73 +
    1.74 +// Reserved for initialization.
    1.75 +#define ATRACE_TAG_NOT_READY        (1LL<<63)
    1.76 +
    1.77 +#define ATRACE_TAG_VALID_MASK ((ATRACE_TAG_LAST - 1) | ATRACE_TAG_LAST)
    1.78 +
    1.79 +#ifndef ATRACE_TAG
    1.80 +#define ATRACE_TAG ATRACE_TAG_NEVER
    1.81 +#elif ATRACE_TAG > ATRACE_TAG_VALID_MASK
    1.82 +#error ATRACE_TAG must be defined to be one of the tags defined in cutils/trace.h
    1.83 +#endif
    1.84 +
    1.85 +#ifdef HAVE_ANDROID_OS
    1.86 +/**
    1.87 + * Maximum size of a message that can be logged to the trace buffer.
    1.88 + * Note this message includes a tag, the pid, and the string given as the name.
    1.89 + * Names should be kept short to get the most use of the trace buffer.
    1.90 + */
    1.91 +#define ATRACE_MESSAGE_LENGTH 1024
    1.92 +
    1.93 +/**
    1.94 + * Opens the trace file for writing and reads the property for initial tags.
    1.95 + * The atrace.tags.enableflags property sets the tags to trace.
    1.96 + * This function should not be explicitly called, the first call to any normal
    1.97 + * trace function will cause it to be run safely.
    1.98 + */
    1.99 +void atrace_setup();
   1.100 +
   1.101 +/**
   1.102 + * If tracing is ready, set atrace_enabled_tags to the system property
   1.103 + * debug.atrace.tags.enableflags. Can be used as a sysprop change callback.
   1.104 + */
   1.105 +void atrace_update_tags();
   1.106 +
   1.107 +/**
   1.108 + * Set whether the process is debuggable.  By default the process is not
   1.109 + * considered debuggable.  If the process is not debuggable then application-
   1.110 + * level tracing is not allowed unless the ro.debuggable system property is
   1.111 + * set to '1'.
   1.112 + */
   1.113 +void atrace_set_debuggable(bool debuggable);
   1.114 +
   1.115 +/**
   1.116 + * Set whether tracing is enabled for the current process.  This is used to
   1.117 + * prevent tracing within the Zygote process.
   1.118 + */
   1.119 +void atrace_set_tracing_enabled(bool enabled);
   1.120 +
   1.121 +/**
   1.122 + * Flag indicating whether setup has been completed, initialized to 0.
   1.123 + * Nonzero indicates setup has completed.
   1.124 + * Note: This does NOT indicate whether or not setup was successful.
   1.125 + */
   1.126 +extern volatile int32_t atrace_is_ready;
   1.127 +
   1.128 +/**
   1.129 + * Set of ATRACE_TAG flags to trace for, initialized to ATRACE_TAG_NOT_READY.
   1.130 + * A value of zero indicates setup has failed.
   1.131 + * Any other nonzero value indicates setup has succeeded, and tracing is on.
   1.132 + */
   1.133 +extern uint64_t atrace_enabled_tags;
   1.134 +
   1.135 +/**
   1.136 + * Handle to the kernel's trace buffer, initialized to -1.
   1.137 + * Any other value indicates setup has succeeded, and is a valid fd for tracing.
   1.138 + */
   1.139 +extern int atrace_marker_fd;
   1.140 +
   1.141 +/**
   1.142 + * atrace_init readies the process for tracing by opening the trace_marker file.
   1.143 + * Calling any trace function causes this to be run, so calling it is optional.
   1.144 + * This can be explicitly run to avoid setup delay on first trace function.
   1.145 + */
   1.146 +#define ATRACE_INIT() atrace_init()
   1.147 +static inline void atrace_init()
   1.148 +{
   1.149 +    if (CC_UNLIKELY(!android_atomic_acquire_load(&atrace_is_ready))) {
   1.150 +        atrace_setup();
   1.151 +    }
   1.152 +}
   1.153 +
   1.154 +/**
   1.155 + * Get the mask of all tags currently enabled.
   1.156 + * It can be used as a guard condition around more expensive trace calculations.
   1.157 + * Every trace function calls this, which ensures atrace_init is run.
   1.158 + */
   1.159 +#define ATRACE_GET_ENABLED_TAGS() atrace_get_enabled_tags()
   1.160 +static inline uint64_t atrace_get_enabled_tags()
   1.161 +{
   1.162 +    atrace_init();
   1.163 +    return atrace_enabled_tags;
   1.164 +}
   1.165 +
   1.166 +/**
   1.167 + * Test if a given tag is currently enabled.
   1.168 + * Returns nonzero if the tag is enabled, otherwise zero.
   1.169 + * It can be used as a guard condition around more expensive trace calculations.
   1.170 + */
   1.171 +#define ATRACE_ENABLED() atrace_is_tag_enabled(ATRACE_TAG)
   1.172 +static inline uint64_t atrace_is_tag_enabled(uint64_t tag)
   1.173 +{
   1.174 +    return atrace_get_enabled_tags() & tag;
   1.175 +}
   1.176 +
   1.177 +/**
   1.178 + * Trace the beginning of a context.  name is used to identify the context.
   1.179 + * This is often used to time function execution.
   1.180 + */
   1.181 +#define ATRACE_BEGIN(name) atrace_begin(ATRACE_TAG, name)
   1.182 +static inline void atrace_begin(uint64_t tag, const char* name)
   1.183 +{
   1.184 +    if (CC_UNLIKELY(atrace_is_tag_enabled(tag))) {
   1.185 +        char buf[ATRACE_MESSAGE_LENGTH];
   1.186 +        size_t len;
   1.187 +
   1.188 +        len = snprintf(buf, ATRACE_MESSAGE_LENGTH, "B|%d|%s", getpid(), name);
   1.189 +        write(atrace_marker_fd, buf, len);
   1.190 +    }
   1.191 +}
   1.192 +
   1.193 +/**
   1.194 + * Trace the end of a context.
   1.195 + * This should match up (and occur after) a corresponding ATRACE_BEGIN.
   1.196 + */
   1.197 +#define ATRACE_END() atrace_end(ATRACE_TAG)
   1.198 +static inline void atrace_end(uint64_t tag)
   1.199 +{
   1.200 +    if (CC_UNLIKELY(atrace_is_tag_enabled(tag))) {
   1.201 +        char c = 'E';
   1.202 +        write(atrace_marker_fd, &c, 1);
   1.203 +    }
   1.204 +}
   1.205 +
   1.206 +/**
   1.207 + * Trace the beginning of an asynchronous event. Unlike ATRACE_BEGIN/ATRACE_END
   1.208 + * contexts, asynchronous events do not need to be nested. The name describes
   1.209 + * the event, and the cookie provides a unique identifier for distinguishing
   1.210 + * simultaneous events. The name and cookie used to begin an event must be
   1.211 + * used to end it.
   1.212 + */
   1.213 +#define ATRACE_ASYNC_BEGIN(name, cookie) \
   1.214 +    atrace_async_begin(ATRACE_TAG, name, cookie)
   1.215 +static inline void atrace_async_begin(uint64_t tag, const char* name,
   1.216 +        int32_t cookie)
   1.217 +{
   1.218 +    if (CC_UNLIKELY(atrace_is_tag_enabled(tag))) {
   1.219 +        char buf[ATRACE_MESSAGE_LENGTH];
   1.220 +        size_t len;
   1.221 +
   1.222 +        len = snprintf(buf, ATRACE_MESSAGE_LENGTH, "S|%d|%s|%d", getpid(),
   1.223 +                name, cookie);
   1.224 +        write(atrace_marker_fd, buf, len);
   1.225 +    }
   1.226 +}
   1.227 +
   1.228 +/**
   1.229 + * Trace the end of an asynchronous event.
   1.230 + * This should have a corresponding ATRACE_ASYNC_BEGIN.
   1.231 + */
   1.232 +#define ATRACE_ASYNC_END(name, cookie) atrace_async_end(ATRACE_TAG, name, cookie)
   1.233 +static inline void atrace_async_end(uint64_t tag, const char* name,
   1.234 +        int32_t cookie)
   1.235 +{
   1.236 +    if (CC_UNLIKELY(atrace_is_tag_enabled(tag))) {
   1.237 +        char buf[ATRACE_MESSAGE_LENGTH];
   1.238 +        size_t len;
   1.239 +
   1.240 +        len = snprintf(buf, ATRACE_MESSAGE_LENGTH, "F|%d|%s|%d", getpid(),
   1.241 +                name, cookie);
   1.242 +        write(atrace_marker_fd, buf, len);
   1.243 +    }
   1.244 +}
   1.245 +
   1.246 +
   1.247 +/**
   1.248 + * Traces an integer counter value.  name is used to identify the counter.
   1.249 + * This can be used to track how a value changes over time.
   1.250 + */
   1.251 +#define ATRACE_INT(name, value) atrace_int(ATRACE_TAG, name, value)
   1.252 +static inline void atrace_int(uint64_t tag, const char* name, int32_t value)
   1.253 +{
   1.254 +    if (CC_UNLIKELY(atrace_is_tag_enabled(tag))) {
   1.255 +        char buf[ATRACE_MESSAGE_LENGTH];
   1.256 +        size_t len;
   1.257 +
   1.258 +        len = snprintf(buf, ATRACE_MESSAGE_LENGTH, "C|%d|%s|%d",
   1.259 +                getpid(), name, value);
   1.260 +        write(atrace_marker_fd, buf, len);
   1.261 +    }
   1.262 +}
   1.263 +
   1.264 +#else // not HAVE_ANDROID_OS
   1.265 +
   1.266 +#define ATRACE_INIT()
   1.267 +#define ATRACE_GET_ENABLED_TAGS()
   1.268 +#define ATRACE_ENABLED()
   1.269 +#define ATRACE_BEGIN(name)
   1.270 +#define ATRACE_END()
   1.271 +#define ATRACE_ASYNC_BEGIN(name, cookie)
   1.272 +#define ATRACE_ASYNC_END(name, cookie)
   1.273 +#define ATRACE_INT(name, value)
   1.274 +
   1.275 +#endif // not HAVE_ANDROID_OS
   1.276 +
   1.277 +__END_DECLS
   1.278 +
   1.279 +#endif // _LIBS_CUTILS_TRACE_H

mercurial