-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
metadata: Extract facility to find process and thread names
The previous implementation was buggy (see PR for a screenshot of the profile). This commit fixes this and adds tests to this new logic. Test Plan ========= Added new tests + manual tests (see PR for screenshot).
- Loading branch information
1 parent
4e4a190
commit 753bd59
Showing
3 changed files
with
58 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
#[derive(Debug, PartialEq, Eq)] | ||
pub struct TaskName { | ||
pub main_thread: String, | ||
pub current_thread: String, | ||
} | ||
|
||
impl TaskName { | ||
pub fn errored() -> Self { | ||
TaskName { | ||
main_thread: "<could not fetch process name>".into(), | ||
current_thread: "<could not fetch thread name>".into(), | ||
} | ||
} | ||
|
||
pub fn for_task(task_id: i32) -> Result<TaskName, anyhow::Error> { | ||
let task = procfs::process::Process::new(task_id)?.stat()?; | ||
let main_task = procfs::process::Process::new(task.pgrp)?.stat()?; | ||
let thread_name = if task.pid == task.pgrp { | ||
"<main thread>".to_string() | ||
} else { | ||
task.comm | ||
}; | ||
Ok(TaskName { | ||
main_thread: main_task.comm, | ||
current_thread: thread_name, | ||
}) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use nix::unistd; | ||
use std::thread; | ||
|
||
#[test] | ||
fn test_thread_name() { | ||
let names = TaskName::for_task(unistd::getpgrp().as_raw()).unwrap(); | ||
assert_eq!(names.current_thread, "<main thread>"); | ||
|
||
let builder = thread::Builder::new().name("funky-thread-name".to_string()); | ||
|
||
builder | ||
.spawn(|| { | ||
let names = TaskName::for_task(unistd::gettid().as_raw()).unwrap(); | ||
assert_eq!(names.current_thread, "funky-thread-na"); | ||
}) | ||
.unwrap() | ||
.join() | ||
.unwrap(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters