#[cfg(target_os = "linux")]
macro_rules! nth {
($a:expr, $b:expr) => {
match $a.nth($b) {
Some(val) => Ok(val),
None => Err(Error::new(
ErrorKind::Other,
"The fields asked for does not exists",
)),
}
};
}
pub mod cpu;
pub mod disks;
pub mod host;
pub mod memory;
pub mod network;
pub mod virt;
#[cfg(target_os = "macos")]
pub mod macos_binding;
#[cfg(target_os = "macos")]
pub mod macos_utils;
#[cfg(target_os = "macos")]
pub(crate) use macos_binding as binding;
#[cfg(target_os = "macos")]
pub(crate) use macos_utils as utils;
use libc::c_char;
use std::ffi::CStr;
#[cfg(target_os = "linux")]
use std::fs;
use std::io::Error;
#[cfg(target_os = "macos")]
lazy_static::lazy_static! {
static ref PAGE_SIZE: u64 = {
unsafe {
libc::sysconf(libc::_SC_PAGESIZE) as u64
}
};
}
pub fn clock_ticks() -> Result<u64, Error> {
let result = unsafe { libc::sysconf(libc::_SC_CLK_TCK) };
if result > 0 {
Ok(result as u64)
} else {
Err(Error::last_os_error())
}
}
lazy_static::lazy_static! {
pub(crate) static ref CLOCK_TICKS: u64 = clock_ticks().expect("Unable to determine CPU number of ticks per second");
}
#[cfg(target_os = "linux")]
pub(crate) fn read_and_trim<P>(path: P) -> Result<String, Error>
where
P: AsRef<std::path::Path>,
{
let content = fs::read_to_string(path)?;
Ok(content.trim().to_owned())
}
#[inline]
pub(crate) fn to_str<'a>(s: *const c_char) -> &'a str {
unsafe {
let res = CStr::from_ptr(s).to_bytes();
std::str::from_utf8_unchecked(res)
}
}