Something went wrong. Try again.
An experimental port of NetBSD, FreeBSD, OpenBSD, and XNU to unkernel unkernel.dev
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778// SPDX-License-Identifier: MIT// Copyright (C) 2026 Pooya Parsa <pooya@pi0.io>// 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 ( <AppletButton id="mem" title={title} tone={shareTone(share)} meter {...props}> <RamIcon size={ICON} /><Bars values={[share]} /> </AppletButton> );}
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 ( <AppletPanel id="mem" title="Memory" icon={<RamIcon size={16} />} {...pill} monitor="status" settings="boot" {...props}> <AppletSection> {m ? ( <> <Spark points={st.points} height={CHART_H} max={m.total} fmt={fmtBytes} label="Guest memory in use" series={[{ get: (p) => p.mem, label: "in use", color: TONE.mem, area: true }]} /> <Meter total={m.total} label="Guest memory" parts={[ { value: used, color: usedColor, label: "in use", text: fmtBytes(used) }, { value: cache, color: cacheColor, label: "cache", text: fmtBytes(cache) }, ]} /> <div className="flex flex-wrap gap-x-4 gap-y-1 text-[11px] text-faint"> <Key color={usedColor}>in use</Key><Key color={cacheColor}>cache</Key><Key color="var(--color-line)">free</Key> </div> </> ) : <AppletNote height={CHART_H}>{whyNot(st)}</AppletNote>} <AppletFacts items={[ ["In use", m ? `${fmtBytes(used)} of ${fmtBytes(m.total)}` : "–"], ["Available", m ? fmtBytes(m.available) : "–"], m && ["Cache", fmtBytes(cache)], m && ["Free", fmtBytes(m.free)], m && m.shmem > 0 && ["Shared", fmtBytes(m.shmem)], m && ["Swap", m.swapTotal > 0 ? `${fmtBytes(m.swapTotal - m.swapFree)} of ${fmtBytes(m.swapTotal)}` : "none"], ["Machine", `${kernelMiB()} MiB`], ]} /> </AppletSection> <AppletSection title="Processes" aside="resident memory" className="border-t border-ink/10"> <ProcList st={st} by="mem" color={TONE.mem} /> </AppletSection> </AppletPanel> );}