use std::collections::{HashMap, HashSet};
use std::sync::{Mutex, PoisonError};
use sysinfo::{ProcessRefreshKind, ProcessesToUpdate, System};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ProcessRss {
pub pid: u32,
pub parent: Option<u32>,
pub bytes: u64,
pub cpu_ms: u64,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ProcessIdentity {
pub pid: u32,
pub parent: Option<u32>,
pub name: String,
}
pub trait MemorySampler: Send + Sync + 'static {
fn sample(&self) -> Vec<ProcessRss>;
fn identify(&self) -> Vec<ProcessIdentity> {
Vec::new()
}
}
#[derive(Debug)]
pub struct SysinfoSampler {
system: Mutex<System>,
}
impl SysinfoSampler {
#[must_use]
pub fn new() -> Self {
Self {
system: Mutex::new(System::new()),
}
}
}
impl Default for SysinfoSampler {
fn default() -> Self {
Self::new()
}
}
fn sample_refresh_kind() -> ProcessRefreshKind {
ProcessRefreshKind::nothing()
.with_memory()
.with_cpu()
.without_tasks()
}
fn identify_refresh_kind() -> ProcessRefreshKind {
ProcessRefreshKind::nothing().without_tasks()
}
impl MemorySampler for SysinfoSampler {
fn sample(&self) -> Vec<ProcessRss> {
let mut system = self.system.lock().unwrap_or_else(PoisonError::into_inner);
system.refresh_processes_specifics(ProcessesToUpdate::All, true, sample_refresh_kind());
system
.processes()
.values()
.map(|process| ProcessRss {
pid: process.pid().as_u32(),
parent: process.parent().map(sysinfo::Pid::as_u32),
bytes: process.memory(),
cpu_ms: process.accumulated_cpu_time(),
})
.collect()
}
fn identify(&self) -> Vec<ProcessIdentity> {
let mut system = System::new();
system.refresh_processes_specifics(ProcessesToUpdate::All, true, identify_refresh_kind());
system
.processes()
.values()
.map(|process| ProcessIdentity {
pid: process.pid().as_u32(),
parent: process.parent().map(sysinfo::Pid::as_u32),
name: process.name().to_string_lossy().into_owned(),
})
.collect()
}
}
#[derive(Debug)]
pub(crate) struct TreeIndex {
bytes_by_pid: HashMap<u32, u64>,
cpu_by_pid: HashMap<u32, u64>,
children_of: HashMap<u32, Vec<u32>>,
}
impl TreeIndex {
pub(crate) fn build(table: &[ProcessRss]) -> Self {
let mut bytes_by_pid: HashMap<u32, u64> = HashMap::with_capacity(table.len());
let mut cpu_by_pid: HashMap<u32, u64> = HashMap::with_capacity(table.len());
let mut children_of: HashMap<u32, Vec<u32>> = HashMap::with_capacity(table.len());
for entry in table {
bytes_by_pid.insert(entry.pid, entry.bytes);
cpu_by_pid.insert(entry.pid, entry.cpu_ms);
if let Some(parent) = entry.parent {
children_of.entry(parent).or_default().push(entry.pid);
}
}
Self {
bytes_by_pid,
cpu_by_pid,
children_of,
}
}
pub(crate) fn sum_from(&self, root: u32) -> u64 {
self.total_over(root, &self.bytes_by_pid)
}
pub(crate) fn cpu_from(&self, root: u32) -> u64 {
self.total_over(root, &self.cpu_by_pid)
}
pub(crate) fn descendants_of(&self, root: u32) -> HashSet<u32> {
let mut seen = HashSet::from([root]);
let mut stack = vec![root];
while let Some(pid) = stack.pop() {
for &child in self.children_of.get(&pid).into_iter().flatten() {
if seen.insert(child) {
stack.push(child);
}
}
}
seen.remove(&root);
seen
}
fn total_over(&self, root: u32, totals: &HashMap<u32, u64>) -> u64 {
let reading = |pid: u32| totals.get(&pid).copied().unwrap_or(0);
self.descendants_of(root)
.into_iter()
.fold(reading(root), |sum, pid| sum.saturating_add(reading(pid)))
}
}
#[must_use]
pub fn tree_rss(table: &[ProcessRss], root: u32) -> u64 {
TreeIndex::build(table).sum_from(root)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::testing::{rss, rss_cpu};
#[test]
fn lone_root_sums_its_own_bytes() {
let table = [rss(1, None, 100)];
assert_eq!(tree_rss(&table, 1), 100);
}
#[test]
fn descendants_reach_every_generation_and_leave_out_a_root_a_cycle_leads_back_to() {
let index = TreeIndex::build(&[
rss(1, None, 0),
rss(2, Some(1), 0),
rss(3, Some(2), 0),
rss(4, None, 0),
rss(1, Some(3), 0),
]);
assert_eq!(index.descendants_of(1), HashSet::from([2, 3]));
assert!(index.descendants_of(99).is_empty());
}
#[test]
fn root_with_two_children_sums_all_three() {
let table = [rss(1, None, 100), rss(2, Some(1), 20), rss(3, Some(1), 30)];
assert_eq!(tree_rss(&table, 1), 150);
}
#[test]
fn three_deep_chain_sums_every_generation() {
let table = [rss(1, None, 100), rss(2, Some(1), 20), rss(3, Some(2), 5)];
assert_eq!(tree_rss(&table, 1), 125);
}
#[test]
fn sibling_subtree_is_excluded() {
let table = [
rss(1, None, 100), rss(2, None, 200), rss(3, Some(2), 30), rss(4, Some(1), 999), ];
assert_eq!(tree_rss(&table, 2), 230);
}
#[test]
fn root_absent_from_table_sums_to_zero() {
let table = [rss(1, None, 100)];
assert_eq!(tree_rss(&table, 999), 0);
}
#[test]
fn cpu_sums_the_whole_tree_including_the_root() {
let table = [
rss_cpu(100, None, 1024, 500),
rss_cpu(101, Some(100), 2048, 250),
rss_cpu(102, Some(101), 4096, 125),
];
let index = TreeIndex::build(&table);
assert_eq!(index.cpu_from(100), 875);
assert_eq!(index.cpu_from(101), 375);
assert_eq!(index.sum_from(100), 7168, "memory must be unaffected");
}
#[test]
fn self_parenting_pid_terminates_and_counts_once() {
let table = [rss(1, Some(1), 100)];
assert_eq!(tree_rss(&table, 1), 100);
}
#[test]
fn two_cycle_terminates_and_counts_each_once() {
let table = [rss(2, Some(3), 40), rss(3, Some(2), 60)];
assert_eq!(tree_rss(&table, 2), 100);
}
#[test]
fn memory_sampler_is_dyn_compatible() {
let _: &dyn MemorySampler = &SysinfoSampler::new();
}
const _: fn() = || {
fn assert_send_sync<T: Send + Sync>() {}
assert_send_sync::<SysinfoSampler>();
};
#[test]
fn sysinfo_sampler_finds_this_process_with_nonzero_rss() {
let sampler = SysinfoSampler::new();
let pid = std::process::id();
let table = sampler.sample();
let reading = table
.iter()
.copied()
.find(|p| p.pid == pid)
.unwrap_or_else(|| panic!("own pid {pid} not found in sampled process table"));
assert!(reading.bytes > 0, "own RSS reading was zero");
assert!(
table.iter().any(|p| p.cpu_ms > 0),
"no entry in the table carried any accumulated CPU time; \
the refresh is missing `.with_cpu()`"
);
assert!(table.len() > 1, "table had only one process in it");
assert!(
table.iter().any(|p| p.parent.is_some()),
"no entry in the table carried a parent pid"
);
let parent = reading
.parent
.unwrap_or_else(|| panic!("own reading for pid {pid} carried no parent"));
#[cfg(unix)]
assert_eq!(
reading.parent,
Some(std::os::unix::process::parent_id()),
"sampler reported a parent other than the one the OS gives this process"
);
assert!(
tree_rss(&table, parent) > reading.bytes,
"tree sum rooted at own parent ({parent}) was not larger than this process's own RSS"
);
}
#[test]
fn scripted_sampler_replays_in_order_then_repeats_last() {
let under = vec![rss(1, None, 100)];
let over = vec![rss(1, None, 900)];
let sampler = crate::testing::ScriptedSampler::new(vec![under.clone(), over.clone()]);
assert_eq!(
sampler.sample(),
under,
"call 1 should be the first reading"
);
assert_eq!(
sampler.sample(),
over,
"call 2 should be the second reading"
);
assert_eq!(
sampler.sample(),
over,
"call 3, past the end of the script, should repeat the last reading"
);
}
#[test]
fn scripted_sampler_calls_counts_every_invocation() {
let sampler = crate::testing::ScriptedSampler::new(vec![vec![rss(1, None, 1)]]);
assert_eq!(sampler.calls(), 0);
sampler.sample();
sampler.sample();
sampler.sample();
assert_eq!(sampler.calls(), 3);
}
#[test]
fn the_sampling_refresh_kind_asks_for_memory_and_cpu_but_never_tasks() {
let kind = sample_refresh_kind();
assert!(kind.memory(), "the tree sum is a memory sum");
assert!(
kind.cpu(),
"without this `accumulated_cpu_time` stays at 0 and every \
percentage reads a plausible, wrong 0.0"
);
assert!(
!kind.tasks(),
"`tasks` makes sysinfo's Linux backend list every thread as a \
process parented to its own process, which multiplies a sheep's \
reported memory by its thread count"
);
}
#[test]
fn the_identify_refresh_kind_asks_for_neither_memory_nor_cpu_nor_tasks() {
let kind = identify_refresh_kind();
assert!(!kind.memory(), "identify reads only `name` and `parent`");
assert!(!kind.cpu(), "identify reads only `name` and `parent`");
assert!(
!kind.tasks(),
"`tasks` would make `shep describe` report every thread of a \
sheep as one of its lambs"
);
}
#[cfg(target_os = "linux")]
mod linux {
use std::sync::mpsc;
use std::thread;
use super::*;
fn own_thread_ids() -> Vec<u32> {
std::fs::read_dir("/proc/self/task")
.expect("/proc/self/task exists on every Linux this daemon runs on")
.filter_map(|entry| {
entry
.ok()?
.file_name()
.to_str()
.and_then(|name| name.parse::<u32>().ok())
})
.collect()
}
fn with_extra_threads<T>(count: usize, body: impl FnOnce() -> T) -> T {
let (report_started, started) = mpsc::channel::<()>();
let mut releases = Vec::with_capacity(count);
let mut handles = Vec::with_capacity(count);
for _ in 0..count {
let (release, parked) = mpsc::channel::<()>();
let report_started = report_started.clone();
releases.push(release);
handles.push(thread::spawn(move || {
report_started
.send(())
.expect("the test thread outlives every thread it parks");
let _ = parked.recv();
}));
}
drop(report_started);
for _ in 0..count {
started
.recv()
.expect("every parked thread reports in before it parks");
}
let out = body();
drop(releases);
for handle in handles {
handle.join().expect("a parked thread cannot panic");
}
out
}
#[test]
fn the_sample_table_holds_no_thread_of_this_process() {
let own_pid = std::process::id();
with_extra_threads(4, || {
let threads: Vec<u32> = own_thread_ids()
.into_iter()
.filter(|tid| *tid != own_pid)
.collect();
assert!(
!threads.is_empty(),
"the case says nothing unless this process really has \
threads other than its main one; it did not"
);
let table = SysinfoSampler::new().sample();
let sampled: HashSet<u32> = table.iter().map(|entry| entry.pid).collect();
let leaked: Vec<u32> = threads
.into_iter()
.filter(|tid| sampled.contains(tid))
.collect();
assert!(
sampled.contains(&own_pid),
"the walk has to see this process at all, or a clean \
result below means only that the walk failed"
);
assert!(
leaked.is_empty(),
"the table must hold processes, not threads; these \
thread ids of pid {own_pid} were sampled as processes \
in their own right: {leaked:?}"
);
});
}
#[test]
fn identify_reports_no_thread_of_this_process() {
let own_pid = std::process::id();
with_extra_threads(4, || {
let threads: Vec<u32> = own_thread_ids()
.into_iter()
.filter(|tid| *tid != own_pid)
.collect();
assert!(
!threads.is_empty(),
"the case says nothing unless this process really has \
threads other than its main one; it did not"
);
let identified: HashSet<u32> = SysinfoSampler::new()
.identify()
.into_iter()
.map(|identity| identity.pid)
.collect();
let leaked: Vec<u32> = threads
.into_iter()
.filter(|tid| identified.contains(tid))
.collect();
assert!(
identified.contains(&own_pid),
"the walk has to see this process at all, or a clean \
result below means only that the walk failed"
);
assert!(
leaked.is_empty(),
"a lamb is a child process, never a thread; these thread \
ids of pid {own_pid} were identified as processes: \
{leaked:?}"
);
});
}
}
mod slow {
#[cfg(unix)]
use std::process::{Command, Stdio};
#[cfg(unix)]
use std::time::{Duration, Instant};
#[cfg(unix)]
use super::*;
#[cfg(unix)]
const GIVE_UP_AFTER: Duration = Duration::from_secs(10);
#[cfg(unix)]
const BETWEEN_WALKS: Duration = Duration::from_millis(10);
#[cfg(unix)]
fn name_of(sampler: &SysinfoSampler, pid: u32) -> Option<String> {
sampler
.identify()
.into_iter()
.find(|identity| identity.pid == pid)
.map(|identity| identity.name)
}
#[cfg(unix)]
fn name_when(
sampler: &SysinfoSampler,
pid: u32,
accept: impl Fn(Option<&str>) -> bool,
) -> Option<String> {
let deadline = Instant::now() + GIVE_UP_AFTER;
loop {
let seen = name_of(sampler, pid);
if accept(seen.as_deref()) || Instant::now() >= deadline {
return seen;
}
std::thread::sleep(BETWEEN_WALKS);
}
}
#[cfg(unix)]
#[test]
fn identify_reports_the_name_a_lamb_execed_into_not_the_one_it_forked_with() {
let mut child = Command::new("/bin/sh")
.arg("-c")
.arg("read _; exec sleep 30")
.stdin(Stdio::piped())
.spawn()
.expect("/bin/sh is present on every platform this daemon supports");
let pid = child.id();
let sampler = SysinfoSampler::new();
let forked_as = name_when(&sampler, pid, |name| name.is_some());
drop(child.stdin.take());
let execed_into = name_when(&sampler, pid, |name| name == Some("sleep"));
let _ = child.kill();
let _ = child.wait();
assert!(
forked_as.is_some(),
"the first walk has to see pid {pid} at all, or the case says \
nothing about what a later walk reports for it"
);
assert_ne!(
forked_as.as_deref(),
Some("sleep"),
"pid {pid} was still blocked on `read` here, so no walk could \
yet have seen the `exec`; this one did"
);
assert_eq!(
execed_into.as_deref(),
Some("sleep"),
"the second walk must report what pid {pid} is NOW, not what \
the first walk recorded for it"
);
}
}
}