/* * splinter -- self: what this session costs the machine. * * Copyright (c) 2026 Konrad Lother * SPDX-License-Identifier: BSD-3-Clause * * Every other widget talks about the world. This one talks about the process * the operator is looking at: how much memory, CPU, threads and file * descriptors splinter itself holds just by running, and how much its children * (a run tool, an MCP server child, a delegate, a CLI transport) hold under it. * * self * 84M rss 3.2% cpu * 18 thr 41 fd * kids 3 120M 1.4% cpu * io 12M r 300k w * * The numbers come from /proc: /proc/self/status (VmRSS, Threads), /proc/self/fd * (count), /proc/self/io (bytes), /proc//stat (utime/stime, rss, PPid). * A child is any process whose parent chain reaches this pid; the walk is one * pass over /proc per tick, which is cheap next to what a model request costs. * * CPU is the delta between two ticks, not a lifetime average, so the widget * says "right now". IO is cumulative for the session; that is what "used" * means here. * * plugin self { interval = 2000 } # ms between samples */ #include #include #include #include #include #include #include #include #include "splinter.h" #include "splinter_plugin.h" #define SELF_KIDS_MAX 256 #define SELF_LIST_MAX 12 #define SELF_DEFAULT_MS 2000 struct kid { int pid; int ppid; unsigned long rss_pages; unsigned long long ticks; }; struct sample { unsigned long rss_kb; int threads; int fds; double cpu_pct; unsigned long io_read; unsigned long io_write; unsigned long long self_ticks; double kids_cpu_pct; unsigned long kids_rss_kb; int kids; }; static pthread_mutex_t self_lock = PTHREAD_MUTEX_INITIALIZER; static struct sample now_s; static char widget[512]; static char detail[4096]; static long hz; static unsigned long long prev_self_ticks; static double prev_time; static struct { int pid; unsigned long long ticks; } prev_kids[SELF_KIDS_MAX]; static size_t prev_nkids; static double mono_now(void) { struct timespec ts; clock_gettime(CLOCK_MONOTONIC, &ts); return (double)ts.tv_sec + (double)ts.tv_nsec / 1e9; } /* one /proc//stat: ppid, resident pages and utime+stime in clock ticks */ static int read_stat(int pid, struct kid *out) { char path[64], buf[2048], *p, *end; FILE *f; char state; long ppid; unsigned long pre[9]; /* pgrp .. cmajflt */ unsigned long long utime, stime; unsigned long post[9]; /* cutime .. rss */ snprintf(path, sizeof(path), "/proc/%d/stat", pid); f = fopen(path, "r"); if (f == NULL) return -1; if (fgets(buf, sizeof(buf), f) == NULL) { fclose(f); return -1; } fclose(f); end = strrchr(buf, ')'); if (end == NULL) return -1; p = end + 2; /* after "comm)": state ppid ... */ if (sscanf(p, "%c %ld %lu %lu %lu %lu %lu %lu %lu %lu %lu " "%llu %llu %lu %lu %lu %lu %lu %lu %lu %lu %lu", &state, &ppid, &pre[0], &pre[1], &pre[2], &pre[3], &pre[4], &pre[5], &pre[6], &pre[7], &pre[8], &utime, &stime, &post[0], &post[1], &post[2], &post[3], &post[4], &post[5], &post[6], &post[7], &post[8]) != 22) return -1; (void)state; out->pid = pid; out->ppid = (int)ppid; out->rss_pages = post[8]; /* the field named rss, in pages */ out->ticks = utime + stime; return 0; } static void read_self(struct sample *s) { FILE *f; char line[512]; s->rss_kb = 0; s->threads = 0; f = fopen("/proc/self/status", "r"); if (f != NULL) { while (fgets(line, sizeof(line), f) != NULL) { unsigned long v; if (sscanf(line, "VmRSS: %lu kB", &v) == 1) s->rss_kb = v; else if (sscanf(line, "Threads: %lu", &v) == 1) s->threads = (int)v; } fclose(f); } { DIR *d = opendir("/proc/self/fd"); int n = 0; if (d != NULL) { struct dirent *e; while ((e = readdir(d)) != NULL) if (e->d_name[0] != '.') n++; closedir(d); } s->fds = n; } f = fopen("/proc/self/io", "r"); if (f != NULL) { while (fgets(line, sizeof(line), f) != NULL) { unsigned long v; if (sscanf(line, "read_bytes: %lu", &v) == 1) s->io_read = v; else if (sscanf(line, "write_bytes: %lu", &v) == 1) s->io_write = v; } fclose(f); } } static void human(unsigned long kb, char *buf, size_t cap) { if (kb >= 1024 * 1024) snprintf(buf, cap, "%.1fG", (double)kb / (1024.0 * 1024.0)); else if (kb >= 1024) snprintf(buf, cap, "%.0fM", (double)kb / 1024.0); else snprintf(buf, cap, "%luk", kb); } static void human_bytes(unsigned long b, char *buf, size_t cap) { if (b >= 1024UL * 1024UL * 1024UL) snprintf(buf, cap, "%.1fG", (double)b / (1024.0 * 1024.0 * 1024.0)); else if (b >= 1024UL * 1024UL) snprintf(buf, cap, "%.1fM", (double)b / (1024.0 * 1024.0)); else if (b >= 1024) snprintf(buf, cap, "%.0fk", (double)b / 1024.0); else snprintf(buf, cap, "%luB", b); } /* * Who is under this process: one read of every /proc//stat, then include * anything whose parent is already included until nothing changes. The tree is * a few levels deep, so a handful of passes is enough. */ static void scan_kids(struct sample *s) { DIR *d; struct kid v[SELF_KIDS_MAX]; unsigned char in[SELF_KIDS_MAX]; size_t n = 0, i, pass; int self = (int)getpid(); d = opendir("/proc"); if (d != NULL) { struct dirent *e; while ((e = readdir(d)) != NULL && n < SELF_KIDS_MAX) { char *end; long pid = strtol(e->d_name, &end, 10); if (*end != '\0' || pid <= 0) continue; memset(&v[n], 0, sizeof(v[n])); if (read_stat((int)pid, &v[n]) != 0) continue; in[n] = pid == self; n++; } closedir(d); } for (pass = 0; pass < 8; pass++) { int changed = 0; for (i = 0; i < n; i++) { size_t j; if (in[i]) continue; for (j = 0; j < n; j++) if (in[j] && v[j].pid == v[i].ppid) { in[i] = 1; changed = 1; break; } } if (!changed) break; } s->kids = 0; s->kids_rss_kb = 0; s->kids_cpu_pct = 0; { double dt = prev_time > 0 ? mono_now() - prev_time : 0; unsigned long long sum = 0; for (i = 0; i < n; i++) { size_t pi2; if (!in[i] || v[i].pid == self) continue; s->kids++; s->kids_rss_kb += v[i].rss_pages * (unsigned long)(sysconf(_SC_PAGESIZE) / 1024); for (pi2 = 0; pi2 < prev_nkids; pi2++) if (prev_kids[pi2].pid == v[i].pid) { sum += v[i].ticks - prev_kids[pi2].ticks; break; } } if (dt > 0 && hz > 0) s->kids_cpu_pct = (double)sum / (double)hz / dt * 100.0; } prev_nkids = 0; for (i = 0; i < n && prev_nkids < SELF_KIDS_MAX; i++) if (in[i] && v[i].pid != self) { prev_kids[prev_nkids].pid = v[i].pid; prev_kids[prev_nkids].ticks = v[i].ticks; prev_nkids++; } } static void sample(struct sample *s) { struct kid self; double now = mono_now(); double dt = prev_time > 0 ? now - prev_time : 0; memset(s, 0, sizeof(*s)); read_self(s); if (read_stat((int)getpid(), &self) == 0) { s->self_ticks = self.ticks; if (dt > 0 && hz > 0 && prev_self_ticks > 0) s->cpu_pct = (double)(self.ticks - prev_self_ticks) / (double)hz / dt * 100.0; prev_self_ticks = self.ticks; } scan_kids(s); prev_time = now; } static void compose(struct sample *s) { char rss[24], kids[24], r[24], w[24]; human(s->rss_kb, rss, sizeof(rss)); human(s->kids_rss_kb, kids, sizeof(kids)); human_bytes(s->io_read, r, sizeof(r)); human_bytes(s->io_write, w, sizeof(w)); snprintf(widget, sizeof(widget), "%s rss %.1f%% cpu\n" "%d thr %d fd\n" "kids %d %s %.1f%% cpu\n" "io %s r %s w", rss, s->cpu_pct, s->threads, s->fds, s->kids, kids, s->kids_cpu_pct, r, w); snprintf(detail, sizeof(detail), "self: pid %d\n" " rss: %s (%lu kB)\n" " threads: %d\n" " fds: %d\n" " cpu: %.1f%%\n" " io: %s read, %s written\n" "children: %d process(es), %s rss, %.1f%% cpu\n", (int)getpid(), rss, s->rss_kb, s->threads, s->fds, s->cpu_pct, r, w, s->kids, kids, s->kids_cpu_pct); } static int self_tick(struct sp_host *h, const char *name, void *ctx) { struct sample s; (void)ctx; if (hz <= 0) hz = sysconf(_SC_CLK_TCK); sample(&s); pthread_mutex_lock(&self_lock); now_s = s; compose(&s); pthread_mutex_unlock(&self_lock); return sp_host_widget_text(h, name, widget); } static void copy_detail(char *out, size_t cap) { pthread_mutex_lock(&self_lock); snprintf(out, cap, "%s", detail); pthread_mutex_unlock(&self_lock); } static int self_tool(const char *args, struct sp_buf *out, void *ctx) { char buf[sizeof(detail)]; (void)args; (void)ctx; copy_detail(buf, sizeof(buf)); sp_buf_puts(out, buf); return 0; } static int self_command(struct sp_host *h, const char *args, struct sp_buf *out, void *ctx) { char buf[sizeof(detail)]; (void)h; (void)ctx; if (args != NULL && *args != '\0') return sp_buf_puts(out, "usage: /self"); copy_detail(buf, sizeof(buf)); sp_buf_puts(out, buf); return 0; } static int self_init(struct sp_host *h) { struct sp_widget w; static const struct sp_status seg = { "self", NULL }; static const struct sp_tool t = { "session_usage", "What this splinter process costs the machine: RSS, CPU, threads " "and file descriptors of the session itself, and the same for " "every child process under it.", "{\"type\":\"object\",\"properties\":{}}", SP_AUTH_READ, self_tool, NULL }; const char *iv; hz = sysconf(_SC_CLK_TCK); memset(&w, 0, sizeof(w)); w.name = "self"; w.tick = self_tick; iv = sp_host_plugin_config(h, "self", "interval"); w.interval_ms = iv != NULL && atoi(iv) > 0 ? (unsigned)atoi(iv) : SELF_DEFAULT_MS; if (sp_host_add_widget(h, &w) != 0) return -1; if (sp_host_add_tool(h, &t) != 0) return -1; if (sp_host_add_command(h, "self", self_command, NULL) != 0) return -1; (void)seg; return 0; } static const struct sp_plugin self_plugin = { SP_PLUGIN_ABI, "self", "1.0.0", self_init, NULL, NULL }; const struct sp_plugin * sp_plugin_describe(void) { return &self_plugin; }