diff --git a/src/profiler/src/profiler.rs b/src/profiler/src/profiler.rs index 612d0988dd663fe113eed7c16740147168ccbb5d..040c9a2b87bdad6359e83e5b69a45ef6c77e2e71 100644 --- a/src/profiler/src/profiler.rs +++ b/src/profiler/src/profiler.rs @@ -17,11 +17,13 @@ use crate::symbollizer::symbolizer::Symbolizer; use crate::utils::lpm::Prefix; use crate::utils::time::init_tstamp; use crate::utils::time::time_delta; +use crate::utils::process::get_comm_by_pid; use crate::MIN_PROCESS_SAMPLES; use anyhow::Result; use std::collections::HashMap; use std::time::Instant; + pub struct Profiler<'a> { pids: HashMap, probes: Probes<'a>, @@ -261,7 +263,7 @@ impl<'a> Profiler<'a> { Ok(Some(a)) => a, Ok(None) => continue, Err(e) => { - log::error!("failed to get executable: {e}"); + log::error!("failed to get executable for comm {}: {:?}, err: {e}", get_comm_by_pid(pid), map); continue; } }; diff --git a/src/profiler/src/utils/process.rs b/src/profiler/src/utils/process.rs index b1b323af18369b42dc2e9159fd6c586e9b122caa..465bca1b9dfc3762c86c78e19d51c6f51c81ebd5 100644 --- a/src/profiler/src/utils/process.rs +++ b/src/profiler/src/utils/process.rs @@ -14,3 +14,40 @@ pub fn find_processes_by_comm(pat: &str) -> Vec { ret } + +pub fn get_comm_by_pid(pid: u32) -> String { + let path = format!("/proc/{}/comm", pid); + match std::fs::read_to_string(path) { + Ok(mut s) => { + s.pop(); // 去除末尾换行符 + s + } + Err(_) => "Unknown".to_string(), + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_valid_pid() { + // 获取当前进程的 PID + let current_pid = std::process::id(); + // 获取当前进程的 comm + let comm = get_comm_by_pid(current_pid); + // 验证 comm 不为空且不为 "Unknown" + assert!(!comm.is_empty(), "Expected non-empty comm for current process"); + assert_ne!(comm, "Unknown", "Expected valid comm for current process"); + log::info!("Current process (PID {}): {}", current_pid, comm); + } + + #[test] + fn test_invalid_pid() { + // 使用一个不可能存在的 PID + let invalid_pid = u32::MAX; // 4294967295 + let comm = get_comm_by_pid(invalid_pid); + assert_eq!(comm, "Unknown", "Expected 'Unknown' for invalid PID"); + log::info!("Invalid process (PID {}): {}", invalid_pid, comm); + } +} \ No newline at end of file