// SPDX-License-Identifier: MIT // Copyright (C) 2026 Pooya Parsa // The Memory applet (#tb-mem, its panel #tb-mem-panel). The item: a memory // module and a bar filled to the guest memory in use (MemTotal less // MemAvailable; amber from 85 %, red from 95 %), the figures in its // tooltip. The panel: the share in use as its pill, the in-use history // (Charts.tsx Spark, scaled to the total), one bar of in use, cache and // free, the figures (in use, available, cache, free, shared, swap, the // machine's memory), then the largest user processes by resident set. // Monitor (the header) opens System's Overview, Settings opens Settings' Machine section // (the memory size). import { fmtBytes, fmtPct } from "../../format.ts"; import { KW } from "../../kw.ts"; import { useStats } from "../../stats.ts"; import { Key, Meter, Spark, TONE, fullness } from "../Charts.tsx"; import type { PopoverProps } from "../ControlCenter.tsx"; import { RamIcon } from "../icons.tsx"; import { AppletButton, AppletFacts, AppletNote, AppletPanel, AppletSection, Bars, ICON, shareTone, type AppletItemProps } from "./Applet.tsx"; import { CHART_H, guestOf, idlePill, ProcList, whyNot } from "./Resource.tsx"; const mib = (n: number) => Math.round(n / 1048576); /** The machine's memory, MiB: what the kernel got, else what was asked for. */ const kernelMiB = () => KW.memActualMiB || KW.config.memMiB; /** A colour, faded: a part of a meter next to its full tone. */ const faded = (c: string, f = 35) => `color-mix(in srgb, ${c} ${f}%, transparent)`; export function MemoryItem(props: AppletItemProps) { const st = useStats("lite"); const m = guestOf(st)?.raw.mem; const used = m ? m.total - m.available : 0, share = m && m.total > 0 ? used / m.total : null; const why = st.idle ? `not sampled (${st.idle})` : "waiting for the guest"; const title = m ? `Memory ${mib(used)} of ${mib(m.total)} MiB in use (${fmtPct(share ?? 0)})` : `Memory: ${why} (kernel memory ${kernelMiB()} MiB)`; return ( ); } export function MemoryPanel(props: PopoverProps) { const st = useStats(); const m = guestOf(st)?.raw.mem; const used = m ? m.total - m.available : 0, cache = m ? Math.max(0, m.available - m.free) : 0; const f = m && m.total > 0 ? used / m.total : 0; const usedColor = fullness(f, TONE.mem), cacheColor = faded(TONE.mem); const pill = m ? { state: `${fmtPct(f)} in use`, tone: shareTone(f) ?? "pending" } : idlePill(st); return ( } {...pill} monitor="status" settings="boot" {...props}> {m ? ( <> p.mem, label: "in use", color: TONE.mem, area: true }]} />
in usecachefree
) : {whyNot(st)}} 0 && ["Shared", fmtBytes(m.shmem)], m && ["Swap", m.swapTotal > 0 ? `${fmtBytes(m.swapTotal - m.swapFree)} of ${fmtBytes(m.swapTotal)}` : "none"], ["Machine", `${kernelMiB()} MiB`], ]} />
); }