Skip to content

Commit

Permalink
fix: hardware info sampler (iopsystems#53)
Browse files Browse the repository at this point in the history
Fix the hardware info sampler to work on platforms such as the
raspi 4B which do not expose die and node as part of the topology.

Also fixes the cpu usage sampler for those platforms.
  • Loading branch information
brayniac authored Aug 7, 2023
1 parent 65acfb3 commit 40e0883
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 21 deletions.
10 changes: 5 additions & 5 deletions src/samplers/hwinfo/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,13 @@ pub fn get_caches() -> Result<Vec<Vec<Cache>>> {

for (index, caches) in ret.iter_mut().enumerate() {
for cpu_id in &cpu_ids {
let cache = Cache::new(*cpu_id, index)?;
if let Ok(cache) = Cache::new(*cpu_id, index) {
if cache.shared_cpus[0] != *cpu_id {
continue;
}

if cache.shared_cpus[0] != *cpu_id {
continue;
caches.push(cache);
}

caches.push(cache);
}
}

Expand Down
15 changes: 11 additions & 4 deletions src/samplers/hwinfo/cpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,21 +48,28 @@ pub fn get_cpus() -> Result<Vec<Cpu>> {
let ids = read_list("/sys/devices/system/cpu/online")?;
for id in ids {
let core_id = read_usize(format!("/sys/devices/system/cpu/cpu{id}/topology/core_id"))?;
let die_id = read_usize(format!("/sys/devices/system/cpu/cpu{id}/topology/die_id"))?;
let package_id = read_usize(format!(
"/sys/devices/system/cpu/cpu{id}/topology/physical_package_id"
))?;

// if the platform does not expose die topology, use the package id
let die_id = read_usize(format!("/sys/devices/system/cpu/cpu{id}/topology/die_id"))
.unwrap_or(package_id);

let core_cpus = read_list(format!(
"/sys/devices/system/cpu/cpu{id}/topology/core_cpus_list"
))?;
let die_cpus = read_list(format!(
"/sys/devices/system/cpu/cpu{id}/topology/die_cpus_list"
))?;
let package_cpus = read_list(format!(
"/sys/devices/system/cpu/cpu{id}/topology/package_cpus_list"
))?;

// if the platform does not expose die topology, treat all cpus in same
// package as on the same die
let die_cpus = read_list(format!(
"/sys/devices/system/cpu/cpu{id}/topology/die_cpus_list"
))
.unwrap_or(package_cpus.clone());

let core_siblings = read_list(format!(
"/sys/devices/system/cpu/cpu{id}/topology/core_siblings_list"
))?;
Expand Down
6 changes: 0 additions & 6 deletions src/samplers/hwinfo/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,6 @@ pub struct Memory {
total_bytes: u64,
}

// impl Memory {
// pub fn total_bytes(&self) -> u64 {
// self.total_bytes
// }
// }

impl Memory {
pub fn new() -> Result<Self> {
let file = File::open("/proc/meminfo")?;
Expand Down
22 changes: 16 additions & 6 deletions src/samplers/hwinfo/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,22 @@ pub struct Node {
pub fn get_nodes() -> Result<Vec<Node>> {
let mut ret = Vec::new();

let ids = read_list("/sys/devices/system/node/online")?;

for id in ids {
let memory = Memory::node(id)?;
let cpus = read_list(format!("/sys/devices/system/node/node{id}/cpulist"))?;
ret.push(Node { id, cpus, memory });
if let Ok(ids) = read_list("/sys/devices/system/node/online") {
for id in ids {
let memory = Memory::node(id)?;
let cpus = read_list(format!("/sys/devices/system/node/node{id}/cpulist"))?;
ret.push(Node { id, cpus, memory });
}
} else {
// Some platforms might not expose node topology. For those platforms we
// will consider all resources part of the same node.
let memory = Memory::new()?;
let cpus = read_list("/sys/devices/system/cpu/cpu0/topology/package_cpus_list")?;
ret.push(Node {
id: 0,
cpus,
memory,
});
}

Ok(ret)
Expand Down

0 comments on commit 40e0883

Please sign in to comment.