2 * Copyright 2015 Advanced Micro Devices, Inc.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 * Nicolai Hähnle <nicolai.haehnle@amd.com>
28 #include "util/u_memory.h"
29 #include "r600_query.h"
30 #include "r600_pipe_common.h"
31 #include "r600d_common.h"
33 /* Max counters per HW block */
34 #define R600_QUERY_MAX_COUNTERS 16
36 static struct r600_perfcounter_block
*
37 lookup_counter(struct r600_perfcounters
*pc
, unsigned index
,
38 unsigned *base_gid
, unsigned *sub_index
)
40 struct r600_perfcounter_block
*block
= pc
->blocks
;
44 for (bid
= 0; bid
< pc
->num_blocks
; ++bid
, ++block
) {
45 unsigned total
= block
->num_groups
* block
->num_selectors
;
53 *base_gid
+= block
->num_groups
;
59 static struct r600_perfcounter_block
*
60 lookup_group(struct r600_perfcounters
*pc
, unsigned *index
)
63 struct r600_perfcounter_block
*block
= pc
->blocks
;
65 for (bid
= 0; bid
< pc
->num_blocks
; ++bid
, ++block
) {
66 if (*index
< block
->num_groups
)
68 *index
-= block
->num_groups
;
74 struct r600_pc_group
{
75 struct r600_pc_group
*next
;
76 struct r600_perfcounter_block
*block
;
77 unsigned sub_gid
; /* only used during init */
78 unsigned result_base
; /* only used during init */
81 unsigned num_counters
;
82 unsigned selectors
[R600_QUERY_MAX_COUNTERS
];
85 struct r600_pc_counter
{
91 #define R600_PC_SHADERS_WINDOWING (1 << 31)
93 struct r600_query_pc
{
94 struct r600_query_hw b
;
97 unsigned num_counters
;
98 struct r600_pc_counter
*counters
;
99 struct r600_pc_group
*groups
;
102 static void r600_pc_query_destroy(struct r600_common_context
*ctx
,
103 struct r600_query
*rquery
)
105 struct r600_query_pc
*query
= (struct r600_query_pc
*)rquery
;
107 while (query
->groups
) {
108 struct r600_pc_group
*group
= query
->groups
;
109 query
->groups
= group
->next
;
113 FREE(query
->counters
);
115 r600_query_hw_destroy(ctx
, rquery
);
118 static void r600_pc_query_emit_start(struct r600_common_context
*ctx
,
119 struct r600_query_hw
*hwquery
,
120 struct r600_resource
*buffer
, uint64_t va
)
122 struct r600_perfcounters
*pc
= ctx
->screen
->perfcounters
;
123 struct r600_query_pc
*query
= (struct r600_query_pc
*)hwquery
;
124 struct r600_pc_group
*group
;
126 int current_instance
= -1;
129 pc
->emit_shaders(ctx
, query
->shaders
);
131 for (group
= query
->groups
; group
; group
= group
->next
) {
132 struct r600_perfcounter_block
*block
= group
->block
;
134 if (group
->se
!= current_se
|| group
->instance
!= current_instance
) {
135 current_se
= group
->se
;
136 current_instance
= group
->instance
;
137 pc
->emit_instance(ctx
, group
->se
, group
->instance
);
140 pc
->emit_select(ctx
, block
, group
->num_counters
, group
->selectors
);
143 if (current_se
!= -1 || current_instance
!= -1)
144 pc
->emit_instance(ctx
, -1, -1);
146 pc
->emit_start(ctx
, buffer
, va
);
149 static void r600_pc_query_emit_stop(struct r600_common_context
*ctx
,
150 struct r600_query_hw
*hwquery
,
151 struct r600_resource
*buffer
, uint64_t va
)
153 struct r600_perfcounters
*pc
= ctx
->screen
->perfcounters
;
154 struct r600_query_pc
*query
= (struct r600_query_pc
*)hwquery
;
155 struct r600_pc_group
*group
;
157 pc
->emit_stop(ctx
, buffer
, va
);
159 for (group
= query
->groups
; group
; group
= group
->next
) {
160 struct r600_perfcounter_block
*block
= group
->block
;
161 unsigned se
= group
->se
>= 0 ? group
->se
: 0;
162 unsigned se_end
= se
+ 1;
164 if ((block
->flags
& R600_PC_BLOCK_SE
) && (group
->se
< 0))
165 se_end
= ctx
->screen
->info
.max_se
;
168 unsigned instance
= group
->instance
>= 0 ? group
->instance
: 0;
171 pc
->emit_instance(ctx
, se
, instance
);
172 pc
->emit_read(ctx
, block
,
173 group
->num_counters
, group
->selectors
,
175 va
+= 4 * group
->num_counters
;
176 } while (group
->instance
< 0 && ++instance
< block
->num_instances
);
177 } while (++se
< se_end
);
180 pc
->emit_instance(ctx
, -1, -1);
183 static void r600_pc_query_clear_result(struct r600_query_hw
*hwquery
,
184 union pipe_query_result
*result
)
186 struct r600_query_pc
*query
= (struct r600_query_pc
*)hwquery
;
188 memset(result
, 0, sizeof(result
->batch
[0]) * query
->num_counters
);
191 static void r600_pc_query_add_result(struct r600_common_context
*ctx
,
192 struct r600_query_hw
*hwquery
,
194 union pipe_query_result
*result
)
196 struct r600_query_pc
*query
= (struct r600_query_pc
*)hwquery
;
197 uint32_t *results
= buffer
;
200 for (i
= 0; i
< query
->num_counters
; ++i
) {
201 struct r600_pc_counter
*counter
= &query
->counters
[i
];
203 for (j
= 0; j
< counter
->dwords
; ++j
) {
204 uint32_t value
= results
[counter
->base
+ j
* counter
->stride
];
205 result
->batch
[i
].u32
+= value
;
210 static struct r600_query_ops batch_query_ops
= {
211 .destroy
= r600_pc_query_destroy
,
212 .begin
= r600_query_hw_begin
,
213 .end
= r600_query_hw_end
,
214 .get_result
= r600_query_hw_get_result
217 static struct r600_query_hw_ops batch_query_hw_ops
= {
218 .emit_start
= r600_pc_query_emit_start
,
219 .emit_stop
= r600_pc_query_emit_stop
,
220 .clear_result
= r600_pc_query_clear_result
,
221 .add_result
= r600_pc_query_add_result
,
224 static struct r600_pc_group
*get_group_state(struct r600_common_screen
*screen
,
225 struct r600_query_pc
*query
,
226 struct r600_perfcounter_block
*block
,
229 struct r600_pc_group
*group
= query
->groups
;
232 if (group
->block
== block
&& group
->sub_gid
== sub_gid
)
237 group
= CALLOC_STRUCT(r600_pc_group
);
241 group
->block
= block
;
242 group
->sub_gid
= sub_gid
;
244 if (block
->flags
& R600_PC_BLOCK_SHADER
) {
245 unsigned sub_gids
= block
->num_instances
;
248 unsigned query_shaders
;
250 if (block
->flags
& R600_PC_BLOCK_SE_GROUPS
)
251 sub_gids
= sub_gids
* screen
->info
.max_se
;
252 shader_id
= sub_gid
/ sub_gids
;
253 sub_gid
= sub_gid
% sub_gids
;
255 shaders
= screen
->perfcounters
->shader_type_bits
[shader_id
];
257 query_shaders
= query
->shaders
& ~R600_PC_SHADERS_WINDOWING
;
258 if (query_shaders
&& query_shaders
!= shaders
) {
259 fprintf(stderr
, "r600_perfcounter: incompatible shader groups\n");
263 query
->shaders
= shaders
;
266 if (block
->flags
& R600_PC_BLOCK_SHADER_WINDOWED
&& !query
->shaders
) {
267 // A non-zero value in query->shaders ensures that the shader
268 // masking is reset unless the user explicitly requests one.
269 query
->shaders
= R600_PC_SHADERS_WINDOWING
;
272 if (block
->flags
& R600_PC_BLOCK_SE_GROUPS
) {
273 group
->se
= sub_gid
/ block
->num_instances
;
274 sub_gid
= sub_gid
% block
->num_instances
;
279 if (block
->flags
& R600_PC_BLOCK_INSTANCE_GROUPS
) {
280 group
->instance
= sub_gid
;
282 group
->instance
= -1;
285 group
->next
= query
->groups
;
286 query
->groups
= group
;
291 struct pipe_query
*r600_create_batch_query(struct pipe_context
*ctx
,
292 unsigned num_queries
,
293 unsigned *query_types
)
295 struct r600_common_context
*rctx
= (struct r600_common_context
*)ctx
;
296 struct r600_common_screen
*screen
= rctx
->screen
;
297 struct r600_perfcounters
*pc
= screen
->perfcounters
;
298 struct r600_perfcounter_block
*block
;
299 struct r600_pc_group
*group
;
300 struct r600_query_pc
*query
;
301 unsigned base_gid
, sub_gid
, sub_index
;
307 query
= CALLOC_STRUCT(r600_query_pc
);
311 query
->b
.b
.ops
= &batch_query_ops
;
312 query
->b
.ops
= &batch_query_hw_ops
;
314 query
->num_counters
= num_queries
;
316 /* Collect selectors per group */
317 for (i
= 0; i
< num_queries
; ++i
) {
320 if (query_types
[i
] < R600_QUERY_FIRST_PERFCOUNTER
)
323 block
= lookup_counter(pc
, query_types
[i
] - R600_QUERY_FIRST_PERFCOUNTER
,
324 &base_gid
, &sub_index
);
328 sub_gid
= sub_index
/ block
->num_selectors
;
329 sub_index
= sub_index
% block
->num_selectors
;
331 group
= get_group_state(screen
, query
, block
, sub_gid
);
335 if (group
->num_counters
>= block
->num_counters
) {
337 "perfcounter group %s: too many selected\n",
341 group
->selectors
[group
->num_counters
] = sub_index
;
342 ++group
->num_counters
;
345 /* Compute result bases and CS size per group */
346 query
->b
.num_cs_dw_begin
= pc
->num_start_cs_dwords
;
347 query
->b
.num_cs_dw_end
= pc
->num_stop_cs_dwords
;
349 query
->b
.num_cs_dw_begin
+= pc
->num_instance_cs_dwords
; /* conservative */
350 query
->b
.num_cs_dw_end
+= pc
->num_instance_cs_dwords
;
353 for (group
= query
->groups
; group
; group
= group
->next
) {
354 struct r600_perfcounter_block
*block
= group
->block
;
355 unsigned select_dw
, read_dw
;
356 unsigned instances
= 1;
358 if ((block
->flags
& R600_PC_BLOCK_SE
) && group
->se
< 0)
359 instances
= rctx
->screen
->info
.max_se
;
360 if (group
->instance
< 0)
361 instances
*= block
->num_instances
;
363 group
->result_base
= i
;
364 query
->b
.result_size
+= 4 * instances
* group
->num_counters
;
365 i
+= instances
* group
->num_counters
;
367 pc
->get_size(block
, group
->num_counters
, group
->selectors
,
368 &select_dw
, &read_dw
);
369 query
->b
.num_cs_dw_begin
+= select_dw
;
370 query
->b
.num_cs_dw_end
+= instances
* read_dw
;
371 query
->b
.num_cs_dw_begin
+= pc
->num_instance_cs_dwords
; /* conservative */
372 query
->b
.num_cs_dw_end
+= instances
* pc
->num_instance_cs_dwords
;
375 if (query
->shaders
) {
376 if (query
->shaders
== R600_PC_SHADERS_WINDOWING
)
377 query
->shaders
= 0xffffffff;
378 query
->b
.num_cs_dw_begin
+= pc
->num_shaders_cs_dwords
;
381 /* Map user-supplied query array to result indices */
382 query
->counters
= CALLOC(num_queries
, sizeof(*query
->counters
));
383 for (i
= 0; i
< num_queries
; ++i
) {
384 struct r600_pc_counter
*counter
= &query
->counters
[i
];
385 struct r600_perfcounter_block
*block
;
387 block
= lookup_counter(pc
, query_types
[i
] - R600_QUERY_FIRST_PERFCOUNTER
,
388 &base_gid
, &sub_index
);
390 sub_gid
= sub_index
/ block
->num_selectors
;
391 sub_index
= sub_index
% block
->num_selectors
;
393 group
= get_group_state(screen
, query
, block
, sub_gid
);
394 assert(group
!= NULL
);
396 for (j
= 0; j
< group
->num_counters
; ++j
) {
397 if (group
->selectors
[j
] == sub_index
)
401 counter
->base
= group
->result_base
+ j
;
402 counter
->stride
= group
->num_counters
;
405 if ((block
->flags
& R600_PC_BLOCK_SE
) && group
->se
< 0)
406 counter
->dwords
= screen
->info
.max_se
;
407 if (group
->instance
< 0)
408 counter
->dwords
*= block
->num_instances
;
411 if (!r600_query_hw_init(rctx
, &query
->b
))
414 return (struct pipe_query
*)query
;
417 r600_pc_query_destroy(rctx
, &query
->b
.b
);
421 static boolean
r600_init_block_names(struct r600_common_screen
*screen
,
422 struct r600_perfcounter_block
*block
)
425 unsigned groups_shader
= 1, groups_se
= 1, groups_instance
= 1;
430 if (block
->flags
& R600_PC_BLOCK_INSTANCE_GROUPS
)
431 groups_instance
= block
->num_instances
;
432 if (block
->flags
& R600_PC_BLOCK_SE_GROUPS
)
433 groups_se
= screen
->info
.max_se
;
434 if (block
->flags
& R600_PC_BLOCK_SHADER
)
435 groups_shader
= screen
->perfcounters
->num_shader_types
;
437 namelen
= strlen(block
->basename
);
438 block
->group_name_stride
= namelen
+ 1;
439 if (block
->flags
& R600_PC_BLOCK_SHADER
)
440 block
->group_name_stride
+= 3;
441 if (block
->flags
& R600_PC_BLOCK_SE_GROUPS
) {
442 assert(groups_se
<= 10);
443 block
->group_name_stride
+= 1;
445 if (block
->flags
& R600_PC_BLOCK_INSTANCE_GROUPS
)
446 block
->group_name_stride
+= 1;
448 if (block
->flags
& R600_PC_BLOCK_INSTANCE_GROUPS
) {
449 assert(groups_instance
<= 100);
450 block
->group_name_stride
+= 2;
453 block
->group_names
= MALLOC(block
->num_groups
* block
->group_name_stride
);
454 if (!block
->group_names
)
457 groupname
= block
->group_names
;
458 for (i
= 0; i
< groups_shader
; ++i
) {
459 const char *shader_suffix
= screen
->perfcounters
->shader_type_suffixes
[i
];
460 unsigned shaderlen
= strlen(shader_suffix
);
461 for (j
= 0; j
< groups_se
; ++j
) {
462 for (k
= 0; k
< groups_instance
; ++k
) {
463 strcpy(groupname
, block
->basename
);
464 p
= groupname
+ namelen
;
466 if (block
->flags
& R600_PC_BLOCK_SHADER
) {
467 strcpy(p
, shader_suffix
);
471 if (block
->flags
& R600_PC_BLOCK_SE_GROUPS
) {
472 p
+= sprintf(p
, "%d", j
);
473 if (block
->flags
& R600_PC_BLOCK_INSTANCE_GROUPS
)
477 if (block
->flags
& R600_PC_BLOCK_INSTANCE_GROUPS
)
478 p
+= sprintf(p
, "%d", k
);
480 groupname
+= block
->group_name_stride
;
485 assert(block
->num_selectors
<= 1000);
486 block
->selector_name_stride
= block
->group_name_stride
+ 4;
487 block
->selector_names
= MALLOC(block
->num_groups
* block
->num_selectors
*
488 block
->selector_name_stride
);
489 if (!block
->selector_names
)
492 groupname
= block
->group_names
;
493 p
= block
->selector_names
;
494 for (i
= 0; i
< block
->num_groups
; ++i
) {
495 for (j
= 0; j
< block
->num_selectors
; ++j
) {
496 sprintf(p
, "%s_%03d", groupname
, j
);
497 p
+= block
->selector_name_stride
;
499 groupname
+= block
->group_name_stride
;
505 int r600_get_perfcounter_info(struct r600_common_screen
*screen
,
507 struct pipe_driver_query_info
*info
)
509 struct r600_perfcounters
*pc
= screen
->perfcounters
;
510 struct r600_perfcounter_block
*block
;
511 unsigned base_gid
, sub
;
517 unsigned bid
, num_queries
= 0;
519 for (bid
= 0; bid
< pc
->num_blocks
; ++bid
) {
520 num_queries
+= pc
->blocks
[bid
].num_selectors
*
521 pc
->blocks
[bid
].num_groups
;
527 block
= lookup_counter(pc
, index
, &base_gid
, &sub
);
531 if (!block
->selector_names
) {
532 if (!r600_init_block_names(screen
, block
))
535 info
->name
= block
->selector_names
+ sub
* block
->selector_name_stride
;
536 info
->query_type
= R600_QUERY_FIRST_PERFCOUNTER
+ index
;
537 info
->max_value
.u64
= 0;
538 info
->type
= PIPE_DRIVER_QUERY_TYPE_UINT
;
539 info
->result_type
= PIPE_DRIVER_QUERY_RESULT_TYPE_CUMULATIVE
;
540 info
->group_id
= base_gid
+ sub
/ block
->num_selectors
;
541 info
->flags
= PIPE_DRIVER_QUERY_FLAG_BATCH
;
542 if (sub
> 0 && sub
+ 1 < block
->num_selectors
* block
->num_groups
)
543 info
->flags
|= PIPE_DRIVER_QUERY_FLAG_DONT_LIST
;
547 int r600_get_perfcounter_group_info(struct r600_common_screen
*screen
,
549 struct pipe_driver_query_group_info
*info
)
551 struct r600_perfcounters
*pc
= screen
->perfcounters
;
552 struct r600_perfcounter_block
*block
;
558 return pc
->num_groups
;
560 block
= lookup_group(pc
, &index
);
564 if (!block
->group_names
) {
565 if (!r600_init_block_names(screen
, block
))
568 info
->name
= block
->group_names
+ index
* block
->group_name_stride
;
569 info
->num_queries
= block
->num_selectors
;
570 info
->max_active_queries
= block
->num_counters
;
574 void r600_perfcounters_destroy(struct r600_common_screen
*rscreen
)
576 if (rscreen
->perfcounters
)
577 rscreen
->perfcounters
->cleanup(rscreen
);
580 boolean
r600_perfcounters_init(struct r600_perfcounters
*pc
,
583 pc
->blocks
= CALLOC(num_blocks
, sizeof(struct r600_perfcounter_block
));
587 pc
->separate_se
= debug_get_bool_option("RADEON_PC_SEPARATE_SE", FALSE
);
588 pc
->separate_instance
= debug_get_bool_option("RADEON_PC_SEPARATE_INSTANCE", FALSE
);
593 void r600_perfcounters_add_block(struct r600_common_screen
*rscreen
,
594 struct r600_perfcounters
*pc
,
595 const char *name
, unsigned flags
,
596 unsigned counters
, unsigned selectors
,
597 unsigned instances
, void *data
)
599 struct r600_perfcounter_block
*block
= &pc
->blocks
[pc
->num_blocks
];
601 assert(counters
<= R600_QUERY_MAX_COUNTERS
);
603 block
->basename
= name
;
604 block
->flags
= flags
;
605 block
->num_counters
= counters
;
606 block
->num_selectors
= selectors
;
607 block
->num_instances
= MAX2(instances
, 1);
610 if (pc
->separate_se
&& (block
->flags
& R600_PC_BLOCK_SE
))
611 block
->flags
|= R600_PC_BLOCK_SE_GROUPS
;
612 if (pc
->separate_instance
&& block
->num_instances
> 1)
613 block
->flags
|= R600_PC_BLOCK_INSTANCE_GROUPS
;
615 if (block
->flags
& R600_PC_BLOCK_INSTANCE_GROUPS
) {
616 block
->num_groups
= block
->num_instances
;
618 block
->num_groups
= 1;
621 if (block
->flags
& R600_PC_BLOCK_SE_GROUPS
)
622 block
->num_groups
*= rscreen
->info
.max_se
;
623 if (block
->flags
& R600_PC_BLOCK_SHADER
)
624 block
->num_groups
*= pc
->num_shader_types
;
627 pc
->num_groups
+= block
->num_groups
;
630 void r600_perfcounters_do_destroy(struct r600_perfcounters
*pc
)
634 for (i
= 0; i
< pc
->num_blocks
; ++i
) {
635 FREE(pc
->blocks
[i
].group_names
);
636 FREE(pc
->blocks
[i
].selector_names
);