From 53416e9cc1c4be13455cf957f0ad5c3118d849c8 Mon Sep 17 00:00:00 2001 From: LGUG2Z Date: Sat, 31 Oct 2026 14:47:04 -0700 Subject: [PATCH] perf(windows): look for komorebi-bar.exe by process name, before the window manager is locked replace-configuration called sysinfo::System::new_all() and refresh_processes under the window manager lock, only to know whether komorebi-bar.exe runs. new_all reads memory and CPU (it opens a performance-counter query, slow the first time), and for every process the command line, environment, owner and disk usage. The stall analysis measured the first replace-configuration of each e2e run holding the lock for about 3 s (17 of 17 runs). Now: komorebi_bar_is_running() reads only the process names (System::new + refresh_processes_specifics(All, true, nothing())), and the connection policy runs it in on_message, i.e. before the window manager is locked for a ReplaceConfiguration, and leaves the answer in a static for the handler to take (the handler scans itself, names only, if the answer is missing, e.g. the message came over TCP). The result is the same: whether a komorebi-bar.exe process exists, now decided when the message is read rather than after the new window manager is built. BEHAVIOUR: the lock hold of replace-configuration shrinks (expected from about 3 s to the preload/init/reload cost); a bar started or stopped between the read of the message and the end of the reload is not noticed. Gate needed: Windows e2e commands and config_reload, the lock stress, and the sweep timing (first replace-configuration, see the new per-step info lines). Verified: Windows clippy -D warnings via tools/fast-check PASS; e2e not run. --- komorebi/src/process_command.rs | 39 +++++++++++++++++++++++++++------ 1 file changed, 32 insertions(+), 7 deletions(-) diff --git a/komorebi/src/process_command.rs b/komorebi/src/process_command.rs index 980457da..31a61eb0 100644 --- a/komorebi/src/process_command.rs +++ b/komorebi/src/process_command.rs @@ -20,6 +20,7 @@ use std::net::TcpStream; use std::num::NonZeroUsize; use std::str::FromStr; use std::sync::Arc; +use std::sync::atomic::AtomicI8; use std::sync::atomic::Ordering; use std::time::Duration; use uds_windows::UnixListener; @@ -141,9 +142,36 @@ impl ConnectionPolicy for WindowsConnectionPolicy { ) { let _ = stream.set_read_timeout(Some(Duration::from_secs(30))); } + + // looked for here, before the window manager is locked for the message: a process scan + // under the lock held every other thread for seconds (see `replace_configuration`) + if matches!(message, SocketMessage::ReplaceConfiguration(_)) { + BAR_RUNNING.store(i8::from(komorebi_bar_is_running()), Ordering::SeqCst); + } } } +/// Whether komorebi-bar.exe was running when a `ReplaceConfiguration` was read, for the handler +/// of that message to take: -1 when it was not looked for +static BAR_RUNNING: AtomicI8 = AtomicI8::new(-1); + +/// Whether a komorebi-bar.exe process is running. Only the names of the processes are read: +/// `System::new_all` also opens the performance counters and reads the command line, the +/// environment and the owner of every process, which is the slow part. +fn komorebi_bar_is_running() -> bool { + let mut system = sysinfo::System::new(); + system.refresh_processes_specifics( + sysinfo::ProcessesToUpdate::All, + true, + sysinfo::ProcessRefreshKind::nothing(), + ); + + system + .processes_by_name("komorebi-bar.exe".as_ref()) + .next() + .is_some() +} + /// What the generic command listener needs of the window manager impl CommandTarget for WindowManager { type Listener = UnixListener; @@ -1629,13 +1657,10 @@ impl WindowManager { lap("swap"); // check if there are any bars - let mut system = sysinfo::System::new_all(); - system.refresh_processes(sysinfo::ProcessesToUpdate::All, true); - - let has_bar = system - .processes_by_name("komorebi-bar.exe".as_ref()) - .next() - .is_some(); + let has_bar = match BAR_RUNNING.swap(-1, Ordering::SeqCst) { + -1 => komorebi_bar_is_running(), + running => running == 1, + }; lap("process scan for komorebi-bar"); // stop bar(s)