sys: remove lazy_static dependency

Signed-off-by: Maximiliano Sandoval <m.sandoval@proxmox.com>
This commit is contained in:
Maximiliano Sandoval 2024-08-14 09:19:56 +02:00 committed by Wolfgang Bumiller
parent 669c39c59f
commit 8d5e864bf1
3 changed files with 19 additions and 25 deletions

View File

@ -6,13 +6,13 @@ edition.workspace = true
license.workspace = true license.workspace = true
repository.workspace = true repository.workspace = true
description = "System tools (using nix)." description = "System tools (using nix)."
rust-version.workspace = true
exclude.workspace = true exclude.workspace = true
[dependencies] [dependencies]
anyhow.workspace = true anyhow.workspace = true
base64.workspace = true base64.workspace = true
lazy_static.workspace = true
libc.workspace = true libc.workspace = true
log.workspace = true log.workspace = true
nix.workspace = true nix.workspace = true

View File

@ -2,6 +2,7 @@
use std::os::fd::{FromRawFd, OwnedFd}; use std::os::fd::{FromRawFd, OwnedFd};
use std::os::unix::ffi::OsStrExt; use std::os::unix::ffi::OsStrExt;
use std::sync::LazyLock;
pub mod boot_mode; pub mod boot_mode;
pub mod command; pub mod command;
@ -20,21 +21,19 @@ pub mod systemd;
/// Returns the hosts node name (UTS node name) /// Returns the hosts node name (UTS node name)
pub fn nodename() -> &'static str { pub fn nodename() -> &'static str {
lazy_static::lazy_static! { static NODENAME: LazyLock<String> = LazyLock::new(|| {
static ref NODENAME: String = { std::str::from_utf8(
std::str::from_utf8( nix::sys::utsname::uname()
nix::sys::utsname::uname() .expect("failed to get nodename")
.expect("failed to get nodename") .nodename()
.nodename() .as_bytes(),
.as_bytes(), )
) .expect("non utf-8 nodename not supported")
.expect("non utf-8 nodename not supported") .split('.')
.split('.') .next()
.next() .unwrap()
.unwrap() .to_owned()
.to_owned() });
};
}
&NODENAME &NODENAME
} }

View File

@ -5,11 +5,10 @@ use std::fs::OpenOptions;
use std::io::{BufRead, BufReader}; use std::io::{BufRead, BufReader};
use std::net::{Ipv4Addr, Ipv6Addr}; use std::net::{Ipv4Addr, Ipv6Addr};
use std::str::FromStr; use std::str::FromStr;
use std::sync::RwLock; use std::sync::{LazyLock, RwLock};
use std::time::Instant; use std::time::Instant;
use anyhow::{bail, format_err, Error}; use anyhow::{bail, format_err, Error};
use lazy_static::lazy_static;
use nix::unistd::Pid; use nix::unistd::Pid;
use serde::Serialize; use serde::Serialize;
@ -27,9 +26,7 @@ pub fn sysconf(name: i32) -> i64 {
unsafe { sysconf(name) } unsafe { sysconf(name) }
} }
lazy_static! { pub static CLOCK_TICKS: LazyLock<f64> = LazyLock::new(|| sysconf(libc::_SC_CLK_TCK) as f64);
pub static ref CLOCK_TICKS: f64 = sysconf(libc::_SC_CLK_TCK) as f64;
}
/// Selected contents of the `/proc/PID/stat` file. /// Selected contents of the `/proc/PID/stat` file.
pub struct PidStat { pub struct PidStat {
@ -223,10 +220,8 @@ pub struct ProcFsStat {
pub iowait_percent: f64, pub iowait_percent: f64,
} }
lazy_static! { static PROC_LAST_STAT: LazyLock<RwLock<(ProcFsStat, Instant, bool)>> =
static ref PROC_LAST_STAT: RwLock<(ProcFsStat, Instant, bool)> = LazyLock::new(|| RwLock::new((ProcFsStat::default(), Instant::now(), true)));
RwLock::new((ProcFsStat::default(), Instant::now(), true));
}
/// reads `/proc/stat`. For now only total host CPU usage is handled as the /// reads `/proc/stat`. For now only total host CPU usage is handled as the
/// other metrics are not really interesting /// other metrics are not really interesting