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_FLOW_BEGIN(name, id) \
27 do { \
28 if (unlikely(util_perfetto_is_tracing_enabled())) \
29 util_perfetto_trace_begin_flow(name, id); \
30 } while (0)
31
32 #define _MESA_TRACE_END() \
33 do { \
34 if (unlikely(util_perfetto_is_tracing_enabled())) \
35 util_perfetto_trace_end(); \
36 } while (0)
37
38 #define _MESA_TRACE_SET_COUNTER(name, value) \
39 do { \
40 if (unlikely(util_perfetto_is_tracing_enabled())) \
41 util_perfetto_counter_set(name, value); \
42 } while (0)
43
44 #define _MESA_TRACE_TIMESTAMP_BEGIN(name, track_id, flow_id, clock, timestamp) \
45 do { \
46 if (unlikely(util_perfetto_is_tracing_enabled())) \
47 util_perfetto_trace_full_begin(name, track_id, flow_id, clock, timestamp); \
48 } while (0)
49
50 #define _MESA_TRACE_TIMESTAMP_END(name, track_id, clock, timestamp) \
51 do { \
52 if (unlikely(util_perfetto_is_tracing_enabled())) \
53 util_perfetto_trace_full_end(name, track_id, clock, timestamp); \
54 } while (0)
55
56 /* NOTE: for now disable atrace for C++ to workaround a ndk bug with ordering
57 * between stdatomic.h and atomic.h. See:
58 *
59 * https://github.com/android/ndk/issues/1178
60 */
61 #elif DETECT_OS_ANDROID && !defined(__cplusplus)
62
63 #include <cutils/trace.h>
64
65 #define _MESA_TRACE_BEGIN(name) \
66 atrace_begin(ATRACE_TAG_GRAPHICS, name)
67 #define _MESA_TRACE_END() atrace_end(ATRACE_TAG_GRAPHICS)
68 #define _MESA_TRACE_FLOW_BEGIN(name, id) \
69 atrace_begin(ATRACE_TAG_GRAPHICS, name)
70 #define _MESA_TRACE_SET_COUNTER(name, value)
71 #define _MESA_TRACE_TIMESTAMP_BEGIN(name, track_id, flow_id, clock, timestamp)
72 #define _MESA_TRACE_TIMESTAMP_END(name, track_id, clock, timestamp)
73
74 #elif DETECT_OS_OHOS
75 #include <hitrace_meter/hitrace_meter_c.h>
76 #include "sys_param.h"
77 #ifndef HITRACE_TAG_GRAPHIC_AGP
78 #define HITRACE_TAG_GRAPHIC_AGP (1ULL << 38) // Graphic module tag
79 #endif
80 #define _MESA_TRACE_BEGIN(name) \
81 do { \
82 if (open_mesa3d_trace) { \
83 HiTraceStartTrace(HITRACE_TAG_GRAPHIC_AGP, name); \
84 } \
85 } while (0)
86 #define _MESA_TRACE_END() \
87 do { \
88 if (open_mesa3d_trace) { \
89 HiTraceFinishTrace(HITRACE_TAG_GRAPHIC_AGP); \
90 } \
91 } while (0)
92 #define _MESA_TRACE_FLOW_BEGIN(name, id) \
93 do { \
94 if (open_mesa3d_trace) { \
95 HiTraceStartTrace(HITRACE_TAG_GRAPHIC_AGP, name); \
96 } \
97 } while (0)
98 #define _MESA_TRACE_SET_COUNTER(name, value)
99 #define _MESA_TRACE_TIMESTAMP_BEGIN(name, track_id, flow_id, clock, timestamp)
100 #define _MESA_TRACE_TIMESTAMP_END(name, track_id, clock, timestamp)
101
102 #else
103
104 #define _MESA_TRACE_BEGIN(name)
105 #define _MESA_TRACE_END()
106 #define _MESA_TRACE_FLOW_BEGIN(name, id)
107 #define _MESA_TRACE_SET_COUNTER(name, value)
108 #define _MESA_TRACE_TIMESTAMP_BEGIN(name, track_id, flow_id, clock, timestamp)
109 #define _MESA_TRACE_TIMESTAMP_END(name, track_id, clock, timestamp)
110
111 #endif /* HAVE_PERFETTO */
112
113 #if defined(HAVE_GPUVIS)
114
115 #define _MESA_GPUVIS_TRACE_BEGIN(name) util_gpuvis_begin(name)
116 #define _MESA_GPUVIS_TRACE_END() util_gpuvis_end()
117
118 #else
119
120 #define _MESA_GPUVIS_TRACE_BEGIN(name)
121 #define _MESA_GPUVIS_TRACE_END()
122
123 #endif /* HAVE_GPUVIS */
124
125 #if __has_attribute(cleanup) && __has_attribute(unused)
126
127 #define _MESA_TRACE_SCOPE_VAR_CONCAT(name, suffix) name##suffix
128 #define _MESA_TRACE_SCOPE_VAR(suffix) \
129 _MESA_TRACE_SCOPE_VAR_CONCAT(_mesa_trace_scope_, suffix)
130
131 /* This must expand to a single non-scoped statement for
132 *
133 * if (cond)
134 * _MESA_TRACE_SCOPE(...)
135 *
136 * to work.
137 */
138 #define _MESA_TRACE_SCOPE(name) \
139 int _MESA_TRACE_SCOPE_VAR(__LINE__) \
140 __attribute__((cleanup(_mesa_trace_scope_end), unused)) = \
141 _mesa_trace_scope_begin(name)
142
143 #define _MESA_TRACE_SCOPE_FLOW(name, id) \
144 int _MESA_TRACE_SCOPE_VAR(__LINE__) \
145 __attribute__((cleanup(_mesa_trace_scope_end), unused)) = \
146 _mesa_trace_scope_flow_begin(name, id)
147
148 static inline int
_mesa_trace_scope_begin(const char * name)149 _mesa_trace_scope_begin(const char *name)
150 {
151 _MESA_TRACE_BEGIN(name);
152 _MESA_GPUVIS_TRACE_BEGIN(name);
153 return 0;
154 }
155
156 static inline int
_mesa_trace_scope_flow_begin(const char * name,uint64_t * id)157 _mesa_trace_scope_flow_begin(const char *name, uint64_t *id)
158 {
159 if (*id == 0)
160 *id = util_perfetto_next_id();
161 _MESA_TRACE_FLOW_BEGIN(name, *id);
162 _MESA_GPUVIS_TRACE_BEGIN(name);
163 return 0;
164 }
165
166 static inline void
_mesa_trace_scope_end(UNUSED int * scope)167 _mesa_trace_scope_end(UNUSED int *scope)
168 {
169 _MESA_GPUVIS_TRACE_END();
170 _MESA_TRACE_END();
171 }
172
173 #else
174
175 #define _MESA_TRACE_SCOPE(name)
176
177 #endif /* __has_attribute(cleanup) && __has_attribute(unused) */
178
179 #define MESA_TRACE_SCOPE(name) _MESA_TRACE_SCOPE(name)
180 #define MESA_TRACE_SCOPE_FLOW(name, id) _MESA_TRACE_SCOPE_FLOW(name, id)
181 #define MESA_TRACE_FUNC() _MESA_TRACE_SCOPE(__func__)
182 #define MESA_TRACE_FUNC_FLOW(id) _MESA_TRACE_SCOPE_FLOW(__func__, id)
183 #define MESA_TRACE_SET_COUNTER(name, value) _MESA_TRACE_SET_COUNTER(name, value)
184 #define MESA_TRACE_TIMESTAMP_BEGIN(name, track_id, flow_id, clock, timestamp) \
185 _MESA_TRACE_TIMESTAMP_BEGIN(name, track_id, flow_id, clock, timestamp)
186 #define MESA_TRACE_TIMESTAMP_END(name, track_id, clock, timestamp) \
187 _MESA_TRACE_TIMESTAMP_END(name, track_id, clock, timestamp)
188
189 static inline void
util_cpu_trace_init()190 util_cpu_trace_init()
191 {
192 util_perfetto_init();
193 util_gpuvis_init();
194 }
195
196 #endif /* CPU_TRACE_H */
197