MCPcopy Create free account
hub / github.com/NodeDB-Lab/nodedb / detect_system_memory_bytes

Function detect_system_memory_bytes

nodedb/src/event/slab_budget.rs:220–231  ·  view source on GitHub ↗

Detect total system memory in bytes by reading `/proc/meminfo`. Returns `None` if the file cannot be read or parsed (non-Linux, containers with restricted procfs, etc.). Callers should fall back to a hardcoded default.

()

Source from the content-addressed store, hash-verified

218/// Returns `None` if the file cannot be read or parsed (non-Linux, containers
219/// with restricted procfs, etc.). Callers should fall back to a hardcoded default.
220fn detect_system_memory_bytes() -> Option<u64> {
221 let contents = std::fs::read_to_string("/proc/meminfo").ok()?;
222 for line in contents.lines() {
223 if let Some(rest) = line.strip_prefix("MemTotal:") {
224 // Format: "MemTotal: 16384000 kB"
225 let kb_str = rest.trim().strip_suffix("kB")?.trim();
226 let kb: u64 = kb_str.parse().ok()?;
227 return Some(kb * 1024);
228 }
229 }
230 None
231}
232
233#[cfg(test)]
234mod tests {

Callers 2

for_coresMethod · 0.85
detect_system_memoryFunction · 0.85

Calls 2

okMethod · 0.45
parseMethod · 0.45

Tested by 1

detect_system_memoryFunction · 0.68