Skip to content

Instantly share code, notes, and snippets.

@Subv
Forked from ds84182/timing_test.cpp
Created December 10, 2017 17:57
Show Gist options
  • Save Subv/76f80c4c6dc3f1d716f14e6c54c91fcc to your computer and use it in GitHub Desktop.
Save Subv/76f80c4c6dc3f1d716f14e6c54c91fcc to your computer and use it in GitHub Desktop.
#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");
while (times++ < 10000) {
Thread t = threadCreate([](void *arg) {
last_tick = first_tick = svcGetSystemTick();
// Busy looping low priority background thread
// thread_order.emplace_back("Low priority background thread start", first_tick - base_tick);
while (!request_done) {
// Record the current tick
last_tick = svcGetSystemTick();
// Yield the thread to another thread
svcSleepThread(0);
}
// thread_order.emplace_back("Low priority background thread done", svcGetSystemTick() - base_tick);
}, nullptr, 0x1000, 0x19, 0, false);
// thread_order.emplace_back("Created Low priority background thread", svcGetSystemTick() - base_tick);
// thread_order.emplace_back("Call Performed", svcGetSystemTick() - base_tick);
GSPGPU_SetLcdForceBlack(0);
request_done = true; // Allow the thread to terminate whenever it gets rescheduled
// thread_order.emplace_back("Call Done", svcGetSystemTick() - base_tick);
threadJoin(t, U64_MAX);
threadFree(t);
// printf("Low Priority Thread Run Time: %lld - %lld\n", first_tick - base_tick, last_tick - base_tick);
// printf("Low Priority Thread Run Duration: %lld\n", last_tick - first_tick);
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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment