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 #include "util/u_memory.h"
32
33 #include "lima_util.h"
34 #include "lima_parser.h"
35 #include "lima_screen.h"
36
37 struct lima_dump {
38 FILE *fp;
39 int id;
40 };
41
lima_get_absolute_timeout(uint64_t * timeout)42 bool lima_get_absolute_timeout(uint64_t *timeout)
43 {
44 struct timespec current;
45 uint64_t current_ns;
46
47 if (*timeout == PIPE_TIMEOUT_INFINITE)
48 return true;
49
50 if (clock_gettime(CLOCK_MONOTONIC, ¤t))
51 return false;
52
53 current_ns = ((uint64_t)current.tv_sec) * 1000000000ull;
54 current_ns += current.tv_nsec;
55 *timeout += current_ns;
56
57 return true;
58 }
59
60 static void
lima_dump_blob(FILE * fp,void * data,int size,bool is_float)61 lima_dump_blob(FILE *fp, void *data, int size, bool is_float)
62 {
63 fprintf(fp, "{\n");
64 for (int i = 0; i * 4 < size; i++) {
65 if (i % 4 == 0)
66 fprintf(fp, "\t");
67
68 if (is_float)
69 fprintf(fp, "%f, ", ((float *)data)[i]);
70 else
71 fprintf(fp, "0x%08x, ", ((uint32_t *)data)[i]);
72
73 if ((i % 4 == 3) || (i == size / 4 - 1)) {
74 fprintf(fp, "/* 0x%08x */", MAX2((i - 3) * 4, 0));
75 if (i) fprintf(fp, "\n");
76 }
77 }
78 fprintf(fp, "}\n");
79 }
80
81 void
lima_dump_vs_command_stream_print(struct lima_dump * dump,void * data,int size,uint32_t start)82 lima_dump_vs_command_stream_print(struct lima_dump *dump, void *data,
83 int size, uint32_t start)
84 {
85 if (dump)
86 lima_parse_vs(dump->fp, (uint32_t *)data, size, start);
87 }
88
89 void
lima_dump_plbu_command_stream_print(struct lima_dump * dump,void * data,int size,uint32_t start)90 lima_dump_plbu_command_stream_print(struct lima_dump *dump, void *data,
91 int size, uint32_t start)
92 {
93 if (dump)
94 lima_parse_plbu(dump->fp, (uint32_t *)data, size, start);
95 }
96
97 void
lima_dump_rsw_command_stream_print(struct lima_dump * dump,void * data,int size,uint32_t start)98 lima_dump_rsw_command_stream_print(struct lima_dump *dump, void *data,
99 int size, uint32_t start)
100 {
101 if (dump)
102 lima_parse_render_state(dump->fp, (uint32_t *)data, size, start);
103 }
104
105 void
lima_dump_texture_descriptor(struct lima_dump * dump,void * data,int size,uint32_t start,uint32_t offset)106 lima_dump_texture_descriptor(struct lima_dump *dump, void *data,
107 int size, uint32_t start, uint32_t offset)
108 {
109 if (dump)
110 lima_parse_texture_descriptor(dump->fp, (uint32_t *)data, size, start, offset);
111 }
112
113 struct lima_dump *
lima_dump_create(void)114 lima_dump_create(void)
115 {
116 static int dump_id = 0;
117
118 if (!(lima_debug & LIMA_DEBUG_DUMP))
119 return NULL;
120
121 struct lima_dump *ret = MALLOC_STRUCT(lima_dump);
122 if (!ret)
123 return NULL;
124
125 ret->id = dump_id++;
126
127 char buffer[PATH_MAX];
128 const char *dump_command = debug_get_option("LIMA_DUMP_FILE", "lima.dump");
129 snprintf(buffer, sizeof(buffer), "%s.staging.%04d", dump_command, ret->id);
130
131 ret->fp = fopen(buffer, "w");
132 if (!ret->fp) {
133 fprintf(stderr, "lima: failed to open command stream log file %s\n", buffer);
134 FREE(ret);
135 return NULL;
136 }
137
138 return ret;
139 }
140
141 void
lima_dump_free(struct lima_dump * dump)142 lima_dump_free(struct lima_dump *dump)
143 {
144 static int frame_count = 0;
145
146 if (!dump)
147 return;
148
149 fclose(dump->fp);
150
151 /* we only know the frame count when flush, not on dump create, so first dump to a
152 * stage file, then move to the final file with frame count as surfix when flush.
153 */
154
155 char stage_name[PATH_MAX];
156 char final_name[PATH_MAX];
157 const char *dump_command = debug_get_option("LIMA_DUMP_FILE", "lima.dump");
158 snprintf(stage_name, sizeof(stage_name), "%s.staging.%04d", dump_command, dump->id);
159 snprintf(final_name, sizeof(final_name), "%s.%04d", dump_command, frame_count++);
160
161 if (rename(stage_name, final_name))
162 fprintf(stderr, "lima: failed to rename log %s to %s\n", stage_name, final_name);
163
164 FREE(dump);
165 }
166
167 void
_lima_dump_command_stream_print(struct lima_dump * dump,void * data,int size,bool is_float,const char * fmt,...)168 _lima_dump_command_stream_print(struct lima_dump *dump, void *data,
169 int size, bool is_float, const char *fmt, ...)
170 {
171 va_list ap;
172 va_start(ap, fmt);
173 vfprintf(dump->fp, fmt, ap);
174 va_end(ap);
175
176 lima_dump_blob(dump->fp, data, size, is_float);
177 }
178