From c083b40500b0496e1e8e1a32fc31b70b5e9b960d Mon Sep 17 00:00:00 2001 From: "Danilo M." Date: Sat, 24 Oct 2026 10:09:11 +0200 Subject: perf: the five heaviest processes in the CPU card, by CPU or RAM A table under RAM lists the top five processes with their CPU and resident memory, sorted by either through a toggle in the card. The toggle re-sorts the frame already held and is not saved. The rows come from one `top -b -d2` that runs only while the page is open and dies with it, checked with pgrep. ps was not used: its %CPU is a lifetime average. top's first frame is the same lifetime figure, so it is skipped. top marks no frame end but writes each frame in one burst, so a frame is published once its output has been quiet for 150ms, and rows appear about 2s after opening, not 4s as when waiting for the next header. SplitParser delivered every header after the first as "\ntop - ...", so the header match is a regex that allows leading whitespace; AGENTS.md records it. The parser is Perf.js procs(), covered in test_perf.js. Columns are right-aligned at fixed widths with tabular figures. Co-Authored-By: Claude Opus 5.5 --- desktop/modules/perf/Perf.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'desktop/modules/perf/Perf.js') diff --git a/desktop/modules/perf/Perf.js b/desktop/modules/perf/Perf.js index d84c694..8294e94 100644 --- a/desktop/modules/perf/Perf.js +++ b/desktop/modules/perf/Perf.js @@ -188,6 +188,25 @@ function push(list, v, n) { return out.length > n ? out.slice(out.length - n) : out; } +// The process rows of one `top -b` frame, run under LC_ALL=C: [{pid, name, +// cpu, mem}], cpu in percent of one core as top shows it, mem the resident +// size in bytes. RES is KiB unless top scaled it to a suffixed unit (1.0g), +// and COMMAND is the last column and may hold spaces ("Isolated Web Co"). +function procs(text) { + const out = []; + let rows = false; + for (const line of text.split("\n")) { + if (/^\s*PID\s+USER\b/.test(line)) { rows = true; continue; } + if (!rows) continue; + const f = line.trim().split(/\s+/); + const res = /^([\d.]+)([kmgtp]?)$/.exec(f[5] || ""); + if (f.length < 12 || !res) continue; + const scale = 1024 ** (1 + Math.max(0, "kmgtp".indexOf(res[2] || "k"))); + out.push({ pid: Number(f[0]), name: f.slice(11).join(" "), cpu: Number(f[8]), mem: Number(res[1]) * scale }); + } + return out; +} + function bytes(n) { if (n === null || n === undefined) return "--"; const u = ["B", "K", "M", "G", "T"]; -- cgit v1.2.3