all repos — mgba @ 7a3db31eb2c58563184fe4e4a0806dd565d98b8c

mGBA Game Boy Advance Emulator

src/platform/perf-main.c (view raw)

  1/* Copyright (c) 2013-2014 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-thread.h"
  7#include "gba-config.h"
  8#include "gba.h"
  9#include "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
 58	struct GBAOptions opts = {};
 59	struct GBAArguments args = {};
 60	if (!parseArguments(&args, &config, argc, argv, &subparser)) {
 61		usage(argv[0], PERF_USAGE);
 62		freeArguments(&args);
 63		GBAConfigFreeOpts(&opts);
 64		GBAConfigDeinit(&config);
 65		return 1;
 66	}
 67
 68	renderer.outputBuffer = malloc(256 * 256 * 4);
 69	renderer.outputBufferStride = 256;
 70
 71	struct GBAThread context = { };
 72	_thread = &context;
 73
 74	if (!perfOpts.noVideo) {
 75		context.renderer = &renderer.d;
 76	}
 77
 78	context.debugger = createDebugger(&args, &context);
 79	char gameCode[5] = { 0 };
 80
 81	GBAConfigMap(&config, &opts);
 82	opts.audioSync = false;
 83	opts.videoSync = false;
 84	GBAMapArgumentsToContext(&args, &context);
 85	GBAMapOptionsToContext(&opts, &context);
 86
 87	GBAThreadStart(&context);
 88	GBAGetGameCode(context.gba, gameCode);
 89
 90	int frames = perfOpts.frames;
 91	if (!frames) {
 92		frames = perfOpts.duration * 60;
 93	}
 94	struct timeval tv;
 95	gettimeofday(&tv, 0);
 96	uint64_t start = 1000000LL * tv.tv_sec + tv.tv_usec;
 97	_GBAPerfRunloop(&context, &frames, perfOpts.csv);
 98	gettimeofday(&tv, 0);
 99	uint64_t end = 1000000LL * tv.tv_sec + tv.tv_usec;
100	uint64_t duration = end - start;
101
102	GBAThreadJoin(&context);
103	GBAConfigFreeOpts(&opts);
104	freeArguments(&args);
105	GBAConfigDeinit(&config);
106	free(context.debugger);
107
108	free(renderer.outputBuffer);
109
110	float scaledFrames = frames * 1000000.f;
111	if (perfOpts.csv) {
112		puts("game_code,frames,duration,renderer");
113		const char* rendererName;
114		if (perfOpts.noVideo) {
115			rendererName = "none";
116		} else {
117			rendererName = "software";
118		}
119		printf("%s,%i,%" PRIu64 ",%s\n", gameCode, frames, duration, rendererName);
120	} else {
121		printf("%u frames in %" PRIu64 " microseconds: %g fps (%gx)\n", frames, duration, scaledFrames / duration, scaledFrames / (duration * 60.f));
122	}
123
124	return GBAThreadHasCrashed(&context);
125}
126
127static void _GBAPerfRunloop(struct GBAThread* context, int* frames, bool quiet) {
128	struct timeval lastEcho;
129	gettimeofday(&lastEcho, 0);
130	int duration = *frames;
131	*frames = 0;
132	int lastFrames = 0;
133	while (context->state < THREAD_EXITING) {
134		if (GBASyncWaitFrameStart(&context->sync, 0)) {
135			++*frames;
136			++lastFrames;
137			if (!quiet) {
138				struct timeval currentTime;
139				long timeDiff;
140				gettimeofday(&currentTime, 0);
141				timeDiff = currentTime.tv_sec - lastEcho.tv_sec;
142				timeDiff *= 1000;
143				timeDiff += (currentTime.tv_usec - lastEcho.tv_usec) / 1000;
144				if (timeDiff >= 1000) {
145					printf("\033[2K\rCurrent FPS: %g (%gx)", lastFrames / (timeDiff / 1000.0f), lastFrames / (float) (60 * (timeDiff / 1000.0f)));
146					fflush(stdout);
147					lastEcho = currentTime;
148					lastFrames = 0;
149				}
150			}
151		}
152		GBASyncWaitFrameEnd(&context->sync);
153		if (*frames == duration) {
154			_GBAPerfShutdown(0);
155		}
156		if (_dispatchExiting) {
157			GBAThreadEnd(context);
158		}
159	}
160	if (!quiet) {
161		printf("\033[2K\r");
162	}
163}
164
165static void _GBAPerfShutdown(int signal) {
166	UNUSED(signal);
167	// This will come in ON the GBA thread, so we have to handle it carefully
168	_dispatchExiting = true;
169	ConditionWake(&_thread->sync.videoFrameAvailableCond);
170}
171
172static bool _parsePerfOpts(struct SubParser* parser, struct GBAConfig* config, int option, const char* arg) {
173	UNUSED(config);
174	struct PerfOpts* opts = parser->opts;
175	errno = 0;
176	switch (option) {
177	case 'F':
178		opts->frames = strtoul(arg, 0, 10);
179		return !errno;
180	case 'N':
181		opts->noVideo = true;
182		return true;
183	case 'P':
184		opts->csv = true;
185		return true;
186	case 'S':
187		opts->duration = strtoul(arg, 0, 10);
188		return !errno;
189	default:
190		return false;
191	}
192}