The MCP plugin's reconnect thread was detached: shutdown killed the child and returned, so the thread could still be reading into its line buffer (up to 16 MiB) and could call sp_host_add_tool() into a host that was being freed -- a use-after-free waiting to happen, and the reason three leaks were suppressed in tests/asan.supp (read_line, register_tools, connect_server). Each server now keeps its pthread_t, init creates it joinable, and shutdown waits (bounded, 5 s) for every thread to return before joining it and freeing its state. teardown() frees the line buffer for the HTTP transport too (it was freed only for stdio) and closes the stdio pipes even when the child was already reaped. The registered tools leaked for a second reason: sp_registry_add copies the struct, not the strings, and sp_registry_free freed only the arrays -- the prefixed name, the description, the printed schema and the toolref of every MCP tool were lost with the registry. struct sp_tool gets a dispose hook; the registry calls it for each tool, and the MCP plugin frees what it allocated (the default schema is strdup'd now, so ownership is uniform). Builtins and the gateway's tools leave it NULL; every positional initializer got the new field. tests/asan.supp is empty. make check-asan: all 39 test binaries clean. make check: all tests passed.
468 lines
11 KiB
C
468 lines
11 KiB
C
/*
|
|
* 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/<pid>/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 <ctype.h>
|
|
#include <dirent.h>
|
|
#include <pthread.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <time.h>
|
|
#include <unistd.h>
|
|
|
|
#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/<pid>/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, "%.1f GB",
|
|
(double)kb / (1024.0 * 1024.0));
|
|
else if (kb >= 1024)
|
|
snprintf(buf, cap, "%.0f MB", (double)kb / 1024.0);
|
|
else
|
|
snprintf(buf, cap, "%lu kB", kb);
|
|
}
|
|
|
|
static void
|
|
human_bytes(unsigned long b, char *buf, size_t cap)
|
|
{
|
|
if (b >= 1024UL * 1024UL * 1024UL)
|
|
snprintf(buf, cap, "%.1f GB",
|
|
(double)b / (1024.0 * 1024.0 * 1024.0));
|
|
else if (b >= 1024UL * 1024UL)
|
|
snprintf(buf, cap, "%.1f MB", (double)b / (1024.0 * 1024.0));
|
|
else if (b >= 1024)
|
|
snprintf(buf, cap, "%.0f kB", (double)b / 1024.0);
|
|
else
|
|
snprintf(buf, cap, "%lu B", b);
|
|
}
|
|
|
|
static int
|
|
kid_cmp(const void *a, const void *b)
|
|
{
|
|
const struct kid *x = a, *y = b;
|
|
|
|
return (x->pid > y->pid) - (x->pid < y->pid);
|
|
}
|
|
|
|
/*
|
|
* Who is under this process: one read of every /proc/<pid>/stat - all of them,
|
|
* a busy machine has thousands and stopping at a fixed count missed our own
|
|
* pid - then include anything whose parent is already included until nothing
|
|
* changes. Sorted by pid, a parent is a binary search away.
|
|
*/
|
|
static void
|
|
scan_kids(struct sample *s)
|
|
{
|
|
DIR *d;
|
|
struct kid *v = NULL;
|
|
unsigned char *in = NULL;
|
|
size_t *parent = NULL;
|
|
size_t n = 0, cap = 0, i;
|
|
int self = (int)getpid();
|
|
|
|
d = opendir("/proc");
|
|
if (d != NULL) {
|
|
struct dirent *e;
|
|
|
|
while ((e = readdir(d)) != NULL) {
|
|
char *end;
|
|
long pid = strtol(e->d_name, &end, 10);
|
|
|
|
if (*end != '\0' || pid <= 0)
|
|
continue;
|
|
if (n == cap) {
|
|
size_t ncap = cap ? cap * 2 : 512;
|
|
struct kid *nv = realloc(v, ncap * sizeof(*nv));
|
|
|
|
if (nv == NULL)
|
|
break;
|
|
v = nv;
|
|
cap = ncap;
|
|
}
|
|
memset(&v[n], 0, sizeof(v[n]));
|
|
if (read_stat((int)pid, &v[n]) != 0)
|
|
continue;
|
|
n++;
|
|
}
|
|
closedir(d);
|
|
}
|
|
if (n > 0) {
|
|
in = calloc(n, sizeof(*in));
|
|
parent = calloc(n, sizeof(*parent));
|
|
if (in == NULL || parent == NULL)
|
|
n = 0;
|
|
}
|
|
if (n > 0) {
|
|
qsort(v, n, sizeof(*v), kid_cmp);
|
|
for (i = 0; i < n; i++) {
|
|
struct kid key, *pp;
|
|
|
|
key.pid = v[i].ppid;
|
|
pp = bsearch(&key, v, n, sizeof(*v), kid_cmp);
|
|
parent[i] = pp != NULL ? (size_t)(pp - v) : n;
|
|
in[i] = v[i].pid == self;
|
|
}
|
|
}
|
|
/* each pass adds at least one more generation, so this ends */
|
|
for (;;) {
|
|
int changed = 0;
|
|
|
|
for (i = 0; i < n; i++)
|
|
if (!in[i] && parent[i] < n && in[parent[i]]) {
|
|
in[i] = 1;
|
|
changed = 1;
|
|
}
|
|
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++;
|
|
}
|
|
free(v);
|
|
free(in);
|
|
free(parent);
|
|
}
|
|
|
|
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));
|
|
if (s->kids == 0)
|
|
snprintf(widget, sizeof(widget),
|
|
"%s memory | %.1f%% cpu\n"
|
|
"%d threads | %d open files\n"
|
|
"no children\n"
|
|
"io %s read | %s written",
|
|
rss, s->cpu_pct, s->threads, s->fds, r, w);
|
|
else
|
|
snprintf(widget, sizeof(widget),
|
|
"%s memory | %.1f%% cpu\n"
|
|
"%d threads | %d open files\n"
|
|
"children %d | %s | %.1f%% cpu\n"
|
|
"io %s read | %s written",
|
|
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"
|
|
" memory (RSS): %s (%lu kB)\n"
|
|
" threads: %d\n"
|
|
" open files: %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,
|
|
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;
|
|
}
|