use std::collections::HashMap;
#[cfg(target_os = "linux")]
use std::fs;
use std::time::{Duration, Instant};
use libc::geteuid;
use crate::collect::udp::UdpDelta;
#[derive(Clone, Copy, PartialEq, Eq)]
pub enum ProcSort {
Name,
NetSum,
NetRate,
DiskSum,
DiskRate,
}
impl ProcSort {
pub fn next(self) -> Self {
match self {
Self::Name => Self::NetSum,
Self::NetSum => Self::NetRate,
Self::NetRate => Self::DiskSum,
Self::DiskSum => Self::DiskRate,
Self::DiskRate => Self::Name,
}
}
pub fn label(self) -> &'static str {
match self {
Self::Name => "name",
Self::NetSum => "net-sum",
Self::NetRate => "net-rate",
Self::DiskSum => "disk-sum",
Self::DiskRate => "disk-rate",
}
}
fn sort_key(&self, r: &ProcRow) -> (u64, f64) {
match self {
Self::Name => (0, 0.0),
Self::NetSum => (r.net_recv + r.net_sent, 0.0),
Self::NetRate => (0, r.net_rr + r.net_sr),
Self::DiskSum => (r.disk_r + r.disk_w, 0.0),
Self::DiskRate => (0, r.disk_rr + r.disk_wr),
}
}
}
#[derive(Clone)]
pub struct ProcRow {
pub pid: u32,
pub name: String,
pub net_recv: u64,
pub net_sent: u64,
pub net_rr: f64,
pub net_sr: f64,
pub udp_recv: u64,
pub udp_sent: u64,
pub udp_rr: f64,
pub udp_sr: f64,
pub disk_r: u64,
pub disk_w: u64,
pub disk_rr: f64,
pub disk_wr: f64,
pub uptime_s: Option<u64>,
pub alive: bool,
pub cmdline: Option<Vec<String>>,
}
impl ProcRow {
pub fn total_rate(&self) -> f64 {
self.net_rr + self.net_sr + self.disk_rr + self.disk_wr
}
pub fn total_bytes(&self) -> u64 {
self.net_recv + self.net_sent + self.disk_r + self.disk_w
}
}
#[derive(Clone)]
struct ProcState {
name: String,
net_recv: u64,
net_sent: u64,
udp_recv: u64,
udp_sent: u64,
disk_r: u64,
disk_w: u64,
start_ticks: Option<u64>,
cmdline: Option<Vec<String>>,
uptime_s: Option<u64>,
last_alive: Option<Instant>,
}
impl ProcState {
fn zero() -> Self {
Self {
name: String::new(),
net_recv: 0,
net_sent: 0,
udp_recv: 0,
udp_sent: 0,
disk_r: 0,
disk_w: 0,
start_ticks: None,
cmdline: None,
uptime_s: None,
last_alive: None,
}
}
#[cfg(target_os = "linux")]
fn first_sight(pid: u32) -> Self {
let mut s = Self::zero();
s.start_ticks = read_pid_start(pid);
s.cmdline = read_pid_cmdline(pid);
s.uptime_s = read_pid_uptime_s(pid);
s.last_alive = Some(Instant::now());
s
}
#[cfg(target_os = "macos")]
fn first_sight_mac(pid: u32, start_epoch: Option<i64>) -> Self {
let mut s = Self::zero();
s.start_ticks = start_epoch.map(|e| e.max(0) as u64);
s.cmdline = crate::collect::macproc::cmdline(pid);
s.last_alive = Some(Instant::now());
s.uptime_s = start_epoch.and_then(|e| {
let now = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.ok()?
.as_secs() as i64;
Some((now - e).max(0) as u64)
});
s
}
}
pub struct ProcCollector {
acc: HashMap<u32, ProcState>,
io_prev: HashMap<u32, (u64, u64)>,
udp_names: HashMap<u32, String>,
#[cfg(target_os = "macos")]
mac_comms: HashMap<u32, String>,
#[cfg(target_os = "macos")]
mac_starts: HashMap<u32, i64>,
last_t: Instant,
pub procs: Vec<ProcRow>,
pub sort: ProcSort,
pub exit_retention: Duration,
pub is_root: bool,
}
impl Default for ProcCollector {
fn default() -> Self {
Self::new()
}
}
impl ProcCollector {
pub fn new() -> Self {
Self {
acc: HashMap::new(),
io_prev: HashMap::new(),
udp_names: HashMap::new(),
#[cfg(target_os = "macos")]
mac_comms: HashMap::new(),
#[cfg(target_os = "macos")]
mac_starts: HashMap::new(),
last_t: Instant::now(),
procs: Vec::new(),
sort: ProcSort::NetSum,
exit_retention: Duration::from_secs(30 * 60),
is_root: unsafe { geteuid() == 0 },
}
}
pub fn sample(&mut self, net: &[UdpDelta], udp: &[UdpDelta]) {
let now = Instant::now();
let dt = now.duration_since(self.last_t).as_secs_f64().max(1e-6);
self.last_t = now;
#[cfg(target_os = "macos")]
let mac_pids = {
let rows = crate::collect::macproc::list_pids();
self.mac_comms = rows.iter().map(|m| (m.pid, m.comm.clone())).collect();
self.mac_starts = rows.iter().map(|m| (m.pid, m.start_epoch)).collect();
rows
};
let mut net_rates: HashMap<u32, (f64, f64)> = HashMap::new();
#[cfg(target_os = "linux")]
let mut start_cache: HashMap<u32, Option<u64>> = HashMap::new();
for d in net {
let name = self.udp_name(d.pid);
#[cfg(target_os = "linux")]
let cur_start = cached_start(&mut start_cache, d.pid);
#[cfg(target_os = "macos")]
let cur_start = self.mac_starts.get(&d.pid).map(|&e| e.max(0) as u64);
let st = self.ledger_entry(d.pid, &name, cur_start);
st.net_recv += d.recv;
st.net_sent += d.sent;
let r = net_rates.entry(d.pid).or_insert((0.0, 0.0));
r.0 += d.recv as f64 / d.dt;
r.1 += d.sent as f64 / d.dt;
}
let mut io_rates: HashMap<u32, (f64, f64)> = HashMap::new();
let mut pids_now: HashMap<u32, u64> = HashMap::new();
#[cfg(target_os = "linux")]
{
if let Ok(pids) = fs::read_dir("/proc") {
for p in pids.flatten() {
let Ok(pid) = p.file_name().to_string_lossy().parse::<u32>() else {
continue;
};
let mut start = None;
if let Some(st) = read_pid_start(pid) {
pids_now.insert(pid, st);
start = Some(st);
}
let (Some(rb), Some(wb), Some(comm)) = read_pid_io(&p.path()) else {
continue;
};
let (dr, dw) = match self.io_prev.get(&pid) {
Some(&(pr, pw)) => (rb.saturating_sub(pr), wb.saturating_sub(pw)),
None => (0, 0),
};
self.io_prev.insert(pid, (rb, wb));
io_rates.insert(pid, (dr as f64 / dt, dw as f64 / dt));
if dr > 0 || dw > 0 {
let st = self.ledger_entry(pid, &comm, start);
st.disk_r += dr;
st.disk_w += dw;
}
}
}
}
#[cfg(target_os = "macos")]
{
for m in &mac_pids {
pids_now.insert(m.pid, m.start_epoch.max(0) as u64);
let Some((rb, wb)) = crate::collect::macproc::disk_io(m.pid) else {
continue;
};
let (dr, dw) = match self.io_prev.get(&m.pid) {
Some(&(pr, pw)) => (rb.saturating_sub(pr), wb.saturating_sub(pw)),
None => (0, 0),
};
self.io_prev.insert(m.pid, (rb, wb));
io_rates.insert(m.pid, (dr as f64 / dt, dw as f64 / dt));
if dr > 0 || dw > 0 {
let start = Some(m.start_epoch.max(0) as u64);
let st = self.ledger_entry(m.pid, &m.comm, start);
st.disk_r += dr;
st.disk_w += dw;
}
}
}
let udp_rates: HashMap<u32, (f64, f64)> = if !udp.is_empty() {
let mut udp_rates: HashMap<u32, (f64, f64)> = HashMap::new();
for d in udp {
let name = self.udp_name(d.pid);
#[cfg(target_os = "linux")]
let cur_start = cached_start(&mut start_cache, d.pid);
#[cfg(target_os = "macos")]
let cur_start = self.mac_starts.get(&d.pid).map(|&e| e.max(0) as u64);
let st = self.ledger_entry(d.pid, &name, cur_start);
st.udp_recv += d.recv;
st.udp_sent += d.sent;
let r = udp_rates.entry(d.pid).or_insert((0.0, 0.0));
r.0 += d.recv as f64 / d.dt;
r.1 += d.sent as f64 / d.dt;
}
udp_rates
} else {
HashMap::new()
};
self.evict_dead(&pids_now, now);
self.procs = self
.acc
.iter()
.map(|(pid, st)| {
let (nrr, nsr) = net_rates.get(pid).copied().unwrap_or((0.0, 0.0));
let (urr, usr) = udp_rates.get(pid).copied().unwrap_or((0.0, 0.0));
let (drr, dwr) = io_rates.get(pid).copied().unwrap_or((0.0, 0.0));
let alive = row_alive(st.start_ticks, pids_now.get(pid).copied());
ProcRow {
pid: *pid,
name: st.name.clone(),
net_recv: st.net_recv,
net_sent: st.net_sent,
net_rr: nrr,
net_sr: nsr,
udp_recv: st.udp_recv,
udp_sent: st.udp_sent,
udp_rr: urr,
udp_sr: usr,
disk_r: st.disk_r,
disk_w: st.disk_w,
disk_rr: drr,
disk_wr: dwr,
uptime_s: st.uptime_s,
alive,
cmdline: st.cmdline.clone(),
}
})
.filter(|r| r.total_bytes() > 0 || r.total_rate() > 0.0)
.collect();
match self.sort {
ProcSort::Name => self.procs.sort_by(|a, b| a.name.cmp(&b.name)),
_ => self.procs.sort_by(|a, b| {
let (a1, a2) = self.sort.sort_key(a);
let (b1, b2) = self.sort.sort_key(b);
b1.cmp(&a1)
.then_with(|| b2.partial_cmp(&a2).unwrap_or(std::cmp::Ordering::Equal))
}),
}
}
pub fn reset_net(&mut self) {
for st in self.acc.values_mut() {
st.net_recv = 0;
st.net_sent = 0;
st.udp_recv = 0;
st.udp_sent = 0;
}
}
fn evict_dead(&mut self, pids_now: &HashMap<u32, u64>, now: Instant) {
let retention = self.exit_retention;
self.acc.retain(|pid: &u32, st: &mut ProcState| {
if row_alive(st.start_ticks, pids_now.get(pid).copied()) {
st.last_alive = Some(now);
true
} else {
match st.last_alive {
Some(t) => now.duration_since(t) < retention,
None => true,
}
}
});
self.io_prev.retain(|pid, _| pids_now.contains_key(pid));
}
fn ledger_entry(&mut self, pid: u32, name: &str, cur_start: Option<u64>) -> &mut ProcState {
let stale = match (self.acc.get(&pid).map(|st| st.start_ticks), cur_start) {
(Some(Some(old)), Some(cur)) => old != cur,
_ => false,
};
if stale {
self.acc.remove(&pid);
}
#[cfg(target_os = "linux")]
let st = self
.acc
.entry(pid)
.or_insert_with(|| ProcState::first_sight(pid));
#[cfg(target_os = "macos")]
let st = self.acc.entry(pid).or_insert_with(|| {
let start = self.mac_starts.get(&pid).copied();
ProcState::first_sight_mac(pid, start)
});
if name != "(gone)" || st.name.is_empty() {
st.name = name.to_string();
}
st
}
#[cfg(target_os = "linux")]
fn udp_name(&mut self, pid: u32) -> String {
if let Ok(comm) = fs::read_to_string(format!("/proc/{pid}/comm")) {
let comm = comm.trim().to_string();
if !comm.is_empty() {
if self.udp_names.len() > 8192 {
self.udp_names.clear();
}
self.udp_names.insert(pid, comm.clone());
return comm;
}
}
self.udp_names
.get(&pid)
.cloned()
.unwrap_or_else(|| "(gone)".to_string())
}
#[cfg(target_os = "macos")]
fn udp_name(&mut self, pid: u32) -> String {
if let Some(name) = self.mac_comms.get(&pid).cloned() {
if !name.is_empty() {
if self.udp_names.len() > 8192 {
self.udp_names.clear();
}
self.udp_names.insert(pid, name.clone());
return name;
}
}
self.udp_names
.get(&pid)
.cloned()
.unwrap_or_else(|| "(gone)".to_string())
}
pub fn reset_disk(&mut self) {
for st in self.acc.values_mut() {
st.disk_r = 0;
st.disk_w = 0;
}
}
}
#[cfg(target_os = "linux")]
fn read_pid_io(path: &std::path::Path) -> (Option<u64>, Option<u64>, Option<String>) {
let mut r = None;
let mut w = None;
if let Ok(content) = fs::read_to_string(path.join("io")) {
for line in content.lines() {
if let Some(v) = line.strip_prefix("read_bytes:") {
r = v.trim().parse().ok();
} else if let Some(v) = line.strip_prefix("write_bytes:") {
w = v.trim().parse().ok();
}
}
}
let comm = fs::read_to_string(path.join("comm"))
.ok()
.map(|s| s.trim().to_string());
(r, w, comm)
}
#[cfg(target_os = "linux")]
fn read_pid_uptime_s(pid: u32) -> Option<u64> {
let start_ticks = read_pid_start(pid)?;
let uptime = fs::read_to_string("/proc/uptime").ok()?;
let up = uptime.split_whitespace().next()?.parse::<f64>().ok()?;
let hz = unsafe { libc::sysconf(libc::_SC_CLK_TCK) } as f64;
let age = up - start_ticks as f64 / hz;
Some(age.max(0.0) as u64)
}
#[cfg(target_os = "linux")]
fn cached_start(cache: &mut HashMap<u32, Option<u64>>, pid: u32) -> Option<u64> {
match cache.entry(pid) {
std::collections::hash_map::Entry::Occupied(e) => *e.get(),
std::collections::hash_map::Entry::Vacant(e) => {
let v = read_pid_start(pid);
e.insert(v);
v
}
}
}
#[cfg(target_os = "linux")]
fn read_pid_start(pid: u32) -> Option<u64> {
let stat = fs::read_to_string(format!("/proc/{pid}/stat")).ok()?;
let after = stat.rsplit_once(')')?.1;
after.split_whitespace().nth(19)?.parse().ok()
}
#[cfg(target_os = "linux")]
fn read_pid_cmdline(pid: u32) -> Option<Vec<String>> {
let raw = fs::read_to_string(format!("/proc/{pid}/cmdline")).ok()?;
let parts: Vec<String> = raw
.split('\0')
.filter(|s| !s.is_empty())
.map(String::from)
.collect();
if parts.is_empty() {
None
} else {
Some(parts)
}
}
fn row_alive(start: Option<u64>, now: Option<u64>) -> bool {
match (start, now) {
(Some(s), Some(cur)) => s == cur,
(Some(_), None) => false,
(None, known) => known.is_some(),
}
}
#[cfg(test)]
mod tests {
use super::*;
#[cfg(target_os = "macos")]
use std::fs;
#[test]
fn ledger_none_start_keeps_retained_row() {
let mut c = ProcCollector::new();
let mut st = ProcState::zero();
st.start_ticks = Some(999);
st.net_recv = 5000;
c.acc.insert(424242, st);
let st = c.ledger_entry(424242, "x", None);
assert_eq!(
st.net_recv, 5000,
"退出瞬间的最后 delta 不能清掉生命周期总量"
);
}
#[test]
fn ledger_resets_on_positive_generation_change() {
let mut c = ProcCollector::new();
let mut st = ProcState::zero();
st.start_ticks = Some(1);
st.net_recv = 5000;
c.acc.insert(424242, st);
let st = c.ledger_entry(424242, "newname", Some(2));
assert_eq!(st.net_recv, 0, "世代更替必须清零");
assert_eq!(st.name, "newname");
}
#[cfg(target_os = "linux")]
#[test]
fn rename_follows_and_keeps_ledger() {
let mut c = ProcCollector::new();
let pid = std::process::id();
let comm_path = format!("/proc/{pid}/comm");
let orig = std::fs::read_to_string(&comm_path).unwrap();
let d = UdpDelta {
pid,
recv: 100,
sent: 0,
dt: 1.0,
};
c.sample(&[d], &[d]);
let before = c.procs.iter().find(|r| r.pid == pid).unwrap().net_recv;
std::fs::write(&comm_path, "ltop-renamed").unwrap();
let d = UdpDelta {
pid,
recv: 50,
sent: 0,
dt: 1.0,
};
c.sample(&[d], &[d]);
let row = c.procs.iter().find(|r| r.pid == pid).unwrap();
assert_eq!(row.net_recv, before + 50, "改名不拆行:累计续算");
assert_eq!(row.name, "ltop-renamed", "名字跟随最新 comm");
std::fs::write(&comm_path, orig.trim()).unwrap();
}
#[test]
fn udp_deltas_land_in_the_ledger_under_the_pid_name() {
let mut c = ProcCollector::new();
let pid = std::process::id(); let delta = UdpDelta {
pid,
recv: 1000,
sent: 250,
dt: 1.0,
};
c.sample(&[delta], &[delta]);
let row = c.procs.iter().find(|r| r.pid == pid).expect("udp row");
assert_eq!(row.udp_recv, 1000);
assert_eq!(row.udp_sent, 250);
assert_eq!(row.net_recv, 1000); assert_eq!(row.net_sent, 250);
assert!((row.udp_rr - 1000.0).abs() < 1e-9);
assert!((row.net_sr - 250.0).abs() < 1e-9);
assert!(!row.name.is_empty());
}
#[test]
fn reset_net_clears_udp_totals_too() {
let mut c = ProcCollector::new();
let pid = std::process::id();
let d = UdpDelta {
pid,
recv: 10,
sent: 0,
dt: 1.0,
};
c.sample(&[d], &[d]);
c.reset_net();
let d = UdpDelta {
pid,
recv: 5,
sent: 0,
dt: 1.0,
};
c.sample(&[d], &[d]);
let row = c.procs.iter().find(|r| r.pid == pid).expect("row");
assert_eq!(row.udp_recv, 5);
}
#[test]
fn row_alive_rules() {
assert!(row_alive(Some(7), Some(7))); assert!(!row_alive(Some(7), Some(9))); assert!(!row_alive(Some(7), None)); assert!(row_alive(None, Some(7))); assert!(!row_alive(None, None));
}
#[test]
#[cfg(target_os = "linux")]
fn pid_start_and_cmdline_read_from_proc() {
let pid = std::process::id();
let start = read_pid_start(pid).expect("stat start ticks");
assert!(start > 0);
let cmd = read_pid_cmdline(pid).expect("own cmdline");
assert!(!cmd.is_empty());
assert!(cmd[0].contains("ltop") || cmd[0].contains("dep"));
}
#[test]
fn exited_process_stays_in_the_ledger_marked_dead() {
let tmp = std::env::current_dir()
.expect("cwd")
.join(format!("target/ltop-alive-{}", std::process::id()));
let mut child = std::process::Command::new("sh")
.arg("-c")
.arg(format!(
"for i in 1 2 3 4 5 6 7 8; do echo aaaaaaaaaaaaaaaaaaaaaaaa > {}; sleep 0.15; done",
tmp.display()
))
.spawn()
.expect("spawn writer child");
let pid = child.id();
let mut c = ProcCollector::new();
let mut saw_alive = false;
for _ in 0..12 {
c.sample(&[], &[]);
if let Some(row) = c.procs.iter().find(|r| r.pid == pid) {
saw_alive = saw_alive || row.alive;
if row.alive {
assert!(row
.cmdline
.as_ref()
.is_some_and(|cmd| cmd[0].contains("sh")));
}
}
if child.try_wait().expect("poll").is_some() {
break;
}
std::thread::sleep(std::time::Duration::from_millis(200));
}
let _ = child.wait();
std::thread::sleep(std::time::Duration::from_millis(150));
c.sample(&[], &[]);
let row = c
.procs
.iter()
.find(|r| r.pid == pid)
.expect("exited process must stay in the ledger");
assert!(!row.alive, "row must be flagged exited after death");
assert!(row.cmdline.is_some());
assert!(saw_alive, "row must have been seen alive first");
let _ = fs::remove_file(&tmp);
}
#[test]
fn evict_dead_caps_ledger_growth() {
use std::time::Duration;
let mut c = ProcCollector::new();
let now = Instant::now();
let mk = |start: Option<u64>, seen: Option<Instant>| ProcState {
net_recv: 1,
start_ticks: start,
last_alive: seen,
..ProcState::zero()
};
let mut s1 = mk(Some(1), Some(now));
s1.start_ticks = Some(1);
let mut s2 = mk(Some(2), Some(now - Duration::from_secs(1200)));
s2.start_ticks = Some(2);
let mut s3 = mk(Some(3), Some(now - Duration::from_secs(7200)));
s3.start_ticks = Some(3);
c.acc.insert(1, s1);
c.acc.insert(2, s2);
c.acc.insert(3, s3);
c.io_prev.insert(1, (10, 10));
c.io_prev.insert(3, (30, 30)); let mut pids_now = HashMap::new();
pids_now.insert(1u32, 1u64);
c.evict_dead(&pids_now, now);
assert!(c.acc.contains_key(&1), "活进程永留");
assert!(c.acc.contains_key(&2), "死进程保留期内留");
assert!(!c.acc.contains_key(&3), "死进程超时清");
assert!(c.io_prev.contains_key(&1), "io_prev 留活 pid");
assert!(!c.io_prev.contains_key(&3), "io_prev 清死 pid");
c.exit_retention = Duration::MAX;
c.acc
.insert(3, mk(Some(3), Some(now - Duration::from_secs(7200))));
c.evict_dead(&pids_now, now);
assert!(c.acc.contains_key(&3), "retention=0(永久)不清");
}
}