1 /*
2 * Copyright © 2024 Raspberry Pi Ltd
3 * SPDX-License-Identifier: MIT
4 */
5
6 #include <assert.h>
7 #include "common/v3d_device_info.h"
8 #include "common/v3d_util.h"
9 #include "util/ralloc.h"
10 #include "v3d_perfcntrs.h"
11
12 #define v3dX(x) v3d42_##x
13 #include "v3dx_counter.h"
14 #undef v3dX
15
16 #define v3dX(x) v3d71_##x
17 #include "v3dx_counter.h"
18 #undef v3dX
19
20 struct v3d_perfcntrs *
v3d_perfcntrs_init(const struct v3d_device_info * devinfo,int fd)21 v3d_perfcntrs_init(const struct v3d_device_info *devinfo, int fd)
22 {
23 struct v3d_perfcntrs *perfcounters;
24
25 if (!devinfo)
26 return NULL;
27
28 perfcounters = rzalloc(NULL, struct v3d_perfcntrs);
29 if (!perfcounters)
30 return NULL;
31
32 perfcounters->name_table = _mesa_hash_table_create(NULL, _mesa_hash_string, _mesa_key_string_equal);
33 if (!perfcounters->name_table) {
34 v3d_perfcntrs_fini(perfcounters);
35 return NULL;
36 }
37
38 perfcounters->fd = fd;
39 perfcounters->devinfo = devinfo;
40
41 perfcounters->max_perfcnt = v3d_X(perfcounters->devinfo, perfcounters_num)(perfcounters->devinfo);
42 assert(perfcounters->max_perfcnt);
43
44 perfcounters->perfcnt = rzalloc_array(perfcounters, struct v3d_perfcntr_desc *, perfcounters->max_perfcnt);
45 if (!perfcounters->perfcnt) {
46 fprintf(stderr, "Error allocating performance counters names");
47 v3d_perfcntrs_fini(perfcounters);
48 return NULL;
49 }
50
51 /* pre-fill our array and hash_table */
52 for (unsigned i = 0; i < perfcounters->max_perfcnt; i++) {
53 struct v3d_perfcntr_desc *desc = v3d_X(perfcounters->devinfo, perfcounters_get)(perfcounters, i);
54
55 _mesa_hash_table_insert(perfcounters->name_table, desc->name, desc);
56 }
57
58 return perfcounters;
59 }
60
61 void
v3d_perfcntrs_fini(struct v3d_perfcntrs * perfcounters)62 v3d_perfcntrs_fini(struct v3d_perfcntrs *perfcounters)
63 {
64 if (!perfcounters)
65 return;
66
67 _mesa_hash_table_destroy(perfcounters->name_table, NULL);
68 ralloc_free(perfcounters);
69 }
70