use crate::context::ExecContext;
use crate::dag::{self, MatrixPlan};
use crate::frozen::FrozenPlan;
use crate::plan;
use serde::{Deserialize, Serialize};
use std::path::Path;
use std::process::Stdio;
pub const JOB_THREAD_ENV: [&str; 3] = [
"CARGO_BUILD_JOBS",
"NEXTEST_TEST_THREADS",
"RUST_TEST_THREADS",
];
const MIN_SHARE: usize = 4;
pub const UNGATED_PARALLEL: usize = 4;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Share {
pub budget: usize,
pub jobs_at_once: usize,
pub threads_per_job: usize,
}
pub fn share(budget: usize, parallel: Option<usize>, gated: bool, peak: usize) -> Option<Share> {
let budget = budget.max(1);
let jobs_at_once = match parallel {
Some(_) => peak.max(1),
None => {
let wanted = if gated {
budget / MIN_SHARE
} else {
UNGATED_PARALLEL
};
wanted.clamp(1, peak.max(1))
}
};
let threads_per_job = (budget / jobs_at_once).max(1);
(threads_per_job < budget).then_some(Share {
budget,
jobs_at_once,
threads_per_job,
})
}
pub fn peak(
fp: &FrozenPlan,
matrix_at_once: bool,
isolated: bool,
units_at_once: usize,
all_os_legs: bool,
) -> usize {
let mut widths: Vec<usize> = fp
.units
.iter()
.map(|u| {
if !matrix_at_once {
return 1;
}
let locked = u.exclusive
&& (!isolated
|| u.jobs
.iter()
.any(|id| !fp.job(u.workflow, id).services.is_empty()));
u.jobs
.iter()
.map(|id| {
let job = fp.job(u.workflow, id);
if plan::is_exclusive(&job.to_job()) && locked {
return 1;
}
let entries = match dag::matrix_plan(job) {
MatrixPlan::None => 1,
MatrixPlan::Static(v) if all_os_legs => v.len(),
MatrixPlan::Static(v) => crate::os_legs::running_legs(
crate::os_legs::Family::host(),
&job.to_job(),
&v,
),
MatrixPlan::Dynamic(_) => usize::MAX,
};
entries.min(job.max_parallel.unwrap_or(usize::MAX)).max(1)
})
.max()
.unwrap_or(1)
})
.collect();
widths.sort_unstable_by(|a, b| b.cmp(a));
widths
.into_iter()
.take(units_at_once.max(1))
.fold(0usize, usize::saturating_add)
}
pub fn cores() -> usize {
std::thread::available_parallelism()
.map(|n| n.get())
.unwrap_or(1)
}
pub fn controller_budget(ctx: &ExecContext, controller: &Path) -> Result<usize, String> {
let out = ctx
.command(controller)
.arg("inspect")
.stdin(Stdio::null())
.stderr(Stdio::null())
.output()
.map_err(|e| format!("`inspect` could not be run: {e}"))?;
if !out.status.success() {
return Err(format!("`inspect` failed ({})", out.status));
}
parse_budget(&String::from_utf8_lossy(&out.stdout))
.ok_or_else(|| "`inspect` has no budget".to_string())
}
fn parse_budget(json: &str) -> Option<usize> {
let v: serde_json::Value = serde_json::from_str(json).ok()?;
let b = v.get("limits")?.get("budget")?.as_u64()?;
(b > 0).then_some(b as usize)
}
#[cfg(test)]
mod tests {
use super::*;
fn s(budget: usize, jobs_at_once: usize, threads_per_job: usize) -> Option<Share> {
Some(Share {
budget,
jobs_at_once,
threads_per_job,
})
}
#[test]
fn gated_jobs_get_at_least_four_threads_while_the_budget_has_them() {
assert_eq!(share(12, None, true, 9), s(12, 3, 4));
assert_eq!(share(10, None, true, 9), s(10, 2, 5));
assert_eq!(share(16, None, true, 9), s(16, 4, 4));
for budget in 1..=64 {
for peak in 1..=20 {
if let Some(x) = share(budget, None, true, peak) {
assert!(x.jobs_at_once * x.threads_per_job <= budget, "{x:?}");
assert!(x.threads_per_job >= MIN_SHARE.min(budget / 2), "{x:?}");
}
}
}
}
#[test]
fn a_job_that_runs_alone_keeps_the_whole_budget() {
assert_eq!(share(12, None, true, 1), None);
assert_eq!(share(12, None, false, 1), None);
assert_eq!(share(12, Some(1), true, 1), None);
assert_eq!(share(4, None, true, 9), None);
assert_eq!(share(1, None, false, 9), None);
assert_eq!(share(0, None, true, 9), None);
}
#[test]
fn fewer_jobs_than_places_share_between_themselves_only() {
assert_eq!(share(12, None, true, 2), s(12, 2, 6));
assert_eq!(share(12, Some(8), true, 3), s(12, 3, 4));
assert_eq!(share(12, Some(1), true, 4), s(12, 4, 3));
}
#[test]
fn parallel_decides_when_it_is_given() {
assert_eq!(share(12, Some(6), true, 6), s(12, 6, 2));
assert_eq!(share(12, Some(2), false, 2), s(12, 2, 6));
assert_eq!(share(4, Some(16), false, 16), s(4, 16, 1));
}
#[test]
fn without_the_controller_the_default_places_divide_the_cores() {
assert_eq!(share(12, None, false, 9), s(12, 4, 3));
assert_eq!(share(4, None, false, 9), s(4, 4, 1));
}
#[test]
fn the_budget_is_read_from_inspect() {
assert_eq!(
parse_budget(r#"{"entries":[],"limits":{"budget":12}}"#),
Some(12)
);
assert_eq!(parse_budget(r#"{"limits":{"budget":0}}"#), None);
assert_eq!(parse_budget(r#"{"limits":{}}"#), None);
assert_eq!(parse_budget("not json"), None);
}
}