Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ winapi = { version = "0.3.9", features = ["psapi", "processthreadsapi"] }
procfs = "0.9.0"
libc = "0.2.72"

[target.'cfg(target_os = "freebsd")'.dependencies]
bsd-kvm = "0.1.5"
bsd-kvm-sys = "0.2.0"
sysinfo = "0.35.1"
libc = "0.2.72"

[target.'cfg(target_os = "macos")'.dependencies]
darwin-libproc = "0.2.0"
libc = "0.2.72"
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ println!("{:?}", process_stats);

On Linux, this library reads `/proc/self/stat` and uses the `sysconf` libc function.

On FreeBSD, this library uses getkerninfo().

On Windows, the library uses `GetCurrentProcess` combined with `GetProcessTimes` and `K32GetProcessMemoryInfo`.

On macOS, this library uses `proc_pidinfo` from `libproc` (and current process ID is determined via `libc`).
52 changes: 52 additions & 0 deletions src/freebsd.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
use crate::{Error, ProcessStats};
use std::time::Duration;
use sysinfo::{get_current_pid, Pid, System};

pub fn get_info() -> Result<ProcessStats, Error> {
let pid = match get_current_pid() {
Ok(pid) => { pid }
Err(e) => { panic!("failed to get current pid: {}", e) }
};

let s = System::new_all();
if let Some(p) = s.process(Pid::from(pid)) {
let memory_usage_bytes = p.memory();
let user_mode_seconds = p.run_time();
let kernel_mode_seconds = p.run_time();

Ok(ProcessStats {
cpu_time_user: Duration::from_secs_f64(user_mode_seconds as f64),
cpu_time_kernel: Duration::from_secs_f64(kernel_mode_seconds as f64),
memory_usage_bytes,
})
} else {
Ok(
ProcessStats {
cpu_time_user: Duration::from_secs_f64(0 as f64),
cpu_time_kernel: Duration::from_secs_f64(0 as f64),
memory_usage_bytes: 0,
}
)
}
}

#[cfg(test)]
pub mod tests {
use crate::freebsd;

#[test]
pub fn test_no_error() {
#[no_mangle]
fn spin_for_a_bit() {
let mut _a = 0;
for _i in 0..9999999 {
_a += 1;
}
}

// to get some nonzero number for cpu_time_user
spin_for_a_bit();

dbg!(freebsd::get_info().unwrap());
}
}
10 changes: 9 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

//! A small library to get memory usage and elapsed CPU time.
//!
//! Supports Windows, Linux and macOS.
//! Supports Windows, Linux, FreeBSD and macOS.
//!
//! ```rust
//! use simple_process_stats::ProcessStats;
Expand Down Expand Up @@ -31,6 +31,8 @@ mod linux;
mod macos;
#[cfg(target_os = "windows")]
mod windows;
#[cfg(target_os = "freebsd")]
mod freebsd;

use std::path::PathBuf;
use std::time::Duration;
Expand Down Expand Up @@ -65,6 +67,12 @@ impl ProcessStats {
pub fn get() -> Result<ProcessStats, Error> {
macos::get_info()
}

/// Get the statistics using the OS-specific method.
#[cfg(target_os = "freebsd")]
pub fn get() -> Result<ProcessStats, Error> {
freebsd::get_info()
}
}

/// An error that occurred while trying to get the process stats.
Expand Down
Loading