1 /*
2 * Copyright © 2023 Raspberry Pi Ltd
3 *
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:
10 *
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
13 * Software.
14 *
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
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 #include "v3dv_private.h"
25
26 #include "common/v3d_performance_counters.h"
27
28 VkResult
v3dX(enumerate_performance_query_counters)29 v3dX(enumerate_performance_query_counters)(struct v3dv_physical_device *pDevice,
30 uint32_t *pCounterCount,
31 VkPerformanceCounterKHR *pCounters,
32 VkPerformanceCounterDescriptionKHR *pCounterDescriptions)
33 {
34 struct v3d_device_info *devinfo = &pDevice->devinfo;
35 uint32_t desc_count = *pCounterCount;
36 uint8_t ncounters = devinfo->max_perfcnt ? devinfo->max_perfcnt
37 : ARRAY_SIZE(v3d_performance_counters);
38
39 VK_OUTARRAY_MAKE_TYPED(VkPerformanceCounterKHR,
40 out, pCounters, pCounterCount);
41 VK_OUTARRAY_MAKE_TYPED(VkPerformanceCounterDescriptionKHR,
42 out_desc, pCounterDescriptions, &desc_count);
43
44 for (int i = 0; i < ncounters; i++) {
45 struct drm_v3d_perfmon_get_counter counter = {
46 .counter = i,
47 };
48 const char *name, *category, *description;
49
50 if (devinfo->max_perfcnt) {
51 int ret = v3dv_ioctl(pDevice->render_fd, DRM_IOCTL_V3D_PERFMON_GET_COUNTER,
52 &counter);
53 if (ret) {
54 fprintf(stderr, "Failed to get counter description for counter %d: %s\n",
55 i, strerror(errno));
56 }
57
58 name = (char *) counter.name;
59 category = (char *) counter.category;
60 description = (char *) counter.description;
61 } else {
62 /* Legacy path for kernels without support for DRM_IOCTL_V3D_PERFMON_GET_COUNTER */
63 name = v3d_performance_counters[i][V3D_PERFCNT_NAME];
64 category = v3d_performance_counters[i][V3D_PERFCNT_CATEGORY];
65 description = v3d_performance_counters[i][V3D_PERFCNT_DESCRIPTION];
66 }
67
68 vk_outarray_append_typed(VkPerformanceCounterKHR, &out, counter) {
69 counter->unit = VK_PERFORMANCE_COUNTER_UNIT_GENERIC_KHR;
70 counter->scope = VK_PERFORMANCE_COUNTER_SCOPE_COMMAND_KHR;
71 counter->storage = VK_PERFORMANCE_COUNTER_STORAGE_UINT64_KHR;
72
73 unsigned char sha1_result[20];
74 _mesa_sha1_compute(name, strlen(name), sha1_result);
75
76 memcpy(counter->uuid, sha1_result, sizeof(counter->uuid));
77 }
78
79 vk_outarray_append_typed(VkPerformanceCounterDescriptionKHR,
80 &out_desc, desc) {
81 desc->flags = 0;
82 snprintf(desc->name, sizeof(desc->name), "%s", name);
83 snprintf(desc->category, sizeof(desc->category), "%s", category);
84 snprintf(desc->description, sizeof(desc->description), "%s", description);
85 }
86 }
87
88 return vk_outarray_status(&out);
89 }
90