use crate::core::memory::{EmcInfo, IramInfo, MemoryStats, RamInfo, SwapInfo};
use crate::error::Result;
use crate::platform::common::*;
use std::fs;
pub fn read_memory_stats() -> Result<MemoryStats> {
let mut stats = MemoryStats::empty();
let meminfo = fs::read_to_string("/proc/meminfo")?;
stats.ram = parse_ram_info(&meminfo)?;
stats.swap = parse_swap_info(&meminfo)?;
stats.emc = read_emc_info().ok();
stats.iram = read_iram_info().ok();
Ok(stats)
}
fn parse_ram_info(meminfo: &str) -> Result<RamInfo> {
let mut mem_total = None;
let mut mem_free = None;
let mut mem_available = None;
let mut buffers = None;
let mut cached = None;
let mut s_reclaimable = None;
let mut shmem = None;
for line in meminfo.lines() {
let parts: Vec<&str> = line.split_whitespace().collect();
if parts.len() < 2 {
continue;
}
let key = parts[0].trim_end_matches(':');
let Ok(value) = parts[1].parse::<u64>() else {
continue;
};
match key {
"MemTotal" => mem_total = Some(value),
"MemFree" => mem_free = Some(value),
"MemAvailable" => mem_available = Some(value),
"Buffers" => buffers = Some(value),
"Cached" => cached = Some(value),
"SReclaimable" => s_reclaimable = Some(value),
"Shmem" => shmem = Some(value),
_ => {}
}
}
let missing = |field: &str| {
crate::error::IronError::Parse(format!(
"/proc/meminfo has no {field} line; refusing to report it as zero"
))
};
let total = mem_total.ok_or_else(|| missing("MemTotal"))?;
let free = mem_free.ok_or_else(|| missing("MemFree"))?;
let available = mem_available.ok_or_else(|| missing("MemAvailable"))?;
Ok(RamInfo {
total,
free: Some(free),
used: total.saturating_sub(available),
buffers,
cached: match (cached, s_reclaimable) {
(None, None) => None,
(c, s) => Some(c.unwrap_or(0) + s.unwrap_or(0)),
},
shared: shmem,
lfb: read_lfb().ok(),
})
}
fn parse_swap_info(meminfo: &str) -> Result<SwapInfo> {
let mut swap = SwapInfo::default();
for line in meminfo.lines() {
let parts: Vec<&str> = line.split_whitespace().collect();
if parts.len() < 2 {
continue;
}
let key = parts[0].trim_end_matches(':');
let value: u64 = parts[1].parse().unwrap_or(0);
match key {
"SwapTotal" => swap.total = Some(value),
"SwapFree" => {
swap.used = swap.total.map(|t| t.saturating_sub(value));
}
"SwapCached" => swap.cached = Some(value),
_ => {}
}
}
Ok(swap)
}
fn read_lfb() -> Result<u32> {
Ok(0)
}
fn read_emc_info() -> Result<EmcInfo> {
let emc_path = "/sys/class/devfreq/17000000.mc";
if !path_exists(emc_path) {
let alt_path = "/sys/class/devfreq/13d00000.mc";
if !path_exists(alt_path) {
return Err(crate::error::IronError::FeatureNotAvailable(
"EMC not available".to_string(),
));
}
}
let cur = read_file_u32(format!("{}/cur_freq", emc_path))? / 1000;
let min = read_file_u32(format!("{}/min_freq", emc_path))? / 1000;
let max = read_file_u32(format!("{}/max_freq", emc_path))? / 1000;
let value = if max > 0 {
((cur as f32 / max as f32) * 100.0) as u32
} else {
0
};
Ok(EmcInfo {
online: true,
value,
current: cur,
max,
min,
})
}
fn read_iram_info() -> Result<IramInfo> {
Err(crate::error::IronError::FeatureNotAvailable(
"IRAM reading not yet implemented".to_string(),
))
}