all repos — mgba @ e9a2b2a57c4f478fb3600fe9d87e1b97804d73de

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