radv: dump shader stats when a hang occured
[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 "ac_debug.h"
32 #include "radv_debug.h"
33 #include "radv_shader.h"
34
35 #define TRACE_BO_SIZE 4096
36
37 /* Trace BO layout (offsets are 4 bytes):
38 *
39 * [0]: primary trace ID
40 * [1]: secondary trace ID
41 * [2-3]: 64-bit GFX pipeline pointer
42 * [4-5]: 64-bit COMPUTE pipeline pointer
43 */
44
45 bool
46 radv_init_trace(struct radv_device *device)
47 {
48 struct radeon_winsys *ws = device->ws;
49
50 device->trace_bo = ws->buffer_create(ws, TRACE_BO_SIZE, 8,
51 RADEON_DOMAIN_VRAM,
52 RADEON_FLAG_CPU_ACCESS);
53 if (!device->trace_bo)
54 return false;
55
56 device->trace_id_ptr = ws->buffer_map(device->trace_bo);
57 if (!device->trace_id_ptr)
58 return false;
59
60 memset(device->trace_id_ptr, 0, TRACE_BO_SIZE);
61
62 ac_vm_fault_occured(device->physical_device->rad_info.chip_class,
63 &device->dmesg_timestamp, NULL);
64
65 return true;
66 }
67
68 static void
69 radv_dump_trace(struct radv_device *device, struct radeon_winsys_cs *cs)
70 {
71 const char *filename = getenv("RADV_TRACE_FILE");
72 FILE *f = fopen(filename, "w");
73
74 if (!f) {
75 fprintf(stderr, "Failed to write trace dump to %s\n", filename);
76 return;
77 }
78
79 fprintf(f, "Trace ID: %x\n", *device->trace_id_ptr);
80 device->ws->cs_dump(cs, f, (const int*)device->trace_id_ptr, 2);
81 fclose(f);
82 }
83
84 static void
85 radv_dump_shader(struct radv_pipeline *pipeline,
86 struct radv_shader_variant *shader, gl_shader_stage stage,
87 FILE *f)
88 {
89 if (!shader)
90 return;
91
92 fprintf(f, "%s:\n%s\n\n", radv_get_shader_name(shader, stage),
93 shader->disasm_string);
94
95 radv_shader_dump_stats(pipeline->device, shader, stage, f);
96 }
97
98 static void
99 radv_dump_shaders(struct radv_pipeline *pipeline,
100 struct radv_shader_variant *compute_shader, FILE *f)
101 {
102 unsigned mask;
103
104 /* Dump active graphics shaders. */
105 mask = pipeline->active_stages;
106 while (mask) {
107 int stage = u_bit_scan(&mask);
108
109 radv_dump_shader(pipeline, pipeline->shaders[stage], stage, f);
110 }
111
112 radv_dump_shader(pipeline, compute_shader, MESA_SHADER_COMPUTE, f);
113 }
114
115 static void
116 radv_dump_graphics_state(struct radv_pipeline *graphics_pipeline,
117 struct radv_pipeline *compute_pipeline, FILE *f)
118 {
119 struct radv_shader_variant *compute_shader =
120 compute_pipeline ? compute_pipeline->shaders[MESA_SHADER_COMPUTE] : NULL;
121
122 if (!graphics_pipeline)
123 return;
124
125 radv_dump_shaders(graphics_pipeline, compute_shader, f);
126 }
127
128 static void
129 radv_dump_compute_state(struct radv_pipeline *compute_pipeline, FILE *f)
130 {
131 if (!compute_pipeline)
132 return;
133
134 radv_dump_shaders(compute_pipeline,
135 compute_pipeline->shaders[MESA_SHADER_COMPUTE], f);
136 }
137
138 static struct radv_pipeline *
139 radv_get_saved_graphics_pipeline(struct radv_device *device)
140 {
141 uint64_t *ptr = (uint64_t *)device->trace_id_ptr;
142
143 return (struct radv_pipeline *)ptr[1];
144 }
145
146 static struct radv_pipeline *
147 radv_get_saved_compute_pipeline(struct radv_device *device)
148 {
149 uint64_t *ptr = (uint64_t *)device->trace_id_ptr;
150
151 return (struct radv_pipeline *)ptr[2];
152 }
153
154 static bool
155 radv_gpu_hang_occured(struct radv_queue *queue, enum ring_type ring)
156 {
157 struct radeon_winsys *ws = queue->device->ws;
158
159 if (!ws->ctx_wait_idle(queue->hw_ctx, ring, queue->queue_idx))
160 return true;
161
162 return false;
163 }
164
165 void
166 radv_check_gpu_hangs(struct radv_queue *queue, struct radeon_winsys_cs *cs)
167 {
168 struct radv_pipeline *graphics_pipeline, *compute_pipeline;
169 struct radv_device *device = queue->device;
170 enum ring_type ring;
171 uint64_t addr;
172
173 ring = radv_queue_family_to_ring(queue->queue_family_index);
174
175 bool hang_occurred = radv_gpu_hang_occured(queue, ring);
176 bool vm_fault_occurred = false;
177 if (queue->device->instance->debug_flags & RADV_DEBUG_VM_FAULTS)
178 vm_fault_occurred = ac_vm_fault_occured(device->physical_device->rad_info.chip_class,
179 &device->dmesg_timestamp, &addr);
180 if (!hang_occurred && !vm_fault_occurred)
181 return;
182
183 graphics_pipeline = radv_get_saved_graphics_pipeline(device);
184 compute_pipeline = radv_get_saved_compute_pipeline(device);
185
186 if (vm_fault_occurred) {
187 fprintf(stderr, "VM fault report.\n\n");
188 fprintf(stderr, "Failing VM page: 0x%08"PRIx64"\n\n", addr);
189 }
190
191 switch (ring) {
192 case RING_GFX:
193 radv_dump_graphics_state(graphics_pipeline, compute_pipeline,
194 stderr);
195 break;
196 case RING_COMPUTE:
197 radv_dump_compute_state(compute_pipeline, stderr);
198 break;
199 default:
200 assert(0);
201 break;
202 }
203
204 radv_dump_trace(queue->device, cs);
205 abort();
206 }
207
208 void
209 radv_print_spirv(struct radv_shader_module *module, FILE *fp)
210 {
211 char path[] = "/tmp/fileXXXXXX";
212 char line[2048], command[128];
213 FILE *p;
214 int fd;
215
216 /* Dump the binary into a temporary file. */
217 fd = mkstemp(path);
218 if (fd < 0)
219 return;
220
221 if (write(fd, module->data, module->size) == -1)
222 goto fail;
223
224 sprintf(command, "spirv-dis %s", path);
225
226 /* Disassemble using spirv-dis if installed. */
227 p = popen(command, "r");
228 if (p) {
229 while (fgets(line, sizeof(line), p))
230 fprintf(fp, "%s", line);
231 pclose(p);
232 }
233
234 fail:
235 close(fd);
236 unlink(path);
237 }