1 /* 2 * Copyright 2022 Google LLC 3 * SPDX-License-Identifier: MIT 4 */ 5 6 #ifndef CPU_TRACE_H 7 #define CPU_TRACE_H 8 9 #include "u_perfetto.h" 10 #include "u_gpuvis.h" 11 12 #include "util/detect_os.h" 13 #include "util/macros.h" 14 15 #if defined(HAVE_PERFETTO) 16 17 /* note that util_perfetto_is_tracing_enabled always returns false util 18 * util_perfetto_init is called 19 */ 20 #define _MESA_TRACE_BEGIN(name) \ 21 do { \ 22 if (unlikely(util_perfetto_is_tracing_enabled())) \ 23 util_perfetto_trace_begin(name); \ 24 } while (0) 25 26 #define _MESA_TRACE_END() \ 27 do { \ 28 if (unlikely(util_perfetto_is_tracing_enabled())) \ 29 util_perfetto_trace_end(); \ 30 } while (0) 31 32 /* NOTE: for now disable atrace for C++ to workaround a ndk bug with ordering 33 * between stdatomic.h and atomic.h. See: 34 * 35 * https://github.com/android/ndk/issues/1178 36 */ 37 #elif DETECT_OS_ANDROID && !defined(__cplusplus) 38 39 #include <cutils/trace.h> 40 41 #define _MESA_TRACE_BEGIN(name) \ 42 atrace_begin(ATRACE_TAG_GRAPHICS, name) 43 #define _MESA_TRACE_END() atrace_end(ATRACE_TAG_GRAPHICS) 44 45 #else 46 47 #define _MESA_TRACE_BEGIN(name) 48 #define _MESA_TRACE_END() 49 50 #endif /* HAVE_PERFETTO */ 51 52 #if defined(HAVE_GPUVIS) 53 54 #define _MESA_GPUVIS_TRACE_BEGIN(name) util_gpuvis_begin(name) 55 #define _MESA_GPUVIS_TRACE_END() util_gpuvis_end() 56 57 #else 58 59 #define _MESA_GPUVIS_TRACE_BEGIN(name) 60 #define _MESA_GPUVIS_TRACE_END() 61 62 #endif /* HAVE_GPUVIS */ 63 64 #if __has_attribute(cleanup) && __has_attribute(unused) 65 66 #define _MESA_TRACE_SCOPE_VAR_CONCAT(name, suffix) name##suffix 67 #define _MESA_TRACE_SCOPE_VAR(suffix) \ 68 _MESA_TRACE_SCOPE_VAR_CONCAT(_mesa_trace_scope_, suffix) 69 70 /* This must expand to a single non-scoped statement for 71 * 72 * if (cond) 73 * _MESA_TRACE_SCOPE(...) 74 * 75 * to work. 76 */ 77 #define _MESA_TRACE_SCOPE(name) \ 78 int _MESA_TRACE_SCOPE_VAR(__LINE__) \ 79 __attribute__((cleanup(_mesa_trace_scope_end), unused)) = \ 80 _mesa_trace_scope_begin(name) 81 82 static inline int _mesa_trace_scope_begin(const char * name)83_mesa_trace_scope_begin(const char *name) 84 { 85 _MESA_TRACE_BEGIN(name); 86 _MESA_GPUVIS_TRACE_BEGIN(name); 87 return 0; 88 } 89 90 static inline void _mesa_trace_scope_end(UNUSED int * scope)91_mesa_trace_scope_end(UNUSED int *scope) 92 { 93 _MESA_GPUVIS_TRACE_END(); 94 _MESA_TRACE_END(); 95 } 96 97 #else 98 99 #define _MESA_TRACE_SCOPE(name) 100 101 #endif /* __has_attribute(cleanup) && __has_attribute(unused) */ 102 103 #define MESA_TRACE_SCOPE(name) _MESA_TRACE_SCOPE(name) 104 #define MESA_TRACE_FUNC() _MESA_TRACE_SCOPE(__func__) 105 106 static inline void util_cpu_trace_init()107util_cpu_trace_init() 108 { 109 util_perfetto_init(); 110 util_gpuvis_init(); 111 } 112 113 #endif /* CPU_TRACE_H */ 114