all repos — mgba @ 73d2c8fda8b7d324ab2b338b17bd10d36651f0f4

mGBA Game Boy Advance Emulator

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