radv: add radv_check_gpu_hangs() helper function
[mesa.git] / src / amd / vulkan / radv_debug.c
1 /*
2 * Copyright © 2016 Red Hat.
3 * Copyright © 2016 Bas Nieuwenhuizen
4 *
5 * based in part on anv driver which is:
6 * Copyright © 2015 Intel Corporation
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice (including the next
16 * paragraph) shall be included in all copies or substantial portions of the
17 * Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
25 * IN THE SOFTWARE.
26 */
27
28 #include <stdlib.h>
29 #include <stdio.h>
30
31 #include "radv_debug.h"
32
33 bool
34 radv_init_trace(struct radv_device *device)
35 {
36 struct radeon_winsys *ws = device->ws;
37
38 device->trace_bo = ws->buffer_create(ws, 4096, 8, RADEON_DOMAIN_VRAM,
39 RADEON_FLAG_CPU_ACCESS);
40 if (!device->trace_bo)
41 return false;
42
43 device->trace_id_ptr = ws->buffer_map(device->trace_bo);
44 if (!device->trace_id_ptr)
45 return false;
46
47 return true;
48 }
49
50 static void
51 radv_dump_trace(struct radv_device *device, struct radeon_winsys_cs *cs)
52 {
53 const char *filename = getenv("RADV_TRACE_FILE");
54 FILE *f = fopen(filename, "w");
55
56 if (!f) {
57 fprintf(stderr, "Failed to write trace dump to %s\n", filename);
58 return;
59 }
60
61 fprintf(f, "Trace ID: %x\n", *device->trace_id_ptr);
62 device->ws->cs_dump(cs, f, (const int*)device->trace_id_ptr, 2);
63 fclose(f);
64 }
65
66 static bool
67 radv_gpu_hang_occured(struct radv_queue *queue)
68 {
69 struct radeon_winsys *ws = queue->device->ws;
70 enum ring_type ring;
71
72 ring = radv_queue_family_to_ring(queue->queue_family_index);
73
74 if (!ws->ctx_wait_idle(queue->hw_ctx, ring, queue->queue_idx))
75 return true;
76
77 return false;
78 }
79
80 void
81 radv_check_gpu_hangs(struct radv_queue *queue, struct radeon_winsys_cs *cs)
82 {
83 if (!radv_gpu_hang_occured(queue))
84 return;
85
86 radv_dump_trace(queue->device, cs);
87 abort();
88 }
89
90 void
91 radv_print_spirv(struct radv_shader_module *module, FILE *fp)
92 {
93 char path[] = "/tmp/fileXXXXXX";
94 char line[2048], command[128];
95 FILE *p;
96 int fd;
97
98 /* Dump the binary into a temporary file. */
99 fd = mkstemp(path);
100 if (fd < 0)
101 return;
102
103 if (write(fd, module->data, module->size) == -1)
104 goto fail;
105
106 sprintf(command, "spirv-dis %s", path);
107
108 /* Disassemble using spirv-dis if installed. */
109 p = popen(command, "r");
110 if (p) {
111 while (fgets(line, sizeof(line), p))
112 fprintf(fp, "%s", line);
113 pclose(p);
114 }
115
116 fail:
117 close(fd);
118 unlink(path);
119 }