all repos — mgba @ 012c70a0cea381aefbaef9d4cff18bdd4af1e43c

mGBA Game Boy Advance Emulator

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	FSUSER_OpenArchive(&sdmcArchive, ARCHIVE_SDMC, fsMakePath(PATH_EMPTY, ""));
 73#else
 74	signal(SIGINT, _mPerfShutdown);
 75#endif
 76	int didFail = 0;
 77
 78	struct mLogger logger = { .log = _log };
 79	mLogSetDefaultLogger(&logger);
 80
 81	struct PerfOpts perfOpts = { false, false, 0, 0, 0, false };
 82	struct mSubParser subparser = {
 83		.usage = PERF_USAGE,
 84		.parse = _parsePerfOpts,
 85		.extraOptions = PERF_OPTIONS,
 86		.opts = &perfOpts
 87	};
 88
 89	struct mArguments args = {};
 90	bool parsed = parseArguments(&args, argc, argv, &subparser);
 91	if (!args.fname) {
 92		parsed = false;
 93	}
 94	if (!parsed || args.showHelp) {
 95		usage(argv[0], PERF_USAGE);
 96		didFail = !parsed;
 97		goto cleanup;
 98	}
 99
100	if (args.showVersion) {
101		version(argv[0]);
102		goto cleanup;
103	}
104
105	if (perfOpts.savestate) {
106		_savestate = VFileOpen(perfOpts.savestate, O_RDONLY);
107		free(perfOpts.savestate);
108	}
109
110	_outputBuffer = malloc(256 * 256 * 4);
111	if (perfOpts.csv) {
112		puts("game_code,frames,duration,renderer");
113	}
114	if (perfOpts.server) {
115		didFail = !_mPerfRunServer(args.fname, &args, &perfOpts);
116	} else {
117		didFail = !_mPerfRunCore(args.fname, &args, &perfOpts);
118	}
119	free(_outputBuffer);
120
121	if (_savestate) {
122		_savestate->close(_savestate);
123	}
124	cleanup:
125	freeArguments(&args);
126
127#ifdef _3DS
128	gfxExit();
129	acExit();
130#endif
131
132	return didFail;
133}
134
135bool _mPerfRunCore(const char* fname, const struct mArguments* args, const struct PerfOpts* perfOpts) {
136	struct mCore* core = mCoreFind(fname);
137	if (!core) {
138		return false;
139	}
140
141	// TODO: Put back debugger
142	char gameCode[9] = { 0 };
143
144	core->init(core);
145	if (!perfOpts->noVideo) {
146		core->setVideoBuffer(core, _outputBuffer, 256);
147	}
148	mCoreLoadFile(core, fname);
149	mCoreConfigInit(&core->config, "perf");
150	mCoreConfigLoad(&core->config);
151
152	struct mCoreOptions opts = {};
153	mCoreConfigMap(&core->config, &opts);
154	opts.audioSync = false;
155	opts.videoSync = false;
156	applyArguments(args, NULL, &core->config);
157	mCoreConfigLoadDefaults(&core->config, &opts);
158	mCoreConfigSetDefaultValue(&core->config, "idleOptimization", "detect");
159	mCoreLoadConfig(core);
160
161	core->reset(core);
162	if (_savestate) {
163		mCoreLoadStateNamed(core, _savestate, 0);
164	}
165
166	core->getGameCode(core, gameCode);
167
168	int frames = perfOpts->frames;
169	if (!frames) {
170		frames = perfOpts->duration * 60;
171	}
172	struct timeval tv;
173	gettimeofday(&tv, 0);
174	uint64_t start = 1000000LL * tv.tv_sec + tv.tv_usec;
175	_mPerfRunloop(core, &frames, perfOpts->csv);
176	gettimeofday(&tv, 0);
177	uint64_t end = 1000000LL * tv.tv_sec + tv.tv_usec;
178	uint64_t duration = end - start;
179
180	core->deinit(core);
181
182	float scaledFrames = frames * 1000000.f;
183	if (perfOpts->csv) {
184		char buffer[256];
185		const char* rendererName;
186		if (perfOpts->noVideo) {
187			rendererName = "none";
188		} else {
189			rendererName = "software";
190		}
191		snprintf(buffer, sizeof(buffer), "%s,%i,%" PRIu64 ",%s\n", gameCode, frames, duration, rendererName);
192		printf("%s", buffer);
193		if (_socket != INVALID_SOCKET) {
194			SocketSend(_socket, buffer, strlen(buffer));
195		}
196	} else {
197		printf("%u frames in %" PRIu64 " microseconds: %g fps (%gx)\n", frames, duration, scaledFrames / duration, scaledFrames / (duration * 60.f));
198	}
199
200	mCoreConfigFreeOpts(&opts);
201	mCoreConfigDeinit(&core->config);
202	return true;
203}
204
205static void _mPerfRunloop(struct mCore* core, int* frames, bool quiet) {
206	struct timeval lastEcho;
207	gettimeofday(&lastEcho, 0);
208	int duration = *frames;
209	*frames = 0;
210	int lastFrames = 0;
211	while (!_dispatchExiting) {
212		core->runFrame(core);
213		++*frames;
214		++lastFrames;
215		if (!quiet) {
216			struct timeval currentTime;
217			long timeDiff;
218			gettimeofday(&currentTime, 0);
219			timeDiff = currentTime.tv_sec - lastEcho.tv_sec;
220			timeDiff *= 1000;
221			timeDiff += (currentTime.tv_usec - lastEcho.tv_usec) / 1000;
222			if (timeDiff >= 1000) {
223				printf("\033[2K\rCurrent FPS: %g (%gx)", lastFrames / (timeDiff / 1000.0f), lastFrames / (float) (60 * (timeDiff / 1000.0f)));
224				fflush(stdout);
225				lastEcho = currentTime;
226				lastFrames = 0;
227			}
228		}
229		if (duration > 0 && *frames == duration) {
230			break;
231		}
232	}
233	if (!quiet) {
234		printf("\033[2K\r");
235	}
236}
237
238static bool _mPerfRunServer(const char* listen, const struct mArguments* args, const struct PerfOpts* perfOpts) {
239	SocketSubsystemInit();
240	Socket server = SocketOpenTCP(7216, NULL);
241	if (SOCKET_FAILED(server)) {
242		SocketSubsystemDeinit();
243		return false;
244	}
245	if (SOCKET_FAILED(SocketListen(server, 0))) {
246		SocketClose(server);
247		SocketSubsystemDeinit();
248		return false;
249	}
250	_socket = SocketAccept(server, NULL);
251	if (perfOpts->csv) {
252		const char* header = "game_code,frames,duration,renderer\n";
253		SocketSend(_socket, header, strlen(header));
254	}
255	char path[PATH_MAX];
256	while (SocketRecv(_socket, path, sizeof(path)) > 0) {
257		char* nl = strchr(path, '\n');
258		if (nl == path) {
259			break;
260		}
261		if (nl) {
262			nl[0] = '\0';
263		}
264		if (!_mPerfRunCore(path, args, perfOpts)) {
265			break;
266		}
267	}
268	SocketClose(_socket);
269	SocketClose(server);
270	SocketSubsystemDeinit();
271	return true;
272}
273
274static void _mPerfShutdown(int signal) {
275	UNUSED(signal);
276	_dispatchExiting = true;
277	SocketClose(_socket);
278}
279
280static bool _parsePerfOpts(struct mSubParser* parser, int option, const char* arg) {
281	struct PerfOpts* opts = parser->opts;
282	errno = 0;
283	switch (option) {
284	case 'D':
285		opts->server = true;
286		return true;
287	case 'F':
288		opts->frames = strtoul(arg, 0, 10);
289		return !errno;
290	case 'N':
291		opts->noVideo = true;
292		return true;
293	case 'P':
294		opts->csv = true;
295		return true;
296	case 'S':
297		opts->duration = strtoul(arg, 0, 10);
298		return !errno;
299	case 'L':
300		opts->savestate = strdup(arg);
301		return true;
302	default:
303		return false;
304	}
305}
306
307static void _log(struct mLogger* log, int category, enum mLogLevel level, const char* format, va_list args) {
308	UNUSED(log);
309	UNUSED(category);
310	UNUSED(level);
311	UNUSED(format);
312	UNUSED(args);
313}