Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FreeBSD support #10

Merged
merged 1 commit into from
Jun 26, 2024
Merged
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
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ serde = { version = "1.0", optional = true, features = ["derive"] }
[target.'cfg(target_os = "windows")'.dependencies]
winapi = { version = "0.3.9", features = ["minwindef", "processthreadsapi", "psapi"] }

[target.'cfg(any(target_os = "linux", target_os = "android", target_os = "macos", target_os = "ios"))'.dependencies]
[target.'cfg(any(target_os = "linux", target_os = "android", target_os = "macos", target_os = "ios", target_os = "freebsd"))'.dependencies]
libc = "0.2"

[features]
Expand Down
44 changes: 44 additions & 0 deletions src/freebsd.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
use std::ffi::c_void;
use std::sync::atomic::Ordering;

use crate::MemoryStats;

#[path = "page_size.rs"]
mod page_size;

#[link(name = "util")]
extern "C" {
fn kinfo_getproc(pid: libc::pid_t) -> *mut libc::kinfo_proc;
}

pub fn memory_stats() -> Option<MemoryStats> {
struct FreeLater<T> {
p: *mut T,
}

impl<T> Drop for FreeLater<T> {
fn drop(&mut self) {
if !self.p.is_null() {
unsafe { libc::free(self.p as *mut c_void) };
}
}
}

page_size::load_page_size()?;

let info_ptr = FreeLater {
p: unsafe { kinfo_getproc(libc::getpid()) },
};

if info_ptr.p.is_null() {
None
} else {
// SAFETY: ptr is not null
let info = unsafe { info_ptr.p.read() };
let page_size = page_size::PAGE_SIZE.load(Ordering::Relaxed);
Some(MemoryStats {
physical_mem: (info.ki_rssize as usize) * page_size,
virtual_mem: info.ki_size,
})
}
}
5 changes: 5 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,17 @@ mod platform;
#[path = "darwin.rs"]
mod platform;

#[cfg(target_os = "freebsd")]
#[path = "freebsd.rs"]
mod platform;

#[cfg(not(any(
target_os = "windows",
target_os = "linux",
target_os = "android",
target_os = "macos",
target_os = "ios",
target_os = "freebsd",
)))]
mod platform {
use crate::MemoryStats;
Expand Down
26 changes: 7 additions & 19 deletions src/linux.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
use std::fs;
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
use std::sync::atomic::{AtomicBool, Ordering};

use crate::MemoryStats;

#[path = "page_size.rs"]
mod page_size;

#[cfg(not(feature = "always_use_statm"))]
const SMAPS: &str = "/proc/self/smaps";
const STATM: &str = "/proc/self/statm";
Expand All @@ -11,7 +14,6 @@ const STATM: &str = "/proc/self/statm";
static SMAPS_CHECKED: AtomicBool = AtomicBool::new(false);
#[cfg(not(feature = "always_use_statm"))]
static SMAPS_EXIST: AtomicBool = AtomicBool::new(false);
static PAGE_SIZE: AtomicUsize = AtomicUsize::new(0);

pub fn memory_stats() -> Option<MemoryStats> {
// If possible, we try to use /proc/self/smaps to retrieve
Expand All @@ -20,14 +22,14 @@ pub fn memory_stats() -> Option<MemoryStats> {
// as a fallback in case smaps isn't avaliable.

#[cfg(feature = "always_use_statm")]
load_page_size()?;
page_size::load_page_size()?;

#[cfg(not(feature = "always_use_statm"))]
if let Ok(false) = SMAPS_CHECKED.compare_exchange(false, true, Ordering::Relaxed, Ordering::Relaxed) {
let smaps_exist = fs::metadata(SMAPS).is_ok();

if !smaps_exist {
load_page_size()?;
page_size::load_page_size()?;
}

// store SMAPS_EXIST last to prevent code from loading a PAGE_SIZE of 0
Expand Down Expand Up @@ -71,7 +73,7 @@ pub fn memory_stats() -> Option<MemoryStats> {
// multiples of the page size, as the first
// two columns of output.

let page_size = PAGE_SIZE.load(Ordering::Relaxed);
let page_size = page_size::PAGE_SIZE.load(Ordering::Relaxed);
let (total_size_pages, idx) = scan_int(&statm_info);
let (total_rss_pages, _) = scan_int(&statm_info[idx..]);
Some(MemoryStats {
Expand All @@ -83,20 +85,6 @@ pub fn memory_stats() -> Option<MemoryStats> {
}
}

/// Grabs the value of the SC_PAGESIZE if needed.
fn load_page_size() -> Option<()> {
if PAGE_SIZE.load(Ordering::Relaxed) == 0 {
let page_size = unsafe { libc::sysconf(libc::_SC_PAGESIZE) };
if page_size == -1 {
// sysconf returned error
return None;
} else {
PAGE_SIZE.store(page_size as usize, Ordering::Relaxed);
}
}
Some(())
}

/// Extracts a positive integer from a string that
/// may contain leading spaces and trailing chars.
/// Returns the extracted number and the index of
Expand Down
17 changes: 17 additions & 0 deletions src/page_size.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use std::sync::atomic::{AtomicUsize, Ordering};

pub static PAGE_SIZE: AtomicUsize = AtomicUsize::new(0);

/// Grabs the value of the SC_PAGESIZE if needed.
pub fn load_page_size() -> Option<()> {
if PAGE_SIZE.load(Ordering::Relaxed) == 0 {
let page_size = unsafe { libc::sysconf(libc::_SC_PAGESIZE) };
if page_size == -1 {
// sysconf returned error
return None;
} else {
PAGE_SIZE.store(page_size as usize, Ordering::Relaxed);
}
}
Some(())
}
Loading