Debian Codegraph @

src/kosmickrisp/vulkan/kk_query_table.c

raw symbols SCIP
1 /*
2 * Copyright © 2022 Collabora Ltd. and Red Hat Inc.
3 * Copyright 2025 LunarG, Inc.
4 * Copyright 2025 Google LLC
5 * SPDX-License-Identifier: MIT
6 */
7
8 #include "kk_query_table.h"
9
10 #include "kk_device.h"
11 #include "kk_physical_device.h"
12
13 static uint32_t query_size = sizeof(uint64_t);
14
15 static VkResult
16 kk_query_table_grow_locked(struct kk_device *dev, struct kk_query_table *table,
17 uint32_t new_alloc)
18 {
19 struct kk_bo *bo;
20 BITSET_WORD *new_in_use;
21 uint32_t *new_free_table;
22 VkResult result;
23
24 assert(new_alloc <= table->max_alloc);
25
26 const uint32_t new_mem_size = new_alloc * query_size;
27 result = kk_alloc_bo(dev, &dev->vk.base, new_mem_size, 256, &bo);
28 if (result != VK_SUCCESS)
29 return result;
30
31 /* We don't allow resize */
32 assert(table->bo == NULL);
33 table->bo = bo;
34
35 assert((new_alloc % BITSET_WORDBITS) == 0);
36 const size_t new_in_use_size = BITSET_BYTES(new_alloc);
37 new_in_use =
38 vk_realloc(&dev->vk.alloc, table->in_use, new_in_use_size,
39 sizeof(BITSET_WORD), VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
40 if (new_in_use == NULL) {
41 return vk_errorf(dev, VK_ERROR_OUT_OF_HOST_MEMORY,
42 "Failed to allocate query in-use set");
43 }
44 memset((char *)new_in_use, 0, new_in_use_size);
45 table->in_use = new_in_use;
46
47 const size_t new_free_table_size = new_alloc * sizeof(uint32_t);
48 new_free_table =
49 vk_realloc(&dev->vk.alloc, table->free_table, new_free_table_size, 4,
50 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
51 if (new_free_table == NULL) {
52 return vk_errorf(dev, VK_ERROR_OUT_OF_HOST_MEMORY,
53 "Failed to allocate query free table");
54 }
55 table->free_table = new_free_table;
56
57 return VK_SUCCESS;
58 }
59
60 VkResult
61 kk_query_table_init(struct kk_device *dev, struct kk_query_table *table,
62 uint32_t query_count)
63 {
64 memset(table, 0, sizeof(*table));
65 VkResult result;
66
67 simple_mtx_init(&table->mutex, mtx_plain);
68
69 assert(util_is_power_of_two_nonzero(query_count));
70
71 table->max_alloc = query_count;
72 table->next_query = 0;
73 table->free_count = 0;
74
75 result = kk_query_table_grow_locked(dev, table, query_count);
76 if (result != VK_SUCCESS) {
77 kk_query_table_finish(dev, table);
78 return result;
79 }
80
81 return VK_SUCCESS;
82 }
83
84 void
85 kk_query_table_finish(struct kk_device *dev, struct kk_query_table *table)
86 {
87 if (table->bo != NULL)
88 kk_destroy_bo(dev, table->bo);
89 vk_free(&dev->vk.alloc, table->in_use);
90 vk_free(&dev->vk.alloc, table->free_table);
91 simple_mtx_destroy(&table->mutex);
92 }
93
94 static VkResult
95 kk_query_table_alloc_locked(struct kk_device *dev, struct kk_query_table *table,
96 uint32_t *index_out)
97 {
98 while (1) {
99 uint32_t index;
100 if (table->free_count > 0) {
101 index = table->free_table[--table->free_count];
102 } else if (table->next_query < table->max_alloc) {
103 index = table->next_query++;
104 } else {
105 return vk_errorf(dev, VK_ERROR_OUT_OF_HOST_MEMORY,
106 "Query table not large enough");
107 }
108
109 if (!BITSET_TEST(table->in_use, index)) {
110 BITSET_SET(table->in_use, index);
111 *index_out = index;
112 return VK_SUCCESS;
113 }
114 }
115 }
116
117 static VkResult
118 kk_query_table_take_locked(struct kk_device *dev, struct kk_query_table *table,
119 uint32_t index)
120 {
121 if (index >= table->max_alloc) {
122 return vk_errorf(dev, VK_ERROR_INVALID_OPAQUE_CAPTURE_ADDRESS,
123 "Query %u does not exist", index);
124 }
125
126 if (BITSET_TEST(table->in_use, index)) {
127 return vk_errorf(dev, VK_ERROR_INVALID_OPAQUE_CAPTURE_ADDRESS,
128 "Query %u is already in use", index);
129 } else {
130 BITSET_SET(table->in_use, index);
131 return VK_SUCCESS;
132 }
133 }
134
135 static VkResult
136 kk_query_table_add_locked(struct kk_device *dev, struct kk_query_table *table,
137 uint64_t value, uint32_t *index_out)
138 {
139 VkResult result = kk_query_table_alloc_locked(dev, table, index_out);
140 if (result != VK_SUCCESS)
141 return result;
142
143 uint64_t *map = (uint64_t *)table->bo->cpu + *index_out;
144 *map = value;
145
146 return VK_SUCCESS;
147 }
148
149 VkResult
150 kk_query_table_add(struct kk_device *dev, struct kk_query_table *table,
151 uint64_t value, uint32_t *index_out)
152 {
153 simple_mtx_lock(&table->mutex);
154 VkResult result = kk_query_table_add_locked(dev, table, value, index_out);
155 simple_mtx_unlock(&table->mutex);
156
157 return result;
158 }
159
160 static VkResult
161 kk_query_table_insert_locked(struct kk_device *dev,
162 struct kk_query_table *table, uint32_t index,
163 uint64_t value)
164 {
165 VkResult result = kk_query_table_take_locked(dev, table, index);
166 if (result != VK_SUCCESS)
167 return result;
168
169 uint64_t *map = (uint64_t *)table->bo->cpu + index;
170 *map = value;
171
172 return result;
173 }
174
175 VkResult
176 kk_query_table_insert(struct kk_device *dev, struct kk_query_table *table,
177 uint32_t index, uint64_t value)
178 {
179 simple_mtx_lock(&table->mutex);
180 VkResult result = kk_query_table_insert_locked(dev, table, index, value);
181 simple_mtx_unlock(&table->mutex);
182
183 return result;
184 }
185
186 static int
187 compar_u32(const void *_a, const void *_b)
188 {
189 const uint32_t *a = _a, *b = _b;
190 return *a - *b;
191 }
192
193 static void
194 kk_query_table_compact_free_table(struct kk_query_table *table)
195 {
196 if (table->free_count <= 1)
197 return;
198
199 qsort(table->free_table, table->free_count, sizeof(*table->free_table),
200 compar_u32);
201
202 uint32_t j = 1;
203 for (uint32_t i = 1; i < table->free_count; i++) {
204 if (table->free_table[i] == table->free_table[j - 1])
205 continue;
206
207 assert(table->free_table[i] > table->free_table[j - 1]);
208 table->free_table[j++] = table->free_table[i];
209 }
210
211 table->free_count = j;
212 }
213
214 void
215 kk_query_table_remove(struct kk_device *dev, struct kk_query_table *table,
216 uint32_t index)
217 {
218 simple_mtx_lock(&table->mutex);
219
220 uint64_t *map = (uint64_t *)table->bo->cpu + index;
221 *map = 0u;
222
223 assert(BITSET_TEST(table->in_use, index));
224
225 /* There may be duplicate entries in the free table. For most operations,
226 * this is fine as we always consult kk_query_table::in_use when
227 * allocating. However, it does mean that there's nothing preventing our
228 * free table from growing larger than the memory we allocated for it. In
229 * the unlikely event that we end up with more entries than we can fit in
230 * the allocated space, compact the table to ensure that the new entry
231 * we're about to add fits.
232 */
233 if (table->free_count >= table->max_alloc)
234 kk_query_table_compact_free_table(table);
235 assert(table->free_count < table->max_alloc);
236
237 BITSET_CLEAR(table->in_use, index);
238 table->free_table[table->free_count++] = index;
239
240 simple_mtx_unlock(&table->mutex);
241 }