Skip to content

Instantly share code, notes, and snippets.

@izabera
Last active July 8, 2025 11:06
Show Gist options
  • Save izabera/3cb56733967455c56655e0f32b93e3ff to your computer and use it in GitHub Desktop.
Save izabera/3cb56733967455c56655e0f32b93e3ff to your computer and use it in GitHub Desktop.
floatcomplex nerd sniped me into testing how many sigwinches i can get per second via terminal resizes
1
/sent/ { stot += sent[s++] = $2 }
/recv/ { rtot += recv[r++] = $2 }
END {
if (r != s)
printf "error: s=%d r=%d\n", s, r
for (i in sent)
if (sent[i] < recv[i])
printf "error: sent[%d]=%d recv[%d]=%d\n", i, sent[i], i, recv[i]
printf "sent=%'d avg=%'d\n", stot, stot/s
printf "recv=%'d avg=%'d\n", rtot, rtot/r
printf "recv/sent=%.1f%%\n", 100*rtot/stot
}
#define _GNU_SOURCE
#include <pty.h>
#include <sched.h>
#include <signal.h>
#include <stdio.h>
#include <sys/mman.h>
#include <sys/signalfd.h>
#include <sys/time.h>
#include <unistd.h>
void sched(int cpu, int policy, int priority) {
cpu_set_t cpuset = {};
CPU_SET(cpu, &cpuset);
sched_setaffinity(0, sizeof cpuset, &cpuset);
struct sched_param param = { .sched_priority = priority };
sched_setscheduler(0, policy, &param);
}
int main() {
int count = 0;
char *map = mmap(0, 4096, 7, MAP_ANON|MAP_SHARED, -1, 0);
sigset_t set = {};
sigaddset(&set, SIGWINCH);
sigaddset(&set, SIGALRM);
sigprocmask(SIG_BLOCK, &set, 0);
int sigfd = signalfd(-1, &set, SFD_NONBLOCK);
int outfd = dup(1);
int masterfd;
struct winsize win = {};
int pid = forkpty(&masterfd, 0, 0, &win);
if (pid == 0) {
sched(5, SCHED_RR, 10);
struct itimerval it = { .it_value.tv_sec = 1 };
setitimer(ITIMER_REAL, &it, 0);
struct signalfd_siginfo si = {};
while (1) {
if (read(sigfd, &si, sizeof si) != sizeof si)
continue;
if (si.ssi_signo == SIGALRM)
break;
count++;
}
}
else {
sched(4, SCHED_RR, 10);
while (!*map) {
win.ws_col = win.ws_row = count++%100 + 10;
ioctl(masterfd, TIOCSWINSZ, &win);
}
}
*map = 1;
dprintf(outfd, "%s: %d\n", pid==0 ? "recv" : "sent", count);
}
isabella@melon:~$ gcc -O3 pty.c && sudo nice -20 bash -c 'for i in {1..10}; do ./a.out; done' | awk -f check.awk
sent: 3223005
recv: 3221137
recv: 3216769
sent: 3219834
sent: 3213705
recv: 3210557
recv: 3194343
sent: 3196125
recv: 3200260
sent: 3202761
recv: 3202258
sent: 3205005
sent: 3154642
recv: 3151621
recv: 3184421
sent: 3186805
sent: 3182962
recv: 3181104
recv: 3156765
sent: 3159112
sent=31,943,956 avg=3,194,395
recv=31,919,235 avg=3,191,923
recv/sent=99.9%
holy fuck cgroups are crazy expensive
isabella@melon:~$ sudo nice -20 bash -c 'echo $$ >> /sys/fs/cgroup/cgroup.procs; for i in {1..10}; do ./a.out; done' | awk -f check.awk
recv: 3618086
sent: 3619961
sent: 3588182
recv: 3586635
sent: 3547247
recv: 3543605
recv: 3566707
sent: 3568710
recv: 3574457
sent: 3576766
sent: 3564380
recv: 3559270
sent: 3537433
recv: 3534281
recv: 3531923
sent: 3534677
recv: 3531909
sent: 3533686
recv: 3522657
sent: 3524621
sent=35,595,663 avg=3,559,566
recv=35,569,530 avg=3,556,953
recv/sent=99.9%
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment