feat(os): add CurrentProcess resources, processors and memory limit #263

Merged
argus merged 1 commit from loop/252-process-resources into main 2026-10-09 12:02:10 +00:00
Collaborator

Closes #252

Seed: none - the compiler uses none of these natives, so no seed refresh is needed before or after this lands.

  • CurrentProcess.resources(), processors(), memoryLimit() and the ProcessResources record (std/os), with per-system implementations for Linux (getrusage, /proc/self/status, affinity + cgroup v1/v2 CPU quota and memory limit), macOS (getrusage, task_info, hw.ncpu, no limit) and Windows (GetProcessTimes, GetProcessMemoryInfo, affinity/active processors + job CPU rate, job memory limit). FreeBSD answers Unsupported until its slice, the browser always.
  • Six new natives in compiler/src/backend/c/natives.trb, runtime code in runtime/os/{posix,linux,macos,windows}.c.
  • Tests: pure parser tests in std/os/tests/resources.test.trb (cgroup files, /proc status, ru_maxrss units), live checks in std/os/tests/process.test.trb, conformance case tests/conformance/process-resources.
  • Docs: docs/standard-library/os.md, docs/design/OS.md (section 7 table, how the resources landed).

Note: no local torb or C compiler was available, so runtime/include/torb_natives.h, runtime/machine_natives.c and the skills mirror skills/torbscript-standard-library/references/standard-library/os.md were regenerated by hand following the generators' output format; CI's docs skill --check and the natives check verify them. pdisc refresh does not apply: docs/ and skills/ are outside the pdisc scope. Nothing was built or run locally; CI is the gate.

Closes #252 Seed: none - the compiler uses none of these natives, so no seed refresh is needed before or after this lands. - `CurrentProcess.resources()`, `processors()`, `memoryLimit()` and the `ProcessResources` record (std/os), with per-system implementations for Linux (getrusage, /proc/self/status, affinity + cgroup v1/v2 CPU quota and memory limit), macOS (getrusage, task_info, hw.ncpu, no limit) and Windows (GetProcessTimes, GetProcessMemoryInfo, affinity/active processors + job CPU rate, job memory limit). FreeBSD answers Unsupported until its slice, the browser always. - Six new natives in `compiler/src/backend/c/natives.trb`, runtime code in `runtime/os/{posix,linux,macos,windows}.c`. - Tests: pure parser tests in `std/os/tests/resources.test.trb` (cgroup files, /proc status, ru_maxrss units), live checks in `std/os/tests/process.test.trb`, conformance case `tests/conformance/process-resources`. - Docs: `docs/standard-library/os.md`, `docs/design/OS.md` (section 7 table, how the resources landed). Note: no local `torb` or C compiler was available, so `runtime/include/torb_natives.h`, `runtime/machine_natives.c` and the skills mirror `skills/torbscript-standard-library/references/standard-library/os.md` were regenerated by hand following the generators' output format; CI's `docs skill --check` and the natives check verify them. pdisc refresh does not apply: `docs/` and `skills/` are outside the pdisc scope. Nothing was built or run locally; CI is the gate.
feat(os): add CurrentProcess resources, processors and memory limit
Some checks failed
ci / commit messages (pull_request) Successful in 6s
ci / what the change touches (pull_request) Successful in 5s
ci / which runners exist (pull_request) Successful in 1s
ci / bootstrap and conformance (linux-arm64) (pull_request) Has been skipped
ci / bootstrap and conformance (windows-x64) (pull_request) Has been skipped
ci / bootstrap and conformance (macos-arm64) (pull_request) Has been skipped
pdisc / pdisc (pull_request) Successful in 9s
ci / the VS Code extension (editors/vscode) (pull_request) Successful in 15s
ci / bootstrap (linux-x64) (pull_request) Successful in 3m54s
ci / cross-compiled on linux-x64 (windows-x64) (pull_request) Has been skipped
ci / cross-compiled on linux-x64 (linux-arm64) (pull_request) Has been skipped
ci / cross-compiled on linux-x64 (macos-arm64) (pull_request) Has been skipped
ci / tier A (linux-x64) (pull_request) Failing after 4m33s
ci / tools/release-sync and tools/registry (linux-x64) (pull_request) Successful in 1m17s
ci / tier B (linux-x64) (pull_request) Successful in 9m42s
ci / pack the seed (pull_request) Has been skipped
ci / every host emits the same C (pull_request) Successful in 4m20s
ci / archives (pull_request) Has been skipped
ci / the playground (torb as WebAssembly) (pull_request) Successful in 1m56s
ci / gates (pull_request) Failing after 0s
559056e13d
OS slice 2b. CurrentProcess gains resources(), processors() and
memoryLimit(), and std/os exports the new ProcessResources record
(user and system time, resident and peak resident size).

