A command that talks to the network (/connect, /model probe, a plugin command, a commands/ program) ran on the UI thread: the prompt froze until it returned. ui_command_run() now runs be->input on its own thread while the screen keeps reading keys. One next line can be queued and is processed when the command ends; Ctrl-C clears the edit, Ctrl-D quits. The worker never touches curses, and the backend is not polled while it runs. Widget text may now have continuation lines without "name:": they are rendered under the widget above. sysinfo uses that for one metric per row instead of wrapping "RAM ... CPU ..." into the same paragraph. A blank row stays between widgets, not between their rows. The Ctrl-C UI test looks for the active steer prompt; "working" is replaced by it on the first 100 ms tick. make check: all tests passed.
192 lines
4.7 KiB
C
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\nCPU %.0f%% %s\nSWAP %.0f%% %s\nDISK %.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;
|
|
}
|