diff --git a/include/Mcde/McdeSys.h b/include/Mcde/McdeSys.h index 9183b986..770a6e7c 100644 --- a/include/Mcde/McdeSys.h +++ b/include/Mcde/McdeSys.h @@ -435,6 +435,50 @@ typedef struct { int mcde_sys_processes(McdeSysProcess **out, size_t *n); +/* + * The load of the computer, for the activity window (t0091). + * + * mcde_sys_cpu_times: one McdeSysCpu for each processor. The numbers are the + * ticks each state has had since the system started, all of one length: only + * the proportions between two readings mean something (the share of "user" in + * the ticks that passed is "% User"). "other" is what is neither of the four + * (NetBSD: interrupts; Linux: waiting for the disk, interrupts, time stolen by + * a hypervisor). NetBSD: sysctl kern.cp_time with room for every CPU + * (sys/kern/init_sysctl.c); Linux: /proc/stat. *OUT is malloc'd (free(3)). + * + * mcde_sys_memory: bytes. "free" is not used at all; "cached" is what the + * system keeps from files and gives back when a program wants it (counted as + * available); "used" is the rest, what programs and the kernel really hold: + * total = free + cached + used. Swap in use and in all. NetBSD: sysctl + * vm.uvmexp2 (as top(1) reads it); Linux: /proc/meminfo. + */ +typedef struct { + unsigned long long user, nice, system, idle, other; +} McdeSysCpu; + +int mcde_sys_cpu_times(McdeSysCpu **out, size_t *n); + +typedef struct { + unsigned long long total, free, cached, used; + unsigned long long swap_total, swap_used; +} McdeSysMemory; + +int mcde_sys_memory(McdeSysMemory *m); + +/* + * Ends or stops a process (the activity window's Quit Process). Only these + * signals, and only a process of its own: PID must be above 1 -- never init, + * never "all my processes" or a group (kill(2) with 0 or a negative number). + * MCDE_SYS_EPERM when it is another user's (the system's answer), MCDE_SYS_ENOENT + * when there is no such process. + */ +typedef enum { + MCDE_SIGNAL_TERM, MCDE_SIGNAL_KILL, MCDE_SIGNAL_STOP, + MCDE_SIGNAL_CONT, MCDE_SIGNAL_HUP, MCDE_SIGNAL_INT +} McdeSysSignal; + +int mcde_sys_signal(long pid, McdeSysSignal sig); + /* * A file's comment and its lock, as the Info window of the file manager * shows them. The comment is an extended attribute in the user namespace diff --git a/lib/McdeSys/Makefile.am b/lib/McdeSys/Makefile.am index 504a7be7..fd365a3b 100644 --- a/lib/McdeSys/Makefile.am +++ b/lib/McdeSys/Makefile.am @@ -21,7 +21,7 @@ endif libMcdeSys_la_LDFLAGS = -version-info 1:0:0 # what the system tells about itself, for a look by hand: make check runs it -check_PROGRAMS = test_sysinfo test_netmount test_updatesource test_processes test_job test_netinput +check_PROGRAMS = test_sysinfo test_netmount test_updatesource test_processes test_job test_netinput test_sysload test_sysinfo_SOURCES = test_sysinfo.c test_sysinfo_CPPFLAGS = -I$(top_srcdir)/include test_sysinfo_LDADD = libMcdeSys.la @@ -40,4 +40,7 @@ test_job_LDADD = libMcdeSys.la test_netinput_SOURCES = test_netinput.c test_netinput_CPPFLAGS = -I$(top_srcdir)/include test_netinput_LDADD = libMcdeSys.la -TESTS = test_sysinfo test_netmount test_updatesource test_processes test_job test_netinput +test_sysload_SOURCES = test_sysload.c +test_sysload_CPPFLAGS = -I$(top_srcdir)/include +test_sysload_LDADD = libMcdeSys.la +TESTS = test_sysinfo test_netmount test_updatesource test_processes test_job test_netinput test_sysload diff --git a/lib/McdeSys/common.c b/lib/McdeSys/common.c index e873bb73..e81f0bac 100644 --- a/lib/McdeSys/common.c +++ b/lib/McdeSys/common.c @@ -642,6 +642,57 @@ mcde_sys_process_stopped(pid_t pid) return mcde_sys_process_stopped_backend(pid); } +int +mcde_sys_cpu_times(McdeSysCpu **out, size_t *n) +{ + if (out == NULL || n == NULL) + return mcde_sys_fail(MCDE_SYS_EINVAL, "No place for the CPU " + "times."); + *out = NULL; + *n = 0; + return mcde_sys_cpu_times_backend(out, n); +} + +int +mcde_sys_memory(McdeSysMemory *m) +{ + if (m == NULL) + return mcde_sys_fail(MCDE_SYS_EINVAL, "No place for the " + "memory."); + memset(m, 0, sizeof *m); + if (mcde_sys_memory_backend(m) != MCDE_SYS_OK) + return MCDE_SYS_EFAIL; + /* the three parts add up to the whole, whatever the system rounded */ + if (m->free > m->total) + m->free = m->total; + if (m->cached > m->total - m->free) + m->cached = m->total - m->free; + m->used = m->total - m->free - m->cached; + if (m->swap_used > m->swap_total) + m->swap_used = m->swap_total; + return MCDE_SYS_OK; +} + +int +mcde_sys_signal(long pid, McdeSysSignal sig) +{ + static const int numbers[] = { SIGTERM, SIGKILL, SIGSTOP, SIGCONT, + SIGHUP, SIGINT }; + + if (pid <= 1 || (unsigned) sig >= sizeof numbers / sizeof numbers[0]) + return mcde_sys_fail(MCDE_SYS_EINVAL, "That is no process to " + "send a signal to."); + if (kill((pid_t) pid, numbers[sig]) == 0) + return MCDE_SYS_OK; + if (errno == EPERM) + return mcde_sys_fail(MCDE_SYS_EPERM, "The process belongs to " + "somebody else."); + if (errno == ESRCH) + return mcde_sys_fail(MCDE_SYS_ENOENT, "The process is gone."); + return mcde_sys_fail(MCDE_SYS_EFAIL, "Cannot signal the process: %s.", + strerror(errno)); +} + int mcde_sys_processes(McdeSysProcess **out, size_t *n) { diff --git a/lib/McdeSys/linux.c b/lib/McdeSys/linux.c index d719d178..07376378 100644 --- a/lib/McdeSys/linux.c +++ b/lib/McdeSys/linux.c @@ -1425,3 +1425,101 @@ mcde_sys_firewall_get(int *on, char *conf, size_t len) return mcde_sys_fail(MCDE_SYS_ENOTSUP, "npf(7) is NetBSD's packet " "filter; this system is not NetBSD."); } + +/* ---- the load of the computer: /proc/stat and /proc/meminfo */ + +int +mcde_sys_cpu_times_backend(McdeSysCpu **out, size_t *n) +{ + FILE *fp; + char line[512]; + McdeSysCpu *v = NULL, *nv; + size_t cnt = 0, cap = 0; + + if ((fp = fopen("/proc/stat", "r")) == NULL) + return mcde_sys_fail(MCDE_SYS_EFAIL, "Cannot read /proc/stat: " + "%s.", strerror(errno)); + while (fgets(line, sizeof line, fp) != NULL) { + unsigned long long u = 0, ni = 0, sy = 0, id = 0, io = 0, + irq = 0, sirq = 0, st = 0; + int cpu; + + /* "cpu " is the sum of all of them, "cpuN" is one processor */ + if (strncmp(line, "cpu", 3) != 0 || + !isdigit((unsigned char) line[3])) + continue; + if (sscanf(line, "cpu%d %llu %llu %llu %llu %llu %llu %llu " + "%llu", &cpu, &u, &ni, &sy, &id, &io, &irq, &sirq, + &st) < 5) + continue; + if (cnt == cap) { + cap = cap != 0 ? cap * 2 : 8; + if ((nv = realloc(v, cap * sizeof *v)) == NULL) { + free(v); + fclose(fp); + return mcde_sys_fail(MCDE_SYS_EFAIL, "Out of " + "memory."); + } + v = nv; + } + v[cnt].user = u; + v[cnt].nice = ni; + v[cnt].system = sy; + v[cnt].idle = id; + v[cnt].other = io + irq + sirq + st; + cnt++; + } + fclose(fp); + if (cnt == 0) { + free(v); + return mcde_sys_fail(MCDE_SYS_EFAIL, "/proc/stat tells no " + "processor."); + } + *out = v; + *n = cnt; + return MCDE_SYS_OK; +} + +int +mcde_sys_memory_backend(McdeSysMemory *m) +{ + FILE *fp; + char line[256]; + unsigned long long v, total = 0, mfree = 0, avail = 0, buffers = 0, + cached = 0, swt = 0, swf = 0; + int have_avail = 0; + + if ((fp = fopen("/proc/meminfo", "r")) == NULL) + return mcde_sys_fail(MCDE_SYS_EFAIL, "Cannot read " + "/proc/meminfo: %s.", strerror(errno)); + while (fgets(line, sizeof line, fp) != NULL) { + if (sscanf(line, "MemTotal: %llu kB", &v) == 1) + total = v; + else if (sscanf(line, "MemFree: %llu kB", &v) == 1) + mfree = v; + else if (sscanf(line, "MemAvailable: %llu kB", &v) == 1) { + avail = v; + have_avail = 1; + } else if (sscanf(line, "Buffers: %llu kB", &v) == 1) + buffers = v; + else if (sscanf(line, "Cached: %llu kB", &v) == 1) + cached = v; + else if (sscanf(line, "SwapTotal: %llu kB", &v) == 1) + swt = v; + else if (sscanf(line, "SwapFree: %llu kB", &v) == 1) + swf = v; + } + fclose(fp); + if (total == 0) + return mcde_sys_fail(MCDE_SYS_EFAIL, "/proc/meminfo tells no " + "memory."); + m->total = total * 1024; + m->free = mfree * 1024; + /* what the system gives back when it is asked: available - free (kernel + * 3.14 and later), else the buffers and the file cache */ + m->cached = have_avail ? (avail > mfree ? (avail - mfree) * 1024 : 0) : + (buffers + cached) * 1024; + m->swap_total = swt * 1024; + m->swap_used = swt > swf ? (swt - swf) * 1024 : 0; + return MCDE_SYS_OK; +} diff --git a/lib/McdeSys/netbsd.c b/lib/McdeSys/netbsd.c index 895b4a04..57841667 100644 --- a/lib/McdeSys/netbsd.c +++ b/lib/McdeSys/netbsd.c @@ -35,6 +35,8 @@ #include #include #include +#include +#include #include #include #include @@ -2037,3 +2039,76 @@ mcde_sys_firewall_get(int *on, char *conf, size_t len) *on = RcConfGet("npf", v, sizeof v) && strcasecmp(v, "YES") == 0; return MCDE_SYS_OK; } + +/* ---- the load of the computer. The CPU ticks: kern.cp_time given room for + * every processor returns CPUSTATES counters for each, in the order of the + * CP_ constants (sys/kern/init_sysctl.c, sysctl_kern_cptime: "alternately, you + * can pass an additional mib number and get cp_time for that particular + * processor"; a buffer of the size of one processor's counters gets the sum). + * The memory: vm.uvmexp2, struct uvmexp_sysctl in pages, as top(1) reads it + * (src/external/bsd/top/dist/machine/m_netbsd.c). */ + +int +mcde_sys_cpu_times_backend(McdeSysCpu **out, size_t *n) +{ + int mib[2], ncpu = 0, i; + size_t len = sizeof ncpu; + uint64_t *t; + McdeSysCpu *v; + + mib[0] = CTL_HW; + mib[1] = HW_NCPU; + if (sysctl(mib, 2, &ncpu, &len, NULL, 0) != 0 || ncpu < 1) + return mcde_sys_fail(MCDE_SYS_EFAIL, "The system tells no " + "number of processors."); + len = (size_t) ncpu * CPUSTATES * sizeof(uint64_t); + if ((t = malloc(len)) == NULL || (v = calloc((size_t) ncpu, + sizeof *v)) == NULL) { + free(t); + return mcde_sys_fail(MCDE_SYS_EFAIL, "Out of memory."); + } + mib[0] = CTL_KERN; + mib[1] = KERN_CP_TIME; + if (sysctl(mib, 2, t, &len, NULL, 0) != 0 || + len != (size_t) ncpu * CPUSTATES * sizeof(uint64_t)) { + free(t); + free(v); + return mcde_sys_fail(MCDE_SYS_EFAIL, "The system tells no CPU " + "times: %s.", strerror(errno)); + } + for (i = 0; i < ncpu; i++) { + v[i].user = t[i * CPUSTATES + CP_USER]; + v[i].nice = t[i * CPUSTATES + CP_NICE]; + v[i].system = t[i * CPUSTATES + CP_SYS]; + v[i].idle = t[i * CPUSTATES + CP_IDLE]; + v[i].other = t[i * CPUSTATES + CP_INTR]; + } + free(t); + *out = v; + *n = (size_t) ncpu; + return MCDE_SYS_OK; +} + +int +mcde_sys_memory_backend(McdeSysMemory *m) +{ + struct uvmexp_sysctl u; + int mib[2]; + size_t len = sizeof u; + unsigned long long ps; + + mib[0] = CTL_VM; + mib[1] = VM_UVMEXP2; + if (sysctl(mib, 2, &u, &len, NULL, 0) != 0) + return mcde_sys_fail(MCDE_SYS_EFAIL, "The system tells no " + "memory: %s.", strerror(errno)); + ps = (unsigned long long) u.pagesize; + m->total = (unsigned long long) u.npages * ps; + m->free = (unsigned long long) u.free * ps; + /* what is only a copy of a file or of a program's text can be dropped */ + m->cached = ((unsigned long long) u.filepages + + (unsigned long long) u.execpages) * ps; + m->swap_total = (unsigned long long) u.swpages * ps; + m->swap_used = (unsigned long long) u.swpginuse * ps; + return MCDE_SYS_OK; +} diff --git a/lib/McdeSys/private.h b/lib/McdeSys/private.h index d77656f0..1ef516c4 100644 --- a/lib/McdeSys/private.h +++ b/lib/McdeSys/private.h @@ -42,6 +42,9 @@ int mcde_sys_file_value(const char *file, const char *key, char *buf, /* the backend: the processes (mcde_sys_processes checks the arguments) */ int mcde_sys_processes_backend(McdeSysProcess **out, size_t *n); +/* the backends: the CPU ticks of every processor, and the memory */ +int mcde_sys_cpu_times_backend(McdeSysCpu **out, size_t *n); +int mcde_sys_memory_backend(McdeSysMemory *m); /* an update source the root update may use: a directory, file:/// or https:// * (not http://); 1 if so (common.c, tasksd t0078) */ diff --git a/lib/McdeSys/test_sysload.c b/lib/McdeSys/test_sysload.c new file mode 100644 index 00000000..10c192f0 --- /dev/null +++ b/lib/McdeSys/test_sysload.c @@ -0,0 +1,219 @@ +/* + * 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 + * 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 + +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) { + 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); + } +} + +int +main(void) +{ + cpu(); + memory(); + signals(); + if (failures == 0) + printf("test_sysload: all passed\n"); + return failures != 0; +}