all repos — mgba @ 407335e2f464828d5fe517372442fce798b58490

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/supervisor/thread.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#include <errno.h>
 17#include <fcntl.h>
 18#include <signal.h>
 19#include <inttypes.h>
 20#include <sys/time.h>
 21
 22#define PERF_OPTIONS "F:L:NPS:"
 23#define PERF_USAGE \
 24	"\nBenchmark options:\n" \
 25	"  -F FRAMES        Run for the specified number of FRAMES before exiting\n" \
 26	"  -N               Disable video rendering entirely\n" \
 27	"  -P               CSV output, useful for parsing\n" \
 28	"  -S SEC           Run for SEC in-game seconds before exiting\n" \
 29	"  -L FILE          Load a savestate when starting the test"
 30
 31struct PerfOpts {
 32	bool noVideo;
 33	bool csv;
 34	unsigned duration;
 35	unsigned frames;
 36	char* savestate;
 37};
 38
 39static void _GBAPerfRunloop(struct GBAThread* context, int* frames, bool quiet);
 40static void _GBAPerfShutdown(int signal);
 41static bool _parsePerfOpts(struct mSubParser* parser, int option, const char* arg);
 42static void _loadSavestate(struct GBAThread* context);
 43
 44static struct GBAThread* _thread;
 45static bool _dispatchExiting = false;
 46static struct VFile* _savestate = 0;
 47
 48int main(int argc, char** argv) {
 49	signal(SIGINT, _GBAPerfShutdown);
 50
 51	struct GBAVideoSoftwareRenderer renderer;
 52	GBAVideoSoftwareRendererCreate(&renderer);
 53
 54	struct PerfOpts perfOpts = { false, false, 0, 0, 0 };
 55	struct mSubParser subparser = {
 56		.usage = PERF_USAGE,
 57		.parse = _parsePerfOpts,
 58		.extraOptions = PERF_OPTIONS,
 59		.opts = &perfOpts
 60	};
 61
 62	struct mCoreConfig config;
 63	mCoreConfigInit(&config, "perf");
 64	mCoreConfigLoad(&config);
 65
 66	struct mCoreOptions opts = {}; // TODO: Put back idle loops
 67	mCoreConfigLoadDefaults(&config, &opts);
 68
 69	struct mArguments args;
 70	bool parsed = parseArguments(&args, argc, argv, &subparser);
 71	if (!parsed || args.showHelp) {
 72		usage(argv[0], PERF_USAGE);
 73		freeArguments(&args);
 74		mCoreConfigFreeOpts(&opts);
 75		mCoreConfigDeinit(&config);
 76		return !parsed;
 77	}
 78	if (args.showVersion) {
 79		version(argv[0]);
 80		freeArguments(&args);
 81		mCoreConfigFreeOpts(&opts);
 82		mCoreConfigDeinit(&config);
 83		return 0;
 84	}
 85	applyArguments(&args, NULL, &config);
 86
 87	renderer.outputBuffer = malloc(256 * 256 * 4);
 88	renderer.outputBufferStride = 256;
 89
 90	struct GBAThread context = {};
 91	_thread = &context;
 92
 93	if (!perfOpts.noVideo) {
 94		context.renderer = &renderer.d;
 95	}
 96	if (perfOpts.savestate) {
 97		_savestate = VFileOpen(perfOpts.savestate, O_RDONLY);
 98		free(perfOpts.savestate);
 99	}
100	if (_savestate) {
101		context.startCallback = _loadSavestate;
102	}
103
104	context.debugger = createDebugger(&args, &context);
105	context.overrides = mCoreConfigGetOverrides(&config);
106	char gameCode[5] = { 0 };
107
108	mCoreConfigMap(&config, &opts);
109	opts.audioSync = false;
110	opts.videoSync = false;
111	GBAMapArgumentsToContext(&args, &context);
112	GBAMapOptionsToContext(&opts, &context);
113
114	int didStart = GBAThreadStart(&context);
115
116	if (!didStart) {
117		goto cleanup;
118	}
119	GBAThreadInterrupt(&context);
120	if (GBAThreadHasCrashed(&context)) {
121		GBAThreadJoin(&context);
122		goto cleanup;
123	}
124
125	GBAGetGameCode(context.gba, gameCode);
126	GBAThreadContinue(&context);
127
128	int frames = perfOpts.frames;
129	if (!frames) {
130		frames = perfOpts.duration * 60;
131	}
132	struct timeval tv;
133	gettimeofday(&tv, 0);
134	uint64_t start = 1000000LL * tv.tv_sec + tv.tv_usec;
135	_GBAPerfRunloop(&context, &frames, perfOpts.csv);
136	gettimeofday(&tv, 0);
137	uint64_t end = 1000000LL * tv.tv_sec + tv.tv_usec;
138	uint64_t duration = end - start;
139
140	GBAThreadJoin(&context);
141
142	float scaledFrames = frames * 1000000.f;
143	if (perfOpts.csv) {
144		puts("game_code,frames,duration,renderer");
145		const char* rendererName;
146		if (perfOpts.noVideo) {
147			rendererName = "none";
148		} else {
149			rendererName = "software";
150		}
151		printf("%s,%i,%" PRIu64 ",%s\n", gameCode, frames, duration, rendererName);
152	} else {
153		printf("%u frames in %" PRIu64 " microseconds: %g fps (%gx)\n", frames, duration, scaledFrames / duration, scaledFrames / (duration * 60.f));
154	}
155
156cleanup:
157	if (_savestate) {
158		_savestate->close(_savestate);
159	}
160	mCoreConfigFreeOpts(&opts);
161	freeArguments(&args);
162	mCoreConfigDeinit(&config);
163	free(context.debugger);
164	free(renderer.outputBuffer);
165
166	return !didStart || GBAThreadHasCrashed(&context);
167}
168
169static void _GBAPerfRunloop(struct GBAThread* context, int* frames, bool quiet) {
170	struct timeval lastEcho;
171	gettimeofday(&lastEcho, 0);
172	int duration = *frames;
173	*frames = 0;
174	int lastFrames = 0;
175	while (context->state < THREAD_EXITING) {
176		if (mCoreSyncWaitFrameStart(&context->sync)) {
177			++*frames;
178			++lastFrames;
179			if (!quiet) {
180				struct timeval currentTime;
181				long timeDiff;
182				gettimeofday(&currentTime, 0);
183				timeDiff = currentTime.tv_sec - lastEcho.tv_sec;
184				timeDiff *= 1000;
185				timeDiff += (currentTime.tv_usec - lastEcho.tv_usec) / 1000;
186				if (timeDiff >= 1000) {
187					printf("\033[2K\rCurrent FPS: %g (%gx)", lastFrames / (timeDiff / 1000.0f), lastFrames / (float) (60 * (timeDiff / 1000.0f)));
188					fflush(stdout);
189					lastEcho = currentTime;
190					lastFrames = 0;
191				}
192			}
193		}
194		mCoreSyncWaitFrameEnd(&context->sync);
195		if (duration > 0 && *frames == duration) {
196			_GBAPerfShutdown(0);
197		}
198		if (_dispatchExiting) {
199			GBAThreadEnd(context);
200		}
201	}
202	if (!quiet) {
203		printf("\033[2K\r");
204	}
205}
206
207static void _GBAPerfShutdown(int signal) {
208	UNUSED(signal);
209	// This will come in ON the GBA thread, so we have to handle it carefully
210	_dispatchExiting = true;
211	ConditionWake(&_thread->sync.videoFrameAvailableCond);
212}
213
214static bool _parsePerfOpts(struct mSubParser* parser, int option, const char* arg) {
215	struct PerfOpts* opts = parser->opts;
216	errno = 0;
217	switch (option) {
218	case 'F':
219		opts->frames = strtoul(arg, 0, 10);
220		return !errno;
221	case 'N':
222		opts->noVideo = true;
223		return true;
224	case 'P':
225		opts->csv = true;
226		return true;
227	case 'S':
228		opts->duration = strtoul(arg, 0, 10);
229		return !errno;
230	case 'L':
231		opts->savestate = strdup(arg);
232		return true;
233	default:
234		return false;
235	}
236}
237
238static void _loadSavestate(struct GBAThread* context) {
239	GBALoadStateNamed(context->gba, _savestate, 0);
240	_savestate->close(_savestate);
241	_savestate = 0;
242}