Repository navigation
Expand file tree
/
Copy pathradv_compute_queue_empty_probe_safe.c
More file actions
449 lines (378 loc) · 13.3 KB
/
Copy pathradv_compute_queue_empty_probe_safe.c
File metadata and controls
449 lines (378 loc) · 13.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
/*
* Xclipse 940 RADV - dedicated compute-queue empty-submit probe
*
* Tests:
* HAL -> Instance -> PhysicalDevice -> Device -> Queue
* -> CommandPool -> empty primary CommandBuffer
* -> vkQueueSubmit(empty CB, fence)
* -> vkWaitForFences with finite timeout
*
* Deliberately NOT done:
* - no shaders
* - no draw / dispatch
* - no images
* - no buffer access by GPU
* - no vkQueueWaitIdle
* - no vkDeviceWaitIdle
*
* Safety:
* - fence wait is finite (default 1 second)
* - whole risky section runs in a child process
* - parent watchdog defaults to 6 seconds
*
* NOTE: A userspace watchdog cannot guarantee recovery from a kernel/GPU
* lockup. This probe minimizes the submitted work but still exercises the
* real kernel command-submission path.
*/
#define _GNU_SOURCE
#include <dlfcn.h>
#include <errno.h>
#include <signal.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <time.h>
#include <unistd.h>
#include <hardware/hardware.h>
#include <hardware/hwvulkan.h>
#include <vulkan/vulkan.h>
static const char *
vk_result_name(VkResult r)
{
switch (r) {
case VK_SUCCESS: return "VK_SUCCESS";
case VK_NOT_READY: return "VK_NOT_READY";
case VK_TIMEOUT: return "VK_TIMEOUT";
case VK_EVENT_SET: return "VK_EVENT_SET";
case VK_EVENT_RESET: return "VK_EVENT_RESET";
case VK_INCOMPLETE: return "VK_INCOMPLETE";
case VK_ERROR_OUT_OF_HOST_MEMORY: return "VK_ERROR_OUT_OF_HOST_MEMORY";
case VK_ERROR_OUT_OF_DEVICE_MEMORY: return "VK_ERROR_OUT_OF_DEVICE_MEMORY";
case VK_ERROR_INITIALIZATION_FAILED: return "VK_ERROR_INITIALIZATION_FAILED";
case VK_ERROR_DEVICE_LOST: return "VK_ERROR_DEVICE_LOST";
case VK_ERROR_MEMORY_MAP_FAILED: return "VK_ERROR_MEMORY_MAP_FAILED";
case VK_ERROR_LAYER_NOT_PRESENT: return "VK_ERROR_LAYER_NOT_PRESENT";
case VK_ERROR_EXTENSION_NOT_PRESENT: return "VK_ERROR_EXTENSION_NOT_PRESENT";
case VK_ERROR_FEATURE_NOT_PRESENT: return "VK_ERROR_FEATURE_NOT_PRESENT";
case VK_ERROR_INCOMPATIBLE_DRIVER: return "VK_ERROR_INCOMPATIBLE_DRIVER";
case VK_ERROR_TOO_MANY_OBJECTS: return "VK_ERROR_TOO_MANY_OBJECTS";
case VK_ERROR_FORMAT_NOT_SUPPORTED: return "VK_ERROR_FORMAT_NOT_SUPPORTED";
default: return "VkResult(?)";
}
}
static int
run_child(const char *driver_path, uint64_t fence_timeout_ns)
{
setvbuf(stdout, NULL, _IONBF, 0);
setvbuf(stderr, NULL, _IONBF, 0);
printf("=== Xclipse 940 RADV dedicated compute-queue empty-submit probe ===\n");
printf("driver: %s\n", driver_path);
printf("GPU work: EMPTY COMMAND BUFFER ONLY on dedicated COMPUTE queue\n");
printf("fence timeout: %.3f s\n\n",
(double)fence_timeout_ns / 1000000000.0);
void *lib = dlopen(driver_path, RTLD_NOW | RTLD_LOCAL);
if (!lib) {
fprintf(stderr, "FAIL: dlopen: %s\n", dlerror());
return 10;
}
printf("[1/12] dlopen OK\n");
hwvulkan_module_t *module =
(hwvulkan_module_t *)dlsym(lib, HAL_MODULE_INFO_SYM_AS_STR);
if (!module) {
fprintf(stderr, "FAIL: dlsym(%s): %s\n",
HAL_MODULE_INFO_SYM_AS_STR, dlerror());
return 11;
}
hw_device_t *base_dev = NULL;
int hr = module->common.methods->open(&module->common,
HWVULKAN_DEVICE_0,
&base_dev);
if (hr != 0 || !base_dev) {
fprintf(stderr, "FAIL: HAL open = %d\n", hr);
return 12;
}
hwvulkan_device_t *hal = (hwvulkan_device_t *)base_dev;
printf("[2/12] HAL open OK\n");
VkApplicationInfo app = {
.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
.pApplicationName = "x940-empty-submit-probe",
.applicationVersion = 1,
.pEngineName = "none",
.engineVersion = 1,
.apiVersion = VK_API_VERSION_1_3,
};
VkInstanceCreateInfo ici = {
.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
.pApplicationInfo = &app,
};
VkInstance instance = VK_NULL_HANDLE;
VkResult vr = hal->CreateInstance(&ici, NULL, &instance);
printf("[3/12] vkCreateInstance = %d (%s)\n",
vr, vk_result_name(vr));
if (vr != VK_SUCCESS)
return 20;
#define GET_INST(name) \
PFN_##name name = (PFN_##name)hal->GetInstanceProcAddr(instance, #name); \
if (!(name)) { \
fprintf(stderr, "FAIL: GetInstanceProcAddr(%s)=NULL\n", #name); \
return 21; \
}
GET_INST(vkEnumeratePhysicalDevices);
GET_INST(vkGetPhysicalDeviceProperties);
GET_INST(vkGetPhysicalDeviceQueueFamilyProperties);
GET_INST(vkCreateDevice);
GET_INST(vkGetDeviceProcAddr);
uint32_t phys_count = 0;
vr = vkEnumeratePhysicalDevices(instance, &phys_count, NULL);
if (vr != VK_SUCCESS || phys_count == 0) {
fprintf(stderr, "FAIL: physical-device count=%u result=%d\n",
phys_count, vr);
return 22;
}
VkPhysicalDevice *phys = calloc(phys_count, sizeof(*phys));
if (!phys)
return 23;
vr = vkEnumeratePhysicalDevices(instance, &phys_count, phys);
if (vr != VK_SUCCESS || phys_count == 0)
return 24;
VkPhysicalDevice pdev = phys[0];
VkPhysicalDeviceProperties props;
memset(&props, 0, sizeof(props));
vkGetPhysicalDeviceProperties(pdev, &props);
printf("[4/12] GPU: %s, vendor=0x%04x device=0x%08x Vulkan=%u.%u.%u\n",
props.deviceName, props.vendorID, props.deviceID,
VK_API_VERSION_MAJOR(props.apiVersion),
VK_API_VERSION_MINOR(props.apiVersion),
VK_API_VERSION_PATCH(props.apiVersion));
uint32_t qf_count = 0;
vkGetPhysicalDeviceQueueFamilyProperties(pdev, &qf_count, NULL);
if (!qf_count)
return 25;
VkQueueFamilyProperties *qf = calloc(qf_count, sizeof(*qf));
if (!qf)
return 26;
vkGetPhysicalDeviceQueueFamilyProperties(pdev, &qf_count, qf);
uint32_t selected_qf = UINT32_MAX;
for (uint32_t i = 0; i < qf_count; i++) {
printf(" qf[%u]: queues=%u flags=0x%x\n",
i, qf[i].queueCount, qf[i].queueFlags);
/* Prefer a dedicated compute family: COMPUTE set, GRAPHICS clear. */
if (selected_qf == UINT32_MAX &&
qf[i].queueCount > 0 &&
(qf[i].queueFlags & VK_QUEUE_COMPUTE_BIT) &&
!(qf[i].queueFlags & VK_QUEUE_GRAPHICS_BIT))
selected_qf = i;
}
if (selected_qf == UINT32_MAX) {
fprintf(stderr, "FAIL: no dedicated compute queue family\n");
return 27;
}
printf(" selected dedicated compute queue family = %u\n", selected_qf);
float priority = 1.0f;
VkDeviceQueueCreateInfo qci = {
.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO,
.queueFamilyIndex = selected_qf,
.queueCount = 1,
.pQueuePriorities = &priority,
};
VkDeviceCreateInfo dci = {
.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
.queueCreateInfoCount = 1,
.pQueueCreateInfos = &qci,
.enabledExtensionCount = 0,
.pEnabledFeatures = NULL,
};
VkDevice device = VK_NULL_HANDLE;
vr = vkCreateDevice(pdev, &dci, NULL, &device);
printf("[5/12] vkCreateDevice = %d (%s), device=%p\n",
vr, vk_result_name(vr), (void *)device);
if (vr != VK_SUCCESS)
return 30;
#define GET_DEV(name) \
PFN_##name name = (PFN_##name)vkGetDeviceProcAddr(device, #name); \
if (!(name)) { \
fprintf(stderr, "FAIL: GetDeviceProcAddr(%s)=NULL\n", #name); \
return 31; \
}
GET_DEV(vkGetDeviceQueue);
GET_DEV(vkCreateCommandPool);
GET_DEV(vkDestroyCommandPool);
GET_DEV(vkAllocateCommandBuffers);
GET_DEV(vkBeginCommandBuffer);
GET_DEV(vkEndCommandBuffer);
GET_DEV(vkCreateFence);
GET_DEV(vkDestroyFence);
GET_DEV(vkQueueSubmit);
GET_DEV(vkWaitForFences);
GET_DEV(vkGetFenceStatus);
VkQueue queue = VK_NULL_HANDLE;
vkGetDeviceQueue(device, selected_qf, 0, &queue);
printf("[6/12] vkGetDeviceQueue = %p\n", (void *)queue);
if (!queue)
return 32;
VkCommandPoolCreateInfo cpci = {
.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
.flags = VK_COMMAND_POOL_CREATE_TRANSIENT_BIT,
.queueFamilyIndex = selected_qf,
};
VkCommandPool pool = VK_NULL_HANDLE;
vr = vkCreateCommandPool(device, &cpci, NULL, &pool);
printf("[7/12] vkCreateCommandPool = %d (%s), pool=%p\n",
vr, vk_result_name(vr), (void *)pool);
if (vr != VK_SUCCESS)
return 40;
VkCommandBufferAllocateInfo cbai = {
.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
.commandPool = pool,
.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
.commandBufferCount = 1,
};
VkCommandBuffer cb = VK_NULL_HANDLE;
vr = vkAllocateCommandBuffers(device, &cbai, &cb);
printf("[8/12] vkAllocateCommandBuffers = %d (%s), cb=%p\n",
vr, vk_result_name(vr), (void *)cb);
if (vr != VK_SUCCESS)
return 41;
VkCommandBufferBeginInfo cbbi = {
.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT,
};
vr = vkBeginCommandBuffer(cb, &cbbi);
printf("[9/12] vkBeginCommandBuffer = %d (%s)\n",
vr, vk_result_name(vr));
if (vr != VK_SUCCESS)
return 42;
/* Intentionally record zero commands. */
vr = vkEndCommandBuffer(cb);
printf("[10/12] vkEndCommandBuffer = %d (%s)\n",
vr, vk_result_name(vr));
if (vr != VK_SUCCESS)
return 43;
VkFenceCreateInfo fci = {
.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,
.flags = 0,
};
VkFence fence = VK_NULL_HANDLE;
vr = vkCreateFence(device, &fci, NULL, &fence);
if (vr != VK_SUCCESS) {
fprintf(stderr, "FAIL: vkCreateFence = %d (%s)\n",
vr, vk_result_name(vr));
return 44;
}
VkSubmitInfo si = {
.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO,
.commandBufferCount = 1,
.pCommandBuffers = &cb,
};
printf("[11/12] vkQueueSubmit(empty CB) ...\n");
vr = vkQueueSubmit(queue, 1, &si, fence);
printf(" vkQueueSubmit = %d (%s)\n",
vr, vk_result_name(vr));
if (vr != VK_SUCCESS)
return 50;
VkResult fs = vkGetFenceStatus(device, fence);
printf(" immediate vkGetFenceStatus = %d (%s)\n",
fs, vk_result_name(fs));
printf("[12/12] vkWaitForFences(timeout=%.3f s) ...\n",
(double)fence_timeout_ns / 1000000000.0);
vr = vkWaitForFences(device, 1, &fence, VK_TRUE, fence_timeout_ns);
printf(" vkWaitForFences = %d (%s)\n",
vr, vk_result_name(vr));
if (vr == VK_TIMEOUT) {
fprintf(stderr,
"\nTIMEOUT: submit did not signal the fence in time.\n"
"Exiting immediately; no idle wait and no teardown calls.\n");
return 60;
}
if (vr != VK_SUCCESS) {
fprintf(stderr,
"\nFAIL: fence wait returned %d (%s).\n",
vr, vk_result_name(vr));
return 61;
}
fs = vkGetFenceStatus(device, fence);
printf(" final vkGetFenceStatus = %d (%s)\n",
fs, vk_result_name(fs));
/*
* Submission completed, so it is safe to destroy the fence/pool without
* queue/device idle calls. Destroying the pool releases the command buffer.
*/
vkDestroyFence(device, fence, NULL);
vkDestroyCommandPool(device, pool, NULL);
printf("\nPASS: empty command buffer on dedicated compute queue submitted and fence signaled.\n");
printf("NO SHADER/DRAW/DISPATCH/MEMORY ACCESS WAS SUBMITTED.\n");
return 0;
}
static double
monotonic_seconds(void)
{
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return (double)ts.tv_sec + (double)ts.tv_nsec / 1000000000.0;
}
int
main(int argc, char **argv)
{
setvbuf(stdout, NULL, _IONBF, 0);
setvbuf(stderr, NULL, _IONBF, 0);
const char *driver_path =
argc >= 2 ? argv[1] : "./libvulkan_radeon.so";
int watchdog_sec = 6;
const char *wd = getenv("PROBE_TIMEOUT_SEC");
if (wd && *wd) {
int v = atoi(wd);
if (v >= 2 && v <= 30)
watchdog_sec = v;
}
uint64_t fence_timeout_ns = 1000000000ull; /* 1 second */
const char *ft = getenv("FENCE_TIMEOUT_MS");
if (ft && *ft) {
long ms = strtol(ft, NULL, 10);
if (ms >= 10 && ms <= 5000)
fence_timeout_ns = (uint64_t)ms * 1000000ull;
}
printf("Watchdog timeout: %d seconds\n", watchdog_sec);
pid_t child = fork();
if (child < 0) {
fprintf(stderr, "fork failed: %s\n", strerror(errno));
return 100;
}
if (child == 0) {
int rc = run_child(driver_path, fence_timeout_ns);
_exit(rc);
}
const double deadline = monotonic_seconds() + watchdog_sec;
for (;;) {
int status = 0;
pid_t r = waitpid(child, &status, WNOHANG);
if (r == child) {
if (WIFEXITED(status)) {
int rc = WEXITSTATUS(status);
printf("\nwatchdog: child exited normally, rc=%d\n", rc);
return rc;
}
if (WIFSIGNALED(status)) {
int sig = WTERMSIG(status);
printf("\nwatchdog: child died from signal %d\n", sig);
return 128 + sig;
}
return 101;
}
if (r < 0) {
fprintf(stderr, "waitpid failed: %s\n", strerror(errno));
return 102;
}
if (monotonic_seconds() >= deadline) {
fprintf(stderr,
"\nWATCHDOG TIMEOUT: probe exceeded %d seconds.\n"
"Killing userspace child %d and returning.\n",
watchdog_sec, (int)child);
kill(child, SIGKILL);
return 124;
}
usleep(100000);
}
}