#![allow(dead_code)]
#![allow(unused_imports)]
extern crate libc;
extern crate sysctl;
use sysctl::Sysctl;
#[derive(Debug)]
#[repr(C)]
struct ClockInfo {
hz: libc::c_int,
tick: libc::c_int,
spare: libc::c_int,
stathz: libc::c_int,
profhz: libc::c_int,
}
#[repr(C)]
struct LoadAvg {
ldavg: [u32; 3],
fscale: u64,
}
impl std::fmt::Debug for LoadAvg {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
let s = self.fscale as f32;
write!(
f,
"LoadAvg {{ {:.2} {:.2} {:.2} }}",
self.ldavg[0] as f32 / s,
self.ldavg[1] as f32 / s,
self.ldavg[2] as f32 / s
)
}
}
#[cfg(any(target_os = "macos", target_os = "freebsd"))]
fn main() {
println!("Read sysctl kern.clockrate as struct directly");
let val: Box<ClockInfo> = sysctl::Ctl::new("kern.clockrate")
.expect("could not get sysctl: kern.clockrate")
.value_as()
.expect("could not read sysctl as struct");
println!("{:?}", val);
println!();
println!("Read sysctl vm.loadavg as struct directly");
let val = sysctl::Ctl::new("vm.loadavg")
.expect("could not get sysctl: vm.loadavg")
.value_as::<LoadAvg>()
.expect("could not read sysctl as LoadAvg");
println!("{:?}", val);
}
#[cfg(any(target_os = "linux", target_os = "android"))]
fn main() {
println!("This operation is not supported on Linux.");
}