Skip to content

Commit

Permalink
Default to device name if subdevice name is not found (#151)
Browse files Browse the repository at this point in the history
  • Loading branch information
Rolv-Apneseth authored Mar 28, 2023
1 parent 97e8ce3 commit d5a6e99
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/linux/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,7 @@ impl GeneralReadout for LinuxGeneralReadout {
continue;
};

if let Some(sub_device_name) = device.get_sub_device_name(&db) {
if let Some(sub_device_name) = device.get_device_name(&db) {
gpus.push(sub_device_name);
};
}
Expand Down
12 changes: 5 additions & 7 deletions src/linux/pci_devices.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ impl PciDevice {
}
}

pub fn get_sub_device_name(&self, db: &Database) -> Option<String> {
pub fn get_device_name(&self, db: &Database) -> Option<String> {
let vendor_value = self.read_value(PciDeviceReadableValues::Vendor);
let sub_vendor_value = self.read_value(PciDeviceReadableValues::SubVendor);
let device_value = self.read_value(PciDeviceReadableValues::Device);
Expand All @@ -75,6 +75,8 @@ impl PciDevice {
let Some(device) = vendor.devices.get(&device_value) else {
return None;
};
// To return device name if no valid subdevice name is found
let device_name = device.name.to_owned();

let sub_device_id = SubDeviceId {
subvendor: sub_vendor_value,
Expand All @@ -84,20 +86,16 @@ impl PciDevice {
if let Some(sub_device) = device.subdevices.get(&sub_device_id) {
let start = match sub_device.find('[') {
Some(i) => i + 1,
_ => panic!(
"Could not find opening square bracket for sub device: {}",
sub_device
),
_ => return Some(device_name),
};
let end = sub_device.len() - 1;

Some(sub_device.chars().take(end).skip(start).collect::<String>())
} else {
None
Some(device_name)
}
}
}

pub fn get_pci_devices() -> Result<Vec<PciDevice>, io::Error> {
let devices_dir = read_dir("/sys/bus/pci/devices/")?;

Expand Down

0 comments on commit d5a6e99

Please sign in to comment.