-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Set
AVAILABLE_PARALLELISM
at build and run time
- Loading branch information
1 parent
7da2d23
commit 310b47b
Showing
10 changed files
with
181 additions
and
6 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// Required due to: https://github.com/rust-lang/rust/issues/95513 | ||
#![allow(unused_crate_dependencies)] | ||
|
||
use heroku_nodejs_utils::available_parallelism::available_parallelism_env; | ||
use libcnb::data::exec_d::ExecDProgramOutputKey; | ||
use libcnb::exec_d::write_exec_d_program_output; | ||
use std::collections::HashMap; | ||
|
||
fn main() { | ||
let mut output: HashMap<ExecDProgramOutputKey, String> = HashMap::with_capacity(1); | ||
let (available_parallelism_env_key, available_parallelism_env_value) = | ||
available_parallelism_env(); | ||
if let Ok(exec_d_output_key) = available_parallelism_env_key.parse::<ExecDProgramOutputKey>() { | ||
output.insert(exec_d_output_key, available_parallelism_env_value); | ||
} | ||
write_exec_d_program_output(output); | ||
} |
38 changes: 38 additions & 0 deletions
38
buildpacks/nodejs-engine/src/configure_available_parallelism.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
use crate::{NodeJsEngineBuildpack, NodeJsEngineBuildpackError}; | ||
use heroku_nodejs_utils::available_parallelism::available_parallelism_env; | ||
use libcnb::additional_buildpack_binary_path; | ||
use libcnb::build::BuildContext; | ||
use libcnb::data::layer_name; | ||
use libcnb::layer::UncachedLayerDefinition; | ||
use libcnb::layer_env::{LayerEnv, ModificationBehavior, Scope}; | ||
|
||
pub(crate) fn configure_available_parallelism( | ||
context: &BuildContext<NodeJsEngineBuildpack>, | ||
) -> Result<(), libcnb::Error<NodeJsEngineBuildpackError>> { | ||
let available_parallelism_layer = context.uncached_layer( | ||
layer_name!("available_parallelism"), | ||
UncachedLayerDefinition { | ||
build: true, | ||
launch: true, | ||
}, | ||
)?; | ||
|
||
let (available_parallelism_env_key, available_parallelism_env_value) = | ||
available_parallelism_env(); | ||
|
||
// set for the build time env (for webpack plugins or other tools that spin up processes) | ||
available_parallelism_layer.write_env(LayerEnv::new().chainable_insert( | ||
Scope::Build, | ||
ModificationBehavior::Override, | ||
available_parallelism_env_key, | ||
available_parallelism_env_value, | ||
))?; | ||
|
||
// set for the run time env | ||
available_parallelism_layer.write_exec_d_programs([( | ||
"available_parallelism", | ||
additional_buildpack_binary_path!("available_parallelism"), | ||
)])?; | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
pub const HEROKU_AVAILABLE_PARALLELISM: &str = "HEROKU_AVAILABLE_PARALLELISM"; | ||
|
||
#[must_use] | ||
pub fn available_parallelism_env() -> (String, String) { | ||
( | ||
HEROKU_AVAILABLE_PARALLELISM.to_string(), | ||
std::thread::available_parallelism() | ||
// XXX: The Rust implementation always rounds down the value reported here if the | ||
// (quota / period) calculated from cgroups cpu.max produces a fractional value. | ||
// For Heroku Fir Dynos this will always end up reducing the cpu allocation | ||
// value by 1 since a small amount of quota is reserved for the system so we need | ||
// to add that back unless Rust changes how they deal with rounding. | ||
.map(|value| (value.get() + 1).to_string()) | ||
.unwrap_or_default(), | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters