From 193c02e0cfcf832e4556ada5de251f32e6ca4768 Mon Sep 17 00:00:00 2001 From: Mike Blumenkrantz Date: Tue, 6 Apr 2021 09:40:21 -0400 Subject: [PATCH] aux/trace: add GALLIUM_TRACE_TRIGGER mode similar to amd/radv driver debug modes for sqtt, this specifies a filename which is checked on every flush(PIPE_FLUSH_END_OF_FRAME); when it exists, the next frame (and only that frame) is captured into the trace to use, specify a file with the env var, run your app, and 'touch /path/to/file' when you want to capture a trace Reviewed-by: Adam Jackson Part-of: --- docs/gallium/debugging.rst | 7 +++ .../auxiliary/driver_trace/tr_context.c | 3 ++ src/gallium/auxiliary/driver_trace/tr_dump.c | 46 ++++++++++++++++++- src/gallium/auxiliary/driver_trace/tr_dump.h | 3 ++ 4 files changed, 58 insertions(+), 1 deletion(-) diff --git a/docs/gallium/debugging.rst b/docs/gallium/debugging.rst index 6e97de70de5..7252357a945 100644 --- a/docs/gallium/debugging.rst +++ b/docs/gallium/debugging.rst @@ -32,6 +32,13 @@ specified file. Paths may be relative or absolute; relative paths are relative to the working directory. For example, setting it to "trace.xml" will cause the trace to be written to a file of the same name in the working directory. +.. envvar:: GALLIUM_TRACE_TRIGGER ("") + +If set while :ref:`trace` is active, this variable specifies a filename to monitor. +Once the file exists (e.g., from the user running 'touch /path/to/file'), a single +frame will be recorded into the trace output. +Paths may be relative or absolute; relative paths are relative to the working directory. + .. envvar:: GALLIUM_DUMP_CPU (false) Dump information about the current CPU that the driver is running on. diff --git a/src/gallium/auxiliary/driver_trace/tr_context.c b/src/gallium/auxiliary/driver_trace/tr_context.c index 60d151dd49a..1a25678f11a 100644 --- a/src/gallium/auxiliary/driver_trace/tr_context.c +++ b/src/gallium/auxiliary/driver_trace/tr_context.c @@ -1370,6 +1370,9 @@ trace_context_flush(struct pipe_context *_pipe, trace_dump_ret(ptr, *fence); trace_dump_call_end(); + + if (flags & PIPE_FLUSH_END_OF_FRAME) + trace_dump_check_trigger(); } diff --git a/src/gallium/auxiliary/driver_trace/tr_dump.c b/src/gallium/auxiliary/driver_trace/tr_dump.c index 0be21ac6fb4..20f498ea1b5 100644 --- a/src/gallium/auxiliary/driver_trace/tr_dump.c +++ b/src/gallium/auxiliary/driver_trace/tr_dump.c @@ -43,6 +43,11 @@ #include #include +/* for access() */ +#ifdef _WIN32 +# include +#endif + #include "pipe/p_compiler.h" #include "os/os_thread.h" #include "util/os_time.h" @@ -63,11 +68,41 @@ static mtx_t call_mutex = _MTX_INITIALIZER_NP; static long unsigned call_no = 0; static bool dumping = false; +static bool trigger_active = true; +static char *trigger_filename = NULL; + +void +trace_dump_trigger_active(bool active) +{ + trigger_active = active; +} + +void +trace_dump_check_trigger(void) +{ + if (!trigger_filename) + return; + + mtx_lock(&call_mutex); + if (trigger_active) { + trigger_active = false; + } else { + if (!access(trigger_filename, 2 /* W_OK but compiles on Windows */)) { + if (!unlink(trigger_filename)) { + trigger_active = true; + } else { + fprintf(stderr, "error removing trigger file\n"); + trigger_active = false; + } + } + } + mtx_unlock(&call_mutex); +} static inline void trace_dump_write(const char *buf, size_t size) { - if (stream) { + if (stream && trigger_active) { fwrite(buf, size, 1, stream); } } @@ -175,6 +210,7 @@ static void trace_dump_trace_close(void) { if (stream) { + trigger_active = true; trace_dump_writes("\n"); if (close_stream) { fclose(stream); @@ -182,6 +218,7 @@ trace_dump_trace_close(void) stream = NULL; } call_no = 0; + free(trigger_filename); } } @@ -234,6 +271,13 @@ trace_dump_trace_begin(void) * time. */ atexit(trace_dump_trace_close); + + const char *trigger = debug_get_option("GALLIUM_TRACE_TRIGGER", NULL); + if (trigger) { + trigger_filename = strdup(trigger); + trigger_active = false; + } else + trigger_active = true; } return true; diff --git a/src/gallium/auxiliary/driver_trace/tr_dump.h b/src/gallium/auxiliary/driver_trace/tr_dump.h index 3c14edfc903..48e648b2822 100644 --- a/src/gallium/auxiliary/driver_trace/tr_dump.h +++ b/src/gallium/auxiliary/driver_trace/tr_dump.h @@ -108,6 +108,9 @@ void trace_dump_ptr(const void *value); void trace_dump_surface_ptr(struct pipe_surface *_surface); void trace_dump_transfer_ptr(struct pipe_transfer *_transfer); +void trace_dump_trigger_active(bool active); +void trace_dump_check_trigger(void); + /* * Code saving macros. */