Perf: Add server mode, support for 3DS

This commit is contained in:
Jeffrey Pfau 2016-03-01 22:01:54 -08:00
parent 6c67389e9c
commit 8d7cbbe669
6 changed files with 139 additions and 46 deletions

View File

@ -14,7 +14,7 @@ set(OS_DEFINES COLOR_16_BIT COLOR_5_6_5 IOAPI_NO_64)
include_directories(${CMAKE_CURRENT_BINARY_DIR})
list(APPEND OS_LIB ctru)
file(GLOB OS_SRC ${CMAKE_CURRENT_SOURCE_DIR}/3ds-*.c ${CMAKE_CURRENT_SOURCE_DIR}/ctru-heap.c)
file(GLOB OS_SRC ${CMAKE_CURRENT_SOURCE_DIR}/3ds-*.c ${CMAKE_CURRENT_SOURCE_DIR}/ctru-heap.c ${CMAKE_CURRENT_SOURCE_DIR}/socket.c)
set(OS_SRC ${OS_SRC} PARENT_SCOPE)
set(OS_LIB ${OS_LIB} PARENT_SCOPE)
source_group("3DS-specific code" FILES ${OS_SRC})

View File

@ -33,16 +33,6 @@ extern u32 __ctru_linear_heap_size;
static u32 __custom_heap_size = 0x02400000;
static u32 __custom_linear_heap_size = 0x01400000;
extern void (*__system_retAddr)(void);
void envDestroyHandles(void);
void __appExit();
void __libc_fini_array(void);
Result __sync_fini(void) __attribute__((weak));
uint32_t* romBuffer;
size_t romBufferSize;

View File

@ -137,7 +137,7 @@ bool parseArguments(struct mArguments* args, int argc, char* const* argv, struct
return true;
}
void applyArguments(struct mArguments* args, struct mSubParser* subparser, struct mCoreConfig* config) {
void applyArguments(const struct mArguments* args, struct mSubParser* subparser, struct mCoreConfig* config) {
if (args->frameskip >= 0) {
mCoreConfigSetOverrideIntValue(config, "frameskip", args->frameskip);
}

View File

@ -41,7 +41,7 @@ struct mGraphicsOpts {
bool parseArguments(struct mArguments* args, int argc, char* const* argv,
struct mSubParser* subparser);
void applyArguments(struct mArguments* args, struct mSubParser* subparser, struct mCoreConfig* config);
void applyArguments(const struct mArguments* args, struct mSubParser* subparser, struct mCoreConfig* config);
void freeArguments(struct mArguments* args);
void usage(const char* arg0, const char* extraOptions);

View File

@ -10,6 +10,7 @@
#include "gba/serialize.h"
#include "platform/commandline.h"
#include "util/socket.h"
#include "util/string.h"
#include "util/vfs.h"
@ -23,14 +24,15 @@
#include <inttypes.h>
#include <sys/time.h>
#define PERF_OPTIONS "F:L:NPS:"
#define PERF_OPTIONS "DF:L:NPS:"
#define PERF_USAGE \
"\nBenchmark options:\n" \
" -F FRAMES Run for the specified number of FRAMES before exiting\n" \
" -N Disable video rendering entirely\n" \
" -P CSV output, useful for parsing\n" \
" -S SEC Run for SEC in-game seconds before exiting\n" \
" -L FILE Load a savestate when starting the test"
" -L FILE Load a savestate when starting the test\n" \
" -D Act as a server"
struct PerfOpts {
bool noVideo;
@ -38,6 +40,7 @@ struct PerfOpts {
unsigned duration;
unsigned frames;
char* savestate;
bool server;
};
#ifdef _3DS
@ -49,12 +52,17 @@ static void _mPerfRunloop(struct mCore* context, int* frames, bool quiet);
static void _mPerfShutdown(int signal);
static bool _parsePerfOpts(struct mSubParser* parser, int option, const char* arg);
static void _log(struct mLogger*, int, enum mLogLevel, const char*, va_list);
static bool _mPerfRunCore(const char* fname, const struct mArguments*, const struct PerfOpts*);
static bool _mPerfRunServer(const char* listen, const struct mArguments*, const struct PerfOpts*);
static bool _dispatchExiting = false;
static struct VFile* _savestate = 0;
static void* _outputBuffer = NULL;
static Socket _socket = INVALID_SOCKET;
int main(int argc, char** argv) {
#ifdef _3DS
UNUSED(_mPerfShutdown);
gfxInitDefault();
osSetSpeedupEnable(true);
consoleInit(GFX_BOTTOM, NULL);
@ -75,7 +83,7 @@ int main(int argc, char** argv) {
struct mLogger logger = { .log = _log };
mLogSetDefaultLogger(&logger);
struct PerfOpts perfOpts = { false, false, 0, 0, 0 };
struct PerfOpts perfOpts = { false, false, 0, 0, 0, false };
struct mSubParser subparser = {
.usage = PERF_USAGE,
.parse = _parsePerfOpts,
@ -96,26 +104,50 @@ int main(int argc, char** argv) {
goto cleanup;
}
void* outputBuffer = malloc(256 * 256 * 4);
struct mCore* core = mCoreFind(args.fname);
if (!core) {
didFail = 1;
goto cleanup;
}
if (perfOpts.savestate) {
_savestate = VFileOpen(perfOpts.savestate, O_RDONLY);
free(perfOpts.savestate);
}
_outputBuffer = malloc(256 * 256 * 4);
if (perfOpts.csv) {
puts("game_code,frames,duration,renderer");
}
if (perfOpts.server) {
didFail = !_mPerfRunServer(args.fname, &args, &perfOpts);
} else {
didFail = !_mPerfRunCore(args.fname, &args, &perfOpts);
}
free(_outputBuffer);
if (_savestate) {
_savestate->close(_savestate);
}
cleanup:
freeArguments(&args);
#ifdef _3DS
gfxExit();
acExit();
#endif
return didFail;
}
bool _mPerfRunCore(const char* fname, const struct mArguments* args, const struct PerfOpts* perfOpts) {
struct mCore* core = mCoreFind(fname);
if (!core) {
return false;
}
// TODO: Put back debugger
char gameCode[5] = { 0 };
core->init(core);
if (!perfOpts.noVideo) {
core->setVideoBuffer(core, outputBuffer, 256);
if (!perfOpts->noVideo) {
core->setVideoBuffer(core, _outputBuffer, 256);
}
mCoreLoadFile(core, args.fname);
mCoreLoadFile(core, fname);
mCoreConfigInit(&core->config, "perf");
mCoreConfigLoad(&core->config);
@ -123,7 +155,7 @@ int main(int argc, char** argv) {
mCoreConfigMap(&core->config, &opts);
opts.audioSync = false;
opts.videoSync = false;
applyArguments(&args, NULL, &core->config);
applyArguments(args, NULL, &core->config);
mCoreConfigLoadDefaults(&core->config, &opts);
mCoreConfigSetDefaultValue(&core->config, "idleOptimization", "detect");
mCoreLoadConfig(core);
@ -131,20 +163,18 @@ int main(int argc, char** argv) {
core->reset(core);
if (_savestate) {
core->loadState(core, _savestate, 0);
_savestate->close(_savestate);
_savestate = NULL;
}
core->getGameCode(core, gameCode);
int frames = perfOpts.frames;
int frames = perfOpts->frames;
if (!frames) {
frames = perfOpts.duration * 60;
frames = perfOpts->duration * 60;
}
struct timeval tv;
gettimeofday(&tv, 0);
uint64_t start = 1000000LL * tv.tv_sec + tv.tv_usec;
_mPerfRunloop(core, &frames, perfOpts.csv);
_mPerfRunloop(core, &frames, perfOpts->csv);
gettimeofday(&tv, 0);
uint64_t end = 1000000LL * tv.tv_sec + tv.tv_usec;
uint64_t duration = end - start;
@ -152,31 +182,26 @@ int main(int argc, char** argv) {
core->deinit(core);
float scaledFrames = frames * 1000000.f;
if (perfOpts.csv) {
puts("game_code,frames,duration,renderer");
if (perfOpts->csv) {
char buffer[256];
const char* rendererName;
if (perfOpts.noVideo) {
if (perfOpts->noVideo) {
rendererName = "none";
} else {
rendererName = "software";
}
printf("%s,%i,%" PRIu64 ",%s\n", gameCode, frames, duration, rendererName);
snprintf(buffer, sizeof(buffer), "%s,%i,%" PRIu64 ",%s\n", gameCode, frames, duration, rendererName);
printf("%s", buffer);
if (_socket != INVALID_SOCKET) {
SocketSend(_socket, buffer, strlen(buffer));
}
} else {
printf("%u frames in %" PRIu64 " microseconds: %g fps (%gx)\n", frames, duration, scaledFrames / duration, scaledFrames / (duration * 60.f));
}
mCoreConfigFreeOpts(&opts);
mCoreConfigDeinit(&core->config);
free(outputBuffer);
cleanup:
freeArguments(&args);
#ifdef _3DS
gfxExit();
#endif
return didFail;
return true;
}
static void _mPerfRunloop(struct mCore* core, int* frames, bool quiet) {
@ -212,15 +237,55 @@ static void _mPerfRunloop(struct mCore* core, int* frames, bool quiet) {
}
}
static bool _mPerfRunServer(const char* listen, const struct mArguments* args, const struct PerfOpts* perfOpts) {
SocketSubsystemInit();
Socket server = SocketOpenTCP(7216, NULL);
if (SOCKET_FAILED(server)) {
SocketSubsystemDeinit();
return false;
}
if (SOCKET_FAILED(SocketListen(server, 0))) {
SocketClose(server);
SocketSubsystemDeinit();
return false;
}
_socket = SocketAccept(server, NULL);
if (perfOpts->csv) {
const char* header = "game_code,frames,duration,renderer\n";
SocketSend(_socket, header, strlen(header));
}
char path[PATH_MAX];
while (SocketRecv(_socket, path, sizeof(path)) > 0) {
char* nl = strchr(path, '\n');
if (nl == path) {
break;
}
if (nl) {
nl[0] = '\0';
}
if (!_mPerfRunCore(path, args, perfOpts)) {
break;
}
}
SocketClose(_socket);
SocketClose(server);
SocketSubsystemDeinit();
return true;
}
static void _mPerfShutdown(int signal) {
UNUSED(signal);
_dispatchExiting = true;
SocketClose(_socket);
}
static bool _parsePerfOpts(struct mSubParser* parser, int option, const char* arg) {
struct PerfOpts* opts = parser->opts;
errno = 0;
switch (option) {
case 'D':
opts->server = true;
return true;
case 'F':
opts->frames = strtoul(arg, 0, 10);
return !errno;

View File

@ -19,6 +19,7 @@
#define SOCKET_FAILED(s) ((s) == INVALID_SOCKET)
typedef SOCKET Socket;
#else
#include <arpa/inet.h>
#include <errno.h>
#include <fcntl.h>
#include <netinet/in.h>
@ -44,10 +45,35 @@ struct Address {
};
};
#ifdef _3DS
#include <3ds.h>
#include <malloc.h>
#define SOCU_ALIGN 0x1000
#define SOCU_BUFFERSIZE 0x100000
extern u32* SOCUBuffer;
#endif
static inline void SocketSubsystemInit() {
#ifdef _WIN32
WSADATA data;
WSAStartup(MAKEWORD(2, 2), &data);
#elif defined(_3DS)
if (!SOCUBuffer) {
SOCUBuffer = memalign(SOCU_ALIGN, SOCU_BUFFERSIZE);
socInit(SOCUBuffer, SOCU_BUFFERSIZE);
}
#endif
}
static inline void SocketSubsystemDeinit() {
#ifdef _WIN32
WSACleanup();
#elif defined(_3DS)
socExit();
free(SOCUBuffer);
SOCUBuffer = NULL;
#endif
}
@ -95,6 +121,11 @@ static inline Socket SocketOpenTCP(int port, const struct Address* bindAddress)
memset(&bindInfo, 0, sizeof(bindInfo));
bindInfo.sin_family = AF_INET;
bindInfo.sin_port = htons(port);
#ifndef _3DS
bindInfo.sin_addr.s_addr = INADDR_ANY;
#else
bindInfo.sin_addr.s_addr = gethostid();
#endif
err = bind(sock, (const struct sockaddr*) &bindInfo, sizeof(bindInfo));
} else if (bindAddress->version == IPV4) {
struct sockaddr_in bindInfo;
@ -103,6 +134,7 @@ static inline Socket SocketOpenTCP(int port, const struct Address* bindAddress)
bindInfo.sin_port = htons(port);
bindInfo.sin_addr.s_addr = bindAddress->ipv4;
err = bind(sock, (const struct sockaddr*) &bindInfo, sizeof(bindInfo));
#ifndef _3DS
} else {
struct sockaddr_in6 bindInfo;
memset(&bindInfo, 0, sizeof(bindInfo));
@ -110,6 +142,7 @@ static inline Socket SocketOpenTCP(int port, const struct Address* bindAddress)
bindInfo.sin6_port = htons(port);
memcpy(bindInfo.sin6_addr.s6_addr, bindAddress->ipv6, sizeof(bindInfo.sin6_addr.s6_addr));
err = bind(sock, (const struct sockaddr*) &bindInfo, sizeof(bindInfo));
#endif
}
if (err) {
close(sock);
@ -138,6 +171,7 @@ static inline Socket SocketConnectTCP(int port, const struct Address* destinatio
bindInfo.sin_port = htons(port);
bindInfo.sin_addr.s_addr = destinationAddress->ipv4;
err = connect(sock, (const struct sockaddr*) &bindInfo, sizeof(bindInfo));
#ifndef _3DS
} else {
struct sockaddr_in6 bindInfo;
memset(&bindInfo, 0, sizeof(bindInfo));
@ -145,6 +179,7 @@ static inline Socket SocketConnectTCP(int port, const struct Address* destinatio
bindInfo.sin6_port = htons(port);
memcpy(bindInfo.sin6_addr.s6_addr, destinationAddress->ipv6, sizeof(bindInfo.sin6_addr.s6_addr));
err = connect(sock, (const struct sockaddr*) &bindInfo, sizeof(bindInfo));
#endif
}
if (err) {
@ -169,6 +204,7 @@ static inline Socket SocketAccept(Socket socket, struct Address* address) {
addrInfo.sin_addr.s_addr = address->ipv4;
socklen_t len = sizeof(addrInfo);
return accept(socket, (struct sockaddr*) &addrInfo, &len);
#ifndef _3DS
} else {
struct sockaddr_in6 addrInfo;
memset(&addrInfo, 0, sizeof(addrInfo));
@ -176,7 +212,9 @@ static inline Socket SocketAccept(Socket socket, struct Address* address) {
memcpy(addrInfo.sin6_addr.s6_addr, address->ipv6, sizeof(addrInfo.sin6_addr.s6_addr));
socklen_t len = sizeof(addrInfo);
return accept(socket, (struct sockaddr*) &addrInfo, &len);
#endif
}
return INVALID_SOCKET;
}
static inline int SocketClose(Socket socket) {