From bb741cf6b4780b6726afd7f5db002b43f0effd31 Mon Sep 17 00:00:00 2001 From: ClementTsang <34804052+ClementTsang@users.noreply.github.com> Date: Sun, 31 Aug 2025 01:20:21 -0400 Subject: [PATCH] reduce some allocations in amd step --- Cargo.toml | 2 +- src/collection/amd.rs | 15 +++++++++------ src/collection/processes/linux/mod.rs | 4 +--- src/collection/processes/unix/user_table.rs | 4 +++- 4 files changed, 14 insertions(+), 11 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 3f2df0a1..4da48754 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -124,7 +124,7 @@ strum = { version = "0.27.2", features = ["derive"], optional = true } libc = "0.2.175" [target.'cfg(target_os = "linux")'.dependencies] -rustix = { version = "1.0.8", features = ["fs", "param"] } +rustix = { version = "1.0.8", features = ["fs", "param", "alloc"] } [target.'cfg(target_os = "macos")'.dependencies] core-foundation = "0.10.1" diff --git a/src/collection/amd.rs b/src/collection/amd.rs index 18fe5d73..6b813037 100644 --- a/src/collection/amd.rs +++ b/src/collection/amd.rs @@ -162,7 +162,7 @@ fn diff_usage(pre: u64, cur: u64, interval: &Duration) -> u64 { } // from amdgpu_top: https://github.com/Umio-Yasuno/amdgpu_top/blob/c961cf6625c4b6d63fda7f03348323048563c584/crates/libamdgpu_top/src/stat/fdinfo/proc_info.rs#L13-L27 -fn get_amdgpu_pid_fds(pid: u32, device_path: Vec) -> Option> { +fn get_amdgpu_pid_fds(pid: u32, device_path: &[String]) -> Option> { let Ok(fd_list) = fs::read_dir(format!("/proc/{pid}/fd/")) else { return None; }; @@ -170,10 +170,13 @@ fn get_amdgpu_pid_fds(pid: u32, device_path: Vec) -> Option> { let valid_fds: Vec = fd_list .filter_map(|fd_link| { let dir_entry = fd_link.map(|fd_link| fd_link.path()).ok()?; - let link = fs::read_link(&dir_entry).ok()?; + let link = rustix::fs::readlink(&dir_entry, vec![]).ok()?; // e.g. "/dev/dri/renderD128" or "/dev/dri/card0" - if device_path.iter().any(|path| link.starts_with(path)) { + if device_path + .iter() + .any(|path| link.to_string_lossy().starts_with(path)) + { dir_entry.file_name()?.to_str()?.parse::().ok() } else { None @@ -188,7 +191,7 @@ fn get_amdgpu_pid_fds(pid: u32, device_path: Vec) -> Option> { } } -fn get_amdgpu_drm(device_path: &Path) -> Option> { +fn get_amdgpu_drm(device_path: &Path) -> Option> { let mut drm_devices = Vec::new(); let drm_root = device_path.join("drm"); @@ -212,7 +215,7 @@ fn get_amdgpu_drm(device_path: &Path) -> Option> { continue; } - drm_devices.push(PathBuf::from(format!("/dev/dri/{drm_name}"))); + drm_devices.push(format!("/dev/dri/{drm_name}")); } if drm_devices.is_empty() { @@ -254,7 +257,7 @@ fn get_amd_fdinfo(device_path: &Path) -> Option> { for pid in pids { // collect file descriptors that point to our device renderers - let Some(fds) = get_amdgpu_pid_fds(pid, drm_paths.clone()) else { + let Some(fds) = get_amdgpu_pid_fds(pid, &drm_paths) else { continue; }; diff --git a/src/collection/processes/linux/mod.rs b/src/collection/processes/linux/mod.rs index 992722e3..a64952f7 100644 --- a/src/collection/processes/linux/mod.rs +++ b/src/collection/processes/linux/mod.rs @@ -137,7 +137,7 @@ fn read_proc( thread_parent: Option, ) -> CollectionResult<(ProcessHarvest, u64)> { let Process { - pid: _pid, + pid: _, uid, stat, io, @@ -366,8 +366,6 @@ pub(crate) fn linux_process_data( prev_non_idle, } = prev_proc; - // TODO: [PROC THREADS] Add threads - let CpuUsage { mut cpu_usage, cpu_fraction, diff --git a/src/collection/processes/unix/user_table.rs b/src/collection/processes/unix/user_table.rs index a9d48378..b2836a51 100644 --- a/src/collection/processes/unix/user_table.rs +++ b/src/collection/processes/unix/user_table.rs @@ -8,11 +8,13 @@ pub struct UserTable { } impl UserTable { + /// Get the username associated with a UID. On first access of a name, it will + /// be cached for future accesses. pub fn uid_to_username(&mut self, uid: libc::uid_t) -> CollectionResult { if let Some(user) = self.uid_user_mapping.get(&uid) { Ok(user.clone()) } else { - // SAFETY: getpwuid returns a null pointer if no passwd entry is found for the uid. + // SAFETY: getpwuid returns a null pointer if no passwd entry is found for the uid which we check. let passwd = unsafe { libc::getpwuid(uid) }; if passwd.is_null() {