Files
splinter/plugins/sysinfo/sysinfo.c
T
lotherk 67b4d5d070 goal in the session, /continue, a white sidebar, sysinfo
The goal lived only in the focus plugin's memory: a resume lost it and
there was no way to start the next turn from it. It is now stored in the
session's .meta (one line, 1000 bytes) and read back when the plugin sees a
new session id, so /open and a resumed process find their own goal again.
/continue reads the same sidecar and starts a turn with "Continue working
toward this goal: ...". /goal still sets it and starts the recap turn.

The sidebar was a patchwork of pairs and widgets could inherit the wrong
background. It now uses one PAIR_SIDE (white on a darkened theme
background) for every heading, label, usage row and widget; the window
gets that pair as its background, so no line paints its own surface.

New plugins/sysinfo: a widget thread samples /proc/meminfo, /proc/stat and
statvfs("/"), and publishes RAM, CPU, swap and disk with five-cell
[|||| ] gauges every two seconds. It is added to the shipped plugin list
and to the user's config.

agent.c keeps the last prompt/cache counters when a tool-call step reports
no usage. A zero there used to reset the ctx gauge in the status bar after
every tool call.

Tests: session goal survives reopen, /continue returns the prompt,
make check: all tests passed.
2026-11-16 18:30:42 +00:00

192 lines
4.7 KiB
C

/*
* splinter -- sysinfo: RAM, CPU, swap and disk in the sidebar.
*
* Copyright (c) 2026 Konrad Lother
* SPDX-License-Identifier: BSD-3-Clause
*
* Four numbers the operator checks before starting a long turn: is the box
* swapping, is the CPU already busy, is the disk full. Linux exposes all of
* them in /proc and statvfs(2); the widget thread samples them every two
* seconds and publishes one compact line:
*
* RAM 62% [||| ] CPU 13% [| ] SWAP 0% [ ] DISK 71% [|||| ]
*
* The sidebar wraps it at spaces. A missing /proc entry is not an error:
* the plugin still loads and says n/a for what it cannot read.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/statvfs.h>
#include <unistd.h>
#include "splinter.h"
#include "splinter_plugin.h"
#define BAR_WIDTH 5
static int have_cpu;
static unsigned long long prev_total, prev_idle;
/* one [||| ] gauge, width 5 */
static void
gauge(char *b, size_t cap, double pct)
{
int i, filled;
if (cap < BAR_WIDTH + 3)
return;
if (pct < 0)
pct = 0;
if (pct > 100)
pct = 100;
filled = (int)((pct * BAR_WIDTH + 50) / 100);
b[0] = '[';
for (i = 0; i < BAR_WIDTH; i++)
b[i + 1] = i < filled ? '|' : ' ';
b[BAR_WIDTH + 1] = ']';
b[BAR_WIDTH + 2] = '\0';
}
/* /proc/stat's first line: total and idle ticks */
static int
cpu_read(unsigned long long *total, unsigned long long *idle)
{
FILE *f = fopen("/proc/stat", "r");
char line[512];
unsigned long long v[10];
int n;
if (f == NULL)
return -1;
if (fgets(line, sizeof(line), f) == NULL) {
fclose(f);
return -1;
}
fclose(f);
memset(v, 0, sizeof(v));
n = sscanf(line, "cpu %llu %llu %llu %llu %llu %llu %llu %llu %llu",
&v[0], &v[1], &v[2], &v[3], &v[4], &v[5], &v[6], &v[7], &v[8]);
if (n < 4)
return -1;
*total = v[0] + v[1] + v[2] + v[3] + v[4] + v[5] + v[6] + v[7] +
v[8];
*idle = v[3] + v[4]; /* idle + iowait */
return *total > 0 ? 0 : -1;
}
/* /proc/meminfo: MemTotal, MemAvailable or Free+Buffers+Cached, Swap */
static int
mem_read(unsigned long long *mt, unsigned long long *ma,
unsigned long long *st, unsigned long long *sf)
{
FILE *f = fopen("/proc/meminfo", "r");
char line[256];
unsigned long long v;
unsigned long long free_kb = 0, buffers = 0, cached = 0;
*mt = *ma = *st = *sf = 0;
if (f == NULL)
return -1;
while (fgets(line, sizeof(line), f) != NULL) {
if (sscanf(line, "MemTotal: %llu", &v) == 1)
*mt = v;
else if (sscanf(line, "MemAvailable: %llu", &v) == 1)
*ma = v;
else if (sscanf(line, "MemFree: %llu", &v) == 1)
free_kb = v;
else if (sscanf(line, "Buffers: %llu", &v) == 1)
buffers = v;
else if (sscanf(line, "Cached: %llu", &v) == 1)
cached = v;
else if (sscanf(line, "SwapTotal: %llu", &v) == 1)
*st = v;
else if (sscanf(line, "SwapFree: %llu", &v) == 1)
*sf = v;
}
fclose(f);
if (*ma == 0)
*ma = free_kb + buffers + cached;
return *mt > 0 ? 0 : -1;
}
static double
pct(unsigned long long used, unsigned long long total)
{
if (total == 0)
return 0;
if (used > total)
used = total;
return (double)used * 100.0 / (double)total;
}
static int
tick(struct sp_host *h, const char *name, void *ctx)
{
unsigned long long mt, ma, st, sf, total, idle;
double ram = 0, swap = 0, disk = 0, cpu = 0;
char rb[16], cb[16], sb[16], db[16], line[512];
(void)ctx;
if (mem_read(&mt, &ma, &st, &sf) == 0) {
ram = pct(mt - ma, mt);
if (st > 0)
swap = pct(st - sf, st);
}
if (cpu_read(&total, &idle) == 0) {
if (have_cpu && total > prev_total &&
idle >= prev_idle && total - prev_total > 0) {
unsigned long long dt = total - prev_total;
unsigned long long di = idle - prev_idle;
cpu = pct(dt > di ? dt - di : 0, dt);
}
prev_total = total;
prev_idle = idle;
have_cpu = 1;
}
{
struct statvfs vfs;
if (statvfs("/", &vfs) == 0 && vfs.f_blocks > 0) {
unsigned long long used = (unsigned long long)
(vfs.f_blocks - vfs.f_bfree);
disk = pct(used, (unsigned long long)vfs.f_blocks);
}
}
gauge(rb, sizeof(rb), ram);
gauge(cb, sizeof(cb), cpu);
gauge(sb, sizeof(sb), swap);
gauge(db, sizeof(db), disk);
snprintf(line, sizeof(line),
"RAM %.0f%% %s CPU %.0f%% %s SWAP %.0f%% %s DISK %.0f%% %s",
ram, rb, cpu, cb, swap, sb, disk, db);
return sp_host_widget_text(h, name, line);
}
static int
init(struct sp_host *h)
{
static const struct sp_widget w = { "sysinfo", tick, NULL, NULL, 2000 };
unsigned long long total, idle;
(void)cpu_read(&total, &idle);
if (total > 0) {
prev_total = total;
prev_idle = idle;
have_cpu = 1;
}
return sp_host_add_widget(h, &w);
}
static const struct sp_plugin plugin = {
SP_PLUGIN_ABI, "sysinfo", "0.1.0", init, NULL, NULL
};
const struct sp_plugin *
sp_plugin_describe(void)
{
return &plugin;
}