xref: /aosp_15_r20/external/mesa3d/src/gallium/drivers/radeonsi/si_query.h (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Copyright 2015 Advanced Micro Devices, Inc.
3  *
4  * SPDX-License-Identifier: MIT
5  */
6 
7 #ifndef SI_QUERY_H
8 #define SI_QUERY_H
9 
10 #include "util/u_threaded_context.h"
11 
12 #include "ac_perfcounter.h"
13 
14 struct pipe_context;
15 struct pipe_query;
16 struct pipe_resource;
17 
18 struct si_screen;
19 struct si_context;
20 struct si_query;
21 struct si_query_buffer;
22 struct si_query_hw;
23 struct si_resource;
24 
25 #define SI_MAX_STREAMS 4
26 
27 enum
28 {
29    SI_QUERY_DRAW_CALLS = PIPE_QUERY_DRIVER_SPECIFIC,
30    SI_QUERY_DECOMPRESS_CALLS,
31    SI_QUERY_COMPUTE_CALLS,
32    SI_QUERY_CP_DMA_CALLS,
33    SI_QUERY_NUM_VS_FLUSHES,
34    SI_QUERY_NUM_PS_FLUSHES,
35    SI_QUERY_NUM_CS_FLUSHES,
36    SI_QUERY_NUM_CB_CACHE_FLUSHES,
37    SI_QUERY_NUM_DB_CACHE_FLUSHES,
38    SI_QUERY_NUM_L2_INVALIDATES,
39    SI_QUERY_NUM_L2_WRITEBACKS,
40    SI_QUERY_NUM_RESIDENT_HANDLES,
41    SI_QUERY_TC_OFFLOADED_SLOTS,
42    SI_QUERY_TC_DIRECT_SLOTS,
43    SI_QUERY_TC_NUM_SYNCS,
44    SI_QUERY_CS_THREAD_BUSY,
45    SI_QUERY_GALLIUM_THREAD_BUSY,
46    SI_QUERY_REQUESTED_VRAM,
47    SI_QUERY_REQUESTED_GTT,
48    SI_QUERY_MAPPED_VRAM,
49    SI_QUERY_MAPPED_GTT,
50    SI_QUERY_SLAB_WASTED_VRAM,
51    SI_QUERY_SLAB_WASTED_GTT,
52    SI_QUERY_BUFFER_WAIT_TIME,
53    SI_QUERY_NUM_MAPPED_BUFFERS,
54    SI_QUERY_NUM_GFX_IBS,
55    SI_QUERY_GFX_BO_LIST_SIZE,
56    SI_QUERY_GFX_IB_SIZE,
57    SI_QUERY_NUM_BYTES_MOVED,
58    SI_QUERY_NUM_EVICTIONS,
59    SI_QUERY_NUM_VRAM_CPU_PAGE_FAULTS,
60    SI_QUERY_VRAM_USAGE,
61    SI_QUERY_VRAM_VIS_USAGE,
62    SI_QUERY_GTT_USAGE,
63    SI_QUERY_GPU_TEMPERATURE,
64    SI_QUERY_CURRENT_GPU_SCLK,
65    SI_QUERY_CURRENT_GPU_MCLK,
66    SI_QUERY_GPU_LOAD,
67    SI_QUERY_GPU_SHADERS_BUSY,
68    SI_QUERY_GPU_TA_BUSY,
69    SI_QUERY_GPU_GDS_BUSY,
70    SI_QUERY_GPU_VGT_BUSY,
71    SI_QUERY_GPU_IA_BUSY,
72    SI_QUERY_GPU_SX_BUSY,
73    SI_QUERY_GPU_WD_BUSY,
74    SI_QUERY_GPU_BCI_BUSY,
75    SI_QUERY_GPU_SC_BUSY,
76    SI_QUERY_GPU_PA_BUSY,
77    SI_QUERY_GPU_DB_BUSY,
78    SI_QUERY_GPU_CP_BUSY,
79    SI_QUERY_GPU_CB_BUSY,
80    SI_QUERY_GPU_SDMA_BUSY,
81    SI_QUERY_GPU_PFP_BUSY,
82    SI_QUERY_GPU_MEQ_BUSY,
83    SI_QUERY_GPU_ME_BUSY,
84    SI_QUERY_GPU_SURF_SYNC_BUSY,
85    SI_QUERY_GPU_CP_DMA_BUSY,
86    SI_QUERY_GPU_SCRATCH_RAM_BUSY,
87    SI_QUERY_NUM_COMPILATIONS,
88    SI_QUERY_NUM_SHADERS_CREATED,
89    SI_QUERY_BACK_BUFFER_PS_DRAW_RATIO,
90    SI_QUERY_GPIN_ASIC_ID,
91    SI_QUERY_GPIN_NUM_SIMD,
92    SI_QUERY_GPIN_NUM_RB,
93    SI_QUERY_GPIN_NUM_SPI,
94    SI_QUERY_GPIN_NUM_SE,
95    SI_QUERY_LIVE_SHADER_CACHE_HITS,
96    SI_QUERY_LIVE_SHADER_CACHE_MISSES,
97    SI_QUERY_MEMORY_SHADER_CACHE_HITS,
98    SI_QUERY_MEMORY_SHADER_CACHE_MISSES,
99    SI_QUERY_DISK_SHADER_CACHE_HITS,
100    SI_QUERY_DISK_SHADER_CACHE_MISSES,
101 
102    SI_QUERY_FIRST_PERFCOUNTER = PIPE_QUERY_DRIVER_SPECIFIC + 100,
103 };
104 
105 enum
106 {
107    SI_QUERY_GROUP_GPIN = 0,
108    SI_NUM_SW_QUERY_GROUPS
109 };
110 
111 struct si_query_ops {
112    void (*destroy)(struct si_context *, struct si_query *);
113    bool (*begin)(struct si_context *, struct si_query *);
114    bool (*end)(struct si_context *, struct si_query *);
115    bool (*get_result)(struct si_context *, struct si_query *, bool wait,
116                       union pipe_query_result *result);
117    void (*get_result_resource)(struct si_context *, struct si_query *,
118                                enum pipe_query_flags flags,
119                                enum pipe_query_value_type result_type, int index,
120                                struct pipe_resource *resource, unsigned offset);
121 
122    void (*suspend)(struct si_context *, struct si_query *);
123    void (*resume)(struct si_context *, struct si_query *);
124 };
125 
126 struct si_query {
127    struct threaded_query b;
128    const struct si_query_ops *ops;
129 
130    /* The PIPE_QUERY_xxx type of query */
131    unsigned type;
132 
133    /* The number of dwords for suspend. */
134    unsigned num_cs_dw_suspend;
135 
136    /* Linked list of queries that must be suspended at end of CS. */
137    struct list_head active_list;
138 };
139 
140 enum
141 {
142    SI_QUERY_HW_FLAG_NO_START = (1 << 0),
143    /* gap */
144    /* whether begin_query doesn't clear the result */
145    SI_QUERY_HW_FLAG_BEGIN_RESUMES = (1 << 2),
146    /* whether GS invocations and emitted primitives counters are emulated
147     * using atomic adds.
148     */
149    SI_QUERY_EMULATE_GS_COUNTERS = (1 << 3),
150 };
151 
152 struct si_query_buffer {
153    /* The buffer where query results are stored. */
154    struct si_resource *buf;
155    /* If a query buffer is full, a new buffer is created and the old one
156     * is put in here. When we calculate the result, we sum up the samples
157     * from all buffers. */
158    struct si_query_buffer *previous;
159    /* Offset of the next free result after current query data */
160    unsigned results_end;
161 };
162 
163 void si_query_buffer_destroy(struct si_screen *sctx, struct si_query_buffer *buffer);
164 void si_query_buffer_reset(struct si_context *sctx, struct si_query_buffer *buffer);
165 bool si_query_buffer_alloc(struct si_context *sctx, struct si_query_buffer *buffer,
166                            bool (*prepare_buffer)(struct si_context *, struct si_query_buffer *),
167                            unsigned size);
168 
169 struct si_query_hw {
170    struct si_query b;
171    unsigned flags;
172 
173    /* The query buffer and how many results are in it. */
174    struct si_query_buffer buffer;
175    /* Size of the result in memory for both begin_query and end_query,
176     * this can be one or two numbers, or it could even be a size of a structure. */
177    unsigned result_size;
178    union {
179       /* For transform feedback: which stream the query is for */
180       unsigned stream;
181       /* For pipeline stats: which counter is active */
182       unsigned index;
183    };
184 
185    /* Workaround via compute shader */
186    struct si_resource *workaround_buf;
187    unsigned workaround_offset;
188 };
189 
190 unsigned si_query_pipestat_end_dw_offset(struct si_screen *sscreen,
191                                          enum pipe_statistics_query_index index);
192 
193 /* Shader-based queries */
194 
195 /**
196  * The query buffer is written to by ESGS NGG shaders with statistics about
197  * generated and (streamout-)emitted primitives.
198  *
199  * The context maintains a ring of these query buffers, and queries simply
200  * point into the ring, allowing an arbitrary number of queries to be active
201  * without additional GPU cost.
202  */
203 struct gfx11_sh_query_buffer {
204    struct list_head list;
205    struct si_resource *buf;
206    unsigned refcount;
207 
208    /* Offset into the buffer in bytes; points at the first un-emitted entry. */
209    unsigned head;
210 };
211 
212 /* Memory layout of the query buffer. Must be kept in sync with shaders
213  * (including QBO shaders) and should be aligned to cachelines.
214  *
215  * The somewhat awkward memory layout is for compatibility with the
216  * SET_PREDICATION packet, which also means that we're setting the high bit
217  * of all those values unconditionally.
218  */
219 struct gfx11_sh_query_buffer_mem {
220    struct {
221       uint64_t generated_primitives_start_dummy;
222       uint64_t emitted_primitives_start_dummy;
223       uint64_t generated_primitives;
224       uint64_t emitted_primitives;
225    } stream[4];
226    uint32_t fence; /* bottom-of-pipe fence: set to ~0 when draws have finished */
227    uint32_t pad[31];
228 };
229 
230 struct gfx11_sh_query {
231    struct si_query b;
232 
233    struct gfx11_sh_query_buffer *first;
234    struct gfx11_sh_query_buffer *last;
235    unsigned first_begin;
236    unsigned last_end;
237 
238    unsigned stream;
239 };
240 
241 struct pipe_query *gfx11_sh_query_create(struct si_screen *screen, enum pipe_query_type query_type,
242                                          unsigned index);
243 
244 /* Performance counters */
245 struct si_perfcounters {
246    struct ac_perfcounters base;
247 
248    unsigned num_stop_cs_dwords;
249    unsigned num_instance_cs_dwords;
250 };
251 
252 struct pipe_query *si_create_batch_query(struct pipe_context *ctx, unsigned num_queries,
253                                          unsigned *query_types);
254 
255 int si_get_perfcounter_info(struct si_screen *, unsigned index,
256                             struct pipe_driver_query_info *info);
257 int si_get_perfcounter_group_info(struct si_screen *, unsigned index,
258                                   struct pipe_driver_query_group_info *info);
259 
260 struct si_qbo_state {
261    struct pipe_constant_buffer saved_const0;
262 };
263 
264 #endif /* SI_QUERY_H */
265