• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_hw_ops {
153    bool (*prepare_buffer)(struct si_context *, struct si_query_buffer *);
154    void (*emit_start)(struct si_context *, struct si_query_hw *, struct si_resource *buffer,
155                       uint64_t va);
156    void (*emit_stop)(struct si_context *, struct si_query_hw *, struct si_resource *buffer,
157                      uint64_t va);
158    void (*clear_result)(struct si_query_hw *, union pipe_query_result *);
159    void (*add_result)(struct si_screen *screen, struct si_query_hw *, void *buffer,
160                       union pipe_query_result *result);
161 };
162 
163 struct si_query_buffer {
164    /* The buffer where query results are stored. */
165    struct si_resource *buf;
166    /* If a query buffer is full, a new buffer is created and the old one
167     * is put in here. When we calculate the result, we sum up the samples
168     * from all buffers. */
169    struct si_query_buffer *previous;
170    /* Offset of the next free result after current query data */
171    unsigned results_end;
172    bool unprepared;
173 };
174 
175 void si_query_buffer_destroy(struct si_screen *sctx, struct si_query_buffer *buffer);
176 void si_query_buffer_reset(struct si_context *sctx, struct si_query_buffer *buffer);
177 bool si_query_buffer_alloc(struct si_context *sctx, struct si_query_buffer *buffer,
178                            bool (*prepare_buffer)(struct si_context *, struct si_query_buffer *),
179                            unsigned size);
180 
181 struct si_query_hw {
182    struct si_query b;
183    const struct si_query_hw_ops *ops;
184    unsigned flags;
185 
186    /* The query buffer and how many results are in it. */
187    struct si_query_buffer buffer;
188    /* Size of the result in memory for both begin_query and end_query,
189     * this can be one or two numbers, or it could even be a size of a structure. */
190    unsigned result_size;
191    union {
192       /* For transform feedback: which stream the query is for */
193       unsigned stream;
194       /* For pipeline stats: which counter is active */
195       unsigned index;
196    };
197 
198    /* Workaround via compute shader */
199    struct si_resource *workaround_buf;
200    unsigned workaround_offset;
201 };
202 
203 unsigned si_query_pipestat_end_dw_offset(struct si_screen *sscreen,
204                                          enum pipe_statistics_query_index index);
205 
206 /* Shader-based queries */
207 
208 /**
209  * The query buffer is written to by ESGS NGG shaders with statistics about
210  * generated and (streamout-)emitted primitives.
211  *
212  * The context maintains a ring of these query buffers, and queries simply
213  * point into the ring, allowing an arbitrary number of queries to be active
214  * without additional GPU cost.
215  */
216 struct gfx11_sh_query_buffer {
217    struct list_head list;
218    struct si_resource *buf;
219    unsigned refcount;
220 
221    /* Offset into the buffer in bytes; points at the first un-emitted entry. */
222    unsigned head;
223 };
224 
225 /* Memory layout of the query buffer. Must be kept in sync with shaders
226  * (including QBO shaders) and should be aligned to cachelines.
227  *
228  * The somewhat awkward memory layout is for compatibility with the
229  * SET_PREDICATION packet, which also means that we're setting the high bit
230  * of all those values unconditionally.
231  */
232 struct gfx11_sh_query_buffer_mem {
233    struct {
234       uint64_t generated_primitives_start_dummy;
235       uint64_t emitted_primitives_start_dummy;
236       uint64_t generated_primitives;
237       uint64_t emitted_primitives;
238    } stream[4];
239    uint32_t fence; /* bottom-of-pipe fence: set to ~0 when draws have finished */
240    uint32_t pad[31];
241 };
242 
243 struct gfx11_sh_query {
244    struct si_query b;
245 
246    struct gfx11_sh_query_buffer *first;
247    struct gfx11_sh_query_buffer *last;
248    unsigned first_begin;
249    unsigned last_end;
250 
251    unsigned stream;
252 };
253 
254 struct pipe_query *gfx11_sh_query_create(struct si_screen *screen, enum pipe_query_type query_type,
255                                          unsigned index);
256 
257 /* Performance counters */
258 struct si_perfcounters {
259    struct ac_perfcounters base;
260 
261    unsigned num_stop_cs_dwords;
262    unsigned num_instance_cs_dwords;
263 };
264 
265 struct pipe_query *si_create_batch_query(struct pipe_context *ctx, unsigned num_queries,
266                                          unsigned *query_types);
267 
268 int si_get_perfcounter_info(struct si_screen *, unsigned index,
269                             struct pipe_driver_query_info *info);
270 int si_get_perfcounter_group_info(struct si_screen *, unsigned index,
271                                   struct pipe_driver_query_group_info *info);
272 
273 struct si_qbo_state {
274    struct pipe_constant_buffer saved_const0;
275 };
276 
277 #endif /* SI_QUERY_H */
278