radv: Allow triggering thread traces by file.

Makes it actually feasible to trace games and not just demos/apitraces.

Reviewed-by: Samuel Pitoiset <samuel.pitoiset@gmail.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/6537>
diff --git a/src/amd/vulkan/layers/radv_sqtt_layer.c b/src/amd/vulkan/layers/radv_sqtt_layer.c
index 04909b2..af4264b 100644
--- a/src/amd/vulkan/layers/radv_sqtt_layer.c
+++ b/src/amd/vulkan/layers/radv_sqtt_layer.c
@@ -591,7 +591,20 @@
 		if (radv_get_thread_trace(queue, &thread_trace))
 			radv_dump_thread_trace(queue->device, &thread_trace);
 	} else {
-		if (num_frames == queue->device->thread_trace_start_frame) {
+		bool frame_trigger = num_frames == queue->device->thread_trace_start_frame;
+		bool file_trigger = false;
+		if (queue->device->thread_trace_trigger_file &&
+		    access(queue->device->thread_trace_trigger_file, W_OK) == 0) {
+			if (unlink(queue->device->thread_trace_trigger_file) == 0) {
+				file_trigger = true;
+			} else {
+				/* Do not enable tracing if we cannot remove the file,
+				 * because by then we'll trace every frame ... */
+				fprintf(stderr, "RADV: could not remove thread trace trigger file, ignoring\n");
+			}
+		}
+
+		if (frame_trigger || file_trigger) {
 			radv_begin_thread_trace(queue);
 			assert(!thread_trace_enabled);
 			thread_trace_enabled = true;
diff --git a/src/amd/vulkan/radv_device.c b/src/amd/vulkan/radv_device.c
index e2a5727..a64c90e 100644
--- a/src/amd/vulkan/radv_device.c
+++ b/src/amd/vulkan/radv_device.c
@@ -2488,7 +2488,8 @@
 
 static bool radv_thread_trace_enabled()
 {
-	return radv_get_int_debug_option("RADV_THREAD_TRACE", -1) >= 0;
+	return radv_get_int_debug_option("RADV_THREAD_TRACE", -1) >= 0 ||
+	       getenv("RADV_THREAD_TRACE_TRIGGER");
 }
 
 static void
@@ -2822,6 +2823,10 @@
 			radv_get_int_debug_option("RADV_THREAD_TRACE_BUFFER_SIZE", 1024 * 1024);
 		device->thread_trace_start_frame = radv_get_int_debug_option("RADV_THREAD_TRACE", -1);
 
+		const char *trigger_file = getenv("RADV_THREAD_TRACE_TRIGGER");
+		if (trigger_file)
+			device->thread_trace_trigger_file = strdup(trigger_file);
+
 		if (!radv_thread_trace_init(device))
 			goto fail;
 	}
@@ -2918,6 +2923,7 @@
 	radv_bo_list_finish(&device->bo_list);
 
 	radv_thread_trace_finish(device);
+	free(device->thread_trace_trigger_file);
 
 	radv_trap_handler_finish(device);
 
@@ -2977,6 +2983,7 @@
 	pthread_cond_destroy(&device->timeline_cond);
 	radv_bo_list_finish(&device->bo_list);
 
+	free(device->thread_trace_trigger_file);
 	radv_thread_trace_finish(device);
 
 	vk_free(&device->vk.alloc, device);
diff --git a/src/amd/vulkan/radv_private.h b/src/amd/vulkan/radv_private.h
index 579b16d..bb58fa7 100644
--- a/src/amd/vulkan/radv_private.h
+++ b/src/amd/vulkan/radv_private.h
@@ -846,6 +846,7 @@
 	void *thread_trace_ptr;
 	uint32_t thread_trace_buffer_size;
 	int thread_trace_start_frame;
+	char *thread_trace_trigger_file;
 
 	/* Trap handler. */
 	struct radv_shader_variant *trap_handler_shader;