From aac76408aaf68c219cbb1a9856600211f5e5cd93 Mon Sep 17 00:00:00 2001 From: bg-furiosa Date: Fri, 5 Apr 2024 14:27:32 +0900 Subject: [PATCH] remove populate() from topology Signed-off-by: bg-furiosa --- device-api/bin/show_topology.rs | 6 +---- device-api/src/topology/mod.rs | 45 ++++++++++++++++++--------------- 2 files changed, 25 insertions(+), 26 deletions(-) diff --git a/device-api/bin/show_topology.rs b/device-api/bin/show_topology.rs index acfa724..227d7a0 100644 --- a/device-api/bin/show_topology.rs +++ b/device-api/bin/show_topology.rs @@ -11,11 +11,7 @@ async fn main() -> Result<(), DeviceError> { return Ok(()); } - let mut topology = topology::Topology::new(); - unsafe { - topology.populate(devices.clone())?; - } - + let topology = topology::Topology::new(devices.clone())?; let mut rows = vec![]; let mut header = vec!["Device".cell().bold(true)]; for device in devices.iter() { diff --git a/device-api/src/topology/mod.rs b/device-api/src/topology/mod.rs index 783d86c..b9a30bb 100644 --- a/device-api/src/topology/mod.rs +++ b/device-api/src/topology/mod.rs @@ -51,18 +51,19 @@ pub struct Topology { } impl Topology { - pub fn new() -> Topology { - Self { + pub fn new(devices: Vec) -> DeviceResult { + let mut new_topology = Self { hwloc_topology: Box::new(HwlocTopology::new()), topology_matrix: BTreeMap::new(), - } - } - pub unsafe fn populate(&mut self, devices: Vec) -> DeviceResult<()> { + }; + let keys = devices.iter().map(|d| d.busname().unwrap()).collect(); - self.populate_with_keys(keys) + new_topology.populate_with_keys(keys)?; + + Ok(new_topology) } - unsafe fn populate_with_keys(&mut self, devices: Vec) -> DeviceResult<()> { + fn populate_with_keys(&mut self, devices: Vec) -> DeviceResult<()> { // Initialize hwloc topology self.hwloc_topology.init_topology()?; @@ -79,7 +80,7 @@ impl Topology { } } - unsafe fn populate_topology_matrix(&mut self, devices: Vec) -> DeviceResult<()> { + fn populate_topology_matrix(&mut self, devices: Vec) -> DeviceResult<()> { for i in 0..devices.len() { for j in 0..devices.len() { let dev1_bdf = devices.get(i).unwrap().clone(); @@ -99,21 +100,23 @@ impl Topology { Ok(()) } - unsafe fn search_interconnect(&self, dev1_bdf: &str, dev2_bdf: &str) -> LinkType { - if dev1_bdf == dev2_bdf { - return LinkTypeSoc; - } + fn search_interconnect(&self, dev1_bdf: &str, dev2_bdf: &str) -> LinkType { + unsafe { + if dev1_bdf == dev2_bdf { + return LinkTypeSoc; + } - let ancestor_obj = self - .hwloc_topology - .get_common_ancestor_obj(dev1_bdf, dev2_bdf) - .unwrap(); + let ancestor_obj = self + .hwloc_topology + .get_common_ancestor_obj(dev1_bdf, dev2_bdf) + .unwrap(); - match (*ancestor_obj).type_ { - hwloc_obj_type_t_HWLOC_OBJ_MACHINE => LinkTypeInterconnect, - hwloc_obj_type_t_HWLOC_OBJ_PACKAGE => LinkTypeCPU, - hwloc_obj_type_t_HWLOC_OBJ_BRIDGE => LinkTypeHostBridge, - _ => LinkTypeUnknown, + match (*ancestor_obj).type_ { + hwloc_obj_type_t_HWLOC_OBJ_MACHINE => LinkTypeInterconnect, + hwloc_obj_type_t_HWLOC_OBJ_PACKAGE => LinkTypeCPU, + hwloc_obj_type_t_HWLOC_OBJ_BRIDGE => LinkTypeHostBridge, + _ => LinkTypeUnknown, + } } }