Skip to content

Commit

Permalink
remove populate() from topology
Browse files Browse the repository at this point in the history
Signed-off-by: bg-furiosa <[email protected]>
  • Loading branch information
bg-furiosa committed Apr 5, 2024
1 parent a6458c4 commit aac7640
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 26 deletions.
6 changes: 1 addition & 5 deletions device-api/bin/show_topology.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
45 changes: 24 additions & 21 deletions device-api/src/topology/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,19 @@ pub struct Topology {
}

impl Topology {
pub fn new() -> Topology {
Self {
pub fn new(devices: Vec<Device>) -> DeviceResult<Topology> {
let mut new_topology = Self {
hwloc_topology: Box::new(HwlocTopology::new()),
topology_matrix: BTreeMap::new(),
}
}
pub unsafe fn populate(&mut self, devices: Vec<Device>) -> 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<String>) -> DeviceResult<()> {
fn populate_with_keys(&mut self, devices: Vec<String>) -> DeviceResult<()> {
// Initialize hwloc topology
self.hwloc_topology.init_topology()?;

Expand All @@ -79,7 +80,7 @@ impl Topology {
}
}

unsafe fn populate_topology_matrix(&mut self, devices: Vec<String>) -> DeviceResult<()> {
fn populate_topology_matrix(&mut self, devices: Vec<String>) -> DeviceResult<()> {
for i in 0..devices.len() {
for j in 0..devices.len() {
let dev1_bdf = devices.get(i).unwrap().clone();
Expand All @@ -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,
}
}
}

Expand Down

0 comments on commit aac7640

Please sign in to comment.