From 296791acbf8bde990b42d1a376d3daabe30d401e Mon Sep 17 00:00:00 2001 From: Chris Bury Date: Thu, 25 Jul 2024 09:36:45 +0100 Subject: [PATCH] Reworked pressure example to be more readable. --- procfs/examples/pressure.rs | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/procfs/examples/pressure.rs b/procfs/examples/pressure.rs index 1665d6a2..8f4a2c61 100644 --- a/procfs/examples/pressure.rs +++ b/procfs/examples/pressure.rs @@ -1,8 +1,30 @@ -use procfs::prelude::*; +use procfs::{prelude::*, CpuPressure, IoPressure, MemoryPressure, PressureRecord}; /// A basic example of /proc/pressure/ usage. fn main() { - println!("memory pressure: {:#?}", procfs::MemoryPressure::current()); - println!("cpu pressure: {:#?}", procfs::CpuPressure::current()); - println!("io pressure: {:#?}", procfs::IoPressure::current()); + if let Ok(pressure) = MemoryPressure::current() { + println!("Memory Pressure:"); + println!("{:>10}:", "Some"); + print_pressure(pressure.some, 20); + println!("{:>10}:", "Full"); + print_pressure(pressure.full, 20); + } + if let Ok(pressure) = CpuPressure::current() { + println!("CPU Pressure:"); + print_pressure(pressure.some, 20); + } + if let Ok(pressure) = IoPressure::current() { + println!("IO Pressure:"); + println!("{:>10}:", "Some"); + print_pressure(pressure.some, 20); + println!("{:>10}:", "Full"); + print_pressure(pressure.full, 20); + } +} + +fn print_pressure(pressure: PressureRecord, width: usize) { + println!("{:>width$}: {}", "Average 10", pressure.avg10); + println!("{:>width$}: {}", "Average 60", pressure.avg60); + println!("{:>width$}: {}", "Average 300", pressure.avg300); + println!("{:>width$}: {}", "Total", pressure.total); }