Crates.io | sigar-sys |
lib.rs | sigar-sys |
version | 1.0.3 |
source | src |
created_at | 2018-10-31 08:41:43.686277 |
updated_at | 2018-11-03 07:47:28.92482 |
description | Low level bindings to sigar. |
homepage | https://github.com/dtynn/sigar-rs/tree/master/sigar-sys |
repository | https://github.com/dtynn/sigar-rs |
max_upload_size | |
id | 93772 |
size | 1,080,683 |
low level binding to AlexYaruki/sigar, which is a independent fork version of official hyperic/sigar.
Add this to your Cargo.toml
[dependencies]
sigar-sys = "1"
and this to your crate root:
extern crate sigar_sys;
As a -sys
crate, this provides only the binding to sigar.
The sigar documentation itself is at SourceForge/sigar.
The build script for this crate always makes a static linking.
This crate has been tested to work on Windows(with msvc), Linux and macOS.
Other platforms or envs may work, and PRs to support them are welcome.
extern crate sigar_sys;
use sigar_sys as ffi;
use std::slice::from_raw_parts;
fn main() {
let usage = unsafe { cpuusage().unwrap() };
println!("cpu usage: {:?}", usage);
let pids = unsafe { pidlist().unwrap() };
println!("pids: {:?}", pids);
}
unsafe fn cpuusage() -> Result<ffi::sigar_cpu_t, String> {
let mut sigar_ptr: *mut ffi::sigar_t = std::ptr::null_mut();
let res = ffi::sigar_open(&mut sigar_ptr);
if res != ffi::SIGAR_CODE_OK {
return Err(ffi::error_string(sigar_ptr, res));
}
let mut info: ffi::sigar_cpu_t = Default::default();
let res = ffi::sigar_cpu_get(sigar_ptr, &mut info);
if res != ffi::SIGAR_CODE_OK {
return Err(ffi::error_string(sigar_ptr, res));
}
let res = ffi::sigar_close(sigar_ptr);
if res != ffi::SIGAR_CODE_OK {
return Err("failed to close sigar_t".to_string());
}
Ok(info)
}
unsafe fn pidlist() -> Result<Vec<ffi::sigar_pid_t>, String> {
let mut sigar_ptr: *mut ffi::sigar_t = std::ptr::null_mut();
let res = ffi::sigar_open(&mut sigar_ptr);
if res != ffi::SIGAR_CODE_OK {
return Err(ffi::error_string(sigar_ptr, res));
}
let mut raw: ffi::sigar_proc_list_t = Default::default();
let res = ffi::sigar_proc_list_get(sigar_ptr, &mut raw);
if res != ffi::SIGAR_CODE_OK {
return Err(ffi::error_string(sigar_ptr, res));
}
let infos = from_raw_parts(raw.data, raw.number as usize)
.to_vec()
.clone();
let res = ffi::sigar_proc_list_destroy(sigar_ptr, &mut raw);
if res != ffi::SIGAR_CODE_OK {
return Err("failed to destroy sigar_t".to_string());
}
let res = ffi::sigar_close(sigar_ptr);
if res != ffi::SIGAR_CODE_OK {
return Err("failed to close sigar_t".to_string());
}
Ok(infos)
}
to run the example code on windows, we should link 3 more libs: ws2_32
netapi32
, version
, maybe in the build scripts