Files
LlamaCasty/tools/ticker.c
T
gramps d212d5ae2c tools: WSL ticker for timing-verification takes
Absolute-deadline 1s marker (CLOCK_REALTIME clock_nanosleep, user-readable
timestamp + drift per second) so a recording's real-time pace is checkable
on screen. Deadline pacing mirrors game-loop/OBS canon -
stackoverflow.com/questions/5441464. Build: gcc -O2 -o ticker ticker.c
2026-09-10 08:35:14 -07:00

38 lines
1.1 KiB
C

#include <errno.h>
#include <inttypes.h>
#include <signal.h>
#include <stdio.h>
#include <time.h>
static volatile sig_atomic_t g_run = 1;
static void stop(int s){ (void)s; g_run = 0; }
static double mono(void){
struct timespec t; clock_gettime(CLOCK_MONOTONIC, &t);
return (double)t.tv_sec + (double)t.tv_nsec/1e9;
}
int main(void){
signal(SIGINT, stop); signal(SIGTERM, stop);
struct timespec base; clock_gettime(CLOCK_REALTIME, &base);
base.tv_nsec = 0; base.tv_sec += 1;
double t0 = mono();
uint64_t tick = 0;
while (g_run){
struct timespec dl = base;
dl.tv_sec += (time_t)tick;
int rc = clock_nanosleep(CLOCK_REALTIME, TIMER_ABSTIME, &dl, NULL);
if (rc != 0 && rc != EINTR) break;
double now = mono();
double drift = (now - t0 - (double)tick) * 1e3;
struct timespec r; clock_gettime(CLOCK_REALTIME, &r);
struct tm mv; localtime_r(&r.tv_sec, &mv);
printf("[%05" PRIu64 "] t+%09.6fs drift %+7.2fms %02d:%02d:%02d\n",
tick, now - t0, drift, mv.tm_hour, mv.tm_min, mv.tm_sec);
fflush(stdout);
tick++;
}
return 0;
}