all repos — mgba @ 9f6837da422c9f7b1a83e6ace63b23a2a8f1e393

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 "platform/commandline.h"
 13#include "util/string.h"
 14#include "util/vfs.h"
 15
 16#ifdef _3DS
 17#include <3ds.h>
 18#endif
 19
 20#include <errno.h>
 21#include <fcntl.h>
 22#include <signal.h>
 23#include <inttypes.h>
 24#include <sys/time.h>
 25
 26#define PERF_OPTIONS "F:L:NPS:"
 27#define PERF_USAGE \
 28	"\nBenchmark options:\n" \
 29	"  -F FRAMES        Run for the specified number of FRAMES before exiting\n" \
 30	"  -N               Disable video rendering entirely\n" \
 31	"  -P               CSV output, useful for parsing\n" \
 32	"  -S SEC           Run for SEC in-game seconds before exiting\n" \
 33	"  -L FILE          Load a savestate when starting the test"
 34
 35struct PerfOpts {
 36	bool noVideo;
 37	bool csv;
 38	unsigned duration;
 39	unsigned frames;
 40	char* savestate;
 41};
 42
 43#ifdef _3DS
 44extern bool allocateRomBuffer(void);
 45FS_Archive sdmcArchive;
 46#endif
 47
 48static void _mPerfRunloop(struct mCore* context, int* frames, bool quiet);
 49static void _mPerfShutdown(int signal);
 50static bool _parsePerfOpts(struct mSubParser* parser, int option, const char* arg);
 51static void _log(struct mLogger*, int, enum mLogLevel, const char*, va_list);
 52
 53static bool _dispatchExiting = false;
 54static struct VFile* _savestate = 0;
 55
 56int main(int argc, char** argv) {
 57#ifdef _3DS
 58    gfxInitDefault();
 59    osSetSpeedupEnable(true);
 60	consoleInit(GFX_BOTTOM, NULL);
 61	if (!allocateRomBuffer()) {
 62		return 1;
 63	}
 64	sdmcArchive = (FS_Archive) {
 65		ARCHIVE_SDMC,
 66		(FS_Path) { PATH_EMPTY, 1, "" },
 67		0
 68	};
 69	FSUSER_OpenArchive(&sdmcArchive);
 70#else
 71	signal(SIGINT, _mPerfShutdown);
 72#endif
 73	int didFail = 0;
 74
 75	struct mLogger logger = { .log = _log };
 76	mLogSetDefaultLogger(&logger);
 77
 78	struct PerfOpts perfOpts = { false, false, 0, 0, 0 };
 79	struct mSubParser subparser = {
 80		.usage = PERF_USAGE,
 81		.parse = _parsePerfOpts,
 82		.extraOptions = PERF_OPTIONS,
 83		.opts = &perfOpts
 84	};
 85
 86	struct mArguments args = {};
 87	bool parsed = parseArguments(&args, argc, argv, &subparser);
 88	if (!parsed || args.showHelp) {
 89		usage(argv[0], PERF_USAGE);
 90		didFail = !parsed;
 91		goto cleanup;
 92	}
 93
 94	if (args.showVersion) {
 95		version(argv[0]);
 96		goto cleanup;
 97	}
 98
 99	void* outputBuffer = malloc(256 * 256 * 4);
100
101	struct mCore* core = mCoreFind(args.fname);
102	if (!core) {
103		didFail = 1;
104		goto cleanup;
105	}
106
107	if (!perfOpts.noVideo) {
108		core->setVideoBuffer(core, outputBuffer, 256);
109	}
110	if (perfOpts.savestate) {
111		_savestate = VFileOpen(perfOpts.savestate, O_RDONLY);
112		free(perfOpts.savestate);
113	}
114
115	// TODO: Put back debugger
116	char gameCode[5] = { 0 };
117
118	core->init(core);
119	mCoreLoadFile(core, args.fname);
120	mCoreConfigInit(&core->config, "perf");
121	mCoreConfigLoad(&core->config);
122
123	mCoreConfigSetDefaultIntValue(&core->config, "idleOptimization", IDLE_LOOP_REMOVE);
124	struct mCoreOptions opts = {};
125	mCoreConfigMap(&core->config, &opts);
126	opts.audioSync = false;
127	opts.videoSync = false;
128	applyArguments(&args, NULL, &core->config);
129	mCoreConfigLoadDefaults(&core->config, &opts);
130	mCoreLoadConfig(core);
131
132	core->reset(core);
133	if (_savestate) {
134		core->loadState(core, _savestate, 0);
135		_savestate->close(_savestate);
136		_savestate = NULL;
137	}
138
139	core->getGameCode(core, gameCode);
140
141	int frames = perfOpts.frames;
142	if (!frames) {
143		frames = perfOpts.duration * 60;
144	}
145	struct timeval tv;
146	gettimeofday(&tv, 0);
147	uint64_t start = 1000000LL * tv.tv_sec + tv.tv_usec;
148	_mPerfRunloop(core, &frames, perfOpts.csv);
149	gettimeofday(&tv, 0);
150	uint64_t end = 1000000LL * tv.tv_sec + tv.tv_usec;
151	uint64_t duration = end - start;
152
153	core->deinit(core);
154
155	float scaledFrames = frames * 1000000.f;
156	if (perfOpts.csv) {
157		puts("game_code,frames,duration,renderer");
158		const char* rendererName;
159		if (perfOpts.noVideo) {
160			rendererName = "none";
161		} else {
162			rendererName = "software";
163		}
164		printf("%s,%i,%" PRIu64 ",%s\n", gameCode, frames, duration, rendererName);
165	} else {
166		printf("%u frames in %" PRIu64 " microseconds: %g fps (%gx)\n", frames, duration, scaledFrames / duration, scaledFrames / (duration * 60.f));
167	}
168
169	mCoreConfigFreeOpts(&opts);
170	mCoreConfigDeinit(&core->config);
171	free(outputBuffer);
172
173	cleanup:
174	freeArguments(&args);
175
176#ifdef _3DS
177	gfxExit();
178#endif
179
180	return didFail;
181}
182
183static void _mPerfRunloop(struct mCore* core, int* frames, bool quiet) {
184	struct timeval lastEcho;
185	gettimeofday(&lastEcho, 0);
186	int duration = *frames;
187	*frames = 0;
188	int lastFrames = 0;
189	while (!_dispatchExiting) {
190		core->runFrame(core);
191		++*frames;
192		++lastFrames;
193		if (!quiet) {
194			struct timeval currentTime;
195			long timeDiff;
196			gettimeofday(&currentTime, 0);
197			timeDiff = currentTime.tv_sec - lastEcho.tv_sec;
198			timeDiff *= 1000;
199			timeDiff += (currentTime.tv_usec - lastEcho.tv_usec) / 1000;
200			if (timeDiff >= 1000) {
201				printf("\033[2K\rCurrent FPS: %g (%gx)", lastFrames / (timeDiff / 1000.0f), lastFrames / (float) (60 * (timeDiff / 1000.0f)));
202				fflush(stdout);
203				lastEcho = currentTime;
204				lastFrames = 0;
205			}
206		}
207		if (duration > 0 && *frames == duration) {
208			break;
209		}
210	}
211	if (!quiet) {
212		printf("\033[2K\r");
213	}
214}
215
216static void _mPerfShutdown(int signal) {
217	UNUSED(signal);
218	_dispatchExiting = true;
219}
220
221static bool _parsePerfOpts(struct mSubParser* parser, int option, const char* arg) {
222	struct PerfOpts* opts = parser->opts;
223	errno = 0;
224	switch (option) {
225	case 'F':
226		opts->frames = strtoul(arg, 0, 10);
227		return !errno;
228	case 'N':
229		opts->noVideo = true;
230		return true;
231	case 'P':
232		opts->csv = true;
233		return true;
234	case 'S':
235		opts->duration = strtoul(arg, 0, 10);
236		return !errno;
237	case 'L':
238		opts->savestate = strdup(arg);
239		return true;
240	default:
241		return false;
242	}
243}
244
245static void _log(struct mLogger* log, int category, enum mLogLevel level, const char* format, va_list args) {
246	UNUSED(log);
247	UNUSED(category);
248	UNUSED(level);
249	UNUSED(format);
250	UNUSED(args);
251}