src/platform/test/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/context/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 if (args.showVersion) {
81 version(argv[0]);
82 freeArguments(&args);
83 GBAConfigFreeOpts(&opts);
84 GBAConfigDeinit(&config);
85 return 0;
86 }
87
88 renderer.outputBuffer = malloc(256 * 256 * 4);
89 renderer.outputBufferStride = 256;
90
91 struct GBAThread context = {};
92 _thread = &context;
93
94 if (!perfOpts.noVideo) {
95 context.renderer = &renderer.d;
96 }
97 if (perfOpts.savestate) {
98 _savestate = VFileOpen(perfOpts.savestate, O_RDONLY);
99 free(perfOpts.savestate);
100 }
101 if (_savestate) {
102 context.startCallback = _loadSavestate;
103 }
104
105 context.debugger = createDebugger(&args, &context);
106 context.overrides = GBAConfigGetOverrides(&config);
107 char gameCode[5] = { 0 };
108
109 GBAConfigMap(&config, &opts);
110 opts.audioSync = false;
111 opts.videoSync = false;
112 GBAMapArgumentsToContext(&args, &context);
113 GBAMapOptionsToContext(&opts, &context);
114
115 int didStart = GBAThreadStart(&context);
116
117 if (!didStart) {
118 goto cleanup;
119 }
120 GBAThreadInterrupt(&context);
121 if (GBAThreadHasCrashed(&context)) {
122 GBAThreadJoin(&context);
123 goto cleanup;
124 }
125
126 GBAGetGameCode(context.gba, gameCode);
127 GBAThreadContinue(&context);
128
129 int frames = perfOpts.frames;
130 if (!frames) {
131 frames = perfOpts.duration * 60;
132 }
133 struct timeval tv;
134 gettimeofday(&tv, 0);
135 uint64_t start = 1000000LL * tv.tv_sec + tv.tv_usec;
136 _GBAPerfRunloop(&context, &frames, perfOpts.csv);
137 gettimeofday(&tv, 0);
138 uint64_t end = 1000000LL * tv.tv_sec + tv.tv_usec;
139 uint64_t duration = end - start;
140
141 GBAThreadJoin(&context);
142
143 float scaledFrames = frames * 1000000.f;
144 if (perfOpts.csv) {
145 puts("game_code,frames,duration,renderer");
146 const char* rendererName;
147 if (perfOpts.noVideo) {
148 rendererName = "none";
149 } else {
150 rendererName = "software";
151 }
152 printf("%s,%i,%" PRIu64 ",%s\n", gameCode, frames, duration, rendererName);
153 } else {
154 printf("%u frames in %" PRIu64 " microseconds: %g fps (%gx)\n", frames, duration, scaledFrames / duration, scaledFrames / (duration * 60.f));
155 }
156
157cleanup:
158 if (_savestate) {
159 _savestate->close(_savestate);
160 }
161 GBAConfigFreeOpts(&opts);
162 freeArguments(&args);
163 GBAConfigDeinit(&config);
164 free(context.debugger);
165 free(renderer.outputBuffer);
166
167 return !didStart || GBAThreadHasCrashed(&context);
168}
169
170static void _GBAPerfRunloop(struct GBAThread* context, int* frames, bool quiet) {
171 struct timeval lastEcho;
172 gettimeofday(&lastEcho, 0);
173 int duration = *frames;
174 *frames = 0;
175 int lastFrames = 0;
176 while (context->state < THREAD_EXITING) {
177 if (GBASyncWaitFrameStart(&context->sync)) {
178 ++*frames;
179 ++lastFrames;
180 if (!quiet) {
181 struct timeval currentTime;
182 long timeDiff;
183 gettimeofday(¤tTime, 0);
184 timeDiff = currentTime.tv_sec - lastEcho.tv_sec;
185 timeDiff *= 1000;
186 timeDiff += (currentTime.tv_usec - lastEcho.tv_usec) / 1000;
187 if (timeDiff >= 1000) {
188 printf("\033[2K\rCurrent FPS: %g (%gx)", lastFrames / (timeDiff / 1000.0f), lastFrames / (float) (60 * (timeDiff / 1000.0f)));
189 fflush(stdout);
190 lastEcho = currentTime;
191 lastFrames = 0;
192 }
193 }
194 }
195 GBASyncWaitFrameEnd(&context->sync);
196 if (duration > 0 && *frames == duration) {
197 _GBAPerfShutdown(0);
198 }
199 if (_dispatchExiting) {
200 GBAThreadEnd(context);
201 }
202 }
203 if (!quiet) {
204 printf("\033[2K\r");
205 }
206}
207
208static void _GBAPerfShutdown(int signal) {
209 UNUSED(signal);
210 // This will come in ON the GBA thread, so we have to handle it carefully
211 _dispatchExiting = true;
212 ConditionWake(&_thread->sync.videoFrameAvailableCond);
213}
214
215static bool _parsePerfOpts(struct SubParser* parser, struct GBAConfig* config, int option, const char* arg) {
216 UNUSED(config);
217 struct PerfOpts* opts = parser->opts;
218 errno = 0;
219 switch (option) {
220 case 'F':
221 opts->frames = strtoul(arg, 0, 10);
222 return !errno;
223 case 'N':
224 opts->noVideo = true;
225 return true;
226 case 'P':
227 opts->csv = true;
228 return true;
229 case 'S':
230 opts->duration = strtoul(arg, 0, 10);
231 return !errno;
232 case 'L':
233 opts->savestate = strdup(arg);
234 return true;
235 default:
236 return false;
237 }
238}
239
240static void _loadSavestate(struct GBAThread* context) {
241 GBALoadStateNamed(context->gba, _savestate);
242 _savestate->close(_savestate);
243 _savestate = 0;
244}