radeonsi: move VS_STATE.LS_OUT_PATCH_SIZE a few bits higher to make space there
[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 (test_sdma && !sctx->sdma_cs)
116 continue;
117
118 if (sctx->chip_class == GFX6) {
119 /* GFX6 doesn't support CP DMA operations through L2. */
120 if (test_cp && cache_policy != L2_BYPASS)
121 continue;
122 /* WAVES_PER_SH is in multiples of 16 on GFX6. */
123 if (test_cs && cs_waves_per_sh % 16 != 0)
124 continue;
125 }
126
127 printf("%s ,", placement_str[placement]);
128 if (test_cs) {
129 printf("CS x%-4u,%3s,", cs_dwords_per_thread,
130 cache_policy == L2_LRU ? "LRU" :
131 cache_policy == L2_STREAM ? "Str" : "");
132 } else {
133 printf("%s,%3s,", method_str[method],
134 method == L2_LRU ? "LRU" :
135 method == L2_STREAM ? "Str" : "");
136 }
137 if (test_cs && cs_waves_per_sh)
138 printf("%2u,", cs_waves_per_sh);
139 else
140 printf(" ,");
141
142 double score = 0;
143 for (unsigned size = MIN_SIZE; size <= MAX_SIZE; size <<= SIZE_SHIFT) {
144 /* Don't test bigger sizes if it's too slow. Print 0. */
145 if (size >= 512*1024 &&
146 score < 400 * (size / (4*1024*1024))) {
147 printf("%7.0f ,", 0.0);
148 continue;
149 }
150
151 enum pipe_resource_usage dst_usage, src_usage;
152 struct pipe_resource *dst, *src;
153 struct pipe_query *q[NUM_RUNS];
154 unsigned query_type = PIPE_QUERY_TIME_ELAPSED;
155
156 if (test_sdma) {
157 if (sctx->chip_class == GFX6)
158 query_type = SI_QUERY_TIME_ELAPSED_SDMA_SI;
159 else
160 query_type = SI_QUERY_TIME_ELAPSED_SDMA;
161 }
162
163 if (placement == 0 || placement == 2 || placement == 4)
164 dst_usage = PIPE_USAGE_DEFAULT;
165 else
166 dst_usage = PIPE_USAGE_STREAM;
167
168 if (placement == 2 || placement == 3)
169 src_usage = PIPE_USAGE_DEFAULT;
170 else
171 src_usage = PIPE_USAGE_STREAM;
172
173 dst = pipe_buffer_create(screen, 0, dst_usage, size);
174 src = is_copy ? pipe_buffer_create(screen, 0, src_usage, size) : NULL;
175
176 /* Run tests. */
177 for (unsigned iter = 0; iter < NUM_RUNS; iter++) {
178 q[iter] = ctx->create_query(ctx, query_type, 0);
179 ctx->begin_query(ctx, q[iter]);
180
181 if (test_cp) {
182 /* CP DMA */
183 if (is_copy) {
184 si_cp_dma_copy_buffer(sctx, dst, src, 0, 0, size, 0,
185 SI_COHERENCY_NONE, cache_policy);
186 } else {
187 si_cp_dma_clear_buffer(sctx, sctx->gfx_cs, dst, 0, size,
188 clear_value, 0,
189 SI_COHERENCY_NONE, cache_policy);
190 }
191 } else if (test_sdma) {
192 /* SDMA */
193 if (is_copy) {
194 si_sdma_copy_buffer(sctx, dst, src, 0, 0, size);
195 } else {
196 si_sdma_clear_buffer(sctx, dst, 0, size, clear_value);
197 }
198 } else {
199 /* Compute */
200 /* The memory accesses are coalesced, meaning that the 1st instruction writes
201 * the 1st contiguous block of data for the whole wave, the 2nd instruction
202 * writes the 2nd contiguous block of data, etc.
203 */
204 unsigned instructions_per_thread = MAX2(1, cs_dwords_per_thread / 4);
205 unsigned dwords_per_instruction = cs_dwords_per_thread / instructions_per_thread;
206 unsigned dwords_per_wave = cs_dwords_per_thread * 64;
207
208 unsigned num_dwords = size / 4;
209 unsigned num_instructions = DIV_ROUND_UP(num_dwords, dwords_per_instruction);
210
211 void *cs = si_create_dma_compute_shader(ctx, cs_dwords_per_thread,
212 cache_policy == L2_STREAM, is_copy);
213
214 struct pipe_grid_info info = {};
215 info.block[0] = MIN2(64, num_instructions);
216 info.block[1] = 1;
217 info.block[2] = 1;
218 info.grid[0] = DIV_ROUND_UP(num_dwords, dwords_per_wave);
219 info.grid[1] = 1;
220 info.grid[2] = 1;
221
222 struct pipe_shader_buffer sb[2] = {};
223 sb[0].buffer = dst;
224 sb[0].buffer_size = size;
225
226 if (is_copy) {
227 sb[1].buffer = src;
228 sb[1].buffer_size = size;
229 } else {
230 for (unsigned i = 0; i < 4; i++)
231 sctx->cs_user_data[i] = clear_value;
232 }
233
234 sctx->flags |= SI_CONTEXT_INV_VCACHE |
235 SI_CONTEXT_INV_SCACHE;
236
237 ctx->set_shader_buffers(ctx, PIPE_SHADER_COMPUTE, 0,
238 is_copy ? 2 : 1, sb, 0x1);
239 ctx->bind_compute_state(ctx, cs);
240 sctx->cs_max_waves_per_sh = cs_waves_per_sh;
241
242 ctx->launch_grid(ctx, &info);
243
244 ctx->bind_compute_state(ctx, NULL);
245 ctx->delete_compute_state(ctx, cs);
246 sctx->cs_max_waves_per_sh = 0; /* disable the limit */
247
248 sctx->flags |= SI_CONTEXT_CS_PARTIAL_FLUSH;
249 }
250
251 /* Flush L2, so that we don't just test L2 cache performance. */
252 if (!test_sdma) {
253 sctx->flags |= SI_CONTEXT_WB_L2;
254 sctx->emit_cache_flush(sctx);
255 }
256
257 ctx->end_query(ctx, q[iter]);
258 ctx->flush(ctx, NULL, PIPE_FLUSH_ASYNC);
259 }
260 pipe_resource_reference(&dst, NULL);
261 pipe_resource_reference(&src, NULL);
262
263 /* Get results. */
264 uint64_t min = ~0ull, max = 0, total = 0;
265
266 for (unsigned iter = 0; iter < NUM_RUNS; iter++) {
267 union pipe_query_result result;
268
269 ctx->get_query_result(ctx, q[iter], true, &result);
270 ctx->destroy_query(ctx, q[iter]);
271
272 min = MIN2(min, result.u64);
273 max = MAX2(max, result.u64);
274 total += result.u64;
275 }
276
277 score = get_MBps_rate(size, total / (double)NUM_RUNS);
278 printf("%7.0f ,", score);
279 fflush(stdout);
280
281 struct si_result *r = &results[util_logbase2(size)][placement][method];
282 r->is_valid = true;
283 r->is_cp = test_cp;
284 r->is_sdma = test_sdma;
285 r->is_cs = test_cs;
286 r->cache_policy = cache_policy;
287 r->dwords_per_thread = cs_dwords_per_thread;
288 r->waves_per_sh = cs_waves_per_sh;
289 r->score = score;
290 r->index = method;
291 }
292 puts("");
293 }
294 }
295
296 puts("");
297 puts("static struct si_method");
298 printf("get_best_clear_for_%s(enum radeon_bo_domain dst, uint64_t size64, bool async, bool cached)\n",
299 sctx->screen->info.name);
300 puts("{");
301 puts(" unsigned size = MIN2(size64, UINT_MAX);\n");
302
303 /* Analyze results and find the best methods. */
304 for (unsigned placement = 0; placement < ARRAY_SIZE(placement_str); placement++) {
305 if (placement == 0)
306 puts(" if (dst == RADEON_DOMAIN_VRAM) {");
307 else if (placement == 1)
308 puts(" } else { /* GTT */");
309 else if (placement == 2) {
310 puts("}");
311 puts("");
312 puts("static struct si_method");
313 printf("get_best_copy_for_%s(enum radeon_bo_domain dst, enum radeon_bo_domain src,\n",
314 sctx->screen->info.name);
315 printf(" uint64_t size64, bool async, bool cached)\n");
316 puts("{");
317 puts(" unsigned size = MIN2(size64, UINT_MAX);\n");
318 puts(" if (src == RADEON_DOMAIN_VRAM && dst == RADEON_DOMAIN_VRAM) {");
319 } else if (placement == 3)
320 puts(" } else if (src == RADEON_DOMAIN_VRAM && dst == RADEON_DOMAIN_GTT) {");
321 else
322 puts(" } else { /* GTT -> VRAM */");
323
324 for (unsigned mode = 0; mode < 3; mode++) {
325 bool async = mode == 0;
326 bool cached = mode == 1;
327
328 if (async)
329 puts(" if (async) { /* SDMA or async compute */");
330 else if (cached)
331 puts(" if (cached) { /* gfx ring */");
332 else
333 puts(" } else { /* gfx ring - uncached */");
334
335 /* The list of best chosen methods. */
336 struct si_result *methods[32];
337 unsigned method_max_size[32];
338 unsigned num_methods = 0;
339
340 for (unsigned size = MIN_SIZE; size <= MAX_SIZE; size <<= SIZE_SHIFT) {
341 /* Find the best method. */
342 struct si_result *best = NULL;
343
344 for (unsigned i = 0; i < NUM_METHODS; i++) {
345 struct si_result *r = &results[util_logbase2(size)][placement][i];
346
347 if (!r->is_valid)
348 continue;
349
350 /* Ban CP DMA clears via MC on <= GFX8. They are super slow
351 * on GTT, which we can get due to BO evictions.
352 */
353 if (sctx->chip_class <= GFX8 && placement == 1 &&
354 r->is_cp && r->cache_policy == L2_BYPASS)
355 continue;
356
357 if (async) {
358 /* The following constraints for compute IBs try to limit
359 * resource usage so as not to decrease the performance
360 * of gfx IBs too much.
361 */
362
363 /* Don't use CP DMA on asynchronous rings, because
364 * the engine is shared with gfx IBs.
365 */
366 if (r->is_cp)
367 continue;
368
369 /* Don't use L2 caching on asynchronous rings to minimize
370 * L2 usage.
371 */
372 if (r->cache_policy == L2_LRU)
373 continue;
374
375 /* Asynchronous compute recommends waves_per_sh != 0
376 * to limit CU usage. */
377 if (r->is_cs && r->waves_per_sh == 0)
378 continue;
379 } else {
380 /* SDMA is always asynchronous */
381 if (r->is_sdma)
382 continue;
383
384 if (cached && r->cache_policy == L2_BYPASS)
385 continue;
386 if (!cached && r->cache_policy == L2_LRU)
387 continue;
388 }
389
390 if (!best) {
391 best = r;
392 continue;
393 }
394
395 /* Assume some measurement error. Earlier methods occupy fewer
396 * resources, so the next method is always more greedy, and we
397 * don't want to select it due to a measurement error.
398 */
399 double min_improvement = 1.03;
400
401 if (best->score * min_improvement < r->score)
402 best = r;
403 }
404
405 if (num_methods > 0) {
406 unsigned prev_index = num_methods - 1;
407 struct si_result *prev = methods[prev_index];
408 struct si_result *prev_this_size = &results[util_logbase2(size)][placement][prev->index];
409
410 /* If the best one is also the best for the previous size,
411 * just bump the size for the previous one.
412 *
413 * If there is no best, it means all methods were too slow
414 * for this size and were not tested. Use the best one for
415 * the previous size.
416 */
417 if (!best ||
418 /* If it's the same method as for the previous size: */
419 (prev->is_cp == best->is_cp &&
420 prev->is_sdma == best->is_sdma &&
421 prev->is_cs == best->is_cs &&
422 prev->cache_policy == best->cache_policy &&
423 prev->dwords_per_thread == best->dwords_per_thread &&
424 prev->waves_per_sh == best->waves_per_sh) ||
425 /* If the method for the previous size is also the best
426 * for this size: */
427 (prev_this_size->is_valid &&
428 prev_this_size->score * 1.03 > best->score)) {
429 method_max_size[prev_index] = size;
430 continue;
431 }
432 }
433
434 /* Add it to the list. */
435 assert(num_methods < ARRAY_SIZE(methods));
436 methods[num_methods] = best;
437 method_max_size[num_methods] = size;
438 num_methods++;
439 }
440
441 for (unsigned i = 0; i < num_methods; i++) {
442 struct si_result *best = methods[i];
443 unsigned size = method_max_size[i];
444
445 /* The size threshold is between the current benchmarked
446 * size and the next benchmarked size. */
447 if (i < num_methods - 1)
448 printf(" if (size <= %9u) ", (size + (size << SIZE_SHIFT)) / 2);
449 else if (i > 0)
450 printf(" else ");
451 else
452 printf(" ");
453 printf("return ");
454
455 assert(best);
456 if (best->is_cp) {
457 printf("CP_DMA(%s);\n",
458 best->cache_policy == L2_BYPASS ? "L2_BYPASS" :
459 best->cache_policy == L2_LRU ? "L2_LRU " : "L2_STREAM");
460 }
461 if (best->is_sdma)
462 printf("SDMA;\n");
463 if (best->is_cs) {
464 printf("COMPUTE(%s, %u, %u);\n",
465 best->cache_policy == L2_LRU ? "L2_LRU " : "L2_STREAM",
466 best->dwords_per_thread,
467 best->waves_per_sh);
468 }
469 }
470 }
471 puts(" }");
472 }
473 puts(" }");
474 puts("}");
475
476 ctx->destroy(ctx);
477 exit(0);
478 }