6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 1 | use std::{ffi::CString, os::unix::ffi::OsStrExt, path::Path}; |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 2 | |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 3 | use crate::{sched::FsId, units::Bytes}; |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 4 | |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 5 | pub fn cores() -> u32 { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 6 | std::thread::available_parallelism().map_or(1, |n| n.get() as u32) |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 7 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 8 | |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 9 | pub struct FsStat { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 10 | pub id: FsId, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 11 | pub free: Bytes, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 12 | pub size: Bytes, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 13 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 14 | |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 15 | pub fn fs(path: &Path) -> Option<FsStat> { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 16 | let c = CString::new(path.as_os_str().as_bytes()).ok()?; |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 17 | let mut vfs = std::mem::MaybeUninit::<libc::statvfs>::uninit(); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 18 | let mut st = std::mem::MaybeUninit::<libc::stat>::uninit(); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 19 | unsafe { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 20 | if libc::statvfs(c.as_ptr(), vfs.as_mut_ptr()) != 0 |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 21 | || libc::stat(c.as_ptr(), st.as_mut_ptr()) != 0 |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 22 | { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 23 | return None; |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 24 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 25 | let vfs = vfs.assume_init(); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 26 | let st = st.assume_init(); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 27 | #[allow(clippy::unnecessary_cast)] |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 28 | let block = vfs.f_frsize as u64; |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 29 | #[allow(clippy::unnecessary_cast)] |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 30 | Some(FsStat { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 31 | id: st.st_dev as u64, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 32 | free: Bytes(vfs.f_bavail as u64 * block), |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 33 | size: Bytes(vfs.f_blocks as u64 * block), |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 34 | }) |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 35 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 36 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 37 | |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 38 | pub fn page_size() -> u64 { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 39 | unsafe { libc::sysconf(libc::_SC_PAGESIZE) as u64 } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 40 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 41 | |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 42 | #[cfg(target_os = "linux")] |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 43 | mod imp { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 44 | use std::fs; |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 45 | |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 46 | use crate::units::Bytes; |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 47 | |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 48 | pub fn memory() -> (Bytes, Bytes) { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 49 | let text = fs::read_to_string("/proc/meminfo").unwrap_or_default(); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 50 | let field = |name: &str| { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 51 | text.lines() |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 52 | .find_map(|l| l.strip_prefix(name)) |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 53 | .and_then(|rest| { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 54 | rest.trim() |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 55 | .trim_end_matches("kB") |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 56 | .trim() |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 57 | .parse::<u64>() |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 58 | .ok() |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 59 | }) |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 60 | .map_or(Bytes::ZERO, |kib| Bytes(kib * 1024)) |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 61 | }; |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 62 | (field("MemTotal:"), field("MemAvailable:")) |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 63 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 64 | |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 65 | pub fn pressure() -> Option<f64> { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 66 | let text = fs::read_to_string("/proc/pressure/memory").ok()?; |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 67 | text.lines() |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 68 | .find(|l| l.starts_with("some"))? |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 69 | .split_whitespace() |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 70 | .find_map(|kv| kv.strip_prefix("avg10="))? |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 71 | .parse() |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 72 | .ok() |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 73 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 74 | |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 75 | pub fn group_rss(pgid: i32) -> Bytes { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 76 | let page = super::page_size(); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 77 | let Ok(dir) = fs::read_dir("/proc") else { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 78 | return Bytes::ZERO; |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 79 | }; |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 80 | let mut total = 0; |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 81 | for entry in dir.flatten() { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 82 | let name = entry.file_name(); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 83 | if !name.as_encoded_bytes().iter().all(u8::is_ascii_digit) { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 84 | continue; |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 85 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 86 | let Ok(stat) = fs::read_to_string(entry.path().join("stat")) else { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 87 | continue; |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 88 | }; |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 89 | // comm can hold spaces and parens, fields only make sense after the last ')' |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 90 | let Some(tail) = stat.rsplit_once(')').map(|(_, t)| t) else { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 91 | continue; |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 92 | }; |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 93 | let fields: Vec<&str> = tail.split_whitespace().collect(); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 94 | let pgrp = fields.get(2).and_then(|f| f.parse::<i32>().ok()); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 95 | let rss = fields.get(21).and_then(|f| f.parse::<u64>().ok()); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 96 | if pgrp == Some(pgid) { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 97 | total += rss.unwrap_or(0) * page; |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 98 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 99 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 100 | Bytes(total) |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 101 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 102 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 103 | |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 104 | #[cfg(target_os = "macos")] |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 105 | mod imp { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 106 | use std::process::Command; |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 107 | |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 108 | use crate::units::Bytes; |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 109 | |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 110 | pub fn memory() -> (Bytes, Bytes) { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 111 | let mut total: u64 = 0; |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 112 | let mut len = std::mem::size_of::<u64>(); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 113 | unsafe { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 114 | libc::sysctlbyname( |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 115 | c"hw.memsize".as_ptr(), |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 116 | (&raw mut total).cast(), |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 117 | &raw mut len, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 118 | std::ptr::null_mut(), |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 119 | 0, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 120 | ); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 121 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 122 | // vm_stat over mach calls, libc's mach bindings are deprecated and this |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 123 | // runs once a second at most |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 124 | let out = Command::new("vm_stat") |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 125 | .output() |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 126 | .map(|o| o.stdout) |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 127 | .unwrap_or_default(); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 128 | let text = String::from_utf8_lossy(&out); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 129 | let pages = |name: &str| { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 130 | text.lines() |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 131 | .find_map(|l| l.strip_prefix(name)) |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 132 | .and_then(|r| r.trim().trim_end_matches('.').parse::<u64>().ok()) |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 133 | .unwrap_or(0) |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 134 | }; |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 135 | let avail = (pages("Pages free:") + pages("Pages inactive:") + pages("Pages purgeable:")) |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 136 | * super::page_size(); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 137 | (Bytes(total), Bytes(avail)) |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 138 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 139 | |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 140 | pub const fn pressure() -> Option<f64> { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 141 | None |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 142 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 143 | |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 144 | pub fn group_rss(pgid: i32) -> Bytes { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 145 | let out = Command::new("ps") |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 146 | .args(["-A", "-o", "pgid=,rss="]) |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 147 | .output() |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 148 | .map(|o| o.stdout) |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 149 | .unwrap_or_default(); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 150 | let kib: u64 = String::from_utf8_lossy(&out) |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 151 | .lines() |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 152 | .filter_map(|l| { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 153 | let mut f = l.split_whitespace(); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 154 | let g = f.next()?.parse::<i32>().ok()?; |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 155 | let rss = f.next()?.parse::<u64>().ok()?; |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 156 | (g == pgid).then_some(rss) |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 157 | }) |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 158 | .sum(); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 159 | Bytes(kib * 1024) |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 160 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 161 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 162 | |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 163 | pub use imp::{group_rss, memory, pressure}; |