This repository has been archived by the owner on Apr 8, 2020. It is now read-only.
forked from FillZpp/sys-info-rs
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlib.rs
280 lines (237 loc) · 7.49 KB
/
lib.rs
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
//!
//! This crate focuses on getting system information.
//! For now it supports Linux, Android, Windows, FreeBSD and MacOS.
//!
#[macro_use] extern crate failure;
use std::{
os::raw::c_char,
ffi::CStr,
};
#[repr(C)]
#[derive(Debug)]
pub struct LoadAverage {
pub one: f64,
pub five: f64,
pub fifteen: f64,
}
#[repr(C)]
#[derive(Debug)]
pub struct MemoryInfo {
pub free: u64,
pub total: u64,
}
#[repr(C)]
#[derive(Debug)]
pub struct SwapInfo {
pub free: u64,
pub total: u64,
}
#[derive(Fail, Debug)]
pub enum Error {
#[fail(display = "Unsupported system")]
UnsupportedSystem,
#[fail(display = "System code: {}", _0)]
SystemCode(i32),
}
macro_rules! error_if_unsupported {
() => {
if
!cfg!(target_os = "linux") &&
!cfg!(target_os = "android") &&
!cfg!(target_os = "windows") &&
!cfg!(target_os = "freebsd") &&
!cfg!(target_os = "macos") &&
!cfg!(target_os = "ios")
{
return Err(Error::UnsupportedSystem);
}
}
}
extern "C" {
fn get_os_type(buf: *mut c_char, size: usize) -> i32;
fn get_os_release(buf: *mut c_char, size: usize) -> i32;
fn get_uptime(value: *mut i32) -> i32;
fn get_hostname(buf: *mut c_char, size: usize) -> i32;
fn get_cpu_core_count(value: *mut i32) -> i32;
fn get_cpu_speed(value: *mut i32) -> i32;
fn get_cpu_load_average(value: &LoadAverage) -> i32;
fn get_process_count(value: *mut i32) -> i32;
fn get_memory_info(value: &MemoryInfo) -> i32;
fn get_swap_info(value: &SwapInfo) -> i32;
}
pub fn os_type() -> Result<String, Error> {
error_if_unsupported!();
const SIZE: usize = 256;
let mut buf: [c_char; SIZE] = [0; SIZE];
let result = unsafe { get_os_type(&mut buf[0] as *mut c_char, SIZE) };
if result != 0 {
return Err(Error::SystemCode(result));
}
let result = unsafe { CStr::from_ptr(&buf[0] as *const c_char).to_bytes() };
let result = String::from_utf8_lossy(result).into_owned();
Ok(result)
}
pub fn os_release() -> Result<String, Error> {
error_if_unsupported!();
const SIZE: usize = 256;
let mut buf: [c_char; SIZE] = [0; SIZE];
let result = unsafe { get_os_release(&mut buf[0] as *mut c_char, SIZE) };
if result != 0 {
return Err(Error::SystemCode(result));
}
let result = unsafe { CStr::from_ptr(&buf[0] as *const c_char).to_bytes() };
let result = String::from_utf8_lossy(result).into_owned();
Ok(result)
}
pub fn uptime() -> Result<i32, Error> {
error_if_unsupported!();
let mut value = 0;
let result = unsafe { get_uptime(&mut value as *mut i32) };
if result != 0 {
return Err(Error::SystemCode(result));
}
Ok(value)
}
pub fn hostname() -> Result<String, Error> {
error_if_unsupported!();
const SIZE: usize = 256;
let mut buf: [c_char; SIZE] = [0; SIZE];
let result = unsafe { get_hostname(&mut buf[0] as *mut c_char, SIZE) };
if result != 0 {
return Err(Error::SystemCode(result));
}
let result = unsafe { CStr::from_ptr(&buf[0] as *const c_char).to_bytes() };
let result = String::from_utf8_lossy(result).into_owned();
Ok(result)
}
/// Notice, it returns the logical cpu quantity.
pub fn cpu_core_count() -> Result<i32, Error> {
error_if_unsupported!();
let mut value = 0;
let result = unsafe { get_cpu_core_count(&mut value as *mut i32) };
if result != 0 {
return Err(Error::SystemCode(result));
}
Ok(value)
}
/// Get cpu speed in MHz.
pub fn cpu_speed() -> Result<i32, Error> {
error_if_unsupported!();
let mut value = 0;
let result = unsafe { get_cpu_speed(&mut value as *mut i32) };
if result != 0 {
return Err(Error::SystemCode(result));
}
Ok(value)
}
/// Notice, on windows, one/five/fifteen of the LoadAverage returned are the current load in %%.
pub fn cpu_load_average() -> Result<LoadAverage, Error> {
error_if_unsupported!();
let load = LoadAverage {one: 0.0, five: 0.0, fifteen: 0.0};
let result = unsafe { get_cpu_load_average(&load) };
if result != 0 {
return Err(Error::SystemCode(result));
}
Ok(load)
}
pub fn process_count() -> Result<i32, Error> {
error_if_unsupported!();
let mut value = 0;
let result = unsafe { get_process_count(&mut value as *mut i32) };
if result != 0 {
return Err(Error::SystemCode(result));
}
Ok(value)
}
pub fn memory_info() -> Result<MemoryInfo, Error> {
error_if_unsupported!();
let info = MemoryInfo {free: 0, total: 0};
let result = unsafe { get_memory_info(&info) };
if result != 0 {
return Err(Error::SystemCode(result));
}
Ok(info)
}
pub fn swap_info() -> Result<SwapInfo, Error> {
error_if_unsupported!();
let info = SwapInfo {free: 0, total: 0};
let result = unsafe { get_swap_info(&info) };
if result != 0 {
return Err(Error::SystemCode(result));
}
Ok(info)
}
#[cfg(test)]
mod test {
use super::*;
#[test]
pub fn test_os_type() {
let _ = os_type()
.map(|value| assert!(value.len() > 0, "Empty string is very unlikely"))
.map_err(|error| assert!(false, error.to_string()));
}
#[test]
pub fn test_os_release() {
let _ = os_release()
.map(|value| assert!(value.len() > 0, "Empty string is very unlikely"))
.map_err(|error| assert!(false, error.to_string()));
}
#[test]
pub fn test_uptime() {
let _ = uptime()
.map(|value| assert!(value > 0, "Zero is very unlikely"))
.map_err(|error| assert!(false, error.to_string()));
}
#[test]
pub fn test_hostname() {
let _ = hostname()
.map(|value| assert!(value.len() > 0, "Empty string is very unlikely"))
.map_err(|error| assert!(false, error.to_string()));
}
#[test]
pub fn test_cpu_core_count() {
let _ = cpu_core_count()
.map(|value| assert!(value > 0, "Zero is very unlikely"))
.map_err(|error| assert!(false, error.to_string()));
}
#[test]
pub fn test_cpu_speed() {
let _ = cpu_speed()
.map(|value| assert!(value > 0, "Zero is very unlikely"))
.map_err(|error| assert!(false, error.to_string()));
}
#[test]
pub fn test_cpu_load_average() {
let _ = cpu_load_average()
.map(|value| {
assert!(value.one > 0.0, "Zero is very unlikely");
assert!(value.five > 0.0, "Zero is very unlikely");
assert!(value.fifteen > 0.0, "Zero is very unlikely");
})
.map_err(|error| assert!(false, error.to_string()));
}
#[test]
pub fn test_process_count() {
let _ = process_count()
.map(|value| assert!(value > 0, "Zero is very unlikely"))
.map_err(|error| assert!(false, error.to_string()));
}
#[test]
pub fn test_memory_info() {
let _ = memory_info()
.map(|value| {
assert!(value.free > 0, "Zero is very unlikely");
assert!(value.total > 0, "Zero is very unlikely");
})
.map_err(|error| assert!(false, error.to_string()));
}
#[test]
pub fn test_swap_info() {
let _ = swap_info()
.map(|value| {
assert!(value.free > 0, "Zero is very unlikely");
assert!(value.total > 0, "Zero is very unlikely");
})
.map_err(|error| assert!(false, error.to_string()));
}
}