Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use jobserver in wasm-builder to limit concurrency of spawned cargo processes #4946

Merged
merged 3 commits into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -803,6 +803,7 @@ is-terminal = { version = "0.4.9" }
is_executable = { version = "1.0.1" }
isahc = { version = "1.2" }
itertools = { version = "0.11" }
jobserver = { version = "0.1.26" }
jsonpath_lib = { version = "0.3" }
jsonrpsee = { version = "0.23.2" }
jsonrpsee-core = { version = "0.23.2" }
Expand Down
1 change: 1 addition & 0 deletions substrate/utils/wasm-builder/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ filetime = { workspace = true }
wasm-opt = { workspace = true }
parity-wasm = { workspace = true }
polkavm-linker = { workspace = true }
jobserver = { workspace = true }

# Dependencies required for the `metadata-hash` feature.
merkleized-metadata = { optional = true, workspace = true }
Expand Down
2 changes: 2 additions & 0 deletions substrate/utils/wasm-builder/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,8 @@ fn build_project(
check_for_runtime_version_section: bool,
#[cfg(feature = "metadata-hash")] enable_metadata_hash: Option<MetadataExtraInfo>,
) {
// Init jobserver as soon as possible
crate::wasm_project::get_jobserver();
let cargo_cmd = match crate::prerequisites::check(target) {
Ok(cmd) => cmd,
Err(err_msg) => {
Expand Down
17 changes: 17 additions & 0 deletions substrate/utils/wasm-builder/src/wasm_project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ use std::{
ops::Deref,
path::{Path, PathBuf},
process,
sync::OnceLock,
};
use strum::{EnumIter, IntoEnumIterator};
use toml::value::Table;
Expand Down Expand Up @@ -899,6 +900,12 @@ fn build_bloaty_blob(
}
}

// Inherit jobserver in child cargo command to ensure we don't try to use more concurrency than
// available
if let Some(c) = get_jobserver() {
c.configure(&mut build_cmd);
}

println!("{}", colorize_info_message("Information that should be included in a bug report."));
println!("{} {:?}", colorize_info_message("Executing build command:"), build_cmd);
println!("{} {}", colorize_info_message("Using rustc version:"), cargo_cmd.rustc_version());
Expand Down Expand Up @@ -1178,3 +1185,13 @@ fn copy_blob_to_target_directory(cargo_manifest: &Path, blob_binary: &WasmBinary
)
.expect("Copies blob binary to `WASM_TARGET_DIRECTORY`.");
}

// Get jobserver from parent cargo command
pub fn get_jobserver() -> &'static Option<jobserver::Client> {
static JOBSERVER: OnceLock<Option<jobserver::Client>> = OnceLock::new();

JOBSERVER.get_or_init(|| {
// Unsafe because it deals with raw fds
unsafe { jobserver::Client::from_env() }
Copy link
Member

Choose a reason for hiding this comment

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

Given the documentation, this should be called as early as possible in the process to get the correct fd. This said, we should move this call to a different location. I think at the top of build_project should be fine.

})
}
Loading