From 1cf4f754ac563f0d2f9a4a89d6b4db91f2adf88d Mon Sep 17 00:00:00 2001 From: Ryan Date: Thu, 15 Aug 2024 16:14:27 +0800 Subject: [PATCH] parse /proc/meminfo for memory usage (#7) * fix: calculate memory usage via /proc/meminfo * refactor: move sysinfo call into get_current No longer used by anything but uptime. --- src/main.rs | 7 ++----- src/system.rs | 31 +++++++++++++++++++++---------- src/uptime.rs | 5 +++-- 3 files changed, 26 insertions(+), 17 deletions(-) diff --git a/src/main.rs b/src/main.rs index 5cae03a..aace49d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,23 +9,20 @@ use crate::desktop::get_desktop_info; use crate::release::{get_os_pretty_name, get_system_info}; use crate::system::{get_memory_usage, get_root_disk_usage, get_shell, get_username_and_hostname}; use crate::uptime::get_current; -use std::io; use color_eyre::Report; -use nix::sys::sysinfo::sysinfo; fn main() -> Result<(), Report> { color_eyre::install()?; - let info = sysinfo().map_err(|e| io::Error::new(io::ErrorKind::Other, e))?; let fields = Fields { user_info: get_username_and_hostname(), os_name: get_os_pretty_name()?, kernel_version: get_system_info()?, shell: get_shell(), desktop: get_desktop_info(), - uptime: get_current(&info)?, - memory_usage: get_memory_usage(&info), + uptime: get_current()?, + memory_usage: get_memory_usage()?, storage: get_root_disk_usage()?, colors: print_dots(), }; diff --git a/src/system.rs b/src/system.rs index 5fa8587..964aec1 100644 --- a/src/system.rs +++ b/src/system.rs @@ -1,6 +1,5 @@ use color_eyre::Result; use nix::sys::statvfs::statvfs; -use nix::sys::sysinfo::SysInfo; use std::env; use std::io::{self}; @@ -40,21 +39,33 @@ pub fn get_root_disk_usage() -> Result { )) } -pub fn get_memory_usage(info: &SysInfo) -> String { +pub fn get_memory_usage() -> Result { #[inline(always)] - fn parse_memory_info(info: &SysInfo) -> (f64, f64) { - let total_memory_kb = (info.ram_total() / 1024) as f64; - let available_memory_kb = (info.ram_unused() / 1024) as f64; + fn parse_memory_info() -> Result<(f64, f64), io::Error> { + let mut total_memory_kb = 0.0; + let mut available_memory_kb = 0.0; - let total_memory_gb = total_memory_kb / (1024.0 * 1024.0); - let available_memory_gb = available_memory_kb / (1024.0 * 1024.0); + for line in std::fs::read_to_string("/proc/meminfo")?.lines() { + let mut split = line.split_whitespace(); + let key = split.next().unwrap_or(""); + if key == "MemTotal:" { + total_memory_kb = split.next().unwrap_or("0").parse().unwrap_or(0.0); + } else if key == "MemAvailable:" { + available_memory_kb = split.next().unwrap_or("0").parse().unwrap_or(0.0); + } + } + + let total_memory_gb = total_memory_kb / 1024.0 / 1024.0; + let available_memory_gb = available_memory_kb / 1024.0 / 1024.0; let used_memory_gb = total_memory_gb - available_memory_gb; - (used_memory_gb, total_memory_gb) + Ok((used_memory_gb, total_memory_gb)) } - let (used_memory, total_memory) = parse_memory_info(info); + let (used_memory, total_memory) = parse_memory_info()?; let percentage_used = (used_memory / total_memory * 100.0).round() as u64; - format!("{used_memory:.2} GiB / {total_memory:.2} GiB ({CYAN}{percentage_used}%{RESET})") + Ok(format!( + "{used_memory:.2} GiB / {total_memory:.2} GiB ({CYAN}{percentage_used}%{RESET})" + )) } diff --git a/src/uptime.rs b/src/uptime.rs index 4090acd..6c44640 100644 --- a/src/uptime.rs +++ b/src/uptime.rs @@ -1,8 +1,9 @@ use color_eyre::Result; -use nix::sys::sysinfo::SysInfo; +use nix::sys::sysinfo::sysinfo; use std::io; -pub fn get_current(info: &SysInfo) -> Result { +pub fn get_current() -> Result { + let info = sysinfo().map_err(|e| io::Error::new(io::ErrorKind::Other, e))?; let uptime_seconds = info.uptime().as_secs_f64(); let total_minutes = (uptime_seconds / 60.0).round() as u64;