Skip to content
Merged
3 changes: 2 additions & 1 deletion docs/HTTP-API.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion docs/internal/COMPONENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
41 changes: 41 additions & 0 deletions src/host-load/sampler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import {
FakeHostLoadProbe,
type HostLoadCounters,
JsonLinesLogger,
LinuxHostLoadProbe,
MemoryFilesystem,
MemoryLogSink,
} from "../ports/index.js";
import { HostLoadSampler } from "./index.js";
Expand Down Expand Up @@ -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<void> => {
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: 30.5, cores: 2, limited: true });
sampler.dispose();
});
});
Loading
Loading