#[cfg(feature = "serde1")] use serde::{Deserialize, Serialize}; use std::{collections::HashMap, io::BufRead};
/// Represents the data from `/proc/cpuinfo`. /// /// The `fields` field stores the fields that are common among all CPUs. The `cpus` field stores /// CPU-specific info. /// /// For common fields, there are methods that will return the data, converted to a more appropriate /// data type. These methods will all return `None` if the field doesn't exist, or is in some /// unexpected format (in that case, you'll have to access the string data directly). #[derive(Debug, Clone)] #[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))] pubstruct CpuInfo { /// This stores fields that are common among all CPUs pub fields: HashMap<String, String>, pub cpus: Vec<HashMap<String, String>>,
}
implcrate::FromBufRead for CpuInfo { fn from_buf_read<R: BufRead>(r: R) -> ProcResult<Self> { letmut list = Vec::new(); letmut map = Some(HashMap::new());
// the first line of a cpu block must start with "processor" letmut found_first = false;
for line in r.lines().flatten() { if !line.is_empty() { letmut s = line.split(':'); let key = expect!(s.next()); if !found_first && key.trim() == "processor" {
found_first = true;
} if !found_first { continue;
} iflet Some(value) = s.next() { let key = key.trim().to_owned(); let value = value.trim().to_owned();
impl CpuInfo { /// Get the total number of cpu cores. /// /// This is the number of entries in the `/proc/cpuinfo` file. pubfn num_cores(&self) -> usize { self.cpus.len()
}
/// Get info for a specific cpu. /// /// This will merge the common fields with the cpu-specific fields. /// /// Returns None if the requested cpu index is not found. pubfn get_info(&self, cpu_num: usize) -> Option<HashMap<&str, &str>> { self.cpus.get(cpu_num).map(|info| { self.fields
.iter()
.chain(info.iter())
.map(|(k, v)| (k.as_ref(), v.as_ref()))
.collect()
})
}
/// Get the content of a specific field associated to a CPU /// /// Returns None if the requested cpu index is not found. pubfn get_field(&self, cpu_num: usize, field_name: &str) -> Option<&str> { self.cpus.get(cpu_num).and_then(|cpu_fields| {
cpu_fields
.get(field_name)
.or_else(|| self.fields.get(field_name))
.map(|s| s.as_ref())
})
}
/// May not be available on some older 2.6 kernels pubfn physical_id(&self, cpu_num: usize) -> Option<u32> { self.get_field(cpu_num, "physical id").and_then(|s| s.parse().ok())
}
#[test] fn test_cpuinfo_rpi() { // My rpi system includes some stuff at the end of /proc/cpuinfo that we shouldn't parse let data = r#"processor : 0
model name : ARMv7 Processor rev 4 (v7l)
BogoMIPS : 38.40
Features : half thumb fastmult vfp edsp neon vfpv3 tls vfpv4 idiva idivt vfpd32 lpae evtstrm crc32
CPU implementer : 0x41
CPU architecture: 7
CPU variant : 0x0
CPU part : 0xd03
CPU revision : 4
processor : 1
model name : ARMv7 Processor rev 4 (v7l)
BogoMIPS : 38.40
Features : half thumb fastmult vfp edsp neon vfpv3 tls vfpv4 idiva idivt vfpd32 lpae evtstrm crc32
CPU implementer : 0x41
CPU architecture: 7
CPU variant : 0x0
CPU part : 0xd03
CPU revision : 4
processor : 2
model name : ARMv7 Processor rev 4 (v7l)
BogoMIPS : 38.40
Features : half thumb fastmult vfp edsp neon vfpv3 tls vfpv4 idiva idivt vfpd32 lpae evtstrm crc32
CPU implementer : 0x41
CPU architecture: 7
CPU variant : 0x0
CPU part : 0xd03
CPU revision : 4
processor : 3
model name : ARMv7 Processor rev 4 (v7l)
BogoMIPS : 38.40
Features : half thumb fastmult vfp edsp neon vfpv3 tls vfpv4 idiva idivt vfpd32 lpae evtstrm crc32
CPU implementer : 0x41
CPU architecture: 7
CPU variant : 0x0
CPU part : 0xd03
CPU revision : 4
Hardware : BCM2835
Revision : a020d3
Serial : 0000000012345678
Model : Raspberry Pi 3 Model B Plus Rev 1.3 "#;
let r = std::io::Cursor::new(data.as_bytes());
usecrate::FromRead;
let info = CpuInfo::from_read(r).unwrap();
assert_eq!(info.num_cores(), 4); let info = info.get_info(0).unwrap();
assert!(info.get("model name").is_some());
assert!(info.get("BogoMIPS").is_some());
assert!(info.get("Features").is_some());
assert!(info.get("CPU implementer").is_some());
assert!(info.get("CPU architecture").is_some());
assert!(info.get("CPU variant").is_some());
assert!(info.get("CPU part").is_some());
assert!(info.get("CPU revision").is_some());
}
}
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.18 Sekunden
(vorverarbeitet am 2026-06-19)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.