1bf215546Sopenharmony_ci/* 2bf215546Sopenharmony_ci * Copyright (C) 2012 The Android Open Source Project 3bf215546Sopenharmony_ci * 4bf215546Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 5bf215546Sopenharmony_ci * you may not use this file except in compliance with the License. 6bf215546Sopenharmony_ci * You may obtain a copy of the License at 7bf215546Sopenharmony_ci * 8bf215546Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 9bf215546Sopenharmony_ci * 10bf215546Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 11bf215546Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 12bf215546Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13bf215546Sopenharmony_ci * See the License for the specific language governing permissions and 14bf215546Sopenharmony_ci * limitations under the License. 15bf215546Sopenharmony_ci */ 16bf215546Sopenharmony_ci 17bf215546Sopenharmony_ci#ifndef _LIBS_CUTILS_TRACE_H 18bf215546Sopenharmony_ci#define _LIBS_CUTILS_TRACE_H 19bf215546Sopenharmony_ci 20bf215546Sopenharmony_ci#include <inttypes.h> 21bf215546Sopenharmony_ci#include <stdatomic.h> 22bf215546Sopenharmony_ci#include <stdbool.h> 23bf215546Sopenharmony_ci#include <stdint.h> 24bf215546Sopenharmony_ci#include <stdio.h> 25bf215546Sopenharmony_ci#include <sys/cdefs.h> 26bf215546Sopenharmony_ci#include <sys/types.h> 27bf215546Sopenharmony_ci#include <unistd.h> 28bf215546Sopenharmony_ci#include <cutils/compiler.h> 29bf215546Sopenharmony_ci 30bf215546Sopenharmony_ci__BEGIN_DECLS 31bf215546Sopenharmony_ci 32bf215546Sopenharmony_ci/** 33bf215546Sopenharmony_ci * The ATRACE_TAG macro can be defined before including this header to trace 34bf215546Sopenharmony_ci * using one of the tags defined below. It must be defined to one of the 35bf215546Sopenharmony_ci * following ATRACE_TAG_* macros. The trace tag is used to filter tracing in 36bf215546Sopenharmony_ci * userland to avoid some of the runtime cost of tracing when it is not desired. 37bf215546Sopenharmony_ci * 38bf215546Sopenharmony_ci * Defining ATRACE_TAG to be ATRACE_TAG_ALWAYS will result in the tracing always 39bf215546Sopenharmony_ci * being enabled - this should ONLY be done for debug code, as userland tracing 40bf215546Sopenharmony_ci * has a performance cost even when the trace is not being recorded. Defining 41bf215546Sopenharmony_ci * ATRACE_TAG to be ATRACE_TAG_NEVER or leaving ATRACE_TAG undefined will result 42bf215546Sopenharmony_ci * in the tracing always being disabled. 43bf215546Sopenharmony_ci * 44bf215546Sopenharmony_ci * ATRACE_TAG_HAL should be bitwise ORed with the relevant tags for tracing 45bf215546Sopenharmony_ci * within a hardware module. For example a camera hardware module would set: 46bf215546Sopenharmony_ci * #define ATRACE_TAG (ATRACE_TAG_CAMERA | ATRACE_TAG_HAL) 47bf215546Sopenharmony_ci * 48bf215546Sopenharmony_ci * Keep these in sync with frameworks/base/core/java/android/os/Trace.java. 49bf215546Sopenharmony_ci */ 50bf215546Sopenharmony_ci#define ATRACE_TAG_NEVER 0 // This tag is never enabled. 51bf215546Sopenharmony_ci#define ATRACE_TAG_ALWAYS (1<<0) // This tag is always enabled. 52bf215546Sopenharmony_ci#define ATRACE_TAG_GRAPHICS (1<<1) 53bf215546Sopenharmony_ci#define ATRACE_TAG_INPUT (1<<2) 54bf215546Sopenharmony_ci#define ATRACE_TAG_VIEW (1<<3) 55bf215546Sopenharmony_ci#define ATRACE_TAG_WEBVIEW (1<<4) 56bf215546Sopenharmony_ci#define ATRACE_TAG_WINDOW_MANAGER (1<<5) 57bf215546Sopenharmony_ci#define ATRACE_TAG_ACTIVITY_MANAGER (1<<6) 58bf215546Sopenharmony_ci#define ATRACE_TAG_SYNC_MANAGER (1<<7) 59bf215546Sopenharmony_ci#define ATRACE_TAG_AUDIO (1<<8) 60bf215546Sopenharmony_ci#define ATRACE_TAG_VIDEO (1<<9) 61bf215546Sopenharmony_ci#define ATRACE_TAG_CAMERA (1<<10) 62bf215546Sopenharmony_ci#define ATRACE_TAG_HAL (1<<11) 63bf215546Sopenharmony_ci#define ATRACE_TAG_APP (1<<12) 64bf215546Sopenharmony_ci#define ATRACE_TAG_RESOURCES (1<<13) 65bf215546Sopenharmony_ci#define ATRACE_TAG_DALVIK (1<<14) 66bf215546Sopenharmony_ci#define ATRACE_TAG_RS (1<<15) 67bf215546Sopenharmony_ci#define ATRACE_TAG_BIONIC (1<<16) 68bf215546Sopenharmony_ci#define ATRACE_TAG_POWER (1<<17) 69bf215546Sopenharmony_ci#define ATRACE_TAG_PACKAGE_MANAGER (1<<18) 70bf215546Sopenharmony_ci#define ATRACE_TAG_SYSTEM_SERVER (1<<19) 71bf215546Sopenharmony_ci#define ATRACE_TAG_DATABASE (1<<20) 72bf215546Sopenharmony_ci#define ATRACE_TAG_NETWORK (1<<21) 73bf215546Sopenharmony_ci#define ATRACE_TAG_ADB (1<<22) 74bf215546Sopenharmony_ci#define ATRACE_TAG_VIBRATOR (1<<23) 75bf215546Sopenharmony_ci#define ATRACE_TAG_AIDL (1<<24) 76bf215546Sopenharmony_ci#define ATRACE_TAG_NNAPI (1<<25) 77bf215546Sopenharmony_ci#define ATRACE_TAG_RRO (1<<26) 78bf215546Sopenharmony_ci#define ATRACE_TAG_LAST ATRACE_TAG_RRO 79bf215546Sopenharmony_ci 80bf215546Sopenharmony_ci// Reserved for initialization. 81bf215546Sopenharmony_ci#define ATRACE_TAG_NOT_READY (1ULL<<63) 82bf215546Sopenharmony_ci 83bf215546Sopenharmony_ci#define ATRACE_TAG_VALID_MASK ((ATRACE_TAG_LAST - 1) | ATRACE_TAG_LAST) 84bf215546Sopenharmony_ci 85bf215546Sopenharmony_ci#ifndef ATRACE_TAG 86bf215546Sopenharmony_ci#define ATRACE_TAG ATRACE_TAG_NEVER 87bf215546Sopenharmony_ci#elif ATRACE_TAG > ATRACE_TAG_VALID_MASK 88bf215546Sopenharmony_ci#error ATRACE_TAG must be defined to be one of the tags defined in cutils/trace.h 89bf215546Sopenharmony_ci#endif 90bf215546Sopenharmony_ci 91bf215546Sopenharmony_ci/** 92bf215546Sopenharmony_ci * Opens the trace file for writing and reads the property for initial tags. 93bf215546Sopenharmony_ci * The atrace.tags.enableflags property sets the tags to trace. 94bf215546Sopenharmony_ci * This function should not be explicitly called, the first call to any normal 95bf215546Sopenharmony_ci * trace function will cause it to be run safely. 96bf215546Sopenharmony_ci */ 97bf215546Sopenharmony_civoid atrace_setup(); 98bf215546Sopenharmony_ci 99bf215546Sopenharmony_ci/** 100bf215546Sopenharmony_ci * If tracing is ready, set atrace_enabled_tags to the system property 101bf215546Sopenharmony_ci * debug.atrace.tags.enableflags. Can be used as a sysprop change callback. 102bf215546Sopenharmony_ci */ 103bf215546Sopenharmony_civoid atrace_update_tags(); 104bf215546Sopenharmony_ci 105bf215546Sopenharmony_ci/** 106bf215546Sopenharmony_ci * Set whether tracing is enabled for the current process. This is used to 107bf215546Sopenharmony_ci * prevent tracing within the Zygote process. 108bf215546Sopenharmony_ci */ 109bf215546Sopenharmony_civoid atrace_set_tracing_enabled(bool enabled); 110bf215546Sopenharmony_ci 111bf215546Sopenharmony_ci/** 112bf215546Sopenharmony_ci * This is always set to false. This forces code that uses an old version 113bf215546Sopenharmony_ci * of this header to always call into atrace_setup, in which we call 114bf215546Sopenharmony_ci * atrace_init unconditionally. 115bf215546Sopenharmony_ci */ 116bf215546Sopenharmony_ciextern atomic_bool atrace_is_ready; 117bf215546Sopenharmony_ci 118bf215546Sopenharmony_ci/** 119bf215546Sopenharmony_ci * Set of ATRACE_TAG flags to trace for, initialized to ATRACE_TAG_NOT_READY. 120bf215546Sopenharmony_ci * A value of zero indicates setup has failed. 121bf215546Sopenharmony_ci * Any other nonzero value indicates setup has succeeded, and tracing is on. 122bf215546Sopenharmony_ci */ 123bf215546Sopenharmony_ciextern uint64_t atrace_enabled_tags; 124bf215546Sopenharmony_ci 125bf215546Sopenharmony_ci/** 126bf215546Sopenharmony_ci * Handle to the kernel's trace buffer, initialized to -1. 127bf215546Sopenharmony_ci * Any other value indicates setup has succeeded, and is a valid fd for tracing. 128bf215546Sopenharmony_ci */ 129bf215546Sopenharmony_ciextern int atrace_marker_fd; 130bf215546Sopenharmony_ci 131bf215546Sopenharmony_ci/** 132bf215546Sopenharmony_ci * atrace_init readies the process for tracing by opening the trace_marker file. 133bf215546Sopenharmony_ci * Calling any trace function causes this to be run, so calling it is optional. 134bf215546Sopenharmony_ci * This can be explicitly run to avoid setup delay on first trace function. 135bf215546Sopenharmony_ci */ 136bf215546Sopenharmony_ci#define ATRACE_INIT() atrace_init() 137bf215546Sopenharmony_ci#define ATRACE_GET_ENABLED_TAGS() atrace_get_enabled_tags() 138bf215546Sopenharmony_ci 139bf215546Sopenharmony_civoid atrace_init(); 140bf215546Sopenharmony_ciuint64_t atrace_get_enabled_tags(); 141bf215546Sopenharmony_ci 142bf215546Sopenharmony_ci/** 143bf215546Sopenharmony_ci * Test if a given tag is currently enabled. 144bf215546Sopenharmony_ci * Returns nonzero if the tag is enabled, otherwise zero. 145bf215546Sopenharmony_ci * It can be used as a guard condition around more expensive trace calculations. 146bf215546Sopenharmony_ci */ 147bf215546Sopenharmony_ci#define ATRACE_ENABLED() atrace_is_tag_enabled(ATRACE_TAG) 148bf215546Sopenharmony_cistatic inline uint64_t atrace_is_tag_enabled(uint64_t tag) 149bf215546Sopenharmony_ci{ 150bf215546Sopenharmony_ci return atrace_get_enabled_tags() & tag; 151bf215546Sopenharmony_ci} 152bf215546Sopenharmony_ci 153bf215546Sopenharmony_ci/** 154bf215546Sopenharmony_ci * Trace the beginning of a context. name is used to identify the context. 155bf215546Sopenharmony_ci * This is often used to time function execution. 156bf215546Sopenharmony_ci */ 157bf215546Sopenharmony_ci#define ATRACE_BEGIN(name) atrace_begin(ATRACE_TAG, name) 158bf215546Sopenharmony_cistatic inline void atrace_begin(uint64_t tag, const char* name) 159bf215546Sopenharmony_ci{ 160bf215546Sopenharmony_ci if (CC_UNLIKELY(atrace_is_tag_enabled(tag))) { 161bf215546Sopenharmony_ci void atrace_begin_body(const char*); 162bf215546Sopenharmony_ci atrace_begin_body(name); 163bf215546Sopenharmony_ci } 164bf215546Sopenharmony_ci} 165bf215546Sopenharmony_ci 166bf215546Sopenharmony_ci/** 167bf215546Sopenharmony_ci * Trace the end of a context. 168bf215546Sopenharmony_ci * This should match up (and occur after) a corresponding ATRACE_BEGIN. 169bf215546Sopenharmony_ci */ 170bf215546Sopenharmony_ci#define ATRACE_END() atrace_end(ATRACE_TAG) 171bf215546Sopenharmony_cistatic inline void atrace_end(uint64_t tag) 172bf215546Sopenharmony_ci{ 173bf215546Sopenharmony_ci if (CC_UNLIKELY(atrace_is_tag_enabled(tag))) { 174bf215546Sopenharmony_ci void atrace_end_body(); 175bf215546Sopenharmony_ci atrace_end_body(); 176bf215546Sopenharmony_ci } 177bf215546Sopenharmony_ci} 178bf215546Sopenharmony_ci 179bf215546Sopenharmony_ci/** 180bf215546Sopenharmony_ci * Trace the beginning of an asynchronous event. Unlike ATRACE_BEGIN/ATRACE_END 181bf215546Sopenharmony_ci * contexts, asynchronous events do not need to be nested. The name describes 182bf215546Sopenharmony_ci * the event, and the cookie provides a unique identifier for distinguishing 183bf215546Sopenharmony_ci * simultaneous events. The name and cookie used to begin an event must be 184bf215546Sopenharmony_ci * used to end it. 185bf215546Sopenharmony_ci */ 186bf215546Sopenharmony_ci#define ATRACE_ASYNC_BEGIN(name, cookie) \ 187bf215546Sopenharmony_ci atrace_async_begin(ATRACE_TAG, name, cookie) 188bf215546Sopenharmony_cistatic inline void atrace_async_begin(uint64_t tag, const char* name, 189bf215546Sopenharmony_ci int32_t cookie) 190bf215546Sopenharmony_ci{ 191bf215546Sopenharmony_ci if (CC_UNLIKELY(atrace_is_tag_enabled(tag))) { 192bf215546Sopenharmony_ci void atrace_async_begin_body(const char*, int32_t); 193bf215546Sopenharmony_ci atrace_async_begin_body(name, cookie); 194bf215546Sopenharmony_ci } 195bf215546Sopenharmony_ci} 196bf215546Sopenharmony_ci 197bf215546Sopenharmony_ci/** 198bf215546Sopenharmony_ci * Trace the end of an asynchronous event. 199bf215546Sopenharmony_ci * This should have a corresponding ATRACE_ASYNC_BEGIN. 200bf215546Sopenharmony_ci */ 201bf215546Sopenharmony_ci#define ATRACE_ASYNC_END(name, cookie) atrace_async_end(ATRACE_TAG, name, cookie) 202bf215546Sopenharmony_cistatic inline void atrace_async_end(uint64_t tag, const char* name, int32_t cookie) 203bf215546Sopenharmony_ci{ 204bf215546Sopenharmony_ci if (CC_UNLIKELY(atrace_is_tag_enabled(tag))) { 205bf215546Sopenharmony_ci void atrace_async_end_body(const char*, int32_t); 206bf215546Sopenharmony_ci atrace_async_end_body(name, cookie); 207bf215546Sopenharmony_ci } 208bf215546Sopenharmony_ci} 209bf215546Sopenharmony_ci 210bf215546Sopenharmony_ci/** 211bf215546Sopenharmony_ci * Traces an integer counter value. name is used to identify the counter. 212bf215546Sopenharmony_ci * This can be used to track how a value changes over time. 213bf215546Sopenharmony_ci */ 214bf215546Sopenharmony_ci#define ATRACE_INT(name, value) atrace_int(ATRACE_TAG, name, value) 215bf215546Sopenharmony_cistatic inline void atrace_int(uint64_t tag, const char* name, int32_t value) 216bf215546Sopenharmony_ci{ 217bf215546Sopenharmony_ci if (CC_UNLIKELY(atrace_is_tag_enabled(tag))) { 218bf215546Sopenharmony_ci void atrace_int_body(const char*, int32_t); 219bf215546Sopenharmony_ci atrace_int_body(name, value); 220bf215546Sopenharmony_ci } 221bf215546Sopenharmony_ci} 222bf215546Sopenharmony_ci 223bf215546Sopenharmony_ci/** 224bf215546Sopenharmony_ci * Traces a 64-bit integer counter value. name is used to identify the 225bf215546Sopenharmony_ci * counter. This can be used to track how a value changes over time. 226bf215546Sopenharmony_ci */ 227bf215546Sopenharmony_ci#define ATRACE_INT64(name, value) atrace_int64(ATRACE_TAG, name, value) 228bf215546Sopenharmony_cistatic inline void atrace_int64(uint64_t tag, const char* name, int64_t value) 229bf215546Sopenharmony_ci{ 230bf215546Sopenharmony_ci if (CC_UNLIKELY(atrace_is_tag_enabled(tag))) { 231bf215546Sopenharmony_ci void atrace_int64_body(const char*, int64_t); 232bf215546Sopenharmony_ci atrace_int64_body(name, value); 233bf215546Sopenharmony_ci } 234bf215546Sopenharmony_ci} 235bf215546Sopenharmony_ci 236bf215546Sopenharmony_ci__END_DECLS 237bf215546Sopenharmony_ci 238bf215546Sopenharmony_ci#endif // _LIBS_CUTILS_TRACE_H 239