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