diff --git a/mux/src/lib.rs b/mux/src/lib.rs index 4c8b4d8f3..77ce4c9ad 100644 --- a/mux/src/lib.rs +++ b/mux/src/lib.rs @@ -31,6 +31,7 @@ pub mod localpane; pub mod pane; pub mod renderable; pub mod ssh; +mod sysinfo; pub mod tab; pub mod termwiztermtab; pub mod tmux; diff --git a/mux/src/localpane.rs b/mux/src/localpane.rs index 546e5547e..420118e62 100644 --- a/mux/src/localpane.rs +++ b/mux/src/localpane.rs @@ -341,11 +341,9 @@ impl Pane for LocalPane { fn get_foreground_process_name(&self) -> Option { #[cfg(unix)] { - use sysinfo::{Pid, ProcessExt, ProcessRefreshKind, RefreshKind, System, SystemExt}; + use sysinfo::{Pid, ProcessExt, SystemExt}; let leader = self.pty.borrow().process_group_leader()?; - let system = System::new_with_specifics( - RefreshKind::new().with_processes(ProcessRefreshKind::new()), - ); + let system = crate::sysinfo::get(); let proc = system.process(leader as Pid)?; Some(proc.exe().to_string_lossy().to_string()) } @@ -356,7 +354,7 @@ impl Pane for LocalPane { // so we infer that the equivalent to the process group // leader is the most recently spawned program running // in the console - if let Some(root_proc) = self.divine_process_list() { + if let Some(root_proc) = self.divine_process_list(false) { let mut youngest = &root_proc; fn find_youngest<'a>( @@ -382,7 +380,7 @@ impl Pane for LocalPane { } fn can_close_without_prompting(&self, _reason: CloseReason) -> bool { - if let Some(proc_list) = self.divine_process_list() { + if let Some(proc_list) = self.divine_process_list(true) { log::trace!( "can_close_without_prompting? procs in pane {:#?}", proc_list @@ -857,10 +855,17 @@ impl LocalPane { None } - fn divine_process_list(&self) -> Option { + fn divine_process_list(&self, force_refresh: bool) -> Option { #[cfg(any(windows, target_os = "linux", target_os = "macos"))] if let ProcessState::Running { pid: Some(pid), .. } = &*self.process.borrow() { - return LocalProcessInfo::with_root_pid(*pid); + return LocalProcessInfo::with_root_pid( + &*if force_refresh { + crate::sysinfo::get_with_forced_refresh() + } else { + crate::sysinfo::get() + }, + *pid, + ); } None @@ -937,13 +942,8 @@ impl LocalProcessInfo { #[cfg(any(windows, target_os = "linux", target_os = "macos"))] impl LocalProcessInfo { - fn with_root_pid(pid: u32) -> Option { - use sysinfo::{ - AsU32, Pid, Process, ProcessExt, ProcessRefreshKind, RefreshKind, System, SystemExt, - }; - let system = System::new_with_specifics( - RefreshKind::new().with_processes(ProcessRefreshKind::new()), - ); + fn with_root_pid(system: &sysinfo::System, pid: u32) -> Option { + use sysinfo::{AsU32, Pid, Process, ProcessExt, SystemExt}; fn build_proc(proc: &Process, processes: &HashMap) -> LocalProcessInfo { // Process has a `tasks` field but it does not correspond to child processes, diff --git a/mux/src/sysinfo.rs b/mux/src/sysinfo.rs new file mode 100644 index 000000000..c7fa94cbb --- /dev/null +++ b/mux/src/sysinfo.rs @@ -0,0 +1,57 @@ +#![cfg(any(windows, target_os = "linux", target_os = "macos"))] +use std::sync::{Mutex, MutexGuard}; +use std::time::{Duration, Instant}; +use sysinfo::{ProcessRefreshKind, RefreshKind, System, SystemExt}; + +lazy_static::lazy_static! { + static ref SYSTEM: Mutex = Mutex::new(CachedSystemInfo::new()); +} + +pub struct CachedSystemInfo { + last_update: Instant, + system: sysinfo::System, +} + +impl std::ops::Deref for CachedSystemInfo { + type Target = sysinfo::System; + + fn deref(&self) -> &sysinfo::System { + &self.system + } +} + +impl CachedSystemInfo { + pub fn new() -> Self { + Self { + system: System::new_with_specifics( + RefreshKind::new().with_processes(ProcessRefreshKind::new()), + ), + last_update: Instant::now(), + } + } + + pub fn refresh_now(&mut self) { + self.system + .refresh_processes_specifics(ProcessRefreshKind::new()); + self.last_update = Instant::now(); + } + + pub fn check_refresh(&mut self) { + if self.last_update.elapsed() < Duration::from_millis(1000) { + return; + } + self.refresh_now(); + } +} + +pub fn get() -> MutexGuard<'static, CachedSystemInfo> { + let mut guard = SYSTEM.lock().unwrap(); + guard.check_refresh(); + guard +} + +pub fn get_with_forced_refresh() -> MutexGuard<'static, CachedSystemInfo> { + let mut guard = SYSTEM.lock().unwrap(); + guard.refresh_now(); + guard +}