← Back to Pull requests

feat(scheduler): budget CPU and memory separately #750

Closed
unknown closed this pull request without merging on Oct 19, 2026 00:32 UTC
View on GitHub
unknown unknown commented on Oct 18, 2026 22:03 UTC
Author

Builds on #748 (merged), which lets queued builds share capacity they cannot use yet.

mbx's scheduler charged every compile, link, and test run a single number of permits: the larger of its thread count and its predicted memory divided by scheduler.memory / scheduler.cpus. So one number stood for both CPU and memory. A compile that needs 13 GiB but keeps only one or two cores busy took 17 permits. Each permit also stands for one core, so the other 15 or so cores it was charged for sat idle while CPU-light requests waited.

This change gives the scheduler separate budgets for CPU cores and for memory bytes:

  • A request starts only if both its cores and its memory fit, in the whole pool and in its container's limit. The per-build limit, which comes from Cargo's -j, constrains cores only.

  • Each request asks for the number of threads mbx already charges for it (usually 1 for a compile), plus its predicted memory in bytes. Measured predictions are charged as their raw bytes. A request with no memory history is still charged today's default memory, so nothing is charged less memory than a guess gets today.

  • mbx reserve --cpus N --memory M now reserves exactly N cores and M bytes, instead of the larger of the two as permits.

  • A request larger than a budget may still run on its own in that dimension, as before. It must still fit the other dimension.

  • The priority headroom held back from low-priority work applies to both budgets.

  • #748's pass check runs separately for cores and for memory. A newer request passes only if it fits the spare room in both. Checking each dimension on its own is stricter than a joint check, so #748's guarantee still holds. For that check, memory is rounded up to 16 MiB units with a margin for the rounding error. An exhaustive check of 49,152 boundary cases found no admission that the exact computation would refuse.

  • mbx stats --scheduler and the observation log show cores and memory charged.
  • The settings are unchanged. Their descriptions now say that scheduler.cpus and scheduler.reserve_cpus set a core budget, and scheduler.memory sets an independent memory budget.

    Measured effect. These figures come from a replay of 6,873 admissions recorded by mbx's observation log on a shared build host over about 3.3 hours. The replay keeps the recorded arrival and run times and models a single scope.

    | Policy | Total wait (request-hours) | p95 wait | p99 wait |
    |---|---:|---:|---:|
    | Single permit budget, today's ordering | 55.3 | 207 s | 286 s |
    | Separate budgets, today's ordering | 46.6 | 200 s | 253 s |
    | Single permit budget, #748 | 22.6 | 79 s | 121 s |
    | Separate budgets, #748 | 14.8 | 57 s | 81 s |

  • Under today's ordering, separate budgets delayed no request.

  • With #748, 21 requests started later than under a single budget, by at most 48 s, because the schedule shifts.

  • While requests waited under the single budget, about 95% of the CPU budget was unassigned. For 23% of that waiting time, the first request in the queue would have fit separate budgets. The rest of the waiting was for memory that really was reserved.
  • The modelled baseline accounts for only part of the waiting actually recorded (correlation 0.85), and the model charges exact bytes, where today's permits round up by about 2%. So read these figures as a comparison between policies, not as a forecast.

    Compatibility. Lease and waiting records gain explicit core and memory fields. They keep the old fields, computed exactly as before, and a test checks 168 demand and configuration cases against the previous code.

  • Older mbx versions sharing the cache read the same single-number charge as today. That includes today's cap at the writing process's own capacity, so an old reader still under-counts a request larger than the writer's budget, as it does now.

  • Records from older versions are charged in full on both axes.

  • An older version decides whether its own waiting request fits by adding up single-number weights. So when a newer request would pass an older-version waiter, it must also pass #748's single-number check, computed with that waiter's own capacities. A mixed-version property test, where old waiters use the previous scheduler code to judge their own admission, found no older waiter delayed in 512 seeds.

  • Two new versions see each other's exact cores and bytes.
  • Known behavior: a compile is charged its thread count, but rustc can use more threads during code generation through Cargo's jobserver. With memory no longer taking up CPU permits, more small compiles can run alongside large ones, so the host's CPUs can be oversubscribed. That slows compiles but does not cause out-of-memory kills.

    Validation:

  • the scheduler tests pass (143), as do the reserve (32) and stats (11) tests, cargo clippy -p mbx --all-targets --all-features -- -D warnings, and cargo fmt --check;

  • the generated CLI docs were regenerated with the pinned usage generator and the options from mise.toml, because mise was not available locally;

  • the full library suite passed 1,011 tests before the final commit. The final commit changed only scheduler code and tests. Three tests failed, all of them outside this change: cli::pretty::tests::mascot_keeps_progress_readable_and_leaves_successful_builds, materialize::materialization_tests::a_restore_stamp_is_never_ahead_of_a_write_that_follows_it, and materialize::materialization_tests::a_filesystem_without_clones_links_the_object_instead_of_copying_it. These are renderer and filesystem-timing tests, run on a heavily loaded host.
  • An independent review compared the code with the previous commit using the exact functions from both. It found one mixed-version problem: the container-limit field in waiting records had changed meaning. A second mixed-version problem came from review on this PR: older waiters were not protected by their own single-number rule. Both are fixed here, with regression tests.

    The full mise run ci gate was not run locally.

    AI-assisted — Tool: Claude Code; model: Anthropic/claude-opus-5-5; version: 2.1.302.
    AI-assisted — Tool: Codex; model: OpenAI/gpt-6.1-sol; version: unavailable.

    🤖 Generated with Claude Code

    <!-- This is an auto-generated comment: release notes by coderabbit.ai -->

    Summary by CodeRabbit

    New Features
    The scheduler now budgets CPU cores and memory independently, admitting work based on both available resources.
    Workload history and memory measurements help estimate resource needs; unmeasured work uses defaults.
    Scheduler reports show separate CPU and memory charges. Reservations can specify CPU and memory independently.
    Low-priority work leaves headroom in both budgets for normal-priority work.
    Setting scheduler.memory to "none" enables CPU-only scheduling.
    Documentation
    Updated configuration and scheduling guides to explain resource budgets, estimates, reservations, and admission behavior. mbx reserve --cpus now describes the reserved amount in CPU cores.

    <!-- end of auto-generated comment: release notes by coderabbit.ai -->

    Diff: 650 lines (+487 / -163) across 8 files Reviewer: jbellis Review duration: 149m
    All Vouch review confidence checks passed
    Residual risk 0.21 = Change Risk (0.52) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.52 · 650 lines diff across 8 files
    0.52
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.388
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #750"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 52% (MEDIUM) · Higher = larger blast radius
    52%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 20.5% · Safe to merge
    20.5%