Skip to content

Commit

Permalink
Replace lazy_static with std::sync::LazyLock
Browse files Browse the repository at this point in the history
  • Loading branch information
tamird committed Oct 2, 2024
1 parent 0f16363 commit 2b299d4
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 32 deletions.
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ futures = { version = "0.3.28", default-features = false }
hashbrown = { version = "0.14.3", default-features = false }
indoc = { version = "2.0", default-features = false }
integration-ebpf = { path = "test/integration-ebpf", default-features = false }
lazy_static = { version = "1", default-features = false }
libc = { version = "0.2.105", default-features = false }
log = { version = "0.4", default-features = false }
netns-rs = { version = "0.1", default-features = false }
Expand Down
3 changes: 1 addition & 2 deletions aya/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ description = "An eBPF library with a focus on developer experience and operabil
keywords = ["bpf", "ebpf", "kernel", "linux"]
readme = "README.md"
documentation = "https://docs.rs/aya"
rust-version = "1.66"
rust-version = "1.80.0"
authors.workspace = true
license.workspace = true
repository.workspace = true
Expand All @@ -18,7 +18,6 @@ async-io = { workspace = true, optional = true }
aya-obj = { path = "../aya-obj", version = "^0.1.0", features = ["std"] }
bitflags = { workspace = true }
bytes = { workspace = true }
lazy_static = { workspace = true }
libc = { workspace = true }
log = { workspace = true }
object = { workspace = true, features = ["elf", "read_core", "std", "write"] }
Expand Down
6 changes: 2 additions & 4 deletions aya/src/bpf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::{
raw::c_int,
},
path::{Path, PathBuf},
sync::Arc,
sync::{Arc, LazyLock},
};

use aya_obj::{
Expand Down Expand Up @@ -70,9 +70,7 @@ unsafe impl<T: Pod, const N: usize> Pod for [T; N] {}

pub use aya_obj::maps::{bpf_map_def, PinningType};

lazy_static::lazy_static! {
pub(crate) static ref FEATURES: Features = detect_features();
}
pub(crate) static FEATURES: LazyLock<Features> = LazyLock::new(detect_features);

fn detect_features() -> Features {
let btf = if is_btf_supported() {
Expand Down
8 changes: 3 additions & 5 deletions aya/src/programs/uprobe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::{
mem,
os::{fd::AsFd as _, raw::c_char, unix::ffi::OsStrExt},
path::{Path, PathBuf},
sync::Arc,
sync::{Arc, LazyLock},
};

use libc::pid_t;
Expand All @@ -29,10 +29,8 @@ use crate::{

const LD_SO_CACHE_FILE: &str = "/etc/ld.so.cache";

lazy_static::lazy_static! {
static ref LD_SO_CACHE: Result<LdSoCache, Arc<io::Error>> =
LdSoCache::load(LD_SO_CACHE_FILE).map_err(Arc::new);
}
static LD_SO_CACHE: LazyLock<Result<LdSoCache, Arc<io::Error>>> =
LazyLock::new(|| LdSoCache::load(LD_SO_CACHE_FILE).map_err(Arc::new));
const LD_SO_CACHE_HEADER_OLD: &str = "ld.so-1.7.0\0";
const LD_SO_CACHE_HEADER_NEW: &str = "glibc-ld.so.cache1.1";

Expand Down
40 changes: 20 additions & 20 deletions aya/src/programs/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::{
io::{self, BufRead, BufReader},
os::fd::{AsFd as _, AsRawFd as _, BorrowedFd},
path::Path,
sync::LazyLock,
time::{Duration, SystemTime, UNIX_EPOCH},
};

Expand All @@ -31,27 +32,26 @@ pub(crate) fn attach_raw_tracepoint<T: Link + From<FdLink>>(

/// Find tracefs filesystem path.
pub(crate) fn find_tracefs_path() -> Result<&'static Path, ProgramError> {
lazy_static::lazy_static! {
static ref TRACE_FS: Option<&'static Path> = {
let known_mounts = [
Path::new("/sys/kernel/tracing"),
Path::new("/sys/kernel/debug/tracing"),
];

for mount in known_mounts {
// Check that the mount point exists and is not empty
// Documented here: (https://www.kernel.org/doc/Documentation/trace/ftrace.txt)
// In some cases, tracefs will only mount at /sys/kernel/debug/tracing
// but, the kernel will still create the directory /sys/kernel/tracing.
// The user may be expected to manually mount the directory in order for it to
// exist in /sys/kernel/tracing according to the documentation.
if mount.exists() && mount.read_dir().ok()?.next().is_some() {
return Some(mount);
static TRACE_FS: LazyLock<Option<&'static Path>> = LazyLock::new(|| {
[
Path::new("/sys/kernel/tracing"),
Path::new("/sys/kernel/debug/tracing"),
]
.into_iter()
.find(|&mount| {
// Check that the mount point exists and is not empty
// Documented here: (https://www.kernel.org/doc/Documentation/trace/ftrace.txt)
// In some cases, tracefs will only mount at /sys/kernel/debug/tracing
// but, the kernel will still create the directory /sys/kernel/tracing.
// The user may be expected to manually mount the directory in order for it to
// exist in /sys/kernel/tracing according to the documentation.
mount.exists()
&& match mount.read_dir() {
Ok(mut entries) => entries.next().is_some(),
Err(io::Error { .. }) => false,
}
}
None
};
}
})
});

TRACE_FS
.as_deref()
Expand Down

0 comments on commit 2b299d4

Please sign in to comment.