all repos — mgba @ 5f62e33717f8da0b22673621253cf2e265a71603

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