Skip to content

Commit

Permalink
Merge main into 'rust-2024'
Browse files Browse the repository at this point in the history
  • Loading branch information
nokyan committed Feb 21, 2025
2 parents 8ae3603 + 4360abf commit 30e4da9
Show file tree
Hide file tree
Showing 9 changed files with 271 additions and 60 deletions.
9 changes: 9 additions & 0 deletions data/resources/ui/pages/npu.ui
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,15 @@
<property name="title" translatable="yes">Max Power Cap</property>
</object>
</child>
<child>
<object class="AdwActionRow" id="link"><!--Translators: "Link" refers to something like "PCIe 4.0 ×16"-->
<property name="title" translatable="yes">Link</property>
<style>
<class name="property"/>
</style>
<property name="subtitle-selectable">true</property>
</object>
</child>
</object>
</child>
</object>
Expand Down
13 changes: 6 additions & 7 deletions src/ui/pages/drive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,12 @@ impl ResDrive {
imp.set_tab_detail_string(&drive_data.inner.block_device);
}

if let Ok(link) = drive.link() {
imp.link.set_subtitle(&link.to_string());
} else {
imp.link.set_subtitle(&i18n("N/A"));
}

imp.old_stats
.borrow_mut()
.clone_from(&drive_data.disk_stats);
Expand All @@ -283,7 +289,6 @@ impl ResDrive {
removable,
disk_stats,
capacity,
link,
} = drive_data;

let time_passed = SystemTime::now()
Expand Down Expand Up @@ -423,12 +428,6 @@ impl ResDrive {
imp.removable.set_subtitle(&i18n("N/A"));
}

if let Ok(link) = link {
imp.link.set_subtitle(&link.to_string());
} else {
imp.link.set_subtitle(&i18n("N/A"));
}

self.set_property(
"tab_usage_string",
// Translators: This is an abbreviation for "Read" and "Write". This is displayed in the sidebar so your
Expand Down
13 changes: 6 additions & 7 deletions src/ui/pages/gpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,12 @@ impl ResGPU {
imp.encode_decode_usage.set_visible(true);
}

if let Ok(link) = gpu.link() {
imp.link.set_subtitle(&link.to_string());
} else {
imp.link.set_subtitle(&i18n("N/A"));
}

if let Ok(model_name) = gpu.name() {
imp.set_tab_detail_string(&model_name);
}
Expand Down Expand Up @@ -287,7 +293,6 @@ impl ResGPU {
power_usage,
power_cap,
power_cap_max,
link,
nvidia: _,
} = gpu_data;

Expand Down Expand Up @@ -430,12 +435,6 @@ impl ResGPU {
imp.temperature.set_subtitle(&i18n("N/A"));
}

if let Some(link) = link {
imp.link.set_subtitle(&link.to_string());
} else {
imp.link.set_subtitle(&i18n("N/A"));
}

self.set_property("tab_usage_string", &usage_percentage_string);
}
}
9 changes: 9 additions & 0 deletions src/ui/pages/npu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ mod imp {
pub driver_used: TemplateChild<adw::ActionRow>,
#[template_child]
pub max_power_cap: TemplateChild<adw::ActionRow>,
#[template_child]
pub link: TemplateChild<adw::ActionRow>,

#[property(get)]
uses_progress_bar: Cell<bool>,
Expand Down Expand Up @@ -99,6 +101,7 @@ mod imp {
pci_slot: Default::default(),
driver_used: Default::default(),
max_power_cap: Default::default(),
link: Default::default(),
uses_progress_bar: Cell::new(true),
main_graph_color: glib::Bytes::from_static(&super::ResNPU::MAIN_GRAPH_COLOR),
icon: RefCell::new(ThemedIcon::new("npu-symbolic").into()),
Expand Down Expand Up @@ -218,6 +221,12 @@ impl ResNPU {

imp.driver_used.set_subtitle(&npu.driver());

if let Ok(link) = npu.link() {
imp.link.set_subtitle(&link.to_string());
} else {
imp.link.set_subtitle(&i18n("N/A"));
}

if let Ok(model_name) = npu.name() {
imp.set_tab_detail_string(&model_name);
}
Expand Down
53 changes: 45 additions & 8 deletions src/utils/drive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ static RE_ATA_LINK: Lazy<Regex> = lazy_regex!(r"(^link(\d+))$");

static RE_ATA_SLOT: Lazy<Regex> = lazy_regex!(r"(^.+?/ata(\d+))/");

static RE_USB_SLOT: Lazy<Regex> = lazy_regex!(r"(^.+?/usb(\d+))/(.+?)/");

#[derive(Debug)]
pub struct DriveData {
pub inner: Drive,
Expand All @@ -32,7 +34,6 @@ pub struct DriveData {
pub removable: Result<bool>,
pub disk_stats: HashMap<String, usize>,
pub capacity: Result<u64>,
pub link: Result<Link>,
}

impl DriveData {
Expand All @@ -47,7 +48,6 @@ impl DriveData {
let removable = inner.removable();
let disk_stats = inner.sys_stats().unwrap_or_default();
let capacity = inner.capacity();
let link = inner.link();

let drive_data = Self {
inner,
Expand All @@ -56,7 +56,6 @@ impl DriveData {
removable,
disk_stats,
capacity,
link,
};

trace!(
Expand Down Expand Up @@ -87,10 +86,11 @@ pub enum DriveType {
Unknown,
}

#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash)]
#[derive(Debug, Clone, Default, PartialEq, Eq, Hash)]
pub enum DriveSlot {
Pci(PciSlot),
Ata(AtaSlot),
Usb(UsbSlot),
#[default]
Unknown,
}
Expand All @@ -101,6 +101,12 @@ pub struct AtaSlot {
pub ata_link: u8,
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct UsbSlot {
pub usb_bus: u8,
pub usb_device: String,
}

#[derive(Debug, Clone, Default, Eq)]
pub struct Drive {
pub model: Option<String>,
Expand Down Expand Up @@ -334,10 +340,11 @@ impl Drive {
/// Will return `Err` if there are errors during
/// reading or parsing, or if the drive link type is not supported
pub fn link(&self) -> Result<Link> {
match self.slot {
DriveSlot::Pci(slot) => Ok(Link::Pcie(LinkData::from_pci_slot(&slot)?)),
DriveSlot::Ata(slot) => Ok(Link::Sata(LinkData::from_ata_slot(&slot)?)),
_ => bail!("unsupported drive connection type"),
match &self.slot {
DriveSlot::Pci(slot) => Ok(Link::Pcie(LinkData::from_pci_slot(slot)?)),
DriveSlot::Ata(slot) => Ok(Link::Sata(LinkData::from_ata_slot(slot)?)),
DriveSlot::Usb(slot) => Ok(Link::Usb(LinkData::from_usb_slot(slot)?)),
DriveSlot::Unknown => bail!("unsupported drive connection type"),
}
}

Expand All @@ -346,6 +353,8 @@ impl Drive {
Ok(DriveSlot::Pci(pci_slot))
} else if let Ok(ata_slot) = self.ata_slot() {
Ok(DriveSlot::Ata(ata_slot))
} else if let Ok(usb_slot) = self.usb_slot() {
Ok(DriveSlot::Usb(usb_slot))
} else {
bail!("unsupported drive slot type")
}
Expand Down Expand Up @@ -402,6 +411,34 @@ impl Drive {
})
}

fn usb_slot(&self) -> Result<UsbSlot> {
let symlink = std::fs::read_link(&self.sysfs_path)
.context("Could not read sysfs_path as symlink")?
.to_string_lossy()
.to_string();
// ../../devices/pci0000:00/0000:00:08.1/0000:0e:00.3/usb4/4-2/4-2:1.0/host6/target6:0:0/6:0:0:0/block/sdb

let usb_match = RE_USB_SLOT
.captures(&symlink)
.context("No usb match found, probably no usb device")?;

let usb_bus = usb_match
.get(2)
.and_then(|capture| capture.as_str().parse::<u8>().ok())
.context("could not match digits in usb")?;

let usb_device = usb_match
.get(3)
.context("could not find usb device")?
.as_str()
.to_string();

Ok(UsbSlot {
usb_bus,
usb_device,
})
}

/// Returns the World-Wide Identification of the drive
///
/// # Errors
Expand Down
5 changes: 0 additions & 5 deletions src/utils/gpu/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@ pub struct GpuData {
pub power_cap: Option<f64>,
pub power_cap_max: Option<f64>,

pub link: Option<Link>,

pub nvidia: bool,
}

Expand Down Expand Up @@ -82,8 +80,6 @@ impl GpuData {
let power_cap = gpu.power_cap().ok();
let power_cap_max = gpu.power_cap_max().ok();

let link = gpu.link().ok();

let nvidia = matches!(gpu, Gpu::Nvidia(_));

let gpu_data = Self {
Expand All @@ -99,7 +95,6 @@ impl GpuData {
power_usage,
power_cap,
power_cap_max,
link,
nvidia,
};

Expand Down
Loading

0 comments on commit 30e4da9

Please sign in to comment.