Skip to content
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
68 changes: 2 additions & 66 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,12 @@ nix = { version = "0.29", default-features = false, features = ["process"] }
phf = "0.11.2"
phf_codegen = "0.11.2"
prettytable-rs = "0.10.0"
procfs = "0.17.0"
rand = { version = "0.9.0", features = ["small_rng"] }
ratatui = "0.29.0"
regex = "1.10.4"
sysinfo = "0.34.0"
tempfile = "3.10.1"
terminal_size = "0.4.2"
textwrap = { version = "0.16.1", features = ["terminal_size"] }
thiserror = "2.0.4"
uucore = "0.0.30"
Expand Down
7 changes: 3 additions & 4 deletions src/uu/vmstat/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@ keywords = ["acl", "uutils", "cross-platform", "cli", "utility"]
categories = ["command-line-utilities"]

[dependencies]
uucore = { workspace = true }
bytesize = { workspace = true }
clap = { workspace = true }

[target.'cfg(target_os="linux")'.dependencies]
procfs = { workspace = true }
terminal_size = { workspace = true }
uucore = { workspace = true }

[lib]
path = "src/vmstat.rs"
Expand Down
122 changes: 109 additions & 13 deletions src/uu/vmstat/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,65 @@
// file that was distributed with this source code.

#[cfg(target_os = "linux")]
pub fn parse_proc_file(path: &str) -> std::collections::HashMap<String, String> {
use std::collections::HashMap;

#[cfg(target_os = "linux")]
pub fn parse_proc_file(path: &str) -> HashMap<String, String> {
let file = std::fs::File::open(std::path::Path::new(path)).unwrap();
let content = std::io::read_to_string(file).unwrap();
let mut map: std::collections::HashMap<String, String> = std::collections::HashMap::new();
let mut map: HashMap<String, String> = HashMap::new();

for line in content.lines() {
let parts = line.split_once(char::is_whitespace);
if let Some(parts) = parts {
map.insert(parts.0.to_string(), parts.1.trim_start().to_string());
map.insert(
parts.0.strip_suffix(":").unwrap_or(parts.0).to_string(),
parts.1.trim_start().to_string(),
);
}
}

map
}

#[cfg(target_os = "linux")]
pub struct ProcData {
pub uptime: (f64, f64),
pub stat: HashMap<String, String>,
pub meminfo: HashMap<String, String>,
pub vmstat: HashMap<String, String>,
}
#[cfg(target_os = "linux")]
impl Default for ProcData {
fn default() -> Self {
Self::new()
}
}
#[cfg(target_os = "linux")]
impl ProcData {
pub fn new() -> Self {
let uptime = Self::get_uptime();
let stat = parse_proc_file("/proc/stat");
let meminfo = parse_proc_file("/proc/meminfo");
let vmstat = parse_proc_file("/proc/vmstat");
Self {
uptime,
stat,
meminfo,
vmstat,
}
}

fn get_uptime() -> (f64, f64) {
let file = std::fs::File::open(std::path::Path::new("/proc/uptime")).unwrap();
let content = std::io::read_to_string(file).unwrap();
let mut parts = content.split_whitespace();
let uptime = parts.next().unwrap().parse::<f64>().unwrap();
let idle_time = parts.next().unwrap().parse::<f64>().unwrap();
(uptime, idle_time)
}
}

#[cfg(target_os = "linux")]
pub struct CpuLoad {
pub user: f64,
Expand All @@ -35,18 +79,20 @@ pub struct CpuLoad {

#[cfg(target_os = "linux")]
impl CpuLoad {
pub fn current() -> CpuLoad {
pub fn current() -> Self {
let file = std::fs::File::open(std::path::Path::new("/proc/stat")).unwrap(); // do not use `parse_proc_file` here because only one line is used
let content = std::io::read_to_string(file).unwrap();
let load = content
.lines()
.next()
.unwrap()
.strip_prefix("cpu")
.unwrap()
.split(' ')
.filter(|s| !s.is_empty())
.collect::<Vec<_>>();
let load_str = content.lines().next().unwrap().strip_prefix("cpu").unwrap();
Self::from_str(load_str)
}

pub fn from_proc_map(proc_map: &HashMap<String, String>) -> Self {
let load_str = proc_map.get("cpu").unwrap();
Self::from_str(load_str)
}

fn from_str(s: &str) -> Self {
let load = s.split(' ').filter(|s| !s.is_empty()).collect::<Vec<_>>();
let user = load[0].parse::<f64>().unwrap();
let nice = load[1].parse::<f64>().unwrap();
let system = load[2].parse::<f64>().unwrap();
Expand Down Expand Up @@ -81,3 +127,53 @@ impl CpuLoad {
}
}
}

#[cfg(target_os = "linux")]
pub struct Meminfo {
pub mem_total: bytesize::ByteSize,
pub mem_free: bytesize::ByteSize,
pub mem_available: bytesize::ByteSize,
pub buffers: bytesize::ByteSize,
pub cached: bytesize::ByteSize,
pub swap_cached: bytesize::ByteSize,
pub active: bytesize::ByteSize,
pub inactive: bytesize::ByteSize,
pub swap_total: bytesize::ByteSize,
pub swap_free: bytesize::ByteSize,
}
#[cfg(target_os = "linux")]
impl Meminfo {
pub fn current() -> Self {
let meminfo = parse_proc_file("/proc/meminfo");
Self::from_proc_map(&meminfo)
}

pub fn from_proc_map(proc_map: &HashMap<String, String>) -> Self {
use std::str::FromStr;

let mem_total = bytesize::ByteSize::from_str(proc_map.get("MemTotal").unwrap()).unwrap();
let mem_free = bytesize::ByteSize::from_str(proc_map.get("MemFree").unwrap()).unwrap();
let mem_available =
bytesize::ByteSize::from_str(proc_map.get("MemAvailable").unwrap()).unwrap();
let buffers = bytesize::ByteSize::from_str(proc_map.get("Buffers").unwrap()).unwrap();
let cached = bytesize::ByteSize::from_str(proc_map.get("Cached").unwrap()).unwrap();
let swap_cached =
bytesize::ByteSize::from_str(proc_map.get("SwapCached").unwrap()).unwrap();
let active = bytesize::ByteSize::from_str(proc_map.get("Active").unwrap()).unwrap();
let inactive = bytesize::ByteSize::from_str(proc_map.get("Inactive").unwrap()).unwrap();
let swap_total = bytesize::ByteSize::from_str(proc_map.get("SwapTotal").unwrap()).unwrap();
let swap_free = bytesize::ByteSize::from_str(proc_map.get("SwapFree").unwrap()).unwrap();
Self {
mem_total,
mem_free,
mem_available,
buffers,
cached,
swap_cached,
active,
inactive,
swap_total,
swap_free,
}
}
}
Loading