// SPDX-License-Identifier: MIT // Copyright (C) 2026 Pooya Parsa // 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 (
{name} {fmtBytes(used)} of {fmtBytes(size)}{note ? ` ${note}` : ""} · {fmtPct(f)}
); } /** * 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 {whyNot(st)}; if (!top.length) return Listed with the next sample; return (
    {top.map((p) => { const share = by === "cpu" ? p.cpu ?? 0 : memTotal ? p.rss / memTotal : 0; return (
  1. {p.comm}{p.pid}
  2. ); })}
); }