This rotates the dump files like the mali-syscall-tracker does.
After each finished pp frame a new file is generated. They are
numbered like lima.dump.0000, lima.dump.0001 ...
The filename and path can be given with the new environment
variable LIMA_DUMP_FILE.
Reviewed-by: Vasily Khoruzhick <anarsoul@gmail.com>
Signed-off-by: Andreas Baierl <ichgeh@imkreisrum.de>
Tested-by: Marge Bot <https://gitlab.freedesktop.org/mesa/mesa/merge_requests/3175>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/merge_requests/3175>
    }
 
    ctx->pp_max_stack_size = 0;
+
+   lima_dump_file_next();
 }
 
 void
 
 #include "lima_bo.h"
 #include "lima_fence.h"
 #include "lima_format.h"
+#include "lima_util.h"
 #include "ir/lima_ir.h"
 
 #include "xf86drm.h"
 {
    struct lima_screen *screen = lima_screen(pscreen);
 
-   if (lima_dump_command_stream) {
-      fclose(lima_dump_command_stream);
-      lima_dump_command_stream = NULL;
-   }
+   lima_dump_file_close();
 
    slab_destroy_parent(&screen->transfer_pool);
 
 {
    lima_debug = debug_get_option_lima_debug();
 
-   if (lima_debug & LIMA_DEBUG_DUMP) {
-      const char *dump_command = "lima.dump";
-      printf("lima: dump command stream to file %s\n", dump_command);
-      lima_dump_command_stream = fopen(dump_command, "w");
-      if (!lima_dump_command_stream)
-         fprintf(stderr, "lima: fail to open command stream log file %s\n",
-                 dump_command);
-   }
+   if (lima_debug & LIMA_DEBUG_DUMP)
+      lima_dump_file_open();
 
    lima_ctx_num_plb = debug_get_num_option("LIMA_CTX_NUM_PLB", LIMA_CTX_PLB_DEF_NUM);
    if (lima_ctx_num_plb > LIMA_CTX_PLB_MAX_NUM ||
 
 #define LIMA_DEBUG_BO_CACHE       (1 << 5)
 
 extern uint32_t lima_debug;
-extern FILE *lima_dump_command_stream;
 extern int lima_ctx_num_plb;
 extern int lima_plb_max_blk;
 extern int lima_ppir_force_spilling;
 
 
 #include <pipe/p_defines.h>
 
+#include "util/u_debug.h"
+
 #include "lima_util.h"
 #include "lima_parser.h"
 
 FILE *lima_dump_command_stream = NULL;
+int lima_dump_frame_count = 0;
 
 bool lima_get_absolute_timeout(uint64_t *timeout)
 {
       lima_parse_texture_descriptor(lima_dump_command_stream, (uint32_t *)data, size, start, offset);
 }
 
+void
+lima_dump_file_open(void)
+{
+   if (lima_dump_command_stream)
+      return;
+
+   char buffer[1024];
+   const char *dump_command = debug_get_option("LIMA_DUMP_FILE", "lima.dump");
+   snprintf(buffer, sizeof(buffer), "%s.%04d", dump_command, lima_dump_frame_count);
+
+   printf("lima: dump command stream to file %s\n", buffer);
+   lima_dump_command_stream = fopen(buffer, "w");
+   if (!lima_dump_command_stream)
+      fprintf(stderr, "lima: failed to open command stream log file %s\n",
+              buffer);
+}
+
+void
+lima_dump_file_close(void)
+{
+   if (lima_dump_command_stream) {
+      fclose(lima_dump_command_stream);
+      lima_dump_command_stream = NULL;
+   }
+}
+
+void
+lima_dump_file_next(void)
+{
+   lima_dump_file_close();
+   lima_dump_frame_count++;
+   lima_dump_file_open();
+}
+
 void
 lima_dump_command_stream_print(void *data, int size, bool is_float,
                                const char *fmt, ...)
 
 
 #define LIMA_PAGE_SIZE 4096
 
+FILE *lima_dump_command_stream;
+
 bool lima_get_absolute_timeout(uint64_t *timeout);
+void lima_dump_file_open(void);
+void lima_dump_file_next(void);
+void lima_dump_file_close(void);
 void lima_dump_blob(FILE *fp, void *data, int size, bool is_float);
 void lima_dump_vs_command_stream_print(void *data, int size, uint32_t start);
 void lima_dump_plbu_command_stream_print(void *data, int size, uint32_t start);