Skip to content

Use fifo-based jobserver to enable posix_spawn over fork+exec#2718

Open
badumbatish wants to merge 1 commit into
mozilla:mainfrom
badumbatish:posix_spawn
Open

Use fifo-based jobserver to enable posix_spawn over fork+exec#2718
badumbatish wants to merge 1 commit into
mozilla:mainfrom
badumbatish:posix_spawn

Conversation

@badumbatish

Copy link
Copy Markdown

This commit uses a lot of unsafe, i would love some code reviews for this

jobserver performs pre_exec in its configure() stage, which pushes a closure onto the Command struct
(.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/sys/process/unix/common.rs:316). When it does this, posix_spawn doesn't execute and things fall back to .rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/sys/process/unix/unix.rs:94, which forks and is bad for
mimalloc:

> Use caution when using fork in combination with either large or huge OS pages: on a fork, the OS uses copy-on-write for all pages in the original process including the huge OS pages. When any memory is now written in that area, the OS will copy the entire 1GiB huge page (or 2MiB large page) which can cause the memory usage to grow in large increments.

Creating the jobserver as a named FIFO at /tmp/sccache-jobserver-<uid>/
avoids the pre_exec requirement entirely, letting we use posix_spawn.
The commit falls back to pipe-based jobserver when fifo creation fails.

When using musl malloc, profiling shows the overhead was at the locking stage. Swapping musl malloc for mimalloc switches the overhead to the forking stage, hence the fix.

Although the fix was geared towards mimalloc, this should applies to all
options as it limits the forking.
@codecov-commenter

codecov-commenter commented Jun 4, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 97.87234% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 74.36%. Comparing base (8825b4a) to head (a7a4916).
⚠️ Report is 19 commits behind head on main.

Files with missing lines Patch % Lines
src/jobserver.rs 97.87% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2718      +/-   ##
==========================================
- Coverage   74.41%   74.36%   -0.05%     
==========================================
  Files          70       70              
  Lines       39211    39411     +200     
==========================================
+ Hits        29177    29309     +132     
- Misses      10034    10102      +68     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@codspeed-hq

codspeed-hq Bot commented Jun 4, 2026

Copy link
Copy Markdown

Merging this PR will improve performance by 4.55%

⚡ 1 improved benchmark
✅ 59 untouched benchmarks
⏩ 4 skipped benchmarks1

Performance Changes

Mode Benchmark BASE HEAD Efficiency
Simulation normalize_win_path_typical 1.6 µs 1.5 µs +4.55%

Tip

Curious why this is faster? Comment @codspeedbot explain why this is faster on this PR, or directly use the CodSpeed MCP with your agent.


Comparing badumbatish:posix_spawn (a7a4916) with main (8825b4a)

Open in CodSpeed

Footnotes

  1. 4 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

Comment thread src/jobserver.rs
use std::os::unix::fs::OpenOptionsExt;

let dir = std::path::PathBuf::from(format!(
"/tmp/sccache-jobserver-{}",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
"/tmp/sccache-jobserver-{}",
"{}/sccache-jobserver-{}", std::env::get_var("TMPDIR").unwrap_or("/tmp"))

nit. See Wikipedia for more info

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.

3 participants