Jamie Gennis | f64b1ca | 2012-02-23 11:28:28 -0800 | [diff] [blame] | 1 | /* |
| 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 Brown | 45b80c6 | 2012-03-09 14:46:01 -0800 | [diff] [blame] | 41 | // |
| 42 | // These tags must be kept in sync with frameworks/base/core/java/android/os/Trace.java. |
Jamie Gennis | f64b1ca | 2012-02-23 11:28:28 -0800 | [diff] [blame] | 43 | #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) |
Jeff Brown | 45b80c6 | 2012-03-09 14:46:01 -0800 | [diff] [blame] | 46 | #define ATRACE_TAG_INPUT (1<<2) |
| 47 | #define ATRACE_TAG_VIEW (1<<3) |
| 48 | #define ATRACE_TAG_LAST ATRACE_TAG_VIEW |
Jamie Gennis | f64b1ca | 2012-02-23 11:28:28 -0800 | [diff] [blame] | 49 | |
Jeff Brown | 45b80c6 | 2012-03-09 14:46:01 -0800 | [diff] [blame] | 50 | #define ATRACE_TAG_VALID_MASK ((ATRACE_TAG_LAST - 1) | ATRACE_TAG_LAST) |
Jamie Gennis | f64b1ca | 2012-02-23 11:28:28 -0800 | [diff] [blame] | 51 | |
| 52 | #ifndef ATRACE_TAG |
| 53 | #define ATRACE_TAG ATRACE_TAG_NEVER |
| 54 | #elif ATRACE_TAG > ATRACE_TAG_LAST |
| 55 | #error ATRACE_TAG must be defined to be one of the tags defined in utils/Trace.h |
| 56 | #endif |
| 57 | |
| 58 | // ATRACE_CALL traces the beginning and end of the current function. To trace |
| 59 | // the correct start and end times this macro should be the first line of the |
| 60 | // function body. |
| 61 | #define ATRACE_CALL() android::ScopedTrace ___tracer(ATRACE_TAG, __FUNCTION__) |
| 62 | |
| 63 | // ATRACE_INT traces a named integer value. This can be used to track how the |
| 64 | // value changes over time in a trace. |
| 65 | #define ATRACE_INT(name, value) android::Tracer::traceCounter(ATRACE_TAG, name, value) |
| 66 | |
Jeff Brown | 45b80c6 | 2012-03-09 14:46:01 -0800 | [diff] [blame] | 67 | // ATRACE_ENABLED returns true if the trace tag is enabled. It can be used as a |
| 68 | // guard condition around more expensive trace calculations. |
| 69 | #define ATRACE_ENABLED() android::Tracer::isTagEnabled(ATRACE_TAG) |
| 70 | |
Jamie Gennis | f64b1ca | 2012-02-23 11:28:28 -0800 | [diff] [blame] | 71 | namespace android { |
| 72 | |
| 73 | class Tracer { |
| 74 | |
| 75 | public: |
| 76 | |
Jeff Brown | 45b80c6 | 2012-03-09 14:46:01 -0800 | [diff] [blame] | 77 | static uint64_t getEnabledTags() { |
| 78 | initIfNeeded(); |
| 79 | return sEnabledTags; |
| 80 | } |
| 81 | |
| 82 | static inline bool isTagEnabled(uint64_t tag) { |
| 83 | initIfNeeded(); |
| 84 | return sEnabledTags & tag; |
| 85 | } |
| 86 | |
Jamie Gennis | f64b1ca | 2012-02-23 11:28:28 -0800 | [diff] [blame] | 87 | static inline void traceCounter(uint64_t tag, const char* name, |
| 88 | int32_t value) { |
Jeff Brown | 45b80c6 | 2012-03-09 14:46:01 -0800 | [diff] [blame] | 89 | if (CC_UNLIKELY(isTagEnabled(tag))) { |
Jamie Gennis | f64b1ca | 2012-02-23 11:28:28 -0800 | [diff] [blame] | 90 | char buf[1024]; |
| 91 | snprintf(buf, 1024, "C|%d|%s|%d", getpid(), name, value); |
Jeff Brown | 45b80c6 | 2012-03-09 14:46:01 -0800 | [diff] [blame] | 92 | write(sTraceFD, buf, strlen(buf)); |
Jamie Gennis | f64b1ca | 2012-02-23 11:28:28 -0800 | [diff] [blame] | 93 | } |
| 94 | } |
| 95 | |
| 96 | static inline void traceBegin(uint64_t tag, const char* name) { |
Jeff Brown | 45b80c6 | 2012-03-09 14:46:01 -0800 | [diff] [blame] | 97 | if (CC_UNLIKELY(isTagEnabled(tag))) { |
Jamie Gennis | f64b1ca | 2012-02-23 11:28:28 -0800 | [diff] [blame] | 98 | char buf[1024]; |
| 99 | size_t len = snprintf(buf, 1024, "B|%d|%s", getpid(), name); |
Jeff Brown | 45b80c6 | 2012-03-09 14:46:01 -0800 | [diff] [blame] | 100 | write(sTraceFD, buf, len); |
Jamie Gennis | f64b1ca | 2012-02-23 11:28:28 -0800 | [diff] [blame] | 101 | } |
| 102 | } |
| 103 | |
| 104 | static inline void traceEnd(uint64_t tag) { |
Jeff Brown | 45b80c6 | 2012-03-09 14:46:01 -0800 | [diff] [blame] | 105 | if (CC_UNLIKELY(isTagEnabled(tag))) { |
Jamie Gennis | f64b1ca | 2012-02-23 11:28:28 -0800 | [diff] [blame] | 106 | char buf = 'E'; |
Jeff Brown | 45b80c6 | 2012-03-09 14:46:01 -0800 | [diff] [blame] | 107 | write(sTraceFD, &buf, 1); |
Jamie Gennis | f64b1ca | 2012-02-23 11:28:28 -0800 | [diff] [blame] | 108 | } |
| 109 | } |
| 110 | |
| 111 | private: |
| 112 | |
Jeff Brown | 45b80c6 | 2012-03-09 14:46:01 -0800 | [diff] [blame] | 113 | static inline void initIfNeeded() { |
| 114 | if (!android_atomic_acquire_load(&sIsReady)) { |
| 115 | init(); |
| 116 | } |
Jamie Gennis | f64b1ca | 2012-02-23 11:28:28 -0800 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | // init opens the trace marker file for writing and reads the |
| 120 | // atrace.tags.enableflags system property. It does this only the first |
| 121 | // time it is run, using sMutex for synchronization. |
| 122 | static void init(); |
| 123 | |
| 124 | // sIsReady is a boolean value indicating whether a call to init() has |
| 125 | // completed in this process. It is initialized to 0 and set to 1 when the |
| 126 | // first init() call completes. It is set to 1 even if a failure occurred |
| 127 | // in init (e.g. the trace marker file couldn't be opened). |
| 128 | // |
| 129 | // This should be checked by all tracing functions using an atomic acquire |
| 130 | // load operation before calling init(). This check avoids the need to lock |
| 131 | // a mutex each time a trace function gets called. |
| 132 | static volatile int32_t sIsReady; |
| 133 | |
| 134 | // sTraceFD is the file descriptor used to write to the kernel's trace |
| 135 | // buffer. It is initialized to -1 and set to an open file descriptor in |
| 136 | // init() while a lock on sMutex is held. |
| 137 | // |
| 138 | // This should only be used by a trace function after init() has |
| 139 | // successfully completed. |
| 140 | static int sTraceFD; |
| 141 | |
| 142 | // sEnabledTags is the set of tag bits for which tracing is currently |
| 143 | // enabled. It is initialized to 0 and set based on the |
| 144 | // atrace.tags.enableflags system property in init() while a lock on sMutex |
| 145 | // is held. |
| 146 | // |
| 147 | // This should only be used by a trace function after init() has |
| 148 | // successfully completed. |
Jeff Brown | 45b80c6 | 2012-03-09 14:46:01 -0800 | [diff] [blame] | 149 | // |
| 150 | // This value is only ever non-zero when tracing is initialized and sTraceFD is not -1. |
Jamie Gennis | f64b1ca | 2012-02-23 11:28:28 -0800 | [diff] [blame] | 151 | static uint64_t sEnabledTags; |
| 152 | |
| 153 | // sMutex is used to protect the execution of init(). |
| 154 | static Mutex sMutex; |
| 155 | }; |
| 156 | |
| 157 | class ScopedTrace { |
| 158 | |
| 159 | public: |
| 160 | inline ScopedTrace(uint64_t tag, const char* name) : |
| 161 | mTag(tag) { |
| 162 | Tracer::traceBegin(mTag, name); |
| 163 | } |
| 164 | |
| 165 | inline ~ScopedTrace() { |
| 166 | Tracer::traceEnd(mTag); |
| 167 | } |
| 168 | |
| 169 | private: |
| 170 | |
| 171 | uint64_t mTag; |
| 172 | }; |
| 173 | |
| 174 | }; // namespace android |
| 175 | |
| 176 | #endif // ANDROID_TRACE_H |