all repos — mgba @ 6a05ecf273e25fa31564226cea819f2aedc54dc4

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