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#include "gba/serialize.h"
11
12#include "platform/commandline.h"
13#include "util/string.h"
14#include "util/vfs.h"
15
16#include <errno.h>
17#include <fcntl.h>
18#include <signal.h>
19#include <inttypes.h>
20#include <sys/time.h>
21
22#define PERF_OPTIONS "F:L:NPS:"
23#define PERF_USAGE \
24 "\nBenchmark options:\n" \
25 " -F FRAMES Run for the specified number of FRAMES before exiting\n" \
26 " -N Disable video rendering entirely\n" \
27 " -P CSV output, useful for parsing\n" \
28 " -S SEC Run for SEC in-game seconds before exiting\n" \
29 " -L FILE Load a savestate when starting the test"
30
31struct PerfOpts {
32 bool noVideo;
33 bool csv;
34 unsigned duration;
35 unsigned frames;
36 char* savestate;
37};
38
39static void _GBAPerfRunloop(struct GBAThread* context, int* frames, bool quiet);
40static void _GBAPerfShutdown(int signal);
41static bool _parsePerfOpts(struct SubParser* parser, struct GBAConfig* config, int option, const char* arg);
42static void _loadSavestate(struct GBAThread* context);
43
44static struct GBAThread* _thread;
45static bool _dispatchExiting = false;
46static struct VFile* _savestate = 0;
47
48int main(int argc, char** argv) {
49 signal(SIGINT, _GBAPerfShutdown);
50
51 struct GBAVideoSoftwareRenderer renderer;
52 GBAVideoSoftwareRendererCreate(&renderer);
53
54 struct PerfOpts perfOpts = { false, false, 0, 0, 0 };
55 struct SubParser subparser = {
56 .usage = PERF_USAGE,
57 .parse = _parsePerfOpts,
58 .extraOptions = PERF_OPTIONS,
59 .opts = &perfOpts
60 };
61
62 struct GBAConfig config;
63 GBAConfigInit(&config, "perf");
64 GBAConfigLoad(&config);
65
66 struct GBAOptions opts = {
67 .idleOptimization = IDLE_LOOP_DETECT
68 };
69 GBAConfigLoadDefaults(&config, &opts);
70
71 struct GBAArguments args;
72 bool parsed = parseArguments(&args, &config, argc, argv, &subparser);
73 if (!parsed || args.showHelp) {
74 usage(argv[0], PERF_USAGE);
75 freeArguments(&args);
76 GBAConfigFreeOpts(&opts);
77 GBAConfigDeinit(&config);
78 return !parsed;
79 }
80
81 renderer.outputBuffer = malloc(256 * 256 * 4);
82 renderer.outputBufferStride = 256;
83
84 struct GBAThread context = {};
85 _thread = &context;
86
87 if (!perfOpts.noVideo) {
88 context.renderer = &renderer.d;
89 }
90 if (perfOpts.savestate) {
91 _savestate = VFileOpen(perfOpts.savestate, O_RDONLY);
92 free(perfOpts.savestate);
93 }
94 if (_savestate) {
95 context.startCallback = _loadSavestate;
96 }
97
98 context.debugger = createDebugger(&args, &context);
99 context.overrides = GBAConfigGetOverrides(&config);
100 char gameCode[5] = { 0 };
101
102 GBAConfigMap(&config, &opts);
103 opts.audioSync = false;
104 opts.videoSync = false;
105 GBAMapArgumentsToContext(&args, &context);
106 GBAMapOptionsToContext(&opts, &context);
107
108 int didStart = GBAThreadStart(&context);
109
110 if (!didStart) {
111 goto cleanup;
112 }
113 GBAThreadInterrupt(&context);
114 if (GBAThreadHasCrashed(&context)) {
115 GBAThreadJoin(&context);
116 goto cleanup;
117 }
118
119 GBAGetGameCode(context.gba, gameCode);
120 GBAThreadContinue(&context);
121
122 int frames = perfOpts.frames;
123 if (!frames) {
124 frames = perfOpts.duration * 60;
125 }
126 struct timeval tv;
127 gettimeofday(&tv, 0);
128 uint64_t start = 1000000LL * tv.tv_sec + tv.tv_usec;
129 _GBAPerfRunloop(&context, &frames, perfOpts.csv);
130 gettimeofday(&tv, 0);
131 uint64_t end = 1000000LL * tv.tv_sec + tv.tv_usec;
132 uint64_t duration = end - start;
133
134 GBAThreadJoin(&context);
135
136 float scaledFrames = frames * 1000000.f;
137 if (perfOpts.csv) {
138 puts("game_code,frames,duration,renderer");
139 const char* rendererName;
140 if (perfOpts.noVideo) {
141 rendererName = "none";
142 } else {
143 rendererName = "software";
144 }
145 printf("%s,%i,%" PRIu64 ",%s\n", gameCode, frames, duration, rendererName);
146 } else {
147 printf("%u frames in %" PRIu64 " microseconds: %g fps (%gx)\n", frames, duration, scaledFrames / duration, scaledFrames / (duration * 60.f));
148 }
149
150cleanup:
151 if (_savestate) {
152 _savestate->close(_savestate);
153 }
154 GBAConfigFreeOpts(&opts);
155 freeArguments(&args);
156 GBAConfigDeinit(&config);
157 free(context.debugger);
158 free(renderer.outputBuffer);
159
160 return !didStart || GBAThreadHasCrashed(&context);
161}
162
163static void _GBAPerfRunloop(struct GBAThread* 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 (GBASyncWaitFrameStart(&context->sync, 0)) {
171 ++*frames;
172 ++lastFrames;
173 if (!quiet) {
174 struct timeval currentTime;
175 long timeDiff;
176 gettimeofday(¤tTime, 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 GBASyncWaitFrameEnd(&context->sync);
189 if (*frames == duration) {
190 _GBAPerfShutdown(0);
191 }
192 if (_dispatchExiting) {
193 GBAThreadEnd(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 SubParser* parser, struct GBAConfig* config, int option, const char* arg) {
209 UNUSED(config);
210 struct PerfOpts* opts = parser->opts;
211 errno = 0;
212 switch (option) {
213 case 'F':
214 opts->frames = strtoul(arg, 0, 10);
215 return !errno;
216 case 'N':
217 opts->noVideo = true;
218 return true;
219 case 'P':
220 opts->csv = true;
221 return true;
222 case 'S':
223 opts->duration = strtoul(arg, 0, 10);
224 return !errno;
225 case 'L':
226 opts->savestate = strdup(arg);
227 return true;
228 default:
229 return false;
230 }
231}
232
233static void _loadSavestate(struct GBAThread* context) {
234 GBALoadStateNamed(context->gba, _savestate);
235 _savestate->close(_savestate);
236 _savestate = 0;
237}