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;
39
40int main(int argc, char** argv) {
41 signal(SIGINT, _GBAPerfShutdown);
42
43 struct GBAVideoSoftwareRenderer renderer;
44 GBAVideoSoftwareRendererCreate(&renderer);
45
46 struct PerfOpts perfOpts = { false, false, 0, 0 };
47 struct SubParser subparser = {
48 .usage = PERF_USAGE,
49 .parse = _parsePerfOpts,
50 .extraOptions = PERF_OPTIONS,
51 .opts = &perfOpts
52 };
53
54 struct GBAConfig config;
55 GBAConfigInit(&config, "perf");
56
57 struct GBAOptions opts = {};
58 struct GBAArguments args = {};
59 if (!parseArguments(&args, &config, argc, argv, &subparser)) {
60 usage(argv[0], PERF_USAGE);
61 freeArguments(&args);
62 GBAConfigFreeOpts(&opts);
63 GBAConfigDeinit(&config);
64 return 1;
65 }
66
67 renderer.outputBuffer = malloc(256 * 256 * 4);
68 renderer.outputBufferStride = 256;
69
70 struct GBAThread context = { };
71 _thread = &context;
72
73 if (!perfOpts.noVideo) {
74 context.renderer = &renderer.d;
75 }
76
77 context.debugger = createDebugger(&args, &context);
78 char gameCode[5] = { 0 };
79
80 GBAConfigMap(&config, &opts);
81 opts.audioSync = false;
82 opts.videoSync = false;
83 GBAMapArgumentsToContext(&args, &context);
84 GBAMapOptionsToContext(&opts, &context);
85
86 GBAThreadStart(&context);
87 GBAGetGameCode(context.gba, gameCode);
88
89 int frames = perfOpts.frames;
90 if (!frames) {
91 frames = perfOpts.duration * 60;
92 }
93 struct timeval tv;
94 gettimeofday(&tv, 0);
95 uint64_t start = 1000000LL * tv.tv_sec + tv.tv_usec;
96 _GBAPerfRunloop(&context, &frames, perfOpts.csv);
97 gettimeofday(&tv, 0);
98 uint64_t end = 1000000LL * tv.tv_sec + tv.tv_usec;
99 uint64_t duration = end - start;
100
101 GBAThreadJoin(&context);
102 GBAConfigFreeOpts(&opts);
103 freeArguments(&args);
104 GBAConfigDeinit(&config);
105 free(context.debugger);
106
107 free(renderer.outputBuffer);
108
109 float scaledFrames = frames * 1000000.f;
110 if (perfOpts.csv) {
111 puts("game_code,frames,duration,renderer");
112 const char* rendererName;
113 if (perfOpts.noVideo) {
114 rendererName = "none";
115 } else {
116 rendererName = "software";
117 }
118 printf("%s,%i,%" PRIu64 ",%s\n", gameCode, frames, duration, rendererName);
119 } else {
120 printf("%u frames in %" PRIu64 " microseconds: %g fps (%gx)\n", frames, duration, scaledFrames / duration, scaledFrames / (duration * 60.f));
121 }
122
123 return 0;
124}
125
126static void _GBAPerfRunloop(struct GBAThread* context, int* frames, bool quiet) {
127 struct timeval lastEcho;
128 gettimeofday(&lastEcho, 0);
129 int duration = *frames;
130 *frames = 0;
131 int lastFrames = 0;
132 while (context->state < THREAD_EXITING) {
133 if (GBASyncWaitFrameStart(&context->sync, 0)) {
134 ++*frames;
135 ++lastFrames;
136 if (!quiet) {
137 struct timeval currentTime;
138 long timeDiff;
139 gettimeofday(¤tTime, 0);
140 timeDiff = currentTime.tv_sec - lastEcho.tv_sec;
141 timeDiff *= 1000;
142 timeDiff += (currentTime.tv_usec - lastEcho.tv_usec) / 1000;
143 if (timeDiff >= 1000) {
144 printf("\033[2K\rCurrent FPS: %g (%gx)", lastFrames / (timeDiff / 1000.0f), lastFrames / (float) (60 * (timeDiff / 1000.0f)));
145 fflush(stdout);
146 lastEcho = currentTime;
147 lastFrames = 0;
148 }
149 }
150 }
151 GBASyncWaitFrameEnd(&context->sync);
152 if (*frames == duration) {
153 _GBAPerfShutdown(0);
154 }
155 }
156 if (!quiet) {
157 printf("\033[2K\r");
158 }
159}
160
161static void _GBAPerfShutdown(int signal) {
162 UNUSED(signal);
163 GBAThreadEnd(_thread);
164}
165
166static bool _parsePerfOpts(struct SubParser* parser, struct GBAConfig* config, int option, const char* arg) {
167 UNUSED(config);
168 struct PerfOpts* opts = parser->opts;
169 errno = 0;
170 switch (option) {
171 case 'F':
172 opts->frames = strtoul(arg, 0, 10);
173 return !errno;
174 case 'N':
175 opts->noVideo = true;
176 return true;
177 case 'P':
178 opts->csv = true;
179 return true;
180 case 'S':
181 opts->duration = strtoul(arg, 0, 10);
182 return !errno;
183 default:
184 return false;
185 }
186}