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