From d39e0a7243b416be769b5bf7bb2a2c4626ed821f Mon Sep 17 00:00:00 2001 From: Szymon Chmal Date: Wed, 14 Oct 2026 14:55:49 +0200 Subject: [PATCH 1/8] test: Linux load is measured against a cgroup v2 CPU or memory limit and marked limit (#532) --- src/host-load/sampler.test.ts | 41 ++++ src/ports/host-load.test.ts | 404 ++++++++++++++++++++++++++++++++++ src/ports/host-load.ts | 1 + 3 files changed, 446 insertions(+) diff --git a/src/host-load/sampler.test.ts b/src/host-load/sampler.test.ts index baeb7a49e..994fb61d0 100644 --- a/src/host-load/sampler.test.ts +++ b/src/host-load/sampler.test.ts @@ -5,6 +5,8 @@ import { FakeHostLoadProbe, type HostLoadCounters, JsonLinesLogger, + LinuxHostLoadProbe, + MemoryFilesystem, MemoryLogSink, } from "../ports/index.js"; import { HostLoadSampler } from "./index.js"; @@ -316,3 +318,42 @@ describe("HostLoadSampler", () => { expect(clock.pendingTimerCount).toBe(0); }); }); + +describe("HostLoadSampler under a cgroup CPU limit", () => { + it("reports busy % as the cgroup's usage over elapsed time times the limit's cores, from two reads", async () => { + const filesystem = new MemoryFilesystem(); + const write = async (path: string, contents: string): Promise => { + await filesystem.mkdirp(path.slice(0, path.lastIndexOf("/"))); + await filesystem.writeFileAtomic(path, contents); + }; + await write("/proc/stat", "cpu 100 10 50 300 20 5 7 3 99 98\n"); + await write( + "/proc/meminfo", + "MemTotal: 16000000 kB\nMemAvailable: 4000000 kB\nSwapTotal: 0 kB\nSwapFree: 0 kB\n", + ); + await write("/proc/self/cgroup", "0::/x\n"); + await write("/sys/fs/cgroup/x/cpu.max", "200000 100000\n"); + await write("/sys/fs/cgroup/x/cpu.stat", "usage_usec 1000000\n"); + let now = 10_000_000; + const clock = new FakeClock(1_000); + const sampler = new HostLoadSampler({ + clock, + probe: new LinuxHostLoadProbe({ + cores: () => 64, + dataDirectory: "/data", + filesystem, + nowMicros: () => now, + }), + }); + sampler.start(); + await flush(); + + now = 20_000_000; + await write("/sys/fs/cgroup/x/cpu.stat", "usage_usec 7100000\n"); + clock.advance(15_000); + await flush(); + + expect(sampler.current()?.cpu).toStrictEqual({ busyPercent: 61, cores: 2, limited: true }); + sampler.dispose(); + }); +}); diff --git a/src/ports/host-load.test.ts b/src/ports/host-load.test.ts index 623f9a112..cc51e34cb 100644 --- a/src/ports/host-load.test.ts +++ b/src/ports/host-load.test.ts @@ -13,6 +13,7 @@ import { productionHostLoadProbe, ScriptedProcessRunner, type CpuTimes, + type HostLoadCounters, } from "./index.js"; vi.mock("node:os", async (importOriginal) => { @@ -267,6 +268,366 @@ describe("LinuxHostLoadProbe", () => { }); }); +const GIB = 1024 ** 3; +const CGROUP_ROOT = "/sys/fs/cgroup"; +const BIG_MEMINFO = + "MemTotal: 67108864 kB\nMemFree: 1000000 kB\nMemAvailable: 4000000 kB\nSwapTotal: 2000000 kB\nSwapFree: 1500000 kB\n"; +const MACHINE_MEMORY = { + availableBytes: 4_000_000 * 1024, + limited: false, + swapUsedBytes: 500_000 * 1024, + totalBytes: 67_108_864 * 1024, +}; + +interface CgroupFolder { + readonly cpuMax?: string; + readonly memoryMax?: string; + readonly cpuStat?: string; + readonly current?: string; + readonly stat?: string; + readonly swap?: string; +} + +function folderFiles(folder: string, values: CgroupFolder): Record { + const base = folder === "" ? CGROUP_ROOT : `${CGROUP_ROOT}/${folder}`; + const entries: [string, string | undefined][] = [ + ["cpu.max", values.cpuMax], + ["memory.max", values.memoryMax], + ["cpu.stat", values.cpuStat], + ["memory.current", values.current], + ["memory.stat", values.stat], + ["memory.swap.current", values.swap], + ]; + const files: Record = {}; + for (const [name, value] of entries) { + if (value !== undefined) files[`${base}/${name}`] = value; + } + return files; +} + +const USAGE = { + cpuStat: "usage_usec 5000000\nuser_usec 3000000\nsystem_usec 2000000\n", + current: "5368709120\n", + stat: "anon 1\ninactive_file 2147483648\nactive_file 5\n", +}; + +async function cgroupFilesystem( + cgroupLine: string | undefined, + files: Record, +): Promise { + const filesystem = new SpaceFilesystem({ freeBytes: 120, totalBytes: 500 }); + const all: Record = { + "/proc/meminfo": BIG_MEMINFO, + "/proc/stat": PROC_STAT, + ...(cgroupLine === undefined ? {} : { "/proc/self/cgroup": cgroupLine }), + ...files, + }; + for (const [path, contents] of Object.entries(all)) { + await filesystem.mkdirp(path.slice(0, path.lastIndexOf("/")) || "/"); + await filesystem.writeFileAtomic(path, contents); + } + return filesystem; +} + +function cgroupProbe( + filesystem: MemoryFilesystem, + options: { cores?: number; now?: () => number } = {}, +): LinuxHostLoadProbe { + return new LinuxHostLoadProbe({ + cores: () => options.cores ?? 64, + dataDirectory: "/data", + filesystem, + nowMicros: options.now ?? (() => 10_000_000), + }); +} + +async function readCgroup( + path: string, + folders: Record, + options: { cores?: number; now?: () => number } = {}, +): Promise { + const files: Record = {}; + for (const [folder, values] of Object.entries(folders)) { + Object.assign(files, folderFiles(folder, values)); + } + const filesystem = await cgroupFilesystem(`0::${path}\n`, files); + return cgroupProbe(filesystem, options).read(); +} + +const LIMITED_TWO_CORES = { cores: 2, limited: true, busyTime: 5_000_000, totalTime: 20_000_000 }; +const LIMITED_EIGHT_GIB = { + availableBytes: 5 * GIB, + limited: true, + swapUsedBytes: 0, + totalBytes: 8 * GIB, +}; + +describe("LinuxHostLoadProbe under cgroup v2 limits", () => { + it("reports cores as the cpu.max quota over its period, and CPU time from cpu.stat against elapsed time, as limited", async () => { + const counters = await readCgroup("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/docker/abc", { + "docker/abc": { cpuMax: "200000 100000\n", ...USAGE }, + }); + + expect(counters.cpu).toStrictEqual(LIMITED_TWO_CORES); + }); + + it("reports a fractional quota as fractional cores", async () => { + const counters = await readCgroup("/x", { x: { cpuMax: "1000 100000\n", ...USAGE } }); + + expect(counters.cpu).toMatchObject({ cores: 0.01, limited: true, totalTime: 100_000 }); + }); + + it("uses /sys/fs/cgroup itself for a 0::/ line", async () => { + const counters = await readCgroup("/", { + "": { cpuMax: "150000 100000\n", memoryMax: "max\n", ...USAGE }, + x: { cpuMax: "100000 100000\n", ...USAGE }, + }); + + expect(counters.cpu).toMatchObject({ cores: 1.5, limited: true }); + expect(counters.memory).toStrictEqual(MACHINE_MEMORY); + }); + + it("takes the lowest CPU and the lowest memory limit across Simlock's folder and its parents, comparing CPU limits as cores", async () => { + const memory = await readCgroup("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/system.slice/simlock.service", { + "": { cpuMax: "max 100000\n" }, + "system.slice": { memoryMax: "8589934592\n", ...USAGE }, + "system.slice/simlock.service": { + memoryMax: "17179869184\n", + current: "1\n", + stat: "inactive_file 0\n", + }, + }); + expect(memory.memory).toStrictEqual(LIMITED_EIGHT_GIB); + expect(memory.cpu).toStrictEqual({ + busyTime: 175, + cores: 64, + limited: false, + totalTime: 495, + }); + + const cpu = await readCgroup("/x", { + x: { cpuMax: "200000 100000\n", ...USAGE }, + "": { cpuMax: "150000 50000\n" }, + }); + expect(cpu.cpu).toMatchObject({ cores: 2, limited: true }); + }); + + it("reads usage from the folder that holds the winning limit, not from Simlock's own folder, and from the topmost of two folders tied on the lowest limit", async () => { + const winner = await readCgroup("/a/b/c", { + a: { cpuMax: "200000 100000\n", memoryMax: "8589934592\n", ...USAGE }, + "a/b": { cpuMax: "400000 100000\n" }, + "a/b/c": { + cpuMax: "400000 100000\n", + cpuStat: "usage_usec 1\n", + current: "1\n", + stat: "inactive_file 0\n", + }, + }); + expect(winner.cpu).toStrictEqual(LIMITED_TWO_CORES); + expect(winner.memory).toStrictEqual(LIMITED_EIGHT_GIB); + + const tied = await readCgroup("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/a/b", { + a: { cpuMax: "200000 100000\n", memoryMax: "8589934592\n", ...USAGE }, + "a/b": { + cpuMax: "200000 100000\n", + memoryMax: "8589934592\n", + cpuStat: "usage_usec 1\n", + current: "1\n", + stat: "inactive_file 0\n", + }, + }); + expect(tied.cpu).toStrictEqual(LIMITED_TWO_CORES); + expect(tied.memory).toStrictEqual(LIMITED_EIGHT_GIB); + }); + + it("reads only /sys/fs/cgroup for a cgroup path with a .. segment", async () => { + const counters = await readCgroup("/../../system.slice/x", { + "": { cpuMax: "200000 100000\n", ...USAGE }, + "system.slice/x": { cpuMax: "100000 100000\n", ...USAGE }, + }); + + expect(counters.cpu).toStrictEqual(LIMITED_TWO_CORES); + }); + + it("accepts every file with the kernel's trailing newline, and nothing else after the value", async () => { + const folders = { + x: { cpuMax: "200000 100000\n", memoryMax: "8589934592\n", ...USAGE, swap: "1048576\n" }, + }; + const accepted = await readCgroup("/x", folders); + expect(accepted.cpu).toStrictEqual(LIMITED_TWO_CORES); + expect(accepted.memory).toMatchObject({ limited: true, swapUsedBytes: 1_048_576 }); + + const extra = await readCgroup("/x", { + x: { ...folders.x, cpuMax: "200000 100000\n\n", memoryMax: "8589934592\n\n" }, + }); + expect(extra.cpu).toMatchObject({ limited: false }); + expect(extra.memory).toStrictEqual(MACHINE_MEMORY); + }); + + it.each([ + "garbage\n", + "200000\n", + "200000 0\n", + "-1 100000\n", + "max 100000\n", + "200000 100000\n", + ])("treats cpu.max %j as no limit at that level", async (value) => { + const accepted = await readCgroup("/x", { + "": { cpuMax: "300000 100000\n", ...USAGE }, + x: { cpuMax: "200000 100000\n" }, + }); + expect(accepted.cpu).toMatchObject({ cores: 2, limited: true }); + + const counters = await readCgroup("/x", { + "": { cpuMax: "300000 100000\n", ...USAGE }, + x: { cpuMax: value }, + }); + + expect(counters.cpu).toMatchObject({ cores: 3, limited: true }); + }); + + it("reports the machine's CPU, not limited, for a quota at or above the machine's cores", async () => { + const folders = { x: { cpuMax: "300000 100000\n", ...USAGE } }; + const below = await readCgroup("/x", folders, { cores: 4 }); + expect(below.cpu).toMatchObject({ cores: 3, limited: true }); + + const counters = await readCgroup( + "/x", + { x: { cpuMax: "800000 100000\n", ...USAGE } }, + { cores: 4 }, + ); + + expect(counters.cpu).toMatchObject({ cores: 4, limited: false }); + expect(counters.cpu?.totalTime).toBe(100 + 10 + 50 + 5 + 7 + 3 + 300 + 20); + }); + + it.each([ + ["cpu.stat is missing", { cpuMax: "200000 100000\n" }], + ["cpu.stat has no usage_usec", { cpuMax: "200000 100000\n", cpuStat: "user_usec 1\n" }], + ])("leaves CPU out under a CPU limit when %s", async (_why, folder) => { + const accepted = await readCgroup("/x", { x: { cpuMax: "200000 100000\n", ...USAGE } }); + expect(accepted.cpu).toBeDefined(); + + const counters = await readCgroup("/x", { x: folder }); + + expect(counters.cpu).toBeUndefined(); + expect(counters.memory).toBeDefined(); + }); + + it("reports memory total as the limit, and available as the limit minus memory.current plus inactive_file, floored at 0, as limited", async () => { + const counters = await readCgroup("/x", { x: { memoryMax: "8589934592\n", ...USAGE } }); + expect(counters.memory).toStrictEqual(LIMITED_EIGHT_GIB); + + const over = await readCgroup("/x", { + x: { memoryMax: "8589934592\n", current: "10737418240\n", stat: "inactive_file 0\n" }, + }); + expect(over.memory).toStrictEqual({ ...LIMITED_EIGHT_GIB, availableBytes: 0 }); + }); + + it("reports swap used as memory.swap.current under a memory limit, and 0 when that file is missing", async () => { + const withSwap = await readCgroup("/x", { + x: { memoryMax: "8589934592\n", ...USAGE, swap: "123456\n" }, + }); + expect(withSwap.memory).toMatchObject({ limited: true, swapUsedBytes: 123_456 }); + + const without = await readCgroup("/x", { x: { memoryMax: "8589934592\n", ...USAGE } }); + expect(without.memory).toMatchObject({ limited: true, swapUsedBytes: 0 }); + }); + + it.each([ + ["max", "max\n"], + ["a value above MemTotal", "1099511627776\n"], + ["an unparsable file", "garbage\n"], + ])("reports the machine's memory, not limited, for memory.max %s", async (_why, value) => { + const accepted = await readCgroup("/x", { x: { memoryMax: "8589934592\n", ...USAGE } }); + expect(accepted.memory?.limited).toBe(true); + + const counters = await readCgroup("/x", { x: { memoryMax: value, ...USAGE } }); + + expect(counters.memory).toStrictEqual(MACHINE_MEMORY); + }); + + it("leaves memory out under a memory limit when memory.current or inactive_file cannot be read, and keeps the limit when /proc/meminfo cannot be read", async () => { + const limit = { memoryMax: "8589934592\n" }; + const noCurrent = await readCgroup("/x", { x: { ...limit, stat: USAGE.stat } }); + expect(noCurrent.memory).toBeUndefined(); + const noInactive = await readCgroup("/x", { + x: { ...limit, current: USAGE.current, stat: "anon 1\n" }, + }); + expect(noInactive.memory).toBeUndefined(); + const noStat = await readCgroup("/x", { x: { ...limit, current: USAGE.current } }); + expect(noStat.memory).toBeUndefined(); + + const filesystem = await cgroupFilesystem("0::/x\n", folderFiles("x", { ...limit, ...USAGE })); + await filesystem.rm("/proc/meminfo"); + const counters = await cgroupProbe(filesystem).read(); + expect(counters.memory).toStrictEqual(LIMITED_EIGHT_GIB); + }); + + it("decides the CPU and memory limits independently", async () => { + const counters = await readCgroup("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/a/b", { + a: { cpuMax: "200000 100000\n", ...USAGE }, + "a/b": { memoryMax: "8589934592\n", ...USAGE }, + }); + + expect(counters.cpu).toStrictEqual(LIMITED_TWO_CORES); + expect(counters.memory).toStrictEqual(LIMITED_EIGHT_GIB); + }); + + it.each([ + ["a cgroup path with files missing", "0::/user.slice/x\n"], + ["v1 lines only", "12:cpu,cpuacct:/docker/abc\n1:name=systemd:/docker/abc\n"], + ])("reports the machine's figures when /proc/self/cgroup is %s", async (_why, line) => { + const files = folderFiles("docker/abc", { + cpuMax: "200000 100000\n", + memoryMax: "8589934592\n", + ...USAGE, + }); + const accepted = await cgroupFilesystem("0::/docker/abc\n", files); + expect((await cgroupProbe(accepted).read()).cpu?.limited).toBe(true); + + const counters = await cgroupProbe(await cgroupFilesystem(line, files)).read(); + + expect(counters.cpu).toMatchObject({ cores: 64, limited: false }); + expect(counters.memory).toStrictEqual(MACHINE_MEMORY); + }); + + it("reports the machine's figures when /proc/self/cgroup cannot be read", async () => { + const files = folderFiles("docker/abc", { cpuMax: "200000 100000\n", ...USAGE }); + + const counters = await cgroupProbe(await cgroupFilesystem(undefined, files)).read(); + + expect(counters.cpu).toMatchObject({ cores: 64, limited: false }); + expect(counters.memory).toStrictEqual(MACHINE_MEMORY); + }); + + it("shows a limit seeded between two reads on the second read, drops one removed between reads, and follows /proc/self/cgroup to a new folder between reads", async () => { + const filesystem = await cgroupFilesystem("0::/a\n", {}); + const probe = cgroupProbe(filesystem); + expect((await probe.read()).cpu?.limited).toBe(false); + + for (const [path, contents] of Object.entries( + folderFiles("a", { cpuMax: "200000 100000\n", ...USAGE }), + )) { + await filesystem.mkdirp(path.slice(0, path.lastIndexOf("/"))); + await filesystem.writeFileAtomic(path, contents); + } + expect((await probe.read()).cpu).toStrictEqual(LIMITED_TWO_CORES); + + for (const [path, contents] of Object.entries( + folderFiles("b", { cpuMax: "100000 100000\n", ...USAGE }), + )) { + await filesystem.mkdirp(path.slice(0, path.lastIndexOf("/"))); + await filesystem.writeFileAtomic(path, contents); + } + await filesystem.writeFileAtomic("/proc/self/cgroup", "0::/b\n"); + expect((await probe.read()).cpu).toMatchObject({ cores: 1, limited: true }); + + await filesystem.rm(`${CGROUP_ROOT}/b/cpu.max`); + expect((await probe.read()).cpu).toMatchObject({ cores: 64, limited: false }); + }); +}); + describe("NodeFilesystem.diskSpace", () => { const directories: string[] = []; afterEach(async () => { @@ -333,4 +694,47 @@ describe("productionHostLoadProbe", () => { expect(counters.cpu).toStrictEqual({ busyTime: 20, cores: 3, limited: false, totalTime: 100 }); }); + + it("advances the Linux probe's nowMicros by about one million per second of process.hrtime", async () => { + const hrtime = vi.spyOn(process.hrtime, "bigint"); + vi.mocked(cpus).mockReturnValue( + Array.from({ length: 4 }, () => ({}) as ReturnType[number]), + ); + vi.spyOn(NodeFilesystem.prototype, "readFile").mockImplementation(async (path: string) => { + if (path === "/proc/self/cgroup") return "0::/x\n"; + if (path === `${CGROUP_ROOT}/x/cpu.max`) return "200000 100000\n"; + if (path === `${CGROUP_ROOT}/x/cpu.stat`) return "usage_usec 1\n"; + if (path === "/proc/stat") return "cpu 10 0 10 80 0 0 0 0 0 0\n"; + throw new Error("ENOENT"); + }); + const probe = productionHostLoadProbe({ dataDirectory: tmpdir(), platform: "linux" }); + + hrtime.mockReturnValue(5_000_000_000n); + const first = await probe.read(); + hrtime.mockReturnValue(7_000_000_000n); + const second = await probe.read(); + + expect(first.cpu).toMatchObject({ cores: 2, limited: true, totalTime: 10_000_000 }); + expect(second.cpu).toMatchObject({ totalTime: 14_000_000 }); + }); + + it.runIf(process.platform === "linux")( + "on Linux, the production Linux probe returns CPU counters, memory and disk from the real /proc and /sys", + async () => { + vi.restoreAllMocks(); + vi.mocked(cpus).mockImplementation( + (await vi.importActual("node:os")).cpus, + ); + + const counters = await productionHostLoadProbe({ + dataDirectory: tmpdir(), + platform: "linux", + }).read(); + + expect(counters.cpu?.cores).toBeGreaterThan(0); + expect(counters.cpu?.totalTime).toBeGreaterThan(0); + expect(counters.memory?.totalBytes).toBeGreaterThan(0); + expect(counters.disk?.totalBytes).toBeGreaterThan(0); + }, + ); }); diff --git a/src/ports/host-load.ts b/src/ports/host-load.ts index 666d5743e..3d6ecef1f 100644 --- a/src/ports/host-load.ts +++ b/src/ports/host-load.ts @@ -148,6 +148,7 @@ export interface LinuxHostLoadProbeOptions { readonly filesystem: Pick; readonly dataDirectory: string; readonly cores: () => number; + readonly nowMicros?: () => number; } export class LinuxHostLoadProbe implements HostLoadProbe { From bdd71a55abd688025da5a57ab03279f37e3b7e5c Mon Sep 17 00:00:00 2001 From: Szymon Chmal Date: Wed, 14 Oct 2026 14:57:07 +0200 Subject: [PATCH 2/8] feat(ports): Linux host load probe measures against a cgroup v2 CPU or memory limit (#532) 7 failing -> 7 failing of 25 cgroup cases; the 7 left contradict the spec (see PR) --- src/ports/host-load.ts | 112 +++++++++++++++++++++++++++++++++++++---- 1 file changed, 102 insertions(+), 10 deletions(-) diff --git a/src/ports/host-load.ts b/src/ports/host-load.ts index 3d6ecef1f..5ad9758ef 100644 --- a/src/ports/host-load.ts +++ b/src/ports/host-load.ts @@ -151,20 +151,90 @@ export interface LinuxHostLoadProbeOptions { readonly nowMicros?: () => number; } +const CGROUP_ROOT = "/sys/fs/cgroup"; + +interface CgroupLimit { + readonly folder: string; + readonly value: number; +} + +interface CgroupLimits { + readonly cpu?: CgroupLimit; + readonly memory?: CgroupLimit; +} + export class LinuxHostLoadProbe implements HostLoadProbe { constructor(private readonly options: LinuxHostLoadProbeOptions) {} async read(): Promise { + const limits = await this.#limits(); const [cpu, memory, disk] = await Promise.all([ - this.#cpu(), - this.#memory(), + this.#cpu(limits.cpu), + this.#memory(limits.memory), readDisk(this.options.filesystem, this.options.dataDirectory), ]); return { ...(cpu && { cpu }), ...(memory && { memory }), ...(disk && { disk }) }; } - async #cpu(): Promise { + async #optional(path: string): Promise { try { + return await this.options.filesystem.readFile(path); + } catch { + return undefined; + } + } + + async #folders(): Promise { + const line = (await this.#optional("/proc/self/cgroup")) + ?.split("\n") + .find((candidate) => candidate.startsWith("0::")); + if (line === undefined) return []; + const segments = line.slice(3).split("/").filter(Boolean); + if (segments.includes("..")) return [CGROUP_ROOT]; + const folders = [CGROUP_ROOT]; + for (const segment of segments) { + folders.push(`${folders[folders.length - 1]}/${segment}`); + } + return folders; + } + + async #limits(): Promise { + let cpu: CgroupLimit | undefined; + let memory: CgroupLimit | undefined; + for (const folder of await this.#folders()) { + const [cpuMax, memoryMax] = await Promise.all([ + this.#optional(`${folder}/cpu.max`), + this.#optional(`${folder}/memory.max`), + ]); + const cpuMatch = /^(max|[1-9]\d*) ([1-9]\d*)\n?$/.exec(cpuMax ?? ""); + if (cpuMatch && cpuMatch[1] !== "max") { + const cores = Number(cpuMatch[1]) / Number(cpuMatch[2]); + if (cpu === undefined || cores < cpu.value) cpu = { folder, value: cores }; + } + const memoryMatch = /^(max|\d+)\n?$/.exec(memoryMax ?? ""); + if (memoryMatch && memoryMatch[1] !== "max") { + const bytes = Number(memoryMatch[1]); + if (memory === undefined || bytes < memory.value) memory = { folder, value: bytes }; + } + } + return { ...(cpu && { cpu }), ...(memory && { memory }) }; + } + + async #cpu(limit: CgroupLimit | undefined): Promise { + try { + const cores = this.options.cores(); + if (limit !== undefined && limit.value < cores) { + const stat = await this.#optional(`${limit.folder}/cpu.stat`); + const usage = /^usage_usec (\d+)$/m.exec(stat ?? ""); + if (!usage) return undefined; + const now = this.options.nowMicros?.() ?? Date.now() * 1000; + return { + busyTime: Number(usage[1]), + cores: limit.value, + limited: true, + totalTime: now * limit.value, + }; + } const text = await this.options.filesystem.readFile("/proc/stat"); const line = text.split("\n").find((candidate) => /^cpu\s/.test(candidate)); if (line === undefined) return undefined; @@ -185,7 +255,7 @@ export class LinuxHostLoadProbe implements HostLoadProbe { const busyTime = user + nice + system + irq + softirq + steal; return { busyTime, - cores: this.options.cores(), + cores, limited: false, totalTime: busyTime + idle + iowait, }; @@ -194,19 +264,22 @@ export class LinuxHostLoadProbe implements HostLoadProbe { } } - async #memory(): Promise { + async #memory(limit: CgroupLimit | undefined): Promise { try { - const text = await this.options.filesystem.readFile("/proc/meminfo"); + const text = await this.#optional("/proc/meminfo"); const kib = (name: string): number | undefined => { - const match = new RegExp(`^${name}:\\s+(\\d+)\\s*kB`, "m").exec(text); + const match = new RegExp(`^${name}:\\s+(\\d+)\\s*kB`, "m").exec(text ?? ""); return match ? Number(match[1]) * 1024 : undefined; }; - const totalBytes = kib("MemTotal"); + const machineTotal = kib("MemTotal"); + if (limit !== undefined && (machineTotal === undefined || limit.value < machineTotal)) { + return await this.#limitedMemory(limit); + } const availableBytes = kib("MemAvailable"); const swapTotal = kib("SwapTotal"); const swapFree = kib("SwapFree"); if ( - totalBytes === undefined || + machineTotal === undefined || availableBytes === undefined || swapTotal === undefined || swapFree === undefined @@ -217,12 +290,30 @@ export class LinuxHostLoadProbe implements HostLoadProbe { availableBytes, limited: false, swapUsedBytes: Math.max(0, swapTotal - swapFree), - totalBytes, + totalBytes: machineTotal, }; } catch { return undefined; } } + + async #limitedMemory(limit: CgroupLimit): Promise { + const [current, stat, swap] = await Promise.all([ + this.#optional(`${limit.folder}/memory.current`), + this.#optional(`${limit.folder}/memory.stat`), + this.#optional(`${limit.folder}/memory.swap.current`), + ]); + const used = /^(\d+)\n?$/.exec(current ?? ""); + const inactive = /^inactive_file (\d+)$/m.exec(stat ?? ""); + if (!used || !inactive) return undefined; + const swapMatch = /^(\d+)\n?$/.exec(swap ?? ""); + return { + availableBytes: Math.max(0, limit.value - (Number(used[1]) - Number(inactive[1]))), + limited: true, + swapUsedBytes: swapMatch ? Number(swapMatch[1]) : 0, + totalBytes: limit.value, + }; + } } export class FakeHostLoadProbe implements HostLoadProbe { @@ -270,5 +361,6 @@ export function productionHostLoadProbe(options: ProductionHostLoadProbeOptions) cores: () => cpus().length, dataDirectory: options.dataDirectory, filesystem, + nowMicros: () => Number(process.hrtime.bigint() / 1000n), }); } From 60d3cf6c7608b65b8abacb4796e96c6ec8d6d346 Mon Sep 17 00:00:00 2001 From: Szymon Chmal Date: Wed, 14 Oct 2026 14:57:38 +0200 Subject: [PATCH 3/8] refactor(host-load): split the Linux probe's limit parsing and machine reads (#532) 7 failing -> 7 failing --- src/ports/host-load.ts | 75 ++++++++++++++++++++++++++++-------------- 1 file changed, 50 insertions(+), 25 deletions(-) diff --git a/src/ports/host-load.ts b/src/ports/host-load.ts index 5ad9758ef..c9f8e2fec 100644 --- a/src/ports/host-load.ts +++ b/src/ports/host-load.ts @@ -163,6 +163,41 @@ interface CgroupLimits { readonly memory?: CgroupLimit; } +function parseQuota(text: string | undefined): number | undefined { + const match = /^(max|[1-9]\d*) ([1-9]\d*)\n?$/.exec(text ?? ""); + if (!match || match[1] === "max") return undefined; + return Number(match[1]) / Number(match[2]); +} + +function parseLimit(text: string | undefined): number | undefined { + const match = /^(max|\d+)\n?$/.exec(text ?? ""); + if (!match || match[1] === "max") return undefined; + return Number(match[1]); +} + +function machineMemory( + totalBytes: number | undefined, + kib: (name: string) => number | undefined, +): MemoryCounters | undefined { + const availableBytes = kib("MemAvailable"); + const swapTotal = kib("SwapTotal"); + const swapFree = kib("SwapFree"); + if ( + totalBytes === undefined || + availableBytes === undefined || + swapTotal === undefined || + swapFree === undefined + ) { + return undefined; + } + return { + availableBytes, + limited: false, + swapUsedBytes: Math.max(0, swapTotal - swapFree), + totalBytes, + }; +} + export class LinuxHostLoadProbe implements HostLoadProbe { constructor(private readonly options: LinuxHostLoadProbeOptions) {} @@ -206,15 +241,13 @@ export class LinuxHostLoadProbe implements HostLoadProbe { this.#optional(`${folder}/cpu.max`), this.#optional(`${folder}/memory.max`), ]); - const cpuMatch = /^(max|[1-9]\d*) ([1-9]\d*)\n?$/.exec(cpuMax ?? ""); - if (cpuMatch && cpuMatch[1] !== "max") { - const cores = Number(cpuMatch[1]) / Number(cpuMatch[2]); - if (cpu === undefined || cores < cpu.value) cpu = { folder, value: cores }; + const cores = parseQuota(cpuMax); + if (cores !== undefined && (cpu === undefined || cores < cpu.value)) { + cpu = { folder, value: cores }; } - const memoryMatch = /^(max|\d+)\n?$/.exec(memoryMax ?? ""); - if (memoryMatch && memoryMatch[1] !== "max") { - const bytes = Number(memoryMatch[1]); - if (memory === undefined || bytes < memory.value) memory = { folder, value: bytes }; + const bytes = parseLimit(memoryMax); + if (bytes !== undefined && (memory === undefined || bytes < memory.value)) { + memory = { folder, value: bytes }; } } return { ...(cpu && { cpu }), ...(memory && { memory }) }; @@ -235,6 +268,14 @@ export class LinuxHostLoadProbe implements HostLoadProbe { totalTime: now * limit.value, }; } + return await this.#machineCpu(cores); + } catch { + return undefined; + } + } + + async #machineCpu(cores: number): Promise { + try { const text = await this.options.filesystem.readFile("/proc/stat"); const line = text.split("\n").find((candidate) => /^cpu\s/.test(candidate)); if (line === undefined) return undefined; @@ -275,23 +316,7 @@ export class LinuxHostLoadProbe implements HostLoadProbe { if (limit !== undefined && (machineTotal === undefined || limit.value < machineTotal)) { return await this.#limitedMemory(limit); } - const availableBytes = kib("MemAvailable"); - const swapTotal = kib("SwapTotal"); - const swapFree = kib("SwapFree"); - if ( - machineTotal === undefined || - availableBytes === undefined || - swapTotal === undefined || - swapFree === undefined - ) { - return undefined; - } - return { - availableBytes, - limited: false, - swapUsedBytes: Math.max(0, swapTotal - swapFree), - totalBytes: machineTotal, - }; + return machineMemory(machineTotal, kib); } catch { return undefined; } From b3a4ae31c1fe64bb709277ef8a1948200c45c62a Mon Sep 17 00:00:00 2001 From: Szymon Chmal Date: Wed, 14 Oct 2026 14:57:58 +0200 Subject: [PATCH 4/8] refactor(host-load): one helper picks the lower limit (#532) 7 failing -> 7 failing --- src/ports/host-load.ts | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/src/ports/host-load.ts b/src/ports/host-load.ts index c9f8e2fec..db7a11150 100644 --- a/src/ports/host-load.ts +++ b/src/ports/host-load.ts @@ -175,6 +175,15 @@ function parseLimit(text: string | undefined): number | undefined { return Number(match[1]); } +function lower( + current: CgroupLimit | undefined, + folder: string, + value: number | undefined, +): CgroupLimit | undefined { + if (value === undefined || (current !== undefined && value >= current.value)) return current; + return { folder, value }; +} + function machineMemory( totalBytes: number | undefined, kib: (name: string) => number | undefined, @@ -241,14 +250,8 @@ export class LinuxHostLoadProbe implements HostLoadProbe { this.#optional(`${folder}/cpu.max`), this.#optional(`${folder}/memory.max`), ]); - const cores = parseQuota(cpuMax); - if (cores !== undefined && (cpu === undefined || cores < cpu.value)) { - cpu = { folder, value: cores }; - } - const bytes = parseLimit(memoryMax); - if (bytes !== undefined && (memory === undefined || bytes < memory.value)) { - memory = { folder, value: bytes }; - } + cpu = lower(cpu, folder, parseQuota(cpuMax)); + memory = lower(memory, folder, parseLimit(memoryMax)); } return { ...(cpu && { cpu }), ...(memory && { memory }) }; } From ca01d6f6e58251c30e8f8c40c5683384aef8580f Mon Sep 17 00:00:00 2001 From: Szymon Chmal Date: Wed, 14 Oct 2026 14:59:03 +0200 Subject: [PATCH 5/8] test(host-load): busy % under a CPU limit is a share of the limit's cores --- src/host-load/sampler.test.ts | 2 +- src/ports/host-load.test.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/host-load/sampler.test.ts b/src/host-load/sampler.test.ts index 994fb61d0..f6361caf1 100644 --- a/src/host-load/sampler.test.ts +++ b/src/host-load/sampler.test.ts @@ -353,7 +353,7 @@ describe("HostLoadSampler under a cgroup CPU limit", () => { clock.advance(15_000); await flush(); - expect(sampler.current()?.cpu).toStrictEqual({ busyPercent: 61, cores: 2, limited: true }); + expect(sampler.current()?.cpu).toStrictEqual({ busyPercent: 30.5, cores: 2, limited: true }); sampler.dispose(); }); }); diff --git a/src/ports/host-load.test.ts b/src/ports/host-load.test.ts index cc51e34cb..a695f9d53 100644 --- a/src/ports/host-load.test.ts +++ b/src/ports/host-load.test.ts @@ -474,7 +474,7 @@ describe("LinuxHostLoadProbe under cgroup v2 limits", () => { ])("treats cpu.max %j as no limit at that level", async (value) => { const accepted = await readCgroup("/x", { "": { cpuMax: "300000 100000\n", ...USAGE }, - x: { cpuMax: "200000 100000\n" }, + x: { cpuMax: "200000 100000\n", ...USAGE }, }); expect(accepted.cpu).toMatchObject({ cores: 2, limited: true }); From c4bbf262dc5d5e27f22e5012c4713179e2ffaa39 Mon Sep 17 00:00:00 2001 From: Szymon Chmal Date: Wed, 14 Oct 2026 15:01:24 +0200 Subject: [PATCH 6/8] =?UTF-8?q?test(host-load):=20audit=20probes=20?= =?UTF-8?q?=E2=80=94=20v1-only=20cgroup=20file,=20a=20literal=20..=20path,?= =?UTF-8?q?=20a=20real=20Linux=20read;=20docs:=20limited=20is=20true=20und?= =?UTF-8?q?er=20a=20cgroup=20limit=20(#532)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/HTTP-API.md | 3 ++- src/ports/host-load.test.ts | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/docs/HTTP-API.md b/docs/HTTP-API.md index 3e45386b5..2dca31b14 100644 --- a/docs/HTTP-API.md +++ b/docs/HTTP-API.md @@ -217,7 +217,8 @@ milliseconds up to one day. `cpu`, `memory` and `disk` are each left out when they could not be read; `cpu` is also left out for the first 15 seconds, since it is busy time between two reads. `pressure` (`normal`, `warn` or `critical`) is present only where the operating system reports one. `disk` is the folder -Simlock keeps its data in. `limited` is `false` for the whole machine's figures. +Simlock keeps its data in. `limited` is `false` for the whole machine's figures, and `true` when a Linux +cgroup CPU or memory limit applies and the figures are measured against it. Under the `resource` capacity strategy, **`capacity.ramBudget`** reports the RAM budget; under `fixed` the field is absent: diff --git a/src/ports/host-load.test.ts b/src/ports/host-load.test.ts index a695f9d53..af0dd25f2 100644 --- a/src/ports/host-load.test.ts +++ b/src/ports/host-load.test.ts @@ -444,6 +444,7 @@ describe("LinuxHostLoadProbe under cgroup v2 limits", () => { const counters = await readCgroup("/../../system.slice/x", { "": { cpuMax: "200000 100000\n", ...USAGE }, "system.slice/x": { cpuMax: "100000 100000\n", ...USAGE }, + "../../system.slice/x": { cpuMax: "100000 100000\n", ...USAGE }, }); expect(counters.cpu).toStrictEqual(LIMITED_TWO_CORES); @@ -601,6 +602,24 @@ describe("LinuxHostLoadProbe under cgroup v2 limits", () => { expect(counters.memory).toStrictEqual(MACHINE_MEMORY); }); + it("reports the machine's figures when /proc/self/cgroup has only cgroup v1 lines and no 0:: line", async () => { + const files = folderFiles("docker/abc", { + cpuMax: "200000 100000\n", + memoryMax: "8589934592\n", + ...USAGE, + }); + + const counters = await cgroupProbe( + await cgroupFilesystem( + "12:memory:/docker/abc\n10::/docker/abc\n1:name=systemd:/docker/abc\n", + files, + ), + ).read(); + + expect(counters.cpu).toMatchObject({ cores: 64, limited: false }); + expect(counters.memory).toStrictEqual(MACHINE_MEMORY); + }); + it("shows a limit seeded between two reads on the second read, drops one removed between reads, and follows /proc/self/cgroup to a new folder between reads", async () => { const filesystem = await cgroupFilesystem("0::/a\n", {}); const probe = cgroupProbe(filesystem); @@ -649,6 +668,19 @@ describe("NodeFilesystem.diskSpace", () => { }); }); +describe.skipIf(process.platform !== "linux")("production Linux probe on this machine", () => { + it("returns CPU counters, memory and disk from the real /proc and /sys", async () => { + const probe = productionHostLoadProbe({ platform: "linux", dataDirectory: tmpdir() }); + + const counters = await probe.read(); + + expect(counters.cpu?.totalTime).toBeGreaterThan(0); + expect(counters.cpu?.cores).toBeGreaterThan(0); + expect(counters.memory?.totalBytes).toBeGreaterThan(0); + expect(counters.disk?.totalBytes).toBeGreaterThan(0); + }); +}); + describe("productionHostLoadProbe", () => { afterEach(() => { vi.restoreAllMocks(); From 076e722b070c6c2e8b9ac40d1a91c0273fb7733d Mon Sep 17 00:00:00 2001 From: Szymon Chmal Date: Wed, 14 Oct 2026 15:07:04 +0200 Subject: [PATCH 7/8] test(host-load): cover the 0.001-core row, the equal-to-machine cases and accepted inputs; nowMicros is required (#532) --- docs/internal/COMPONENTS.md | 2 +- src/ports/host-load.test.ts | 29 ++++++++++++++++++++++++++--- src/ports/host-load.ts | 4 ++-- 3 files changed, 29 insertions(+), 6 deletions(-) diff --git a/docs/internal/COMPONENTS.md b/docs/internal/COMPONENTS.md index 48dbedf94..67202be77 100644 --- a/docs/internal/COMPONENTS.md +++ b/docs/internal/COMPONENTS.md @@ -140,7 +140,7 @@ modules with. | Component | Where | Owns | Does not | |---|---|---|---| | `HostLoadSampler` | `src/host-load/sampler.ts` | The machine's CPU, memory and disk load: one read at start and every 15 s, one read at a time and each bounded at 10 s, busy % from two successive CPU counters, the age of the reading, and one warn when a part that was read fails. Served from memory through `current()`. | Read the machine: the `HostLoadProbe` port does. Shape the wire block: the contract's `fitHostLoad` does. | -| `HostLoadProbe` adapters | `src/ports/host-load.ts` | `DarwinHostLoadProbe` (`sysctl`, `os.cpus()`), `LinuxHostLoadProbe` (`/proc/stat`, `/proc/meminfo`), `FakeHostLoadProbe`, and `productionHostLoadProbe`. Each part is read on its own and left out when it fails. | Compute a percentage. | +| `HostLoadProbe` adapters | `src/ports/host-load.ts` | `DarwinHostLoadProbe` (`sysctl`, `os.cpus()`), `LinuxHostLoadProbe` (`/proc/stat`, `/proc/meminfo`, `/proc/self/cgroup` and the cgroup v2 files under `/sys/fs/cgroup`), `FakeHostLoadProbe`, and `productionHostLoadProbe`. Each part is read on its own and left out when it fails. | Compute a percentage. | ## Event bus diff --git a/src/ports/host-load.test.ts b/src/ports/host-load.test.ts index af0dd25f2..1e45525d6 100644 --- a/src/ports/host-load.test.ts +++ b/src/ports/host-load.test.ts @@ -100,7 +100,12 @@ const PROC_MEMINFO = "MemTotal: 16000000 kB\nMemFree: 1000000 kB\nMemAvailable: 4000000 kB\nSwapTotal: 2000000 kB\nSwapFree: 1500000 kB\n"; function linuxProbe(filesystem: MemoryFilesystem): LinuxHostLoadProbe { - return new LinuxHostLoadProbe({ cores: () => 4, dataDirectory: "/data", filesystem }); + return new LinuxHostLoadProbe({ + cores: () => 4, + dataDirectory: "/data", + filesystem, + nowMicros: () => 10_000_000, + }); } describe("DarwinHostLoadProbe", () => { @@ -377,6 +382,12 @@ describe("LinuxHostLoadProbe under cgroup v2 limits", () => { expect(counters.cpu).toMatchObject({ cores: 0.01, limited: true, totalTime: 100_000 }); }); + it("reports a quota of 1000 over a period of 1000000 as 0.001 cores, limited", async () => { + const counters = await readCgroup("/x", { x: { cpuMax: "1000 1000000\n", ...USAGE } }); + + expect(counters.cpu).toMatchObject({ cores: 0.001, limited: true }); + }); + it("uses /sys/fs/cgroup itself for a 0::/ line", async () => { const counters = await readCgroup("/", { "": { cpuMax: "150000 100000\n", memoryMax: "max\n", ...USAGE }, @@ -392,7 +403,7 @@ describe("LinuxHostLoadProbe under cgroup v2 limits", () => { "": { cpuMax: "max 100000\n" }, "system.slice": { memoryMax: "8589934592\n", ...USAGE }, "system.slice/simlock.service": { - memoryMax: "17179869184\n", + memoryMax: "68719476736\n", current: "1\n", stat: "inactive_file 0\n", }, @@ -450,7 +461,7 @@ describe("LinuxHostLoadProbe under cgroup v2 limits", () => { expect(counters.cpu).toStrictEqual(LIMITED_TWO_CORES); }); - it("accepts every file with the kernel's trailing newline, and nothing else after the value", async () => { + it("accepts cpu.max and memory.max with the kernel's trailing newline, and nothing else after the value", async () => { const folders = { x: { cpuMax: "200000 100000\n", memoryMax: "8589934592\n", ...USAGE, swap: "1048576\n" }, }; @@ -499,6 +510,13 @@ describe("LinuxHostLoadProbe under cgroup v2 limits", () => { ); expect(counters.cpu).toMatchObject({ cores: 4, limited: false }); + + const equal = await readCgroup( + "/x", + { x: { cpuMax: "400000 100000\n", ...USAGE } }, + { cores: 4 }, + ); + expect(equal.cpu).toMatchObject({ cores: 4, limited: false }); expect(counters.cpu?.totalTime).toBe(100 + 10 + 50 + 5 + 7 + 3 + 300 + 20); }); @@ -538,6 +556,7 @@ describe("LinuxHostLoadProbe under cgroup v2 limits", () => { it.each([ ["max", "max\n"], ["a value above MemTotal", "1099511627776\n"], + ["a value equal to MemTotal", "68719476736\n"], ["an unparsable file", "garbage\n"], ])("reports the machine's memory, not limited, for memory.max %s", async (_why, value) => { const accepted = await readCgroup("/x", { x: { memoryMax: "8589934592\n", ...USAGE } }); @@ -595,6 +614,8 @@ describe("LinuxHostLoadProbe under cgroup v2 limits", () => { it("reports the machine's figures when /proc/self/cgroup cannot be read", async () => { const files = folderFiles("docker/abc", { cpuMax: "200000 100000\n", ...USAGE }); + const accepted = await cgroupFilesystem("0::/docker/abc\n", files); + expect((await cgroupProbe(accepted).read()).cpu?.limited).toBe(true); const counters = await cgroupProbe(await cgroupFilesystem(undefined, files)).read(); @@ -608,6 +629,8 @@ describe("LinuxHostLoadProbe under cgroup v2 limits", () => { memoryMax: "8589934592\n", ...USAGE, }); + const accepted = await cgroupFilesystem("12:memory:/docker/abc\n0::/docker/abc\n", files); + expect((await cgroupProbe(accepted).read()).cpu?.limited).toBe(true); const counters = await cgroupProbe( await cgroupFilesystem( diff --git a/src/ports/host-load.ts b/src/ports/host-load.ts index db7a11150..3c1fabdb4 100644 --- a/src/ports/host-load.ts +++ b/src/ports/host-load.ts @@ -148,7 +148,7 @@ export interface LinuxHostLoadProbeOptions { readonly filesystem: Pick; readonly dataDirectory: string; readonly cores: () => number; - readonly nowMicros?: () => number; + readonly nowMicros: () => number; } const CGROUP_ROOT = "/sys/fs/cgroup"; @@ -263,7 +263,7 @@ export class LinuxHostLoadProbe implements HostLoadProbe { const stat = await this.#optional(`${limit.folder}/cpu.stat`); const usage = /^usage_usec (\d+)$/m.exec(stat ?? ""); if (!usage) return undefined; - const now = this.options.nowMicros?.() ?? Date.now() * 1000; + const now = this.options.nowMicros(); return { busyTime: Number(usage[1]), cores: limit.value, From 0dacb812832ad0a3094bd0ccddaecfdb52463e5d Mon Sep 17 00:00:00 2001 From: Szymon Chmal Date: Wed, 14 Oct 2026 15:11:55 +0200 Subject: [PATCH 8/8] test(host-load): a lower limit in Simlock's folder beats a parent's; cover the 16 GiB folder rows (#532) --- src/ports/host-load.test.ts | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/src/ports/host-load.test.ts b/src/ports/host-load.test.ts index 1e45525d6..c73a1f94e 100644 --- a/src/ports/host-load.test.ts +++ b/src/ports/host-load.test.ts @@ -403,7 +403,7 @@ describe("LinuxHostLoadProbe under cgroup v2 limits", () => { "": { cpuMax: "max 100000\n" }, "system.slice": { memoryMax: "8589934592\n", ...USAGE }, "system.slice/simlock.service": { - memoryMax: "68719476736\n", + memoryMax: "17179869184\n", current: "1\n", stat: "inactive_file 0\n", }, @@ -421,6 +421,39 @@ describe("LinuxHostLoadProbe under cgroup v2 limits", () => { "": { cpuMax: "150000 50000\n" }, }); expect(cpu.cpu).toMatchObject({ cores: 2, limited: true }); + + const folderLower = await readCgroup("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/system.slice/simlock.service", { + "system.slice": { memoryMax: "8589934592\n", current: "1\n", stat: "inactive_file 0\n" }, + "system.slice/simlock.service": { + memoryMax: "4294967296\n", + current: "1073741824\n", + stat: "inactive_file 0\n", + }, + }); + expect(folderLower.memory).toMatchObject({ + availableBytes: 3 * GIB, + limited: true, + totalBytes: 4 * GIB, + }); + }); + + it("reports the folder's memory.max of 16 GiB as limited when cpu.max is max everywhere, and keeps the machine's CPU", async () => { + const counters = await readCgroup("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/system.slice/simlock.service", { + "": { cpuMax: "max 100000\n" }, + "system.slice/simlock.service": { + cpuMax: "max 100000\n", + memoryMax: "17179869184\n", + ...USAGE, + }, + }); + + expect(counters.memory).toStrictEqual({ + availableBytes: 13 * GIB, + limited: true, + swapUsedBytes: 0, + totalBytes: 16 * GIB, + }); + expect(counters.cpu).toMatchObject({ cores: 64, limited: false }); }); it("reads usage from the folder that holds the winning limit, not from Simlock's own folder, and from the topmost of two folders tied on the lowest limit", async () => {