DF-2949 / diag_timing.c
/* timing diagnostic for DF-2949 rig alignment */ #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <fcntl.h> #include <string.h> #include <signal.h> #include <sys/ioctl.h> #include <sys/types.h> #include <sys/wait.h> #include <sys/time.h> static int gopipe[2]; static double now(void) { struct timeval tv; gettimeofday(&tv, NULL); return (tv.tv_sec + tv.tv_usec / 1e6); } static void victim(int id) { char c; unsigned int seed = getpid() * 2654435761u; int fd; double tgo, t0, t1; signal(SIGHUP, SIG_IGN); fd = open("/dev/tty", O_RDWR); read(gopipe[0], &c, 1); tgo = now(); usleep(rand_r(&seed) % 4000); t0 = now(); if (fd >= 0) close(fd); t1 = now(); { char buf[128]; int n = snprintf(buf, sizeof(buf), "V%d go=%.6f start=%.6f end=%.6f fd=%d\n", id, tgo, t0, t1, fd); int df = open("/tmp/diag.log", O_WRONLY | O_APPEND | O_CREAT, 0644); if (df >= 0) { write(df, buf, n); close(df); } } _exit(0); } int main(int argc, char **argv) { long iters = (argc > 1) ? atol(argv[1]) : 200; long k; pipe(gopipe); unlink("/tmp/diag.log"); for (k = 0; k < iters; k++) { pid_t l = fork(); if (l == 0) { char sname[128]; if (setsid() < 0) _exit(1); int m = posix_openpt(O_RDWR | O_NOCTTY); if (m < 0) _exit(1); char *s = ptsname(m); if (s == NULL) _exit(1); snprintf(sname, sizeof(sname), "%s", s); int sl = open(sname, O_RDWR | O_NOCTTY); if (sl < 0) _exit(1); if (ioctl(sl, TIOCSCTTY, 0) < 0) _exit(1); for (int v = 0; v < 2; v++) { pid_t p = fork(); if (p == 0) victim(v); } char c; read(gopipe[0], &c, 1); double tg = now(); _exit(0); /* leader exit */ } double tg = now(); write(gopipe[1], "ggg", 3); int st; double t0 = now(); waitpid(l, &st, 0); double t1 = now(); if (k < 20) fprintf(stderr, "L go->reap %.6f -> %.6f = %.6f s\n", tg, t1, t1 - tg); } system("cat /tmp/diag.log | tail -8"); return (0); } |