use core::sync::atomic::{AtomicBool, AtomicU64, AtomicUsize, Ordering};
use std::time::Instant;
const RECHECK_INTERVAL_MS: u64 = 250;
fn now_ms() -> u64 {
static ORIGIN: std::sync::OnceLock<Instant> = std::sync::OnceLock::new();
let origin = ORIGIN.get_or_init(Instant::now);
origin.elapsed().as_millis() as u64
}
static ALLOWED: AtomicUsize = AtomicUsize::new(0);
static LAST_READ_MS: AtomicU64 = AtomicU64::new(0);
fn is_due(last_ms: u64, now_ms: u64, interval_ms: u64) -> bool {
now_ms.saturating_sub(last_ms) >= interval_ms
}
fn report_probe_failure(err: &std::io::Error, keeping: usize) {
static SAID: AtomicBool = AtomicBool::new(false);
if !SAID.swap(true, Ordering::Relaxed) {
notice!(
"flynnel: this host does not report how many CPUs the process \
may use ({err}); the pool keeps a width of {keeping} and will \
retry"
);
}
}
#[cfg(target_os = "linux")]
fn process_width() -> std::io::Result<std::num::NonZeroUsize> {
let mask = proc_status_allowed_cpus()?;
let width = match cgroup_quota_cpus() {
Some(quota) => mask.min(quota),
None => mask,
};
std::num::NonZeroUsize::new(width).ok_or_else(|| {
std::io::Error::other("the process affinity mask names no cpu")
})
}
#[cfg(target_os = "linux")]
fn proc_status_allowed_cpus() -> std::io::Result<usize> {
let status = std::fs::read_to_string("/proc/self/status")?;
let list = status
.lines()
.find_map(|line| line.strip_prefix("Cpus_allowed_list:"))
.ok_or_else(|| {
std::io::Error::other("/proc/self/status carries no Cpus_allowed_list")
})?;
count_cpu_list(list.trim())
}
#[cfg(target_os = "linux")]
fn count_cpu_list(list: &str) -> std::io::Result<usize> {
fn malformed(part: &str, why: &dyn std::fmt::Display) -> std::io::Error {
std::io::Error::other(format!("cpu list entry {part:?} is not a cpu or a range: {why}"))
}
let mut total = 0usize;
for part in list.split(',') {
let part = part.trim();
if part.is_empty() {
continue;
}
match part.split_once('-') {
Some((low, high)) => {
let low: usize = low.trim().parse().map_err(|err| malformed(part, &err))?;
let high: usize = high.trim().parse().map_err(|err| malformed(part, &err))?;
if high < low {
return Err(malformed(part, &"its end is below its start"));
}
total += high - low + 1;
}
None => {
part.parse::<usize>().map_err(|err| malformed(part, &err))?;
total += 1;
}
}
}
Ok(total)
}
#[cfg(target_os = "linux")]
fn cgroup_quota_cpus() -> Option<usize> {
static QUOTA: std::sync::OnceLock<Option<usize>> = std::sync::OnceLock::new();
*QUOTA.get_or_init(read_cgroup_quota_cpus)
}
#[cfg(target_os = "linux")]
fn read_cgroup_file(path: &str) -> Option<String> {
match std::fs::read_to_string(path) {
Ok(text) => Some(text),
Err(err) if err.kind() == std::io::ErrorKind::NotFound => None,
Err(err) => {
report_quota_unreadable(path, &err);
None
}
}
}
#[cfg(target_os = "linux")]
fn cgroup_number(path: &str, field: &str) -> Option<u64> {
match field.parse::<u64>() {
Ok(value) => Some(value),
Err(err) => {
report_quota_unreadable(path, &format!("{field:?}: {err}"));
None
}
}
}
#[cfg(target_os = "linux")]
fn read_cgroup_quota_cpus() -> Option<usize> {
const V2: &str = "/sys/fs/cgroup/cpu.max";
if let Some(text) = read_cgroup_file(V2) {
let mut fields = text.split_whitespace();
let Some(quota) = fields.next() else {
report_quota_unreadable(V2, &"the file is empty");
return None;
};
if quota == "max" {
return None;
}
let Some(period) = fields.next() else {
report_quota_unreadable(V2, &format!("{:?} names no period", text.trim()));
return None;
};
return quota_to_cpus(cgroup_number(V2, quota)?, cgroup_number(V2, period)?);
}
const QUOTA_V1: &str = "/sys/fs/cgroup/cpu/cpu.cfs_quota_us";
const PERIOD_V1: &str = "/sys/fs/cgroup/cpu/cpu.cfs_period_us";
let quota = read_cgroup_file(QUOTA_V1)?;
let period = read_cgroup_file(PERIOD_V1)?;
let quota = match quota.trim().parse::<i64>() {
Ok(value) if value <= 0 => return None,
Ok(value) => value as u64,
Err(err) => {
report_quota_unreadable(QUOTA_V1, &format!("{:?}: {err}", quota.trim()));
return None;
}
};
quota_to_cpus(quota, cgroup_number(PERIOD_V1, period.trim())?)
}
#[cfg(target_os = "linux")]
fn quota_to_cpus(quota: u64, period: u64) -> Option<usize> {
if period == 0 {
return None;
}
Some(quota.div_ceil(period).max(1) as usize)
}
#[cfg(target_os = "linux")]
fn report_quota_unreadable(path: &str, saw: &dyn std::fmt::Display) {
static SAID: AtomicBool = AtomicBool::new(false);
if !SAID.swap(true, Ordering::Relaxed) {
notice!(
"flynnel: {path} reads {saw}, which is not a quota and a period; the pool \
sizes by the affinity mask alone and ignores whatever limit that file sets"
);
}
}
#[cfg(all(not(windows), not(target_os = "linux")))]
fn process_width() -> std::io::Result<std::num::NonZeroUsize> {
std::thread::available_parallelism()
}
#[cfg(windows)]
fn process_width() -> std::io::Result<std::num::NonZeroUsize> {
#[link(name = "kernel32")]
unsafe extern "system" {
fn GetCurrentProcess() -> *mut core::ffi::c_void;
fn GetProcessAffinityMask(
process: *mut core::ffi::c_void,
process_mask: *mut usize,
system_mask: *mut usize,
) -> i32;
}
let mut process_mask: usize = 0;
let mut system_mask: usize = 0;
let ok = unsafe {
GetProcessAffinityMask(
GetCurrentProcess(),
&raw mut process_mask,
&raw mut system_mask,
)
};
if ok == 0 {
return std::thread::available_parallelism();
}
std::num::NonZeroUsize::new(process_mask.count_ones() as usize).ok_or_else(|| {
std::io::Error::other("the process affinity mask names no cpu")
})
}
pub fn allowed_parallelism() -> usize {
resolve(
&ALLOWED,
&LAST_READ_MS,
now_ms(),
RECHECK_INTERVAL_MS,
process_width,
)
}
fn resolve<P>(
allowed: &AtomicUsize,
last_read_ms: &AtomicU64,
now: u64,
interval_ms: u64,
probe: P,
) -> usize
where
P: FnOnce() -> std::io::Result<std::num::NonZeroUsize>,
{
let cached = allowed.load(Ordering::Relaxed);
if cached != 0 && !is_due(last_read_ms.load(Ordering::Relaxed), now, interval_ms) {
return cached;
}
match probe() {
Ok(width) => {
let width = width.get();
allowed.store(width, Ordering::Relaxed);
last_read_ms.store(now, Ordering::Relaxed);
width
}
Err(err) => {
let keeping = if cached != 0 { cached } else { 1 };
report_probe_failure(&err, keeping);
keeping
}
}
}
#[cfg(test)]
mod tests {
use super::*;
fn says(n: usize) -> impl FnOnce() -> std::io::Result<std::num::NonZeroUsize> {
move || Ok(std::num::NonZeroUsize::new(n).expect("a width is at least one"))
}
#[cfg(target_os = "linux")]
#[test]
fn a_cpu_list_counts_its_singles_and_its_ranges() {
assert_eq!(count_cpu_list("0-15").expect("a plain range"), 16);
assert_eq!(count_cpu_list("14").expect("one cpu"), 1);
assert_eq!(count_cpu_list("0-3,8,12-15").expect("a mixed list"), 9);
assert_eq!(count_cpu_list("7").expect("a pinned thread"), 1);
}
#[cfg(target_os = "linux")]
#[test]
fn a_cpu_list_that_does_not_parse_is_an_error_not_a_smaller_width() {
for bad in ["0-", "-3", "x", "0-x", "7-3"] {
assert!(
count_cpu_list(bad).is_err(),
"{bad:?} must not be counted as a width"
);
}
}
#[cfg(target_os = "linux")]
#[test]
fn a_quota_rounds_up_to_whole_cpus() {
assert_eq!(quota_to_cpus(100_000, 100_000), Some(1));
assert_eq!(quota_to_cpus(150_000, 100_000), Some(2));
assert_eq!(quota_to_cpus(200_000, 100_000), Some(2));
assert_eq!(quota_to_cpus(50_000, 100_000), Some(1), "never below one");
assert_eq!(quota_to_cpus(100_000, 0), None, "a period of zero is no quota");
}
fn cannot_answer() -> impl FnOnce() -> std::io::Result<std::num::NonZeroUsize> {
|| {
Err(std::io::Error::new(
std::io::ErrorKind::Unsupported,
"this host does not report it",
))
}
}
#[test]
fn a_narrowed_quota_takes_effect_on_the_next_reading() {
let allowed = AtomicUsize::new(0);
let last = AtomicU64::new(0);
assert_eq!(resolve(&allowed, &last, 1_000, 250, says(16)), 16);
assert_eq!(resolve(&allowed, &last, 1_100, 250, says(4)), 16);
assert_eq!(resolve(&allowed, &last, 1_400, 250, says(4)), 4);
}
#[test]
fn a_widened_quota_takes_effect_too() {
let allowed = AtomicUsize::new(0);
let last = AtomicU64::new(0);
assert_eq!(resolve(&allowed, &last, 1_000, 250, says(4)), 4);
assert_eq!(resolve(&allowed, &last, 1_400, 250, says(16)), 16);
}
#[test]
fn a_failed_reading_keeps_the_last_width_and_retries_at_once() {
let allowed = AtomicUsize::new(0);
let last = AtomicU64::new(0);
assert_eq!(resolve(&allowed, &last, 1_000, 250, says(16)), 16);
assert_eq!(resolve(&allowed, &last, 1_400, 250, cannot_answer()), 16);
assert_eq!(resolve(&allowed, &last, 1_401, 250, says(4)), 4);
}
#[test]
fn a_first_reading_that_fails_reports_one_rather_than_zero() {
let allowed = AtomicUsize::new(0);
let last = AtomicU64::new(0);
assert_eq!(resolve(&allowed, &last, 1_000, 250, cannot_answer()), 1);
assert_eq!(
allowed.load(Ordering::Relaxed),
0,
"a failed probe stores nothing, so the next success is still a first reading"
);
}
#[test]
fn a_reading_is_due_only_once_the_interval_has_passed() {
assert!(!is_due(1_000, 1_000, 250));
assert!(!is_due(1_000, 1_249, 250));
assert!(is_due(1_000, 1_250, 250));
assert!(is_due(1_000, 9_000, 250));
}
#[test]
fn a_clock_reading_lower_than_the_last_is_not_due_rather_than_overdue() {
assert!(!is_due(9_000, 1_000, 250));
}
#[test]
fn the_allowed_width_is_a_real_count_and_is_cached_within_the_interval() {
let width = allowed_parallelism();
assert!(width >= 1, "a process may always use at least one CPU");
assert_eq!(width, allowed_parallelism());
}
#[test]
fn a_successful_reading_never_stores_the_no_reading_value() {
let seen = allowed_parallelism();
assert_ne!(seen, 0);
assert_ne!(ALLOWED.load(Ordering::Relaxed), 0);
}
}