Skip to content

Instantly share code, notes, and snippets.

@B3n30
Last active July 18, 2018 20:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save B3n30/9c08a8103880f661fd9eee284044eff4 to your computer and use it in GitHub Desktop.
Save B3n30/9c08a8103880f661fd9eee284044eff4 to your computer and use it in GitHub Desktop.
read_delay
#include <3ds.h>
#include <stdio.h>
#include <cstdint>
#include <string>
#include <string_view>
#include <utility>
#include <vector>
static s64 base_tick;
static std::vector<std::pair<std::string_view, s64>> thread_order;
static volatile bool request_done = false;
static u64 first_tick = 0;
static u64 last_tick = 0;
int main(int argc, char **argv)
{
gfxInitDefault();
//Initialize console on top screen. Using NULL as the second argument tells the console library to use the internal console structure as current one
consoleInit(GFX_TOP, NULL);
y2rInit();
printf("Timing test\n\n");
svcSetThreadPriority(CUR_THREAD_HANDLE, 0x18);
base_tick = svcGetSystemTick();
int times = 0;
FILE *dataSet = fopen("busy_loop_data.txt", "w");
Result rc = romfsInit();
if (rc) {
printf("romfsInit: %08lX\n", rc);
return 0;
}
u32 len;
char mystring[1000];
Handle handle;
FS_Path archivePath;
archivePath.type=PATH_EMPTY;
archivePath.size=0;
archivePath.data=nullptr;
char zeros[12];
for (int i=0;i<12;i++) {
zeros[i] = 0;
}
FS_Path filePath;
filePath.type=PATH_BINARY;
filePath.size=12;
filePath.data=(const void*)zeros;
rc = FSUSER_OpenFileDirectly(&handle, ARCHIVE_ROMFS, archivePath, filePath, 1, 0);
if (rc) {
printf("Failed to open file: %08lX\n\n", rc);
return 0;
}
u32 offset=0;
while (times++ < 10000) {
Thread t = threadCreate([](void *arg) {
last_tick = first_tick = svcGetSystemTick();
while (!request_done) {
// Record the current tick
last_tick = svcGetSystemTick();
// Yield the thread to another thread
svcSleepThread(0);
}
}, nullptr, 0x1000, 0x19, 0, false);
//This is the function we want to measure
FSFILE_Read(handle, &len, 0, mystring, sizeof(mystring));
request_done = true;
threadJoin(t, U64_MAX);
threadFree(t);
fprintf(dataSet, "%lld\n", last_tick - first_tick);
hidScanInput();
if (hidKeysDown() & KEY_START) break;
request_done = false;
}
fclose(dataSet);
printf("\nTimeline: \n");
std::string str;
for (auto &sv : thread_order) {
str = sv.first;
printf("%s: %lld\n", str.c_str(), sv.second);
}
printf("Press Start to exit.\n");
// Main loop
while (aptMainLoop())
{
//Scan all the inputs. This should be done once for each frame
hidScanInput();
//hidKeysDown returns information about which buttons have been just pressed (and they weren't in the previous frame)
u32 kDown = hidKeysDown();
if (kDown & KEY_START) break; // break in order to return to hbmenu
// Flush and swap framebuffers
gfxFlushBuffers();
gfxSwapBuffers();
//Wait for VBlank
gspWaitForVBlank();
}
y2rExit();
gfxExit();
return 0;
}
#include <3ds.h>
#include <stdio.h>
#include <array>
#include <cstdint>
#include <sstream>
#include <cstring>
#include <string>
#include <string_view>
#include <utility>
#include <vector>
static s64 base_tick;
static std::vector<std::pair<std::string_view, s64>> thread_order;
static volatile bool request_done = false;
static u64 first_tick = 0;
static u64 last_tick = 0;
struct SelfNCCHFilePath {
u32 type;
std::array<char, 8> exefs_filename;
};
void EndLoop() {
printf("Press Start to exit.\n");
// Main loop
while (aptMainLoop())
{
//Scan all the inputs. This should be done once for each frame
hidScanInput();
//hidKeysDown returns information about which buttons have been just pressed (and they weren't in the previous frame)
u32 kDown = hidKeysDown();
if (kDown & KEY_START) break; // break in order to return to hbmenu
// Flush and swap framebuffers
gfxFlushBuffers();
gfxSwapBuffers();
//Wait for VBlank
gspWaitForVBlank();
}
y2rExit();
gfxExit();
}
int main(int argc, char **argv)
{
gfxInitDefault();
//Initialize console on top screen. Using NULL as the second argument tells the console library to use the internal console structure as current one
consoleInit(GFX_TOP, NULL);
y2rInit();
printf("Timing test\n\n");
svcSetThreadPriority(CUR_THREAD_HANDLE, 0x18);
base_tick = svcGetSystemTick();
int times = 0;
FILE *dataSet = nullptr;
u32 len;
u32 size = 10;
Handle handle;
FS_Path archivePath;
archivePath.type=PATH_EMPTY;
archivePath.size=0;
archivePath.data=nullptr;
char path[32] = "/Save.dat";
FS_Path filePath;
filePath.type=PATH_ASCII;
filePath.size=32;
filePath.data=(const void*)path;
FS_Archive archive_handle;
Result rc = FSUSER_FormatSaveData(ARCHIVE_SAVEDATA,archivePath, 0x200,7,7,7,7,false);
if (rc) {
printf("Failed to format archive: %08lX\n\n", rc);
EndLoop();
return 0;
}
rc = FSUSER_OpenArchive(&archive_handle, ARCHIVE_SAVEDATA, archivePath);
if (rc) {
printf("Failed to open archive: %08lX\n\n", rc);
EndLoop();
return 0;
}
rc = FSUSER_CreateFile(archive_handle,filePath,0,100000);
if (rc && rc != 0xC82044B4) {
printf("Failed to create file: %08lX\n\n", rc);
EndLoop();
return 0;
}
rc = FSUSER_OpenFileDirectly(&handle, ARCHIVE_SAVEDATA, archivePath, filePath, 7, 0);
if (rc) {
printf("Failed to open file: %08lX\n\n", rc);
EndLoop();
return 0;
}
char* mystring = (char *) malloc (sizeof(char) * 100000);
rc = FSFILE_Write(handle, &len, 0, mystring, 100000, 0);
free(mystring);
while (size < 100000) {
std::stringstream filename;
filename << "busy_loop_data_" << size << ".txt";
dataSet = fopen(filename.str().c_str(), "w");
times = 0;
char* mystring = (char *) malloc (sizeof(char) * size);
while (times++ < 1000) {
Thread t = threadCreate([](void *arg) {
last_tick = first_tick = svcGetSystemTick();
while (!request_done) {
// Record the current tick
last_tick = svcGetSystemTick();
// Yield the thread to another thread
svcSleepThread(0);
}
}, nullptr, 0x1000, 0x19, 0, false);
//This is the function we want to measure
rc = FSFILE_Read(handle, &len, 0, mystring, size);
request_done = true;
threadJoin(t, U64_MAX);
threadFree(t);
if(rc) {
printf("Failed to read file: %08lX\n\n", rc);
EndLoop();
return 0;
}
if (len != size) {
printf("Wrong size: %lu\n\n", len);
EndLoop();
return 0;
}
fprintf(dataSet, "%lld\n", last_tick - first_tick);
hidScanInput();
if (hidKeysDown() & KEY_START) break;
request_done = false;
}
printf("\nSize %lu done \n", size);
free(mystring);
size=size*4;
fclose(dataSet);
}
FSUSER_DeleteFile(archive_handle,filePath);
printf("\nFinished \n");
EndLoop();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment