1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
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(path: &str) -> Result<String, Error> {
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)
}
}