lima: Rotate dump files after each finished pp frame
[mesa.git] / src / gallium / drivers / lima / lima_util.c
1 /*
2 * Copyright (C) 2018-2019 Lima Project
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
21 *
22 */
23
24 #include <stdio.h>
25 #include <stdarg.h>
26 #include <time.h>
27
28 #include <pipe/p_defines.h>
29
30 #include "util/u_debug.h"
31
32 #include "lima_util.h"
33 #include "lima_parser.h"
34
35 FILE *lima_dump_command_stream = NULL;
36 int lima_dump_frame_count = 0;
37
38 bool lima_get_absolute_timeout(uint64_t *timeout)
39 {
40 struct timespec current;
41 uint64_t current_ns;
42
43 if (*timeout == PIPE_TIMEOUT_INFINITE)
44 return true;
45
46 if (clock_gettime(CLOCK_MONOTONIC, &current))
47 return false;
48
49 current_ns = ((uint64_t)current.tv_sec) * 1000000000ull;
50 current_ns += current.tv_nsec;
51 *timeout += current_ns;
52
53 return true;
54 }
55
56 void lima_dump_blob(FILE *fp, void *data, int size, bool is_float)
57 {
58 fprintf(fp, "{\n");
59 for (int i = 0; i * 4 < size; i++) {
60 if (i % 4 == 0)
61 fprintf(fp, "\t");
62
63 if (is_float)
64 fprintf(fp, "%f, ", ((float *)data)[i]);
65 else
66 fprintf(fp, "0x%08x, ", ((uint32_t *)data)[i]);
67
68 if ((i % 4 == 3) || (i == size / 4 - 1)) {
69 fprintf(fp, "/* 0x%08x */", MAX2((i - 3) * 4, 0));
70 if (i) fprintf(fp, "\n");
71 }
72 }
73 fprintf(fp, "}\n");
74 }
75
76 void
77 lima_dump_vs_command_stream_print(void *data, int size, uint32_t start)
78 {
79 if (lima_dump_command_stream)
80 lima_parse_vs(lima_dump_command_stream, (uint32_t *)data, size, start);
81 }
82
83 void
84 lima_dump_plbu_command_stream_print(void *data, int size, uint32_t start)
85 {
86 if (lima_dump_command_stream)
87 lima_parse_plbu(lima_dump_command_stream, (uint32_t *)data, size, start);
88 }
89
90 void
91 lima_dump_rsw_command_stream_print(void *data, int size, uint32_t start)
92 {
93 if (lima_dump_command_stream)
94 lima_parse_render_state(lima_dump_command_stream, (uint32_t *)data, size, start);
95 }
96
97 void
98 lima_dump_texture_descriptor(void *data, int size, uint32_t start, uint32_t offset)
99 {
100 if (lima_dump_command_stream)
101 lima_parse_texture_descriptor(lima_dump_command_stream, (uint32_t *)data, size, start, offset);
102 }
103
104 void
105 lima_dump_file_open(void)
106 {
107 if (lima_dump_command_stream)
108 return;
109
110 char buffer[1024];
111 const char *dump_command = debug_get_option("LIMA_DUMP_FILE", "lima.dump");
112 snprintf(buffer, sizeof(buffer), "%s.%04d", dump_command, lima_dump_frame_count);
113
114 printf("lima: dump command stream to file %s\n", buffer);
115 lima_dump_command_stream = fopen(buffer, "w");
116 if (!lima_dump_command_stream)
117 fprintf(stderr, "lima: failed to open command stream log file %s\n",
118 buffer);
119 }
120
121 void
122 lima_dump_file_close(void)
123 {
124 if (lima_dump_command_stream) {
125 fclose(lima_dump_command_stream);
126 lima_dump_command_stream = NULL;
127 }
128 }
129
130 void
131 lima_dump_file_next(void)
132 {
133 lima_dump_file_close();
134 lima_dump_frame_count++;
135 lima_dump_file_open();
136 }
137
138 void
139 lima_dump_command_stream_print(void *data, int size, bool is_float,
140 const char *fmt, ...)
141 {
142 if (lima_dump_command_stream) {
143 va_list ap;
144 va_start(ap, fmt);
145 vfprintf(lima_dump_command_stream, fmt, ap);
146 va_end(ap);
147
148 lima_dump_blob(lima_dump_command_stream, data, size, is_float);
149 }
150 }