src/platform/test/perf-main.c (view raw)
1/* Copyright (c) 2013-2015 Jeffrey Pfau
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6#include "core/config.h"
7#include "gba/core.h"
8#include "gba/gba.h"
9#include "gba/renderers/video-software.h"
10#include "gba/serialize.h"
11
12#include "feature/commandline.h"
13#include "util/socket.h"
14#include "util/string.h"
15#include "util/vfs.h"
16
17#ifdef _3DS
18#include <3ds.h>
19#endif
20
21#include <errno.h>
22#include <fcntl.h>
23#include <signal.h>
24#include <inttypes.h>
25#include <sys/time.h>
26
27#define PERF_OPTIONS "DF:L:NPS:"
28#define PERF_USAGE \
29 "\nBenchmark options:\n" \
30 " -F FRAMES Run for the specified number of FRAMES before exiting\n" \
31 " -N Disable video rendering entirely\n" \
32 " -P CSV output, useful for parsing\n" \
33 " -S SEC Run for SEC in-game seconds before exiting\n" \
34 " -L FILE Load a savestate when starting the test\n" \
35 " -D Act as a server"
36
37struct PerfOpts {
38 bool noVideo;
39 bool csv;
40 unsigned duration;
41 unsigned frames;
42 char* savestate;
43 bool server;
44};
45
46#ifdef _3DS
47extern bool allocateRomBuffer(void);
48FS_Archive sdmcArchive;
49#endif
50
51static void _mPerfRunloop(struct mCore* context, int* frames, bool quiet);
52static void _mPerfShutdown(int signal);
53static bool _parsePerfOpts(struct mSubParser* parser, int option, const char* arg);
54static void _log(struct mLogger*, int, enum mLogLevel, const char*, va_list);
55static bool _mPerfRunCore(const char* fname, const struct mArguments*, const struct PerfOpts*);
56static bool _mPerfRunServer(const char* listen, const struct mArguments*, const struct PerfOpts*);
57
58static bool _dispatchExiting = false;
59static struct VFile* _savestate = 0;
60static void* _outputBuffer = NULL;
61static Socket _socket = INVALID_SOCKET;
62
63int main(int argc, char** argv) {
64#ifdef _3DS
65 UNUSED(_mPerfShutdown);
66 gfxInitDefault();
67 osSetSpeedupEnable(true);
68 consoleInit(GFX_BOTTOM, NULL);
69 if (!allocateRomBuffer()) {
70 return 1;
71 }
72 sdmcArchive = (FS_Archive) {
73 ARCHIVE_SDMC,
74 (FS_Path) { PATH_EMPTY, 1, "" },
75 0
76 };
77 FSUSER_OpenArchive(&sdmcArchive);
78#else
79 signal(SIGINT, _mPerfShutdown);
80#endif
81 int didFail = 0;
82
83 struct mLogger logger = { .log = _log };
84 mLogSetDefaultLogger(&logger);
85
86 struct PerfOpts perfOpts = { false, false, 0, 0, 0, false };
87 struct mSubParser subparser = {
88 .usage = PERF_USAGE,
89 .parse = _parsePerfOpts,
90 .extraOptions = PERF_OPTIONS,
91 .opts = &perfOpts
92 };
93
94 struct mArguments args = {};
95 bool parsed = parseArguments(&args, argc, argv, &subparser);
96 if (!parsed || args.showHelp) {
97 usage(argv[0], PERF_USAGE);
98 didFail = !parsed;
99 goto cleanup;
100 }
101
102 if (args.showVersion) {
103 version(argv[0]);
104 goto cleanup;
105 }
106
107 if (perfOpts.savestate) {
108 _savestate = VFileOpen(perfOpts.savestate, O_RDONLY);
109 free(perfOpts.savestate);
110 }
111
112 _outputBuffer = malloc(256 * 256 * 4);
113 if (perfOpts.csv) {
114 puts("game_code,frames,duration,renderer");
115 }
116 if (perfOpts.server) {
117 didFail = !_mPerfRunServer(args.fname, &args, &perfOpts);
118 } else {
119 didFail = !_mPerfRunCore(args.fname, &args, &perfOpts);
120 }
121 free(_outputBuffer);
122
123 if (_savestate) {
124 _savestate->close(_savestate);
125 }
126 cleanup:
127 freeArguments(&args);
128
129#ifdef _3DS
130 gfxExit();
131 acExit();
132#endif
133
134 return didFail;
135}
136
137bool _mPerfRunCore(const char* fname, const struct mArguments* args, const struct PerfOpts* perfOpts) {
138 struct mCore* core = mCoreFind(fname);
139 if (!core) {
140 return false;
141 }
142
143 // TODO: Put back debugger
144 char gameCode[9] = { 0 };
145
146 core->init(core);
147 if (!perfOpts->noVideo) {
148 core->setVideoBuffer(core, _outputBuffer, 256);
149 }
150 mCoreLoadFile(core, fname);
151 mCoreConfigInit(&core->config, "perf");
152 mCoreConfigLoad(&core->config);
153
154 struct mCoreOptions opts = {};
155 mCoreConfigMap(&core->config, &opts);
156 opts.audioSync = false;
157 opts.videoSync = false;
158 applyArguments(args, NULL, &core->config);
159 mCoreConfigLoadDefaults(&core->config, &opts);
160 mCoreConfigSetDefaultValue(&core->config, "idleOptimization", "detect");
161 mCoreLoadConfig(core);
162
163 core->reset(core);
164 if (_savestate) {
165 mCoreLoadStateNamed(core, _savestate, 0);
166 }
167
168 core->getGameCode(core, gameCode);
169
170 int frames = perfOpts->frames;
171 if (!frames) {
172 frames = perfOpts->duration * 60;
173 }
174 struct timeval tv;
175 gettimeofday(&tv, 0);
176 uint64_t start = 1000000LL * tv.tv_sec + tv.tv_usec;
177 _mPerfRunloop(core, &frames, perfOpts->csv);
178 gettimeofday(&tv, 0);
179 uint64_t end = 1000000LL * tv.tv_sec + tv.tv_usec;
180 uint64_t duration = end - start;
181
182 core->deinit(core);
183
184 float scaledFrames = frames * 1000000.f;
185 if (perfOpts->csv) {
186 char buffer[256];
187 const char* rendererName;
188 if (perfOpts->noVideo) {
189 rendererName = "none";
190 } else {
191 rendererName = "software";
192 }
193 snprintf(buffer, sizeof(buffer), "%s,%i,%" PRIu64 ",%s\n", gameCode, frames, duration, rendererName);
194 printf("%s", buffer);
195 if (_socket != INVALID_SOCKET) {
196 SocketSend(_socket, buffer, strlen(buffer));
197 }
198 } else {
199 printf("%u frames in %" PRIu64 " microseconds: %g fps (%gx)\n", frames, duration, scaledFrames / duration, scaledFrames / (duration * 60.f));
200 }
201
202 mCoreConfigFreeOpts(&opts);
203 mCoreConfigDeinit(&core->config);
204 return true;
205}
206
207static void _mPerfRunloop(struct mCore* core, int* frames, bool quiet) {
208 struct timeval lastEcho;
209 gettimeofday(&lastEcho, 0);
210 int duration = *frames;
211 *frames = 0;
212 int lastFrames = 0;
213 while (!_dispatchExiting) {
214 core->runFrame(core);
215 ++*frames;
216 ++lastFrames;
217 if (!quiet) {
218 struct timeval currentTime;
219 long timeDiff;
220 gettimeofday(¤tTime, 0);
221 timeDiff = currentTime.tv_sec - lastEcho.tv_sec;
222 timeDiff *= 1000;
223 timeDiff += (currentTime.tv_usec - lastEcho.tv_usec) / 1000;
224 if (timeDiff >= 1000) {
225 printf("\033[2K\rCurrent FPS: %g (%gx)", lastFrames / (timeDiff / 1000.0f), lastFrames / (float) (60 * (timeDiff / 1000.0f)));
226 fflush(stdout);
227 lastEcho = currentTime;
228 lastFrames = 0;
229 }
230 }
231 if (duration > 0 && *frames == duration) {
232 break;
233 }
234 }
235 if (!quiet) {
236 printf("\033[2K\r");
237 }
238}
239
240static bool _mPerfRunServer(const char* listen, const struct mArguments* args, const struct PerfOpts* perfOpts) {
241 SocketSubsystemInit();
242 Socket server = SocketOpenTCP(7216, NULL);
243 if (SOCKET_FAILED(server)) {
244 SocketSubsystemDeinit();
245 return false;
246 }
247 if (SOCKET_FAILED(SocketListen(server, 0))) {
248 SocketClose(server);
249 SocketSubsystemDeinit();
250 return false;
251 }
252 _socket = SocketAccept(server, NULL);
253 if (perfOpts->csv) {
254 const char* header = "game_code,frames,duration,renderer\n";
255 SocketSend(_socket, header, strlen(header));
256 }
257 char path[PATH_MAX];
258 while (SocketRecv(_socket, path, sizeof(path)) > 0) {
259 char* nl = strchr(path, '\n');
260 if (nl == path) {
261 break;
262 }
263 if (nl) {
264 nl[0] = '\0';
265 }
266 if (!_mPerfRunCore(path, args, perfOpts)) {
267 break;
268 }
269 }
270 SocketClose(_socket);
271 SocketClose(server);
272 SocketSubsystemDeinit();
273 return true;
274}
275
276static void _mPerfShutdown(int signal) {
277 UNUSED(signal);
278 _dispatchExiting = true;
279 SocketClose(_socket);
280}
281
282static bool _parsePerfOpts(struct mSubParser* parser, int option, const char* arg) {
283 struct PerfOpts* opts = parser->opts;
284 errno = 0;
285 switch (option) {
286 case 'D':
287 opts->server = true;
288 return true;
289 case 'F':
290 opts->frames = strtoul(arg, 0, 10);
291 return !errno;
292 case 'N':
293 opts->noVideo = true;
294 return true;
295 case 'P':
296 opts->csv = true;
297 return true;
298 case 'S':
299 opts->duration = strtoul(arg, 0, 10);
300 return !errno;
301 case 'L':
302 opts->savestate = strdup(arg);
303 return true;
304 default:
305 return false;
306 }
307}
308
309static void _log(struct mLogger* log, int category, enum mLogLevel level, const char* format, va_list args) {
310 UNUSED(log);
311 UNUSED(category);
312 UNUSED(level);
313 UNUSED(format);
314 UNUSED(args);
315}