/* * test_sysload.c -- McdeSys: CPU times, memory and signals (make check) * * License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree. * * What the activity window (dtactivity, tasksd t0091) shows and does: the CPU * counters, the memory, the network and disk counters, the signals. The * numbers of a running computer cannot be compared with fixed values, so what * is checked is what must hold: the memory adds up and agrees with the number * of pages sysconf(3) knows; the CPU counters only grow, and a minute of * busy work in this process shows in them; a signal ends a child (and a stopped * child is stopped and goes on again); pid 0, 1, a group and a bad signal are * refused before kill(2) is asked; a process that is gone is "gone". A signal * is never sent to a process that is not this test's own child. */ #define _POSIX_C_SOURCE 200809L #include #include #include #include #include #include #include #include #include #include #include #include #include #include static int failures; #define CHECK(cond) do { \ if (!(cond)) { \ printf("FAIL %s:%d: %s\n", __FILE__, __LINE__, #cond); \ failures++; \ } \ } while (0) static double now(void) { struct timespec t; clock_gettime(CLOCK_MONOTONIC, &t); return (double) t.tv_sec + (double) t.tv_nsec / 1e9; } static unsigned long long busy(const McdeSysCpu *c, size_t n) { unsigned long long s = 0; size_t i; for (i = 0; i < n; i++) s += c[i].user + c[i].nice + c[i].system; return s; } /* a child that waits for a signal; its pid */ static pid_t child(void) { pid_t pid = fork(); if (pid == 0) { /* never outlive a test that hangs or is killed: the child * would keep the harness waiting in waitpid forever */ alarm(60); for (;;) pause(); } return pid; } /* the state letter of PID in the process list, or '-' if it is not there */ static char state_of(pid_t pid) { McdeSysProcess *v; size_t n, i; char st = '-'; if (mcde_sys_processes(&v, &n) != MCDE_SYS_OK) return '?'; for (i = 0; i < n; i++) if (v[i].pid == (long) pid) st = v[i].state; free(v); return st; } static void cpu(void) { McdeSysCpu *a, *b; size_t na, nb, i; double t0; volatile unsigned long x = 0; CHECK(mcde_sys_cpu_times(NULL, &na) == MCDE_SYS_EINVAL); CHECK(mcde_sys_cpu_times(&a, NULL) == MCDE_SYS_EINVAL); CHECK(mcde_sys_cpu_times(&a, &na) == MCDE_SYS_OK); CHECK(na >= 1); CHECK(a != NULL); t0 = now(); while (now() - t0 < 0.4) /* work of our own */ x++; CHECK(mcde_sys_cpu_times(&b, &nb) == MCDE_SYS_OK); CHECK(nb == na); for (i = 0; i < na && i < nb; i++) { CHECK(b[i].user >= a[i].user); CHECK(b[i].nice >= a[i].nice); CHECK(b[i].system >= a[i].system); CHECK(b[i].idle >= a[i].idle); CHECK(b[i].other >= a[i].other); } /* 0.4 s of a CPU are about 40 ticks at 100 Hz: five are certain */ CHECK(busy(b, nb) >= busy(a, na) + 5); free(a); free(b); } static void memory(void) { McdeSysMemory m; unsigned long long phys; long pages = sysconf(_SC_PHYS_PAGES), ps = sysconf(_SC_PAGESIZE); CHECK(mcde_sys_memory(NULL) == MCDE_SYS_EINVAL); CHECK(mcde_sys_memory(&m) == MCDE_SYS_OK); CHECK(m.total > 0); CHECK(m.free <= m.total); CHECK(m.cached <= m.total); CHECK(m.free + m.cached + m.used == m.total); CHECK(m.used > 0); /* this program, the kernel ... */ CHECK(m.swap_used <= m.swap_total); if (pages > 0 && ps > 0) { phys = (unsigned long long) pages * (unsigned long long) ps; /* the system's own count of its memory, within five per cent */ CHECK(m.total <= phys + phys / 20); CHECK(m.total + m.total / 20 >= phys); } } static void signals(void) { pid_t p; int st, i; /* refused before kill(2) is asked: init, "my processes", a group */ CHECK(mcde_sys_signal(1, MCDE_SIGNAL_TERM) == MCDE_SYS_EINVAL); CHECK(mcde_sys_signal(0, MCDE_SIGNAL_TERM) == MCDE_SYS_EINVAL); CHECK(mcde_sys_signal(-1, MCDE_SIGNAL_KILL) == MCDE_SYS_EINVAL); CHECK(mcde_sys_signal(-(long) getpgrp(), MCDE_SIGNAL_TERM) == MCDE_SYS_EINVAL); p = child(); CHECK(p > 1); CHECK(mcde_sys_signal((long) p, (McdeSysSignal) 99) == MCDE_SYS_EINVAL); CHECK(mcde_sys_signal((long) p, (McdeSysSignal) -1) == MCDE_SYS_EINVAL); /* stopped, going on, ended */ CHECK(mcde_sys_signal((long) p, MCDE_SIGNAL_STOP) == MCDE_SYS_OK); for (i = 0; i < 40 && state_of(p) != 'T'; i++) usleep(25000); CHECK(state_of(p) == 'T'); CHECK(mcde_sys_signal((long) p, MCDE_SIGNAL_CONT) == MCDE_SYS_OK); for (i = 0; i < 40 && state_of(p) == 'T'; i++) usleep(25000); CHECK(state_of(p) != 'T'); CHECK(mcde_sys_signal((long) p, MCDE_SIGNAL_TERM) == MCDE_SYS_OK); CHECK(waitpid(p, &st, 0) == p); CHECK(WIFSIGNALED(st) && WTERMSIG(st) == SIGTERM); /* it is gone: waited for, the number is free */ CHECK(mcde_sys_signal((long) p, MCDE_SIGNAL_TERM) == MCDE_SYS_ENOENT); CHECK(strstr(mcde_sys_error(), "gone") != NULL); p = child(); CHECK(mcde_sys_signal((long) p, MCDE_SIGNAL_KILL) == MCDE_SYS_OK); CHECK(waitpid(p, &st, 0) == p); CHECK(WIFSIGNALED(st) && WTERMSIG(st) == SIGKILL); p = child(); CHECK(mcde_sys_signal((long) p, MCDE_SIGNAL_HUP) == MCDE_SYS_OK); CHECK(waitpid(p, &st, 0) == p); CHECK(WIFSIGNALED(st) && WTERMSIG(st) == SIGHUP); p = child(); CHECK(mcde_sys_signal((long) p, MCDE_SIGNAL_INT) == MCDE_SYS_OK); CHECK(waitpid(p, &st, 0) == p); CHECK(WIFSIGNALED(st) && WTERMSIG(st) == SIGINT); /* another user's process: the system says no. SIGCONT only, and only * as an ordinary user: nothing is ended, whoever runs the test */ if (geteuid() != 0) { McdeSysProcess *v; size_t n, k; long other = 0; if (mcde_sys_processes(&v, &n) == MCDE_SYS_OK) { for (k = 0; k < n; k++) if (v[k].uid == 0 && v[k].pid > 1) { other = v[k].pid; break; } free(v); } if (other != 0) CHECK(mcde_sys_signal(other, MCDE_SIGNAL_CONT) == MCDE_SYS_EPERM); } } /* the loopback entry of a reading, or NULL */ static const McdeSysNetIo * loopback(const McdeSysNetIo *v, size_t n) { size_t i; for (i = 0; i < n; i++) if (strncmp(v[i].name, "lo", 2) == 0) return &v[i]; return NULL; } static void network(void) { McdeSysNetIo *a, *b; const McdeSysNetIo *la, *lb; size_t na, nb; struct sockaddr_in sa; socklen_t sl = sizeof sa; int ls, cs, as; pid_t p; static char buf[65536]; size_t sent = 0; int st; CHECK(mcde_sys_net_io(NULL, &na) == MCDE_SYS_EINVAL); CHECK(mcde_sys_net_io(&a, NULL) == MCDE_SYS_EINVAL); CHECK(mcde_sys_net_io(&a, &na) == MCDE_SYS_OK); CHECK(na >= 1); la = loopback(a, na); CHECK(la != NULL); if (la == NULL) { free(a); return; } /* 2 MB over a TCP connection on the loopback */ ls = socket(AF_INET, SOCK_STREAM, 0); memset(&sa, 0, sizeof sa); sa.sin_family = AF_INET; sa.sin_addr.s_addr = htonl(INADDR_LOOPBACK); CHECK(ls >= 0 && bind(ls, (struct sockaddr *) &sa, sizeof sa) == 0); CHECK(getsockname(ls, (struct sockaddr *) &sa, &sl) == 0); CHECK(listen(ls, 1) == 0); p = fork(); if (p == 0) { as = accept(ls, NULL, NULL); while (as >= 0 && read(as, buf, sizeof buf) > 0) ; _exit(0); } cs = socket(AF_INET, SOCK_STREAM, 0); CHECK(connect(cs, (struct sockaddr *) &sa, sizeof sa) == 0); memset(buf, 'x', sizeof buf); while (sent < 2u * 1024 * 1024) { ssize_t w = write(cs, buf, sizeof buf); if (w <= 0) break; sent += (size_t) w; } close(cs); CHECK(waitpid(p, &st, 0) == p); close(ls); CHECK(mcde_sys_net_io(&b, &nb) == MCDE_SYS_OK); lb = loopback(b, nb); CHECK(lb != NULL); if (lb != NULL) { CHECK(lb->rx_bytes >= la->rx_bytes + 2000000); CHECK(lb->tx_bytes >= la->tx_bytes + 2000000); CHECK(lb->rx_packets > la->rx_packets); CHECK(lb->tx_packets > la->tx_packets); } free(a); free(b); } static unsigned long long written(const McdeSysDiskIo *v, size_t n) { unsigned long long s = 0; size_t i; for (i = 0; i < n; i++) s += v[i].write_bytes; return s; } static void disks(void) { McdeSysDiskIo *a, *b; size_t na, nb, i, j; char block[65536], name[64]; int fd, k; CHECK(mcde_sys_disk_io(NULL, &na) == MCDE_SYS_EINVAL); CHECK(mcde_sys_disk_io(&a, NULL) == MCDE_SYS_EINVAL); CHECK(mcde_sys_disk_io(&a, &na) == MCDE_SYS_OK); CHECK(na >= 1); for (i = 0; i < na; i++) CHECK(a[i].name[0] != '\0'); /* 8 MB written and synced to a file next to the test: if that file system * lives on a disk, the disks' write counters grow by about that much */ snprintf(name, sizeof name, "test_sysload.%d.tmp", (int) getpid()); memset(block, 'd', sizeof block); fd = open(name, O_WRONLY | O_CREAT | O_TRUNC, 0600); CHECK(fd >= 0); for (k = 0; k < 128 && fd >= 0; k++) if (write(fd, block, sizeof block) != (ssize_t) sizeof block) break; if (fd >= 0) { fsync(fd); close(fd); } unlink(name); sleep(1); /* the counters of some drivers lag */ CHECK(mcde_sys_disk_io(&b, &nb) == MCDE_SYS_OK); /* nothing goes back, by name */ for (i = 0; i < na; i++) for (j = 0; j < nb; j++) if (strcmp(a[i].name, b[j].name) == 0) { CHECK(b[j].read_bytes >= a[i].read_bytes); CHECK(b[j].write_bytes >= a[i].write_bytes); CHECK(b[j].read_ops >= a[i].read_ops); CHECK(b[j].write_ops >= a[i].write_ops); } if (written(b, nb) >= written(a, na) + 4u * 1024 * 1024) printf("disk write counters grew by %llu bytes for 8 MB\n", written(b, nb) - written(a, na)); else printf("SKIP: the disks' write counters did not move (a file " "system without a disk here?)\n"); free(a); free(b); } int main(void) { cpu(); memory(); network(); disks(); signals(); if (failures == 0) printf("test_sysload: all passed\n"); return failures != 0; }