summaryrefslogtreecommitdiffstats
path: root/src/utils/process.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/utils/process.rs')
-rw-r--r--src/utils/process.rs256
1 files changed, 187 insertions, 69 deletions
diff --git a/src/utils/process.rs b/src/utils/process.rs
index 224eab87..a43ea77b 100644
--- a/src/utils/process.rs
+++ b/src/utils/process.rs
@@ -3,7 +3,7 @@ use std::path::Path;
use std::sync::{Arc, Condvar, Mutex, MutexGuard};
use lazy_static::lazy_static;
-use sysinfo::{Pid, Process, ProcessExt, ProcessRefreshKind, SystemExt};
+use sysinfo::{Pid, PidExt, Process, ProcessExt, ProcessRefreshKind, SystemExt};
#[derive(Clone, Debug, PartialEq)]
pub enum CallingProcess {
@@ -320,7 +320,7 @@ trait ProcessInterface {
self.process(parent_pid)
}
fn naive_sibling_process(&mut self, pid: Pid) -> Option<&Self::Out> {
- let sibling_pid = pid - 1;
+ let sibling_pid = Pid::from_u32(pid.as_u32() - 1);
self.refresh_process(sibling_pid).then(|| ())?;
self.process(sibling_pid)
}
@@ -397,7 +397,7 @@ impl ProcessInterface for ProcInfo {
type Out = Process;
fn my_pid(&self) -> Pid {
- std::process::id() as Pid
+ Pid::from_u32(std::process::id() as _)
}
fn refresh_process(&mut self, pid: Pid) -> bool {
self.info
@@ -480,12 +480,13 @@ where
*/
- let pid_range = my_pid.saturating_sub(10)..my_pid.saturating_add(20);
+ let my_pid_u32 = my_pid.as_u32();
+ let pid_range = my_pid_u32.saturating_sub(10)..my_pid_u32.saturating_add(20);
for p in pid_range {
// Processes which were not refreshed do not exist for sysinfo, so by selectively
// letting it know about processes the `find_sibling..` function will only
// consider these.
- info.refresh_process(p);
+ info.refresh_process(Pid::from_u32(p));
}
match info.find_sibling_in_refreshed_processes(my_pid, &extract_args) {
@@ -696,7 +697,7 @@ pub mod tests {
assert_eq!(guess_git_blame_filename_extension(&args), Args("".into()));
}
- #[derive(Debug, Default)]
+ #[derive(Debug)]
struct FakeProc {
#[allow(dead_code)]
pid: Pid,
@@ -704,6 +705,16 @@ pub mod tests {
cmd: Vec<String>,
ppid: Option<Pid>,
}
+ impl Default for FakeProc {
+ fn default() -> Self {
+ Self {
+ pid: Pid::from_u32(0),
+ start_time: 0,
+ cmd: Vec::new(),
+ ppid: None,
+ }
+ }
+ }
impl FakeProc {
fn new(pid: Pid, start_time: u64, cmd: Vec<String>, ppid: Option<Pid>) -> Self {
FakeProc {
@@ -727,15 +738,23 @@ pub mod tests {
}
}
- #[derive(Debug, Default)]
+ #[derive(Debug)]
struct MockProcInfo {
delta_pid: Pid,
info: HashMap<Pid, FakeProc>,
}
+ impl Default for MockProcInfo {
+ fn default() -> Self {
+ Self {
+ delta_pid: Pid::from_u32(0),
+ info: HashMap::new(),
+ }
+ }
+ }
impl MockProcInfo {
fn with(processes: &[(Pid, u64, &str, Option<Pid>)]) -> Self {
MockProcInfo {
- delta_pid: processes.last().map(|p| p.0).unwrap_or(1),
+ delta_pid: processes.last().map(|p| p.0).unwrap_or(Pid::from_u32(1)),
info: processes
.iter()
.map(|(pid, start_time, cmd, ppid)| {
@@ -872,10 +891,15 @@ pub mod tests {
#[test]
fn test_process_blame_no_parent_found() {
let two_trees = MockProcInfo::with(&[
- (2, 100, "-shell", None),
- (3, 100, "git blame src/main.rs", Some(2)),
- (4, 100, "call_delta.sh", None),
- (5, 100, "delta", Some(4)),
+ (Pid::from_u32(2), 100, "-shell", None),
+ (
+ Pid::from_u32(3),
+ 100,
+ "git blame src/main.rs",
+ Some(Pid::from_u32(2)),
+ ),
+ (Pid::from_u32(4), 100, "call_delta.sh", None),
+ (Pid::from_u32(5), 100, "delta", Some(Pid::from_u32(4))),
]);
assert_eq!(
calling_process_cmdline(two_trees, guess_git_blame_filename_extension),
@@ -892,9 +916,14 @@ pub mod tests {
);
let parent = MockProcInfo::with(&[
- (2, 100, "-shell", None),
- (3, 100, "git blame hello.txt", Some(2)),
- (4, 100, "delta", Some(3)),
+ (Pid::from_u32(2), 100, "-shell", None),
+ (
+ Pid::from_u32(3),
+ 100,
+ "git blame hello.txt",
+ Some(Pid::from_u32(2)),
+ ),
+ (Pid::from_u32(4), 100, "delta", Some(Pid::from_u32(3))),
]);
assert_eq!(
calling_process_cmdline(parent, guess_git_blame_filename_extension),
@@ -902,10 +931,20 @@ pub mod tests {
);
let grandparent = MockProcInfo::with(&[
- (2, 100, "-shell", None),
- (3, 100, "git blame src/main.rs", Some(2)),
- (4, 100, "call_delta.sh", Some(3)),
- (5, 100, "delta", Some(4)),
+ (Pid::from_u32(2), 100, "-shell", None),
+ (
+ Pid::from_u32(3),
+ 100,
+ "git blame src/main.rs",
+ Some(Pid::from_u32(2)),
+ ),
+ (
+ Pid::from_u32(4),
+ 100,
+ "call_delta.sh",
+ Some(Pid::from_u32(3)),
+ ),
+ (Pid::from_u32(5), 100, "delta", Some(Pid::from_u32(4))),
]);
assert_eq!(
calling_process_cmdline(grandparent, guess_git_blame_filename_extension),
@@ -916,10 +955,15 @@ pub mod tests {
#[test]
fn test_process_blame_info_with_sibling() {
let sibling = MockProcInfo::with(&[
- (2, 100, "-xterm", None),
- (3, 100, "-shell", Some(2)),
- (4, 100, "git blame src/main.rs", Some(3)),
- (5, 100, "delta", Some(3)),
+ (Pid::from_u32(2), 100, "-xterm", None),
+ (Pid::from_u32(3), 100, "-shell", Some(Pid::from_u32(2))),
+ (
+ Pid::from_u32(4),
+ 100,
+ "git blame src/main.rs",
+ Some(Pid::from_u32(3)),
+ ),
+ (Pid::from_u32(5), 100, "delta", Some(Pid::from_u32(3))),
]);
assert_eq!(
calling_process_cmdline(sibling, guess_git_blame_filename_extension),
@@ -927,17 +971,22 @@ pub mod tests {
);
let indirect_sibling = MockProcInfo::with(&[
- (2, 100, "-xterm", None),
- (3, 100, "-shell", Some(2)),
- (4, 100, "Git.exe blame --correct src/main.abc", Some(3)),
+ (Pid::from_u32(2), 100, "-xterm", None),
+ (Pid::from_u32(3), 100, "-shell", Some(Pid::from_u32(2))),
(
- 10,
+ Pid::from_u32(4),
+ 100,
+ "Git.exe blame --correct src/main.abc",
+ Some(Pid::from_u32(3)),
+ ),
+ (
+ Pid::from_u32(10),
100,
"Git.exe blame --ignored-child src/main.def",
- Some(4),
+ Some(Pid::from_u32(4)),
),
- (5, 100, "delta.sh", Some(3)),
- (20, 100, "delta", Some(5)),
+ (Pid::from_u32(5), 100, "delta.sh", Some(Pid::from_u32(3))),
+ (Pid::from_u32(20), 100, "delta", Some(Pid::from_u32(5))),
]);
assert_eq!(
calling_process_cmdline(indirect_sibling, guess_git_blame_filename_extension),
@@ -945,12 +994,22 @@ pub mod tests {
);
let indirect_sibling2 = MockProcInfo::with(&[
- (2, 100, "-xterm", None),
- (3, 100, "-shell", Some(2)),
- (4, 100, "git wrap src/main.abc", Some(3)),
- (10, 100, "git blame src/main.def", Some(4)),
- (5, 100, "delta.sh", Some(3)),
- (20, 100, "delta", Some(5)),
+ (Pid::from_u32(2), 100, "-xterm", None),
+ (Pid::from_u32(3), 100, "-shell", Some(Pid::from_u32(2))),
+ (
+ Pid::from_u32(4),
+ 100,
+ "git wrap src/main.abc",
+ Some(Pid::from_u32(3)),
+ ),
+ (
+ Pid::from_u32(10),
+ 100,
+ "git blame src/main.def",
+ Some(Pid::from_u32(4)),
+ ),
+ (Pid::from_u32(5), 100, "delta.sh", Some(Pid::from_u32(3))),
+ (Pid::from_u32(20), 100, "delta", Some(Pid::from_u32(5))),
]);
assert_eq!(
calling_process_cmdline(indirect_sibling2, guess_git_blame_filename_extension),
@@ -960,16 +1019,46 @@ pub mod tests {
// 3 blame processes, 2 with matching start times, pick the one with lower
// distance but larger start time difference.
let indirect_sibling_start_times = MockProcInfo::with(&[
- (2, 100, "-xterm", None),
- (3, 100, "-shell", Some(2)),
- (4, 109, "git wrap src/main.abc", Some(3)),
- (10, 109, "git blame src/main.def", Some(4)),
- (20, 100, "git wrap1 src/main.abc", Some(3)),
- (21, 100, "git wrap2 src/main.def", Some(20)),
- (22, 101, "git blame src/main.not", Some(21)),
- (23, 102, "git blame src/main.this", Some(20)),
- (5, 100, "delta.sh", Some(3)),
- (20, 100, "delta", Some(5)),
+ (Pid::from_u32(2), 100, "-xterm", None),
+ (Pid::from_u32(3), 100, "-shell", Some(Pid::from_u32(2))),
+ (
+ Pid::from_u32(4),
+ 109,
+ "git wrap src/main.abc",
+ Some(Pid::from_u32(3)),
+ ),
+ (
+ Pid::from_u32(10),
+ 109,
+ "git blame src/main.def",
+ Some(Pid::from_u32(4)),
+ ),
+ (
+ Pid::from_u32(20),
+ 100,
+ "git wrap1 src/main.abc",
+ Some(Pid::from_u32(3)),
+ ),
+ (
+ Pid::from_u32(21),
+ 100,
+ "git wrap2 src/main.def",
+ Some(Pid::from_u32(20)),
+ ),
+ (
+ Pid::from_u32(22),
+ 101,
+ "git blame src/main.not",
+ Some(Pid::from_u32(21)),
+ ),
+ (
+ Pid::from_u32(23),
+ 102,
+ "git blame src/main.this",
+ Some(Pid::from_u32(20)),
+ ),
+ (Pid::from_u32(5), 100, "delta.sh", Some(Pid::from_u32(3))),
+ (Pid::from_u32(20), 100, "delta", Some(Pid::from_u32(5))),
]);
assert_eq!(
calling_process_cmdline(
@@ -994,9 +1083,14 @@ pub mod tests {
last_arg: Some("hello.txt".to_string()),
};
let parent = MockProcInfo::with(&[
- (2, 100, "-shell", None),
- (3, 100, "git grep pattern hello.txt", Some(2)),
- (4, 100, "delta", Some(3)),
+ (Pid::from_u32(2), 100, "-shell", None),
+ (
+ Pid::from_u32(3),
+ 100,
+ "git grep pattern hello.txt",
+ Some(Pid::from_u32(2)),
+ ),
+ (Pid::from_u32(4), 100, "delta", Some(Pid::from_u32(3))),
]);
assert_eq!(
calling_process_cmdline(parent, describe_calling_process),
@@ -1004,9 +1098,14 @@ pub mod tests {
);
let parent = MockProcInfo::with(&[
- (2, 100, "-shell", None),
- (3, 100, "Git.exe grep pattern hello.txt", Some(2)),
- (4, 100, "delta", Some(3)),
+ (Pid::from_u32(2), 100, "-shell", None),
+ (
+ Pid::from_u32(3),
+ 100,
+ "Git.exe grep pattern hello.txt",
+ Some(Pid::from_u32(2)),
+ ),
+ (Pid::from_u32(4), 100, "delta", Some(Pid::from_u32(3))),
]);
assert_eq!(
calling_process_cmdline(parent, describe_calling_process),
@@ -1020,9 +1119,9 @@ pub mod tests {
"ack.exe pattern hello.txt",
] {
let parent = MockProcInfo::with(&[
- (2, 100, "-shell", None),
- (3, 100, grep_command, Some(2)),
- (4, 100, "delta", Some(3)),
+ (Pid::from_u32(2), 100, "-shell", None),
+ (Pid::from_u32(3), 100, grep_command, Some(Pid::from_u32(2))),
+ (Pid::from_u32(4), 100, "delta", Some(Pid::from_u32(3))),
]);
assert_eq!(
calling_process_cmdline(parent, describe_calling_process),
@@ -1040,9 +1139,14 @@ pub mod tests {
}));
let parent = MockProcInfo::with(&[
- (2, 100, "-shell", None),
- (3, 100, git_grep_command, Some(2)),
- (4, 100, "delta", Some(3)),
+ (Pid::from_u32(2), 100, "-shell", None),
+ (
+ Pid::from_u32(3),
+ 100,
+ git_grep_command,
+ Some(Pid::from_u32(2)),
+ ),
+ (Pid::from_u32(4), 100, "delta", Some(Pid::from_u32(3))),
]);
assert_eq!(
calling_process_cmdline(parent, describe_calling_process),
@@ -1050,10 +1154,20 @@ pub mod tests {
);
let grandparent = MockProcInfo::with(&[
- (2, 100, "-shell", None),
- (3, 100, git_grep_command, Some(2)),
- (4, 100, "call_delta.sh", Some(3)),
- (5, 100, "delta", Some(4)),
+ (Pid::from_u32(2), 100, "-shell", None),
+ (
+ Pid::from_u32(3),
+ 100,
+ git_grep_command,
+ Some(Pid::from_u32(2)),
+ ),
+ (
+ Pid::from_u32(4),
+ 100,
+ "call_delta.sh",
+ Some(Pid::from_u32(3)),
+ ),
+ (Pid::from_u32(5), 100, "delta", Some(Pid::from_u32(4))),
]);
assert_eq!(
calling_process_cmdline(grandparent, describe_calling_process),
@@ -1078,9 +1192,9 @@ pub mod tests {
),
] {
let parent = MockProcInfo::with(&[
- (2, 100, "-shell", None),
- (3, 100, command, Some(2)),
- (4, 100, "delta", Some(3)),
+ (Pid::from_u32(2), 100, "-shell", None),
+ (Pid::from_u32(3), 100, command, Some(Pid::from_u32(2))),
+ (Pid::from_u32(4), 100, "delta", Some(Pid::from_u32(3))),
]);
if let Some(CallingProcess::GitShow(cmd_line, ext)) =
calling_process_cmdline(parent, describe_calling_process)
@@ -1105,10 +1219,14 @@ pub mod tests {
info.refresh_processes();
let mut ppid_distance = Vec::new();
- iter_parents(&info, std::process::id() as Pid, |pid, distance| {
- ppid_distance.push(pid as i32);
- ppid_distance.push(distance as i32)
- });
+ iter_parents(
+ &info,
+ Pid::from_u32(std::process::id() as _),
+ |pid, distance| {
+ ppid_distance.push(pid.as_u32() as i32);
+ ppid_distance.push(distance as i32)
+ },
+ );
assert!(ppid_distance[1] == 1);