Skip to content

feat(priority): use taskpolicy -b on macOS for I/O throttling - #2133

Merged
max-sixty merged 6 commits into
mainfrom
research-macos-priority
Apr 12, 2026
Merged

max-sixty merged 6 commits into
mainfrom
research-macos-priority

Conversation

@max-sixty

Copy link
Copy Markdown
Owner

On Darwin, nice/renice only affect Mach CPU scheduling priority — disk and network I/O throttling require PRIO_DARWIN_BG, set via setpriority(PRIO_DARWIN_PROCESS, …) or taskpolicy -b. For the workloads we lower priority on (reflink-fallback copy in step_copy_ignored, background rm -rf behind wt remove, trash sweep), disk I/O is the dominant cost — the previous nice 19 / renice 19 calls left it un-throttled on APFS.

Changes

  • copy.rs::lower_process_priority — taskpolicy -b -p <pid> on macOS; renice -n 19 -p <pid> plus best-effort ionice -c 3 -p <pid> on Linux.
  • process.rs::low_priority_command — wraps the child in /usr/sbin/taskpolicy -b on macOS, nice -n 19 -- elsewhere. Policy is inherited by children, so the detached shell and its grandchildren (the actual rm -rf) all run at lowered priority.

Shelled out rather than calling the syscalls directly to stay within forbid(unsafe_code). taskpolicy ships at /usr/sbin/taskpolicy in every macOS base install; a missing binary fails the spawn, matching the existing tolerance model.

Evidence

man 2 setpriority on macOS: "When a thread or process is in a background state the scheduling priority is set to the lowest value, disk IO is throttled (with behavior similar to using setiopolicy_np(3) to set a throttleable policy), and network IO is throttled for any sockets opened after going into background state." The Darwin-specific PRIO_DARWIN_BG bit is what makes the difference — the POSIX PRIO_PROCESS path that nice/renice use doesn't touch I/O policy.

man taskpolicy: "uses the setiopolicy_np(3) and setpriority(2) APIs … All children of the specified program also inherit these policies."

This builds on #2130 (which added nice 19 wrapping for internal detached ops); that baseline is preserved for Linux while macOS gets the I/O-throttling upgrade.

This was written by Claude Code on behalf of @max-sixty

max-sixty and others added 5 commits April 12, 2026 14:18
`wt remove`'s background `rm -rf` and `sweep_stale_trash` were competing
with foreground work for CPU/IO. Extends the treatment already applied
to `step_copy_ignored` (see `src/copy.rs::lower_process_priority`) to
internal detached spawns: `spawn_detached` and `spawn_detached_exec` now
wrap the child in `nice -n 19 --` when `hook_log` is `HookLog::Internal`.
User hooks run at normal priority.

`nice` is POSIX and shipped in base macOS/Linux; a missing binary would
fail the spawn, matching the existing `renice` assumption. Windows is
unchanged — priority there is a separate mechanism.

Co-authored-by: Claude <[email protected]>
Wrap `sh` / the target binary in `nice -n 19` when `spawn_detached` and
`spawn_detached_exec` are called for `HookLog::Internal`. Covers the
background `rm -rf` behind `wt remove` and the trash sweep. User hooks
are unaffected — they continue to run at normal priority so their
visible work isn't slowed.

Co-Authored-By: Claude <[email protected]>
On Darwin, `nice`/`renice` only affect Mach CPU scheduling priority —
disk and network I/O throttling require `PRIO_DARWIN_BG`, set via
`setpriority(PRIO_DARWIN_PROCESS, …)` or `taskpolicy -b`. For the
workloads we lower priority on (reflink-fallback copy in
`step_copy_ignored`, background `rm -rf` behind `wt remove`, trash
sweep), disk I/O is the dominant cost — the previous `nice 19` /
`renice 19` calls left it un-throttled on APFS.

- `copy.rs::lower_process_priority`: `taskpolicy -b -p <pid>` on macOS,
  `renice -n 19 -p <pid>` + best-effort `ionice -c 3 -p <pid>` on Linux.
- `process.rs::low_priority_command`: wraps the child in
  `/usr/sbin/taskpolicy -b` on macOS, `nice -n 19 --` elsewhere. Policy
  is inherited by children so the detached shell + its grandchildren
  (the actual `rm -rf`) all run at lowered priority.

Shelled out rather than calling the syscalls directly to stay within
`forbid(unsafe_code)`. `taskpolicy` ships at `/usr/sbin/taskpolicy` in
every macOS base install; a missing binary fails the spawn, matching
the existing tolerance model.

See `man 2 setpriority` (PRIO_DARWIN_BG section) and `man taskpolicy`
for the underlying mechanism.

Co-Authored-By: Claude <[email protected]>
worktrunk-bot
worktrunk-bot previously approved these changes Apr 12, 2026
`low_priority_command` is `#[cfg(unix)]`, so the intra-doc link in
`spawn_detached`'s docstring (which is platform-neutral) fails rustdoc
resolution on Windows with `-Dwarnings`. Drop the link; the surrounding
text already names the function.

Co-Authored-By: Claude <[email protected]>

@worktrunk-bot worktrunk-bot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Windows doc build fails with error: unresolved link to \low_priority_command`atsrc/commands/process.rs:262—low_priority_commandis#[cfg(unix)], so on Windows the function doesn't exist and rustdoc can't resolve the intra-doc link from the unconditionally-compiled spawn_detached`. Dropping the brackets (keeping it as inline code) is the least-invasive fix.

Happy to push this fix if you'd like.

Comment thread src/commands/process.rs Resolved
@worktrunk-bot
worktrunk-bot dismissed their stale review April 12, 2026 22:25

CI failed — Windows rustdoc cannot resolve the new [low_priority_command] intra-doc link (function is cfg(unix))

@max-sixty
max-sixty merged commit 82b7c27 into main Apr 12, 2026
28 checks passed
@max-sixty
max-sixty deleted the research-macos-priority branch April 12, 2026 22:34
max-sixty added a commit that referenced this pull request Apr 13, 2026
…2140)

Collapses the two priority-lowering helpers
(`copy::lower_process_priority` and
`commands::process::low_priority_command`) into a single
`worktrunk::priority` module. The module-level doc-comment now documents
the policy (macOS `taskpolicy -b` vs Linux `ionice`+`nice`), the
`forbid(unsafe_code)` rationale for shelling out, and the two forms in
which we apply it.

Follow-up to #2133. Two small behaviour notes:

- The Linux child-wrapping path now does best-effort `ionice -c 3 --
nice -n 19 --` (was just `nice -n 19`), matching what `taskpolicy -b`
already covers on macOS. `ionice` availability is probed once per
process via `which`. `ionice` ships in `util-linux` on every mainstream
distro and is enabled in Alpine's busybox, so the `nice`-only fallback
is only hit on stripped-down environments (distroless, minimal busybox,
etc.).
- `copy.rs`'s self-lowering path now also gates `ionice` behind the same
`which` probe, so we don't unconditionally fork `ionice` on hosts
without it.

The `command()` function delegates Linux branching to a small
`linux_low_priority_command(program, has_ionice)` helper so both
`has_ionice` arms are unit-testable regardless of whether the CI runner
has `ionice` installed. Tests cover the macOS `taskpolicy` wrap, both
Linux arms, the non-unix no-op, and `lower=false` passthrough.

One canonical policy in one place.

> _This was written by Claude Code on behalf of Maximilian_

---------

Co-authored-by: Claude <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants