Something went wrong. Try again.
An experimental port of NetBSD, FreeBSD, OpenBSD, and XNU to unkernel unkernel.dev
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475// SPDX-License-Identifier: MIT// Copyright (C) 2026 Pooya Parsa <pooya@pi0.io>// What the resource applets (Cpu.tsx, Memory.tsx, Disk.tsx, Gpu.tsx) share:// the guest's last good read, why there is none, a labelled usage meter and// the list of the top processes (by CPU or by memory). Their numbers are// stats.ts's: an item reads the light samples (useStats("lite")), a panel// the full ones (useStats(): every INTERVAL ms while it is open, with the// processes and df).import type { ReactNode } from "react";import { fmtBytes, fmtPct } from "../../format.ts";import type { StatsState, TopProc } from "../../stats.ts";import { Meter, fullness } from "../Charts.tsx";import { AppletNote } from "./Applet.tsx";
/** The panels' chart height, px. */export const CHART_H = 56;/** Rows of a process list. */const TOP = 5;
/** The last guest read while it is fresh and sampling runs, else null (the meters show empty tracks). */export const guestOf = (st: StatsState) => (!st.waiting && !st.idle ? st.guest : null);
/** Why there are no guest figures, in a few words. */export const whyNot = (st: StatsState) => (st.idle ? `Not sampled: ${st.idle}` : "Waiting for the guest");
/** The pill for a panel without figures: the sampler's state. */export const idlePill = (st: StatsState) => (st.idle ? { state: "not sampled", tone: "off" as const } : { state: "waiting", tone: "pending" as const });
/** A labelled meter: the name and the figures on one line, the bar under it. */export function Usage({ name, used, size, color, note }: { name: ReactNode; used: number; size: number; color: string; note?: string }) { const f = size > 0 ? used / size : 0; return ( <div className="flex flex-col gap-1.5"> <div className="flex min-w-0 items-baseline gap-2 text-[11.5px]"> <span className="min-w-0 truncate text-muted">{name}</span> <span className="ml-auto flex-none text-faint tabular-nums">{fmtBytes(used)} of {fmtBytes(size)}{note ? ` ${note}` : ""} · <span className="text-fg">{fmtPct(f)}</span></span> </div> <Meter total={size} label={typeof name === "string" ? name : "use"} parts={[{ value: used, color: fullness(f, color), label: "used", text: fmtPct(f) }]} /> </div> );}
/** * The top user processes of the last full read: by CPU (the share of one * CPU since the sample before) or by memory (the resident set), each with a * small bar of its share in `color`. */export function ProcList({ st, by, color }: { st: StatsState; by: "cpu" | "mem"; color: string }) { const g = guestOf(st); const all = g?.derived.procs ?? []; const memTotal = g?.raw.mem?.total ?? 0; const cmp = by === "cpu" ? (a: TopProc, b: TopProc) => (b.cpu ?? -1) - (a.cpu ?? -1) || b.rss - a.rss : (a: TopProc, b: TopProc) => b.rss - a.rss || (b.cpu ?? -1) - (a.cpu ?? -1); const top = [...all].sort(cmp).slice(0, TOP); if (!g) return <AppletNote>{whyNot(st)}</AppletNote>; if (!top.length) return <AppletNote>Listed with the next sample</AppletNote>; return ( <ol aria-label={by === "cpu" ? "The busiest processes" : "The largest processes"} className="flex flex-col gap-1"> {top.map((p) => { const share = by === "cpu" ? p.cpu ?? 0 : memTotal ? p.rss / memTotal : 0; return ( <li key={p.pid} className="grid min-w-0 grid-cols-[1fr_auto_auto] items-center gap-2.5 text-[12px] tabular-nums" title={`${p.comm}, pid ${p.pid}`}> <span className="min-w-0 truncate text-muted">{p.comm}<span className="ml-1.5 text-[10.5px] text-faint">{p.pid}</span></span> <span aria-hidden="true" className="relative h-1 w-12 overflow-hidden rounded-full bg-ink/10"> <span className="absolute inset-y-0 left-0 rounded-full" style={{ width: `${Math.min(1, share) * 100}%`, background: color }} /> </span> <span className="w-14 text-right text-fg">{by === "cpu" ? (p.cpu != null ? fmtPct(p.cpu) : "–") : fmtBytes(p.rss)}</span> </li> ); })} </ol> );}