Skip to content

dev_cargo: size build jobs from available memory on macOS too (patch ready on a branch) #891

Closed

Description

@timorunge

Summary

select_build_jobs in scripts/dev_cargo.sh sizes cargo jobs from available memory so several self-dev agents on one machine do not each assume the full core count. It is gated on Linux and returns cargo-default on macOS, where the same problem exists.

I have a working macOS implementation, tested, on a public branch. Opening this as an issue rather than a PR because the repo limits pull requests to collaborators.

Branch: https://github.com/timorunge/jcode/tree/macos-adaptive-build-jobs
Compare: master...timorunge:jcode:macos-adaptive-build-jobs

git fetch https://github.com/timorunge/jcode.git macos-adaptive-build-jobs
git cherry-pick 70b8fa0 250b976

One file, scripts/dev_cargo.sh, +81/-8, in two commits.

Why macOS specifically

The problem the Linux path solves exists here too, arguably more sharply: Apple Silicon laptops are commonly 8-16 GiB and are a normal place to run several agents against one workspace. On macOS every one of them was taking the full core count.

How

macOS has no /proc/meminfo, so meminfo_kib_macos assembles the figure from vm_stat page counts and sysctl:

key source
MemTotal hw.memsize
MemAvailable (free + inactive + speculative) * pagesize
SwapTotal vm.swapusage

free + inactive + speculative is the closest honest analogue of Linux's MemAvailable. Purgeable and compressed pages are deliberately excluded: reclaiming them is not free, and counting them would inflate the estimate in exactly the situation the budget exists to protect. mem_kib dispatches on uname, so callers stay platform-agnostic and the Linux reader is untouched.

One behaviour change beyond the port

A memory reading of 0 now falls back to the cargo default instead of computing one job. mem_available_mib / mib_per_job floors to 0 and is then clamped to 1, so a failed reading would have pinned the machine to a single-threaded build indefinitely. A wrong small number is worse than no opinion, because it looks deliberate.

Measured

M-series Mac, 64 GiB, 10 cores:

before        build_jobs_status=cargo-default
after (idle)  build_jobs_status=adaptive:10 (cpus=10, mem_avail=44138MiB)

Back-off exercised through the existing JCODE_BUILD_MIB_PER_JOB knob, which drives the same arithmetic as real memory pressure:

 1792 MiB/job -> 10 jobs
 4096 MiB/job -> 10 jobs
 8192 MiB/job ->  5 jobs
16384 MiB/job ->  2 jobs

JCODE_BUILD_JOBS=3 still wins (override:3); an invalid override still warns and falls through to adaptive. Parsers cross-checked against sysctl: 64 GiB total, ~44 GiB available, available <= total.

Second commit: a bug the first one introduced

Worth reading on its own, because the guard could never have fired. With set -euo pipefail in force, a failing vm_stat | awk pipeline aborts the script before the zero check is evaluated -- --print-setup exited 1 and printed nothing at all. The Linux reader already ends in || true for precisely this reason; the macOS readers now do too. Found by replacing vm_stat with false and watching it, not by rereading the code.

Not verified

  • No Linux run in this change. That path is untouched, but I did not re-exercise it.
  • The back-off was driven through the budget knob rather than by genuinely exhausting 44 GiB, so the arithmetic is proven while the behaviour of a truly pressured macOS host is inferred, not observed.
  • shellcheck is not installed on this machine, so the change is unlinted.

Happy to adjust naming, or the excluded-page-classes decision if you would rather count compressed pages. If you would prefer this as a PR, I am glad to open one if the restriction is lifted or I am added as a collaborator.

Activity

  1. added
    enhancement
    priority: medium
    triage: reproducible
    autonomous: clear
    triage: fixed-pending-release
    on Aug 12, 2026
  2. 1jehuang commented on Aug 12, 2026

    @1jehuang
    Owner

    Implemented macOS available-memory detection and adaptive development build job sizing with override and fallback behavior in #907. Script tests and the integrated TUI build pass.


    — Jcode agent (automated triage), on behalf of @1jehuang

  3. added a commit that references this issue on Aug 12, 2026
    9c9023a
  4. added a commit that references this issue on Aug 15, 2026
    a8e0afd
  5. added 5 commits that reference this issue on Aug 30, 2026
    52ffc36
    0e951c1
    e4b2285
    2eb44f2
    bcc77cb
  6. added a commit that references this issue on Sep 18, 2026
    699771c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    autonomous: clearenhancementpriority: mediumtriage: fixed-pending-releasetriage: reproducible

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions