Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AMDXDNA support #431

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions lib/process_data/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@ anyhow = "1.0.94"
glob = "0.3.1"
lazy-regex = "3.3.0"
libc = "0.2.167"
log = "0.4.22"
num_cpus = "1.16.0"
nutype = { version = "0.5.0", features = ["serde"] }
nvml-wrapper = "0.10.0"
pretty_env_logger = "0.5"
serde = { version = "1.0.215", features = ["serde_derive"] }
syscalls = { version = "0.6.18", features = ["all"] }
sysconf = "0.3.4"
Expand Down
478 changes: 326 additions & 152 deletions lib/process_data/src/lib.rs

Large diffs are not rendered by default.

11 changes: 11 additions & 0 deletions src/bin/resources-processes.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use anyhow::Result;
use log::{info, trace};
use process_data::ProcessData;
use ron::ser::PrettyConfig;
use std::io::{Read, Write};
Expand All @@ -18,6 +19,11 @@ struct Args {
}

fn main() -> Result<()> {
// Initialize logger
pretty_env_logger::init();

info!("Starting resources-processes…");

let args = Args::parse();

if args.once {
Expand All @@ -29,12 +35,14 @@ fn main() -> Result<()> {
let mut buffer = [0; 1];

std::io::stdin().read_exact(&mut buffer)?;
trace!("Received character");

output(args.ron)?;
}
}

fn output(ron: bool) -> Result<()> {
trace!("Gathering process data…");
let data = ProcessData::all_process_data()?;

let encoded = if ron {
Expand All @@ -50,10 +58,13 @@ fn output(ron: bool) -> Result<()> {
let stdout = std::io::stdout();
let mut handle = stdout.lock();

trace!("Sending content length ({})…", encoded.len());
handle.write_all(&len_byte_array)?;

trace!("Sending content…");
handle.write_all(&encoded)?;

trace!("Flushing…");
handle.flush()?;
Ok(())
}
21 changes: 18 additions & 3 deletions src/ui/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -598,17 +598,32 @@ impl MainWindow {
page.refresh_page(&gpu_data);
}

std::mem::drop(apps_context);

/*
* Npu
*/
let npu_pages = imp.npu_pages.borrow();
for ((_, page), npu_data) in npu_pages.values().zip(npu_data) {
for ((_, page), mut npu_data) in npu_pages.values().zip(npu_data) {
let page = page.content().and_downcast::<ResNPU>().unwrap();

let processes_npu_fraction = apps_context.npu_fraction(npu_data.pci_slot);
npu_data.usage_fraction = Some(f64::max(
npu_data.usage_fraction.unwrap_or(0.0),
processes_npu_fraction.into(),
));

if npu_data.total_memory.is_some() {
let processes_npu_memory_fraction = apps_context.npu_mem(npu_data.pci_slot);
npu_data.used_memory = Some(usize::max(
npu_data.used_memory.unwrap_or(0),
processes_npu_memory_fraction as usize,
));
}

page.refresh_page(&npu_data);
}

std::mem::drop(apps_context);

/*
* Cpu
*/
Expand Down
49 changes: 48 additions & 1 deletion src/utils/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use gtk::{
};
use lazy_regex::{lazy_regex, Lazy, Regex};
use log::{debug, info, trace};
use process_data::{Containerization, GpuIdentifier, ProcessData};
use process_data::{pci_slot::PciSlot, Containerization, GpuIdentifier, ProcessData};

use crate::i18n::i18n;

Expand Down Expand Up @@ -640,6 +640,52 @@ impl AppsContext {
.clamp(0.0, 1.0)
}

pub fn npu_fraction(&self, pci_slot: PciSlot) -> f32 {
self.processes_iter()
.map(|process| {
(
&process.data.npu_usage_stats,
&process.npu_usage_stats_last,
process.data.timestamp,
process.timestamp_last,
)
})
.map(|(new, old, timestamp, timestamp_last)| {
(
new.get(&pci_slot),
old.get(&pci_slot),
timestamp,
timestamp_last,
)
})
.filter_map(|(new, old, timestamp, timestamp_last)| match (new, old) {
(Some(new), Some(old)) => Some((new, old, timestamp, timestamp_last)),
_ => None,
})
.map(|(new, old, timestamp, timestamp_last)| {
if old.usage == 0 {
0.0
} else {
((new.usage.saturating_sub(old.usage) as f32)
/ (timestamp.saturating_sub(timestamp_last) as f32))
.finite_or_default()
/ 1_000_000.0
}
})
.sum::<f32>()
.clamp(0.0, 1.0)
}

pub fn npu_mem(&self, pci_slot: PciSlot) -> u64 {
self.processes_iter()
.map(|process| process.data.npu_usage_stats.get(&pci_slot))
.map(|stats| match stats {
Some(stats) => stats.mem,
None => 0,
})
.sum()
}

fn app_associated_with_process(&self, process: &Process) -> Option<String> {
// TODO: tidy this up
// ↓ look for whether we can find an ID in the cgroup
Expand Down Expand Up @@ -797,6 +843,7 @@ impl AppsContext {
old_process.read_bytes_last = old_process.data.read_bytes;
old_process.write_bytes_last = old_process.data.write_bytes;
old_process.gpu_usage_stats_last = old_process.data.gpu_usage_stats.clone();
old_process.npu_usage_stats_last = old_process.data.npu_usage_stats.clone();

old_process.data = process_data.clone();
} else {
Expand Down
24 changes: 10 additions & 14 deletions src/utils/gpu/nvidia.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,16 @@ use process_data::GpuIdentifier;
use std::{path::PathBuf, sync::LazyLock};

static NVML: LazyLock<Result<Nvml, NvmlError>> = LazyLock::new(|| {
let nvml = Nvml::init();

if let Err(error) = nvml.as_ref() {
warn!("Connection to NVML failed, reason: {error}");
if *IS_FLATPAK {
warn!("This can occur when the version of the NVIDIA Flatpak runtime (org.freedesktop.Platform.GL.nvidia) \
and the version of the natively installed NVIDIA driver do not match. Consider updating both your system \
and Flatpak packages before opening an issue.")
}
} else {
debug!("Successfully connected to NVML");
}

nvml
Nvml::init()
.inspect_err(|err| {
warn!("Unable to connect to NVML: {err}");
if *IS_FLATPAK {
warn!("This can occur when the version of the NVIDIA Flatpak runtime \
(org.freedesktop.Platform.GL.nvidia) and the version of the natively installed NVIDIA driver do not \
match. Consider updating both your system and Flatpak packages before opening an issue.");
}
})
.inspect(|_| debug!("Successfully connected to NVML"))
});

use crate::utils::{pci::Device, IS_FLATPAK};
Expand Down
98 changes: 98 additions & 0 deletions src/utils/npu/amd.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
use anyhow::Result;
use process_data::pci_slot::PciSlot;

use std::path::PathBuf;

use crate::utils::pci::Device;

use super::NpuImpl;

#[derive(Debug, Clone, Default)]

pub struct AmdNpu {
pub device: Option<&'static Device>,
pub pci_slot: PciSlot,
pub driver: String,
sysfs_path: PathBuf,
first_hwmon_path: Option<PathBuf>,
}

impl AmdNpu {
pub fn new(
device: Option<&'static Device>,
pci_slot: PciSlot,
driver: String,
sysfs_path: PathBuf,
first_hwmon_path: Option<PathBuf>,
) -> Self {
Self {
device,
pci_slot,
driver,
sysfs_path,
first_hwmon_path,
}
}
}

impl NpuImpl for AmdNpu {
fn device(&self) -> Option<&'static Device> {
self.device
}

fn pci_slot(&self) -> PciSlot {
self.pci_slot
}

fn driver(&self) -> String {
self.driver.clone()
}

fn sysfs_path(&self) -> PathBuf {
self.sysfs_path.clone()
}

fn first_hwmon(&self) -> Option<PathBuf> {
self.first_hwmon_path.clone()
}

fn name(&self) -> Result<String> {
self.drm_name()
}

fn usage(&self) -> Result<f64> {
self.drm_usage().map(|usage| usage as f64 / 100.0)
}

fn used_memory(&self) -> Result<usize> {
self.drm_used_memory().map(|usage| usage as usize)
}

fn total_memory(&self) -> Result<usize> {
self.drm_total_memory().map(|usage| usage as usize)
}

fn temperature(&self) -> Result<f64> {
self.hwmon_temperature()
}

fn power_usage(&self) -> Result<f64> {
self.hwmon_power_usage()
}

fn core_frequency(&self) -> Result<f64> {
self.hwmon_core_frequency()
}

fn memory_frequency(&self) -> Result<f64> {
self.hwmon_vram_frequency()
}

fn power_cap(&self) -> Result<f64> {
self.hwmon_power_cap()
}

fn power_cap_max(&self) -> Result<f64> {
self.hwmon_power_cap_max()
}
}
8 changes: 4 additions & 4 deletions src/utils/npu/intel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,12 @@ impl NpuImpl for IntelNpu {
Ok((delta_busy_time / delta_timestamp) / 1000.0)
}

fn used_vram(&self) -> Result<usize> {
self.drm_used_vram().map(|usage| usage as usize)
fn used_memory(&self) -> Result<usize> {
self.drm_used_memory().map(|usage| usage as usize)
}

fn total_vram(&self) -> Result<usize> {
self.drm_total_vram().map(|usage| usage as usize)
fn total_memory(&self) -> Result<usize> {
self.drm_total_memory().map(|usage| usage as usize)
}

fn temperature(&self) -> Result<f64> {
Expand Down
Loading
Loading