mirror of
https://github.com/ClementTsang/bottom.git
synced 2026-05-29 18:20:57 +00:00
reduce some allocations in amd step
This commit is contained in:
+1
-1
@@ -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"
|
||||
|
||||
@@ -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;
|
||||
};
|
||||
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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() {
|
||||
|
||||
Reference in New Issue
Block a user