diff --git a/Cargo.toml b/Cargo.toml index 1f8b4c28..85522aee 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -78,6 +78,7 @@ netns-rs = { version = "0.1", default-features = false } nix = { version = "0.29.0", default-features = false } num_enum = { version = "0.7", default-features = false } object = { version = "0.36", default-features = false } +once_cell = { version = "1.20.1", default-features = false } proc-macro-error = { version = "1.0", default-features = false } proc-macro2 = { version = "1", default-features = false } public-api = { version = "0.38.0", default-features = false } diff --git a/aya/Cargo.toml b/aya/Cargo.toml index 5222fb5a..d96ad055 100644 --- a/aya/Cargo.toml +++ b/aya/Cargo.toml @@ -21,6 +21,7 @@ bytes = { workspace = true } libc = { workspace = true } log = { workspace = true } object = { workspace = true, features = ["elf", "read_core", "std", "write"] } +once_cell = { workspace = true } thiserror = { workspace = true } tokio = { workspace = true, features = ["rt"], optional = true } diff --git a/aya/src/util.rs b/aya/src/util.rs index b4323163..50f96ff4 100644 --- a/aya/src/util.rs +++ b/aya/src/util.rs @@ -196,7 +196,15 @@ pub fn online_cpus() -> Result, (&'static str, io::Error)> { /// /// See `/sys/devices/system/cpu/possible`. pub fn nr_cpus() -> Result { - read_cpu_ranges(POSSIBLE_CPUS).map(|cpus| cpus.len()) + thread_local! { + // TODO(https://github.com/rust-lang/rust/issues/109737): Use + // `std::cell::OnceCell` when `get_or_try_init` is stabilized. + static CACHE: once_cell::unsync::OnceCell = const { once_cell::unsync::OnceCell::new() }; + } + CACHE.with(|cell| { + cell.get_or_try_init(|| read_cpu_ranges(POSSIBLE_CPUS).map(|cpus| cpus.len())) + .copied() + }) } fn read_cpu_ranges(path: &'static str) -> Result, (&'static str, io::Error)> {