all repos — mgba @ 6d898542c765f4efc4a094c5ebd3f3465c36f417

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