@ -182,11 +182,10 @@ impl Display for KernelVersion {
}
}
}
}
const ONLINE_CPUS : & str = "/sys/devices/system/cpu/online" ;
const POSSIBLE_CPUS : & str = "/sys/devices/system/cpu/possible" ;
/// Returns the numeric IDs of the CPUs currently online.
/// Returns the numeric IDs of the CPUs currently online.
pub fn online_cpus ( ) -> Result < Vec < u32 > , ( & ' static str , io ::Error ) > {
pub fn online_cpus ( ) -> Result < Vec < u32 > , ( & ' static str , io ::Error ) > {
const ONLINE_CPUS : & str = "/sys/devices/system/cpu/online" ;
read_cpu_ranges ( ONLINE_CPUS )
read_cpu_ranges ( ONLINE_CPUS )
}
}
@ -194,14 +193,24 @@ pub fn online_cpus() -> Result<Vec<u32>, (&'static str, io::Error)> {
///
///
/// See `/sys/devices/system/cpu/possible`.
/// See `/sys/devices/system/cpu/possible`.
pub fn nr_cpus ( ) -> Result < usize , ( & ' static str , io ::Error ) > {
pub fn nr_cpus ( ) -> Result < usize , ( & ' static str , io ::Error ) > {
const POSSIBLE_CPUS : & str = "/sys/devices/system/cpu/possible" ;
thread_local! {
thread_local! {
// TODO(https://github.com/rust-lang/rust/issues/109737): Use
// TODO(https://github.com/rust-lang/rust/issues/109737): Use
// `std::cell::OnceCell` when `get_or_try_init` is stabilized.
// `std::cell::OnceCell` when `get_or_try_init` is stabilized.
static CACHE : once_cell ::unsync ::OnceCell < usize > = const { once_cell ::unsync ::OnceCell ::new ( ) } ;
static CACHE : once_cell ::unsync ::OnceCell < usize > = const { once_cell ::unsync ::OnceCell ::new ( ) } ;
}
}
CACHE . with ( | cell | {
CACHE . with ( | cell | {
cell . get_or_try_init ( | | read_cpu_ranges ( POSSIBLE_CPUS ) . map ( | cpus | cpus . len ( ) ) )
cell . get_or_try_init ( | | {
. copied ( )
// error: unsupported operation: `open` not available when isolation is enabled
if cfg! ( miri ) {
parse_cpu_ranges ( "0-3" ) . map_err ( | error | ( POSSIBLE_CPUS , error ) )
} else {
read_cpu_ranges ( POSSIBLE_CPUS )
}
. map ( | cpus | cpus . len ( ) )
} )
. copied ( )
} )
} )
}
}