fix shared memory fallback accounting

This commit is contained in:
Codex
2026-05-17 16:54:21 +00:00
parent 236c5c38f6
commit 3a2f86df9e
6 changed files with 29 additions and 10 deletions
+5 -1
View File
@@ -884,7 +884,11 @@ function ProcessResourceTable({ current, onRaw }: AnyRecord) {
const processSummary = current?.processSummary && typeof current.processSummary === "object" ? current.processSummary : {};
const processes = Array.isArray(current?.processes) ? current.processes : [];
const memoryMode = String(processSummary.memoryMode || "");
const memoryModeLabel = memoryMode === "pss_smaps_rollup" ? "PSS" : "RSS fallback";
const memoryModeLabel = memoryMode.includes("pss_smaps_rollup")
? "PSS"
: memoryMode === "rss_minus_shared_fallback"
? "RSS-shared"
: "RSS fallback";
const rows = useMemo(() => {
const direction = sort.direction === "asc" ? 1 : -1;
return [...processes].sort((left: AnyRecord, right: AnyRecord) => {
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@unidesk/provider-gateway",
"version": "0.2.23",
"version": "0.2.24",
"private": true,
"type": "module",
"scripts": {
+20 -5
View File
@@ -914,7 +914,7 @@ function readProcessIo(pid: number): { readBytes: number; writeBytes: number } {
}
}
function readProcessMemory(pid: number, rssBytes: number): {
function readProcessMemory(pid: number, rssBytes: number, pageSize: number): {
memoryBytes: number;
memoryMode: string;
pssBytes?: number;
@@ -945,7 +945,20 @@ function readProcessMemory(pid: number, rssBytes: number): {
swapPssBytes,
};
} catch {
return { memoryBytes: rssBytes, memoryMode: "rss_fallback" };
try {
const fields = readFileSync(`/proc/${pid}/statm`, "utf8").trim().split(/\s+/);
const sharedPages = Number(fields[2]);
const sharedBytes = Number.isFinite(sharedPages) ? Math.max(0, sharedPages * pageSize) : 0;
const privateBytes = Math.max(0, rssBytes - sharedBytes);
return {
memoryBytes: privateBytes,
memoryMode: "rss_minus_shared_fallback",
privateBytes,
sharedBytes,
};
} catch {
return { memoryBytes: rssBytes, memoryMode: "rss_fallback" };
}
}
}
@@ -1024,7 +1037,7 @@ async function collectProcessResources(totalMemoryBytes: number, cpuCores: numbe
const readBytesPerSecond = previous && ioSeconds > 0 ? Math.max(0, io.readBytes - previous.readBytes) / ioSeconds : 0;
const writeBytesPerSecond = previous && ioSeconds > 0 ? Math.max(0, io.writeBytes - previous.writeBytes) / ioSeconds : 0;
const rssBytes = Math.max(0, stat.rssPages * pageSize);
const processMemory = readProcessMemory(pid, rssBytes);
const processMemory = readProcessMemory(pid, rssBytes, pageSize);
const name = status.name || stat.name;
const uid = status.uid >= 0 ? status.uid : 0;
rows.push({
@@ -1068,6 +1081,7 @@ async function collectProcessResources(totalMemoryBytes: number, cpuCores: numbe
previousProcessSamples = new Map([...previousProcessSamples].filter(([pid]) => seen.has(pid)));
rows.sort((a, b) => b.memoryBytes - a.memoryBytes || b.cpuPercent - a.cpuPercent || a.pid - b.pid);
const pssRows = rows.filter((row) => row.memoryMode === "pss_smaps_rollup").length;
const rssMinusSharedRows = rows.filter((row) => row.memoryMode === "rss_minus_shared_fallback").length;
return {
processes: rows.slice(0, 120),
summary: {
@@ -1076,9 +1090,10 @@ async function collectProcessResources(totalMemoryBytes: number, cpuCores: numbe
skipped,
defaultSort: "memory_desc",
scope: "provider_pid_namespace",
memoryMode: pssRows > 0 ? "pss_smaps_rollup" : "rss_fallback",
memoryMode: pssRows > 0 ? "pss_smaps_rollup_with_fallback" : rssMinusSharedRows > 0 ? "rss_minus_shared_fallback" : "rss_fallback",
pssRows,
rssFallbackRows: rows.length - pssRows,
rssMinusSharedRows,
rssFallbackRows: rows.length - pssRows - rssMinusSharedRows,
cpuPercentMode: hasPreviousProcessSample ? "delta_ticks_per_sample" : "lifetime_average_first_sample",
diskIoMode: "proc_pid_io_delta_bytes_per_second",
},