reduce some allocations in amd step

This commit is contained in:
ClementTsang
2025-08-31 01:20:21 -04:00
parent 8015f0c37a
commit bb741cf6b4
4 changed files with 14 additions and 11 deletions
+1 -1
View File
@@ -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"
+9 -6
View File
@@ -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<PathBuf>) -> Option<Vec<u32>> {
fn get_amdgpu_pid_fds(pid: u32, device_path: &[String]) -> Option<Vec<u32>> {
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<PathBuf>) -> Option<Vec<u32>> {
let valid_fds: Vec<u32> = 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::<u32>().ok()
} else {
None
@@ -188,7 +191,7 @@ fn get_amdgpu_pid_fds(pid: u32, device_path: Vec<PathBuf>) -> Option<Vec<u32>> {
}
}
fn get_amdgpu_drm(device_path: &Path) -> Option<Vec<PathBuf>> {
fn get_amdgpu_drm(device_path: &Path) -> Option<Vec<String>> {
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<Vec<PathBuf>> {
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<HashMap<u32, AmdGpuProc>> {
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;
};
+1 -3
View File
@@ -137,7 +137,7 @@ fn read_proc(
thread_parent: Option<Pid>,
) -> 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,
+3 -1
View File
@@ -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<String> {
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() {