gallium: Add a helper for implementing PIPE_CAP_* default values.
[mesa.git] / src / gallium / drivers / radeonsi / si_test_dma_perf.c
1 /*
2 * Copyright 2018 Advanced Micro Devices, Inc.
3 * All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 *
24 */
25
26 /* This file implements tests on the si_clearbuffer function. */
27
28 #include "si_pipe.h"
29 #include "si_query.h"
30
31 #define MIN_SIZE 512
32 #define MAX_SIZE (128 * 1024 * 1024)
33 #define SIZE_SHIFT 1
34 #define NUM_RUNS 128
35
36 static double get_MBps_rate(unsigned num_bytes, unsigned ns)
37 {
38 return (num_bytes / (1024.0 * 1024.0)) / (ns / 1000000000.0);
39 }
40
41 void si_test_dma_perf(struct si_screen *sscreen)
42 {
43 struct pipe_screen *screen = &sscreen->b;
44 struct pipe_context *ctx = screen->context_create(screen, NULL, 0);
45 struct si_context *sctx = (struct si_context*)ctx;
46 const uint32_t clear_value = 0x12345678;
47 static const unsigned cs_dwords_per_thread_list[] = {64, 32, 16, 8, 4, 2, 1};
48 static const unsigned cs_waves_per_sh_list[] = {1, 2, 4, 8, 16, 0};
49
50 #define NUM_SHADERS ARRAY_SIZE(cs_dwords_per_thread_list)
51 #define NUM_METHODS (4 + 2*NUM_SHADERS * ARRAY_SIZE(cs_waves_per_sh_list))
52
53 static const char *method_str[] = {
54 "CP MC ",
55 "CP L2 ",
56 "CP L2 ",
57 "SDMA ",
58 };
59 static const char *placement_str[] = {
60 /* Clear */
61 "fill->VRAM",
62 "fill->GTT ",
63 /* Copy */
64 "VRAM->VRAM",
65 "VRAM->GTT ",
66 "GTT ->VRAM",
67 };
68
69 printf("DMA rate is in MB/s for each size. Slow cases are skipped and print 0.\n");
70 printf("Heap ,Method ,L2p,Wa,");
71 for (unsigned size = MIN_SIZE; size <= MAX_SIZE; size <<= SIZE_SHIFT) {
72 if (size >= 1024)
73 printf("%6uKB,", size / 1024);
74 else
75 printf(" %6uB,", size);
76 }
77 printf("\n");
78
79 /* results[log2(size)][placement][method][] */
80 struct si_result {
81 bool is_valid;
82 bool is_cp;
83 bool is_sdma;
84 bool is_cs;
85 unsigned cache_policy;
86 unsigned dwords_per_thread;
87 unsigned waves_per_sh;
88 unsigned score;
89 unsigned index; /* index in results[x][y][index] */
90 } results[32][ARRAY_SIZE(placement_str)][NUM_METHODS] = {};
91
92 /* Run benchmarks. */
93 for (unsigned placement = 0; placement < ARRAY_SIZE(placement_str); placement++) {
94 bool is_copy = placement >= 2;
95
96 printf("-----------,--------,---,--,");
97 for (unsigned size = MIN_SIZE; size <= MAX_SIZE; size <<= SIZE_SHIFT)
98 printf("--------,");
99 printf("\n");
100
101 for (unsigned method = 0; method < NUM_METHODS; method++) {
102 bool test_cp = method <= 2;
103 bool test_sdma = method == 3;
104 bool test_cs = method >= 4;
105 unsigned cs_method = method - 4;
106 STATIC_ASSERT(L2_STREAM + 1 == L2_LRU);
107 unsigned cs_waves_per_sh =
108 test_cs ? cs_waves_per_sh_list[cs_method / (2*NUM_SHADERS)] : 0;
109 cs_method %= 2*NUM_SHADERS;
110 unsigned cache_policy = test_cp ? method % 3 :
111 test_cs ? L2_STREAM + (cs_method / NUM_SHADERS) : 0;
112 unsigned cs_dwords_per_thread =
113 test_cs ? cs_dwords_per_thread_list[cs_method % NUM_SHADERS] : 0;
114
115 if (sctx->chip_class == SI) {
116 /* SI doesn't support CP DMA operations through L2. */
117 if (test_cp && cache_policy != L2_BYPASS)
118 continue;
119 /* WAVES_PER_SH is in multiples of 16 on SI. */
120 if (test_cs && cs_waves_per_sh % 16 != 0)
121 continue;
122 }
123
124 printf("%s ,", placement_str[placement]);
125 if (test_cs) {
126 printf("CS x%-4u,%3s,", cs_dwords_per_thread,
127 cache_policy == L2_LRU ? "LRU" :
128 cache_policy == L2_STREAM ? "Str" : "");
129 } else {
130 printf("%s,%3s,", method_str[method],
131 method == L2_LRU ? "LRU" :
132 method == L2_STREAM ? "Str" : "");
133 }
134 if (test_cs && cs_waves_per_sh)
135 printf("%2u,", cs_waves_per_sh);
136 else
137 printf(" ,");
138
139 double score = 0;
140 for (unsigned size = MIN_SIZE; size <= MAX_SIZE; size <<= SIZE_SHIFT) {
141 /* Don't test bigger sizes if it's too slow. Print 0. */
142 if (size >= 512*1024 &&
143 score < 400 * (size / (4*1024*1024))) {
144 printf("%7.0f ,", 0.0);
145 continue;
146 }
147
148 enum pipe_resource_usage dst_usage, src_usage;
149 struct pipe_resource *dst, *src;
150 struct pipe_query *q[NUM_RUNS];
151 unsigned query_type = PIPE_QUERY_TIME_ELAPSED;
152
153 if (test_sdma) {
154 if (sctx->chip_class == SI)
155 query_type = SI_QUERY_TIME_ELAPSED_SDMA_SI;
156 else
157 query_type = SI_QUERY_TIME_ELAPSED_SDMA;
158 }
159
160 if (placement == 0 || placement == 2 || placement == 4)
161 dst_usage = PIPE_USAGE_DEFAULT;
162 else
163 dst_usage = PIPE_USAGE_STREAM;
164
165 if (placement == 2 || placement == 3)
166 src_usage = PIPE_USAGE_DEFAULT;
167 else
168 src_usage = PIPE_USAGE_STREAM;
169
170 dst = pipe_buffer_create(screen, 0, dst_usage, size);
171 src = is_copy ? pipe_buffer_create(screen, 0, src_usage, size) : NULL;
172
173 /* Run tests. */
174 for (unsigned iter = 0; iter < NUM_RUNS; iter++) {
175 q[iter] = ctx->create_query(ctx, query_type, 0);
176 ctx->begin_query(ctx, q[iter]);
177
178 if (test_cp) {
179 /* CP DMA */
180 if (is_copy) {
181 si_copy_buffer(sctx, dst, src, 0, 0, size, 0,
182 cache_policy);
183 } else {
184 si_cp_dma_clear_buffer(sctx, dst, 0, size, clear_value,
185 SI_COHERENCY_NONE, cache_policy);
186 }
187 } else if (test_sdma) {
188 /* SDMA */
189 if (is_copy) {
190 struct pipe_box box;
191 u_box_1d(0, size, &box);
192 sctx->dma_copy(ctx, dst, 0, 0, 0, 0, src, 0, &box);
193 } else {
194 sctx->dma_clear_buffer(sctx, dst, 0, size, clear_value);
195 }
196 } else {
197 /* Compute */
198 /* The memory accesses are coalesced, meaning that the 1st instruction writes
199 * the 1st contiguous block of data for the whole wave, the 2nd instruction
200 * writes the 2nd contiguous block of data, etc.
201 */
202 unsigned instructions_per_thread = MAX2(1, cs_dwords_per_thread / 4);
203 unsigned dwords_per_instruction = cs_dwords_per_thread / instructions_per_thread;
204 unsigned dwords_per_wave = cs_dwords_per_thread * 64;
205
206 unsigned num_dwords = size / 4;
207 unsigned num_instructions = DIV_ROUND_UP(num_dwords, dwords_per_instruction);
208
209 void *cs = si_create_dma_compute_shader(ctx, cs_dwords_per_thread,
210 cache_policy == L2_STREAM, is_copy);
211
212 struct pipe_grid_info info = {};
213 info.block[0] = MIN2(64, num_instructions);
214 info.block[1] = 1;
215 info.block[2] = 1;
216 info.grid[0] = DIV_ROUND_UP(num_dwords, dwords_per_wave);
217 info.grid[1] = 1;
218 info.grid[2] = 1;
219
220 struct pipe_shader_buffer sb[2] = {};
221 sb[0].buffer = dst;
222 sb[0].buffer_size = size;
223
224 if (is_copy) {
225 sb[1].buffer = src;
226 sb[1].buffer_size = size;
227 } else {
228 for (unsigned i = 0; i < 4; i++)
229 sctx->cs_user_data[i] = clear_value;
230 }
231
232 sctx->flags |= SI_CONTEXT_INV_VMEM_L1 |
233 SI_CONTEXT_INV_SMEM_L1;
234
235 ctx->set_shader_buffers(ctx, PIPE_SHADER_COMPUTE, 0, is_copy ? 2 : 1, sb);
236 ctx->bind_compute_state(ctx, cs);
237 sctx->cs_max_waves_per_sh = cs_waves_per_sh;
238
239 ctx->launch_grid(ctx, &info);
240
241 ctx->bind_compute_state(ctx, NULL);
242 ctx->delete_compute_state(ctx, cs);
243 sctx->cs_max_waves_per_sh = 0; /* disable the limit */
244
245 sctx->flags |= SI_CONTEXT_CS_PARTIAL_FLUSH;
246 }
247
248 /* Flush L2, so that we don't just test L2 cache performance. */
249 if (!test_sdma) {
250 sctx->flags |= SI_CONTEXT_WRITEBACK_GLOBAL_L2;
251 si_emit_cache_flush(sctx);
252 }
253
254 ctx->end_query(ctx, q[iter]);
255 ctx->flush(ctx, NULL, PIPE_FLUSH_ASYNC);
256 }
257 pipe_resource_reference(&dst, NULL);
258 pipe_resource_reference(&src, NULL);
259
260 /* Get results. */
261 uint64_t min = ~0ull, max = 0, total = 0;
262
263 for (unsigned iter = 0; iter < NUM_RUNS; iter++) {
264 union pipe_query_result result;
265
266 ctx->get_query_result(ctx, q[iter], true, &result);
267 ctx->destroy_query(ctx, q[iter]);
268
269 min = MIN2(min, result.u64);
270 max = MAX2(max, result.u64);
271 total += result.u64;
272 }
273
274 score = get_MBps_rate(size, total / (double)NUM_RUNS);
275 printf("%7.0f ,", score);
276 fflush(stdout);
277
278 struct si_result *r = &results[util_logbase2(size)][placement][method];
279 r->is_valid = true;
280 r->is_cp = test_cp;
281 r->is_sdma = test_sdma;
282 r->is_cs = test_cs;
283 r->cache_policy = cache_policy;
284 r->dwords_per_thread = cs_dwords_per_thread;
285 r->waves_per_sh = cs_waves_per_sh;
286 r->score = score;
287 r->index = method;
288 }
289 puts("");
290 }
291 }
292
293 puts("");
294 puts("static struct si_method");
295 printf("get_best_clear_for_%s(enum radeon_bo_domain dst, uint64_t size64, bool async, bool cached)\n",
296 sctx->screen->info.name);
297 puts("{");
298 puts(" unsigned size = MIN2(size64, UINT_MAX);\n");
299
300 /* Analyze results and find the best methods. */
301 for (unsigned placement = 0; placement < ARRAY_SIZE(placement_str); placement++) {
302 if (placement == 0)
303 puts(" if (dst == RADEON_DOMAIN_VRAM) {");
304 else if (placement == 1)
305 puts(" } else { /* GTT */");
306 else if (placement == 2) {
307 puts("}");
308 puts("");
309 puts("static struct si_method");
310 printf("get_best_copy_for_%s(enum radeon_bo_domain dst, enum radeon_bo_domain src,\n",
311 sctx->screen->info.name);
312 printf(" uint64_t size64, bool async, bool cached)\n");
313 puts("{");
314 puts(" unsigned size = MIN2(size64, UINT_MAX);\n");
315 puts(" if (src == RADEON_DOMAIN_VRAM && dst == RADEON_DOMAIN_VRAM) {");
316 } else if (placement == 3)
317 puts(" } else if (src == RADEON_DOMAIN_VRAM && dst == RADEON_DOMAIN_GTT) {");
318 else
319 puts(" } else { /* GTT -> VRAM */");
320
321 for (unsigned mode = 0; mode < 3; mode++) {
322 bool async = mode == 0;
323 bool cached = mode == 1;
324
325 if (async)
326 puts(" if (async) { /* SDMA or async compute */");
327 else if (cached)
328 puts(" if (cached) { /* gfx ring */");
329 else
330 puts(" } else { /* gfx ring - uncached */");
331
332 /* The list of best chosen methods. */
333 struct si_result *methods[32];
334 unsigned method_max_size[32];
335 unsigned num_methods = 0;
336
337 for (unsigned size = MIN_SIZE; size <= MAX_SIZE; size <<= SIZE_SHIFT) {
338 /* Find the best method. */
339 struct si_result *best = NULL;
340
341 for (unsigned i = 0; i < NUM_METHODS; i++) {
342 struct si_result *r = &results[util_logbase2(size)][placement][i];
343
344 if (!r->is_valid)
345 continue;
346
347 /* Ban CP DMA clears via MC on <= VI. They are super slow
348 * on GTT, which we can get due to BO evictions.
349 */
350 if (sctx->chip_class <= VI && placement == 1 &&
351 r->is_cp && r->cache_policy == L2_BYPASS)
352 continue;
353
354 if (async) {
355 /* The following constraints for compute IBs try to limit
356 * resource usage so as not to decrease the performance
357 * of gfx IBs too much.
358 */
359
360 /* Don't use CP DMA on asynchronous rings, because
361 * the engine is shared with gfx IBs.
362 */
363 if (r->is_cp)
364 continue;
365
366 /* Don't use L2 caching on asynchronous rings to minimize
367 * L2 usage.
368 */
369 if (r->cache_policy == L2_LRU)
370 continue;
371
372 /* Asynchronous compute recommends waves_per_sh != 0
373 * to limit CU usage. */
374 if (r->is_cs && r->waves_per_sh == 0)
375 continue;
376 } else {
377 /* SDMA is always asynchronous */
378 if (r->is_sdma)
379 continue;
380
381 if (cached && r->cache_policy == L2_BYPASS)
382 continue;
383 if (!cached && r->cache_policy == L2_LRU)
384 continue;
385 }
386
387 if (!best) {
388 best = r;
389 continue;
390 }
391
392 /* Assume some measurement error. Earlier methods occupy fewer
393 * resources, so the next method is always more greedy, and we
394 * don't want to select it due to a measurement error.
395 */
396 double min_improvement = 1.03;
397
398 if (best->score * min_improvement < r->score)
399 best = r;
400 }
401
402 if (num_methods > 0) {
403 unsigned prev_index = num_methods - 1;
404 struct si_result *prev = methods[prev_index];
405 struct si_result *prev_this_size = &results[util_logbase2(size)][placement][prev->index];
406
407 /* If the best one is also the best for the previous size,
408 * just bump the size for the previous one.
409 *
410 * If there is no best, it means all methods were too slow
411 * for this size and were not tested. Use the best one for
412 * the previous size.
413 */
414 if (!best ||
415 /* If it's the same method as for the previous size: */
416 (prev->is_cp == best->is_cp &&
417 prev->is_sdma == best->is_sdma &&
418 prev->is_cs == best->is_cs &&
419 prev->cache_policy == best->cache_policy &&
420 prev->dwords_per_thread == best->dwords_per_thread &&
421 prev->waves_per_sh == best->waves_per_sh) ||
422 /* If the method for the previous size is also the best
423 * for this size: */
424 (prev_this_size->is_valid &&
425 prev_this_size->score * 1.03 > best->score)) {
426 method_max_size[prev_index] = size;
427 continue;
428 }
429 }
430
431 /* Add it to the list. */
432 assert(num_methods < ARRAY_SIZE(methods));
433 methods[num_methods] = best;
434 method_max_size[num_methods] = size;
435 num_methods++;
436 }
437
438 for (unsigned i = 0; i < num_methods; i++) {
439 struct si_result *best = methods[i];
440 unsigned size = method_max_size[i];
441
442 /* The size threshold is between the current benchmarked
443 * size and the next benchmarked size. */
444 if (i < num_methods - 1)
445 printf(" if (size <= %9u) ", (size + (size << SIZE_SHIFT)) / 2);
446 else if (i > 0)
447 printf(" else ");
448 else
449 printf(" ");
450 printf("return ");
451
452 assert(best);
453 if (best->is_cp) {
454 printf("CP_DMA(%s);\n",
455 best->cache_policy == L2_BYPASS ? "L2_BYPASS" :
456 best->cache_policy == L2_LRU ? "L2_LRU " : "L2_STREAM");
457 }
458 if (best->is_sdma)
459 printf("SDMA;\n");
460 if (best->is_cs) {
461 printf("COMPUTE(%s, %u, %u);\n",
462 best->cache_policy == L2_LRU ? "L2_LRU " : "L2_STREAM",
463 best->dwords_per_thread,
464 best->waves_per_sh);
465 }
466 }
467 }
468 puts(" }");
469 }
470 puts(" }");
471 puts("}");
472
473 ctx->destroy(ctx);
474 exit(0);
475 }