From 7ccbb7cafe529a1505856958b68d43111018f8bc Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Tue, 14 Nov 2023 22:35:23 +0100 Subject: [PATCH 1/5] Update `Process::refresh_process` and `Process::refresh_processes` default `ProcessRefreshKind` used --- src/common.rs | 53 ++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 48 insertions(+), 5 deletions(-) diff --git a/src/common.rs b/src/common.rs index 23b1a9805..f0599572f 100644 --- a/src/common.rs +++ b/src/common.rs @@ -234,11 +234,24 @@ impl System { /// Gets all processes and updates their information. /// - /// It does the same as `system.refresh_processes_specifics(ProcessRefreshKind::everything())`. + /// It does the same as: + /// + /// ```no_run + /// # use sysinfo::{ProcessRefreshKind, System}; + /// # let mut system = System::new(); + /// system.refresh_processes_specifics( + /// ProcessRefreshKind::new() + /// .with_memory() + /// .with_cpu() + /// .with_disk_usage(), + /// ); + /// ``` /// /// ⚠️ On Linux, `sysinfo` keeps the `stat` files open by default. You can change this behaviour /// by using [`set_open_files_limit`][crate::set_open_files_limit]. /// + /// Example: + /// /// ```no_run /// use sysinfo::System; /// @@ -246,7 +259,12 @@ impl System { /// s.refresh_processes(); /// ``` pub fn refresh_processes(&mut self) { - self.refresh_processes_specifics(ProcessRefreshKind::everything()); + self.refresh_processes_specifics( + ProcessRefreshKind::new() + .with_memory() + .with_cpu() + .with_disk_usage(), + ); } /// Gets all processes and updates the specified information. @@ -268,8 +286,24 @@ impl System { /// exist (it will **NOT** be removed from the processes if it doesn't exist anymore). If it /// isn't listed yet, it'll be added. /// - /// It is the same as calling - /// `sys.refresh_process_specifics(pid, ProcessRefreshKind::everything())`. + /// It is the same as calling: + /// + /// ```no_run + /// # use sysinfo::{ProcessRefreshKind, System}; + /// # let mut system = System::new(); + /// system.refresh_process_specifics( + /// pid, + /// ProcessRefreshKind::new() + /// .with_memory() + /// .with_cpu() + /// .with_disk_usage(), + /// ); + /// ``` + /// + /// ⚠️ On Linux, `sysinfo` keeps the `stat` files open by default. You can change this behaviour + /// by using [`set_open_files_limit`][crate::set_open_files_limit]. + /// + /// Example: /// /// ```no_run /// use sysinfo::{Pid, System}; @@ -278,13 +312,22 @@ impl System { /// s.refresh_process(Pid::from(1337)); /// ``` pub fn refresh_process(&mut self, pid: Pid) -> bool { - self.refresh_process_specifics(pid, ProcessRefreshKind::everything()) + self.refresh_process_specifics( + pid, + ProcessRefreshKind::new() + .with_memory() + .with_cpu() + .with_disk_usage(), + ) } /// Refreshes *only* the process corresponding to `pid`. Returns `false` if the process doesn't /// exist (it will **NOT** be removed from the processes if it doesn't exist anymore). If it /// isn't listed yet, it'll be added. /// + /// ⚠️ On Linux, `sysinfo` keeps the `stat` files open by default. You can change this behaviour + /// by using [`set_open_files_limit`][crate::set_open_files_limit]. + /// /// ```no_run /// use sysinfo::{Pid, ProcessRefreshKind, System}; /// From f447b0e8d186c386ab4938e2f85bf2b34b3126ec Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Tue, 14 Nov 2023 22:42:16 +0100 Subject: [PATCH 2/5] Improve documentation for `ProcessRefreshKind` --- src/common.rs | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/src/common.rs b/src/common.rs index f0599572f..62a746090 100644 --- a/src/common.rs +++ b/src/common.rs @@ -289,8 +289,9 @@ impl System { /// It is the same as calling: /// /// ```no_run - /// # use sysinfo::{ProcessRefreshKind, System}; + /// # use sysinfo::{Pid, ProcessRefreshKind, System}; /// # let mut system = System::new(); + /// # let pid = Pid::from(0); /// system.refresh_process_specifics( /// pid, /// ProcessRefreshKind::new() @@ -1420,6 +1421,12 @@ assert_eq!(r.", stringify!($name), "().is_some(), false); /// Used to determine what you want to refresh specifically on the [`Process`] type. /// +/// When all refresh are ruled out, a [`Process`] will still retrieve the following information: +/// * Process ID ([`Pid`]) +/// * Parent process ID +/// * Process name +/// * Start time +/// /// ⚠️ Just like all other refresh types, ruling out a refresh doesn't assure you that /// the information won't be retrieved if the information is accessible without needing /// extra computation. @@ -1498,7 +1505,19 @@ impl ProcessRefreshKind { with_disk_usage, without_disk_usage ); - impl_get_set!(ProcessRefreshKind, user, with_user, without_user); + impl_get_set!( + ProcessRefreshKind, + user, + with_user, + without_user, + "\ +It will retrieve the following information: + + * user ID + * user effective ID (if available on the platform) + * user group ID (if available on the platform) + * user effective ID (if available on the platform)" + ); impl_get_set!(ProcessRefreshKind, memory, with_memory, without_memory); impl_get_set!(ProcessRefreshKind, cwd, with_cwd, without_cwd); impl_get_set!(ProcessRefreshKind, root, with_root, without_root); From 347785d17a9e3019986f1e051dc764a0553ff162 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Tue, 14 Nov 2023 23:17:10 +0100 Subject: [PATCH 3/5] Use new "return impl" syntax --- src/common.rs | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/src/common.rs b/src/common.rs index 62a746090..f557f5362 100644 --- a/src/common.rs +++ b/src/common.rs @@ -390,16 +390,13 @@ impl System { /// println!("{} {}", process.pid(), process.name()); /// } /// ``` - // FIXME: replace the returned type with `impl Iterator` when it's supported! pub fn processes_by_name<'a: 'b, 'b>( &'a self, name: &'b str, - ) -> Box + 'b> { - Box::new( - self.processes() - .values() - .filter(move |val: &&Process| val.name().contains(name)), - ) + ) -> impl Iterator + 'b { + self.processes() + .values() + .filter(move |val: &&Process| val.name().contains(name)) } /// Returns an iterator of processes with exactly the given `name`. @@ -421,16 +418,13 @@ impl System { /// println!("{} {}", process.pid(), process.name()); /// } /// ``` - // FIXME: replace the returned type with `impl Iterator` when it's supported! pub fn processes_by_exact_name<'a: 'b, 'b>( &'a self, name: &'b str, - ) -> Box + 'b> { - Box::new( - self.processes() - .values() - .filter(move |val: &&Process| val.name() == name), - ) + ) -> impl Iterator + 'b { + self.processes() + .values() + .filter(move |val: &&Process| val.name() == name) } /// Returns "global" CPUs information (aka the addition of all the CPUs). From cdff8d4d9d15dc736a9ebd3df59b081192053da4 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Tue, 14 Nov 2023 23:17:45 +0100 Subject: [PATCH 4/5] Use `as_os_str` instead of `Path::new("")` comparison --- src/unix/linux/process.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/unix/linux/process.rs b/src/unix/linux/process.rs index f7977d06e..0ae4de88a 100644 --- a/src/unix/linux/process.rs +++ b/src/unix/linux/process.rs @@ -373,7 +373,7 @@ fn update_proc_info( refresh_user_group_ids(p, proc_path); } - if refresh_kind.exe() && p.exe == Path::new("") { + if refresh_kind.exe() && p.exe.as_os_str().is_empty() { match proc_path.join("exe").read_link() { Ok(exe_path) => p.exe = exe_path, Err(_error) => { From 9fa21957cddb14a42058029043b821441b89f50d Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Tue, 14 Nov 2023 23:18:02 +0100 Subject: [PATCH 5/5] Update process refresh tests --- tests/process.rs | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/tests/process.rs b/tests/process.rs index 32c12f810..c7baf243a 100644 --- a/tests/process.rs +++ b/tests/process.rs @@ -1,6 +1,6 @@ // Take a look at the license at the top of the repository in the LICENSE file. -use sysinfo::{Pid, System}; +use sysinfo::{Pid, ProcessRefreshKind, System}; #[test] fn test_process() { @@ -14,7 +14,14 @@ fn test_process() { assert!(s .processes() .values() - .any(|p| !p.exe().to_str().unwrap_or("").is_empty())); + .all(|p| p.exe().as_os_str().is_empty())); + + let mut s = System::new(); + s.refresh_processes_specifics(ProcessRefreshKind::new().with_exe()); + assert!(s + .processes() + .values() + .any(|p| !p.exe().as_os_str().is_empty())); } #[test] @@ -41,7 +48,7 @@ fn test_cwd() { let pid = Pid::from_u32(p.id() as _); std::thread::sleep(std::time::Duration::from_secs(1)); let mut s = System::new(); - s.refresh_processes(); + s.refresh_processes_specifics(ProcessRefreshKind::new().with_cwd()); p.kill().expect("Unable to kill process."); let processes = s.processes(); @@ -78,7 +85,7 @@ fn test_cmd() { std::thread::sleep(std::time::Duration::from_millis(500)); let mut s = System::new(); assert!(s.processes().is_empty()); - s.refresh_processes(); + s.refresh_processes_specifics(ProcessRefreshKind::new().with_cmd()); p.kill().expect("Unable to kill process."); assert!(!s.processes().is_empty()); if let Some(process) = s.process(Pid::from_u32(p.id() as _)) { @@ -153,7 +160,7 @@ fn test_environ() { let pid = Pid::from_u32(p.id() as _); let mut s = System::new(); - s.refresh_processes(); + s.refresh_processes_specifics(ProcessRefreshKind::new().with_environ()); let processes = s.processes(); let proc_ = processes.get(&pid); @@ -179,7 +186,19 @@ fn test_process_refresh() { s.refresh_process(sysinfo::get_current_pid().expect("failed to get current pid")); assert!(s .process(sysinfo::get_current_pid().expect("failed to get current pid")) - .is_some(),); + .is_some()); + + assert!(s + .processes() + .iter() + .all(|(_, p)| p.exe().as_os_str().is_empty() + && p.environ().is_empty() + && p.cwd().as_os_str().is_empty() + && p.cmd().is_empty())); + assert!(s + .processes() + .iter() + .any(|(_, p)| !p.name().is_empty() && p.memory() != 0)); } #[test]