Reduce again the number of match calls (#7815)

- Reduce the number of match calls (see commit messages)
- A few miscellaneous improvements
This commit is contained in:
Hofer-Julian
2023-01-24 12:23:42 +01:00
committed by GitHub
parent 0bb2e47c98
commit 41306aa7e0
49 changed files with 467 additions and 634 deletions

View File

@ -12,10 +12,7 @@ pub enum ProcessTask {
impl ProcessTask {
pub fn stat(&self) -> Result<Stat, ProcError> {
match self {
ProcessTask::Process(x) => match x.stat() {
Ok(it) => Ok(it),
Err(err) => Err(err),
},
ProcessTask::Process(x) => x.stat(),
ProcessTask::Task { stat: x, owner: _ } => Ok(*x.clone()),
}
}
@ -36,13 +33,7 @@ impl ProcessTask {
pub fn fd(&self) -> Result<Vec<FDInfo>, ProcError> {
match self {
ProcessTask::Process(x) => {
let fds = match x.fd() {
Ok(f) => f,
Err(e) => return Err(e),
};
fds.collect()
}
ProcessTask::Process(x) => x.fd()?.collect(),
_ => Err(ProcError::Other("not supported".to_string())),
}
}
@ -113,10 +104,7 @@ pub fn collect_proc(interval: Duration, _with_thread: bool) -> Vec<ProcessInfo>
let curr_status = curr_proc.status().ok();
let curr_time = Instant::now();
let interval = curr_time - prev_time;
let ppid = match curr_proc.stat() {
Ok(p) => p.ppid,
Err(_) => 0,
};
let ppid = curr_proc.stat().map(|p| p.ppid).unwrap_or_default();
let curr_proc = ProcessTask::Process(curr_proc);
let proc = ProcessInfo {
@ -174,23 +162,25 @@ impl ProcessInfo {
/// Get the status of the process
pub fn status(&self) -> String {
match self.curr_proc.stat() {
Ok(p) => match p.state {
'S' => "Sleeping".into(),
'R' => "Running".into(),
'D' => "Disk sleep".into(),
'Z' => "Zombie".into(),
'T' => "Stopped".into(),
't' => "Tracing".into(),
'X' => "Dead".into(),
'x' => "Dead".into(),
'K' => "Wakekill".into(),
'W' => "Waking".into(),
'P' => "Parked".into(),
_ => "Unknown".into(),
},
Err(_) => "Unknown".into(),
if let Ok(p) = self.curr_proc.stat() {
match p.state {
'S' => "Sleeping",
'R' => "Running",
'D' => "Disk sleep",
'Z' => "Zombie",
'T' => "Stopped",
't' => "Tracing",
'X' => "Dead",
'x' => "Dead",
'K' => "Wakekill",
'W' => "Waking",
'P' => "Parked",
_ => "Unknown",
}
} else {
"Unknown"
}
.into()
}
/// CPU usage as a percent of total
@ -223,9 +213,6 @@ impl ProcessInfo {
/// Virtual memory size in bytes
pub fn virtual_size(&self) -> u64 {
match self.curr_proc.stat() {
Ok(p) => p.vsize,
Err(_) => 0u64,
}
self.curr_proc.stat().map(|p| p.vsize).unwrap_or_default()
}
}

View File

@ -194,18 +194,6 @@ pub fn collect_proc(interval: Duration, _with_thread: bool) -> Vec<ProcessInfo>
all_ok &= thread.is_some();
if all_ok {
// let process_params = unsafe { get_process_params(handle) };
// match process_params {
// Ok((pp_cmd, pp_env, pp_cwd)) => {
// eprintln!(
// "cmd: {:?}, env: {:?}, cwd: {:?}",
// pp_cmd,
// "noop".to_string(),
// pp_cwd
// );
// }
// Err(_) => {}
// }
let (proc_cmd, proc_env, proc_cwd) = match unsafe { get_process_params(handle) } {
Ok(pp) => (pp.0, pp.1, pp.2),
Err(_) => (vec![], vec![], PathBuf::new()),
@ -733,10 +721,7 @@ fn get_cmd_line_new(handle: HANDLE) -> Vec<String> {
fn get_cmd_line_old<T: RtlUserProcessParameters>(params: &T, handle: HANDLE) -> Vec<String> {
match params.get_cmdline(handle) {
Ok(buffer) => unsafe { get_cmdline_from_buffer(buffer.as_ptr()) },
Err(_e) => {
// sysinfo_debug!("get_cmd_line_old failed to get data: {}", _e);
Vec::new()
}
Err(_e) => Vec::new(),
}
}
@ -765,10 +750,7 @@ fn get_proc_env<T: RtlUserProcessParameters>(params: &T, handle: HANDLE) -> Vec<
}
result
}
Err(_e) => {
// sysinfo_debug!("get_proc_env failed to get data: {}", _e);
Vec::new()
}
Err(_e) => Vec::new(),
}
}