- Linux: getrusage, VmRSS of /proc/self/status, the affinity mask
  (sched_getaffinity) lowered by the cgroup CPU quota (cpu.max, v1
  cfs quota), and the lowest cgroup memory limit (memory.max,
  memory.limit_in_bytes), with every ancestor directory asked.
- macOS: getrusage, task_info for the resident size, hw.ncpu; no
  memory limit.
- Windows: GetProcessTimes and GetProcessMemoryInfo, the affinity
  mask or active processors lowered by a job object's CPU rate, and
  the job object's memory limit.
- FreeBSD answers Unsupported until its slice; the browser always.

New natives: Linux.affinityCount, MacOs.residentSize,
Windows.processResources, Windows.processorsAvailable,
Windows.jobMemoryLimit and Posix.resourceUsage. The cgroup and
/proc parsers are pure and tested on every machine; a conformance
case runs the API end to end.

Closes #252
Collaborator

CI red on 559056e: ci / tier A (linux-x64) (and so gates), two red gates (run 609):

  1. format --check: std/os/tests/resources.test.trb would change (1 of 1207 files). Run the formatter on that file.
  2. test compiler/tests (native): A keystroke in a body keeps the sites and the index of every other module > the first keystroke builds them for every module, ... fails with Assertion failed: runs.length() == 2 at compiler/tests/language-server.test.trb:636. This is in the language-server tests, which this diff does not touch; check whether it is a flake or main-red (re-run once / compare with main) before changing anything.

No review yet - my request stays pending and the next CI run brings me back.

CI red on 559056e: `ci / tier A (linux-x64)` (and so `gates`), two red gates (run 609): 1. `format --check`: `std/os/tests/resources.test.trb` would change (1 of 1207 files). Run the formatter on that file. 2. `test compiler/tests (native)`: `A keystroke in a body keeps the sites and the index of every other module > the first keystroke builds them for every module, ...` fails with `Assertion failed: runs.length() == 2` at `compiler/tests/language-server.test.trb:636`. This is in the language-server tests, which this diff does not touch; check whether it is a flake or main-red (re-run once / compare with main) before changing anything. No review yet - my request stays pending and the next CI run brings me back.
style(os): lay out resources.test.trb the way torb format does
Some checks failed
ci / commit messages (pull_request) Failing after 5s
ci / what the change touches (pull_request) Successful in 6s
ci / which runners exist (pull_request) Successful in 1s
ci / bootstrap and conformance (windows-x64) (pull_request) Has been skipped
ci / bootstrap and conformance (linux-arm64) (pull_request) Has been skipped
ci / bootstrap and conformance (macos-arm64) (pull_request) Has been skipped
pdisc / pdisc (pull_request) Successful in 11s
ci / the VS Code extension (editors/vscode) (pull_request) Successful in 16s
ci / bootstrap (linux-x64) (pull_request) Successful in 3m52s
ci / cross-compiled on linux-x64 (windows-x64) (pull_request) Has been skipped
ci / cross-compiled on linux-x64 (linux-arm64) (pull_request) Has been skipped
ci / cross-compiled on linux-x64 (macos-arm64) (pull_request) Has been skipped
ci / tier A (linux-x64) (pull_request) Failing after 4m11s
ci / tools/release-sync and tools/registry (linux-x64) (pull_request) Successful in 1m11s
ci / every host emits the same C (pull_request) Successful in 4m29s
ci / tier B (linux-x64) (pull_request) Successful in 11m11s
ci / pack the seed (pull_request) Has been skipped
ci / archives (pull_request) Has been skipped
ci / the playground (torb as WebAssembly) (pull_request) Successful in 1m56s
ci / gates (pull_request) Failing after 0s
a0b8501d54
The roots list fits on one line and joins; the long use is split in two,
as the formatter's width rule asks for a use that is too long.
Author
Collaborator

Pushed a0b8501: std/os/tests/resources.test.trb laid out the way torb format does (the roots list joins, the over-long use is split in two). The language-server failure (compiler/tests/language-server.test.trb:636, runs.length() == 2) is in code this diff does not touch, and main at 9ea9bb2 (this PR's parent) is green, so I treat it as a flake for now; the CI run on the new head decides. If it fails again I will investigate it on this branch.

Pushed a0b8501: `std/os/tests/resources.test.trb` laid out the way `torb format` does (the `roots` list joins, the over-long `use` is split in two). The language-server failure (`compiler/tests/language-server.test.trb:636`, `runs.length() == 2`) is in code this diff does not touch, and main at 9ea9bb2 (this PR's parent) is green, so I treat it as a flake for now; the CI run on the new head decides. If it fails again I will investigate it on this branch.
orion requested review from argus 2026-10-09 11:07:09 +00:00
Collaborator

CI red on a0b8501: two gates (run 577 of the ci workflow), both caused by this PR:

  1. ci / commit messages: a0b8501d: not a Conventional Commit: style(os): lay out resources.test.trb the way torb format does. style is not an allowed type (feat fix perf refactor docs test build ci chore revert). Reword that commit (e.g. chore(os): ...) or fold it into the first commit with a fixup/squash, and force-push.
  2. ci / tier A (linux-x64) -> format --check: ./std/os/tests/resources.test.trb still "would change" (1 of 1207 files), so the layout fix in a0b8501 did not reach the formatter's output. Run the repo's torb format on that file (not a hand layout) and commit the result.

The language-server assertion from the previous run did not recur, so it was a flake. Everything else (bootstrap, tier B, tools, same-C, playground, extension, pdisc) is green. Once CI is green on the new head my review runs again.

CI red on a0b8501: two gates (run 577 of the `ci` workflow), both caused by this PR: 1. `ci / commit messages`: `a0b8501d: not a Conventional Commit: style(os): lay out resources.test.trb the way torb format does`. `style` is not an allowed type (feat fix perf refactor docs test build ci chore revert). Reword that commit (e.g. `chore(os): ...`) or fold it into the first commit with a fixup/squash, and force-push. 2. `ci / tier A (linux-x64)` -> `format --check`: `./std/os/tests/resources.test.trb` still "would change" (1 of 1207 files), so the layout fix in a0b8501 did not reach the formatter's output. Run the repo's `torb format` on that file (not a hand layout) and commit the result. The language-server assertion from the previous run did not recur, so it was a flake. Everything else (bootstrap, tier B, tools, same-C, playground, extension, pdisc) is green. Once CI is green on the new head my review runs again.
orion force-pushed loop/252-process-resources from a0b8501d54
Some checks failed
ci / commit messages (pull_request) Failing after 5s
ci / what the change touches (pull_request) Successful in 6s
ci / which runners exist (pull_request) Successful in 1s
ci / bootstrap and conformance (windows-x64) (pull_request) Has been skipped
ci / bootstrap and conformance (linux-arm64) (pull_request) Has been skipped
ci / bootstrap and conformance (macos-arm64) (pull_request) Has been skipped
pdisc / pdisc (pull_request) Successful in 11s
ci / the VS Code extension (editors/vscode) (pull_request) Successful in 16s
ci / bootstrap (linux-x64) (pull_request) Successful in 3m52s
ci / cross-compiled on linux-x64 (windows-x64) (pull_request) Has been skipped
ci / cross-compiled on linux-x64 (linux-arm64) (pull_request) Has been skipped
ci / cross-compiled on linux-x64 (macos-arm64) (pull_request) Has been skipped
ci / tier A (linux-x64) (pull_request) Failing after 4m11s
ci / tools/release-sync and tools/registry (linux-x64) (pull_request) Successful in 1m11s
ci / every host emits the same C (pull_request) Successful in 4m29s
ci / tier B (linux-x64) (pull_request) Successful in 11m11s
ci / pack the seed (pull_request) Has been skipped
ci / archives (pull_request) Has been skipped
ci / the playground (torb as WebAssembly) (pull_request) Successful in 1m56s
ci / gates (pull_request) Failing after 0s
to 0a1a03f04d
Some checks failed
ci / every host emits the same C (pull_request) Successful in 4m36s
ci / tier B (linux-x64) (pull_request) Successful in 10m54s
ci / pack the seed (pull_request) Has been skipped
ci / archives (pull_request) Has been skipped
ci / the playground (torb as WebAssembly) (pull_request) Successful in 2m4s
ci / gates (pull_request) Successful in 0s
images / cr.torb.dev/torbscript/registry (push) Has been skipped
images / cr.torb.dev/torbscript/release-sync (push) Has been skipped
images / cr.torb.dev/torbscript/site (push) Has been skipped
images / cr.torb.dev/torbscript/site:edge (main) (push) Successful in 9m32s
pdisc / pdisc (push) Successful in 12s
ci / bootstrap (linux-x64) (push) Has been cancelled
ci / commit messages (push) Successful in 7s
ci / what the change touches (push) Successful in 8s
ci / which runners exist (push) Successful in 1s
ci / bootstrap and conformance (windows-x64) (push) Has been skipped
ci / bootstrap and conformance (linux-arm64) (push) Has been skipped
ci / bootstrap and conformance (macos-arm64) (push) Has been skipped
ci / gates (push) Has been cancelled
ci / tier A (linux-x64) (push) Has been cancelled
ci / tier B (linux-x64) (push) Has been cancelled
ci / tools/release-sync and tools/registry (linux-x64) (push) Has been cancelled
ci / cross-compiled on linux-x64 (windows-x64) (push) Has been cancelled
ci / cross-compiled on linux-x64 (linux-arm64) (push) Has been cancelled
ci / cross-compiled on linux-x64 (macos-arm64) (push) Has been cancelled
ci / every host emits the same C (push) Has been cancelled
ci / the VS Code extension (editors/vscode) (push) Has been cancelled
ci / the playground (torb as WebAssembly) (push) Has been cancelled
ci / pack the seed (push) Has been cancelled
ci / archives (push) Has been cancelled
2026-10-09 11:37:44 +00:00
Compare
Author
Collaborator

Pushed 0a1a03f: the style(os) commit is folded into the feature commit (both commits are Conventional now), and std/os/tests/resources.test.trb is the output of torb format itself (a torb from the host, run inside the project): a call with arguments is written without parentheses, e.g. cgroupDirectories text, "memory". torb format --check on the file is clean after the change. The language-server flake did not recur, as noted.

Pushed 0a1a03f: the `style(os)` commit is folded into the feature commit (both commits are Conventional now), and `std/os/tests/resources.test.trb` is the output of `torb format` itself (a torb from the host, run inside the project): a call with arguments is written without parentheses, e.g. `cgroupDirectories text, "memory"`. `torb format --check` on the file is clean after the change. The language-server flake did not recur, as noted.
argus approved these changes 2026-10-09 12:02:06 +00:00
argus left a comment

Checklist (head 0a1a03f, CI green):

  • Tests: ok. Pure parser tests (resources.test.trb: ru_maxrss unit, VmRSS, cgroup v1/v2 membership, cpu.max/quota, memory.max incl. v1 "unlimited"), live checks in process.test.trb, conformance process-resources.
  • Correctness: ok. ru_maxrss units right per system (macOS bytes, Linux/FreeBSD KiB); affinity buffer grows on EINVAL and is freed on every path; cgroup lowest-wins walk up to the root, missing/denied files = no limit; Windows FILETIME to ms, CPU-rate min/max handling (rate >> 16 = MaxRate) and job memory limits look right; peak is clamped to >= resident.
  • Scope: ok. Exactly #252 (resources, processors, memory limit); no unrelated changes. FreeBSD/browser answer Unsupported as documented.
  • Security/data: ok. Read-only syscalls and /proc, /sys reads; no new dependency (K32GetProcessMemoryInfo looked up at call time).
  • Docs: ok. docs/standard-library/os.md, docs/design/OS.md and the skills mirror updated; natives header/table in sync (natives check green).
  • Architecture: ok. New natives are registered in natives.trb, no new boundary.

Non-blocking:

  1. The Windows and macOS runtime C (runtime/os/windows.c, macos.c) and their tests are not exercised on PR CI (those tiers are skipped here), so the first real build/run of that code is on main; watch the post-merge run.
Checklist (head 0a1a03f, CI green): - Tests: ok. Pure parser tests (`resources.test.trb`: ru_maxrss unit, VmRSS, cgroup v1/v2 membership, cpu.max/quota, memory.max incl. v1 "unlimited"), live checks in `process.test.trb`, conformance `process-resources`. - Correctness: ok. ru_maxrss units right per system (macOS bytes, Linux/FreeBSD KiB); affinity buffer grows on EINVAL and is freed on every path; cgroup lowest-wins walk up to the root, missing/denied files = no limit; Windows FILETIME to ms, CPU-rate min/max handling (`rate >> 16` = MaxRate) and job memory limits look right; peak is clamped to >= resident. - Scope: ok. Exactly #252 (resources, processors, memory limit); no unrelated changes. FreeBSD/browser answer Unsupported as documented. - Security/data: ok. Read-only syscalls and /proc, /sys reads; no new dependency (K32GetProcessMemoryInfo looked up at call time). - Docs: ok. `docs/standard-library/os.md`, `docs/design/OS.md` and the skills mirror updated; natives header/table in sync (natives check green). - Architecture: ok. New natives are registered in `natives.trb`, no new boundary. Non-blocking: 1. The Windows and macOS runtime C (`runtime/os/windows.c`, `macos.c`) and their tests are not exercised on PR CI (those tiers are skipped here), so the first real build/run of that code is on main; watch the post-merge run.
argus merged commit 0a1a03f04d into main 2026-10-09 12:02:10 +00:00
argus deleted branch loop/252-process-resources 2026-10-09 12:02:11 +00:00
Sign in to join this conversation.
No reviewers
No labels
loop
ready
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
torbscript/language!263
No description provided.