mirror of
https://github.com/LGUG2Z/komorebi.git
synced 2026-11-19 23:28:34 +01:00
perf(windows): ask should_manage for an event before the window manager is locked
The stall analysis of the cand5 gate: the remaining real holds of 1 to 4.5 s are process_event for Show/Uncloak/FocusChange of freshly created windows, with no log line between the lock and the first line of the handler: that gap is Window::should_manage, i.e. Win32 calls on the new window (IsWindow, GetWindowRect, title, DwmGetWindowAttribute for the cloaked state, process name and path, class, styles) and rule matching, run with the lock held. Read: should_manage reads only the window, the rule lists (taken in the order documented at the statics in lib.rs, unchanged), the transparency manager's list of known layered windows (its own lock) and size settings. It does not touch the WindowManager. The event thread now calls ManageCheck::compute(event) before locking, and process_event_checked(event, check) uses it. The old process_event(event) stays and computes inline. Race windows this opens, and the guards. (1) The rule lists change between the check and the handling: only commands change them, so COMMAND_GENERATION is read before the check and incremented when each command ends (a drop guard in process_command, after the command has changed what it changes); if it differs under the lock the check is thrown away and computed again there, as it always was. (2) The window changes while the event waits for the lock (closed, cloaked, retitled): the wait was already there before (the event sat in the queue; the check used to run after the lock, now it runs up to one wait earlier) and the handlers cope with a window that is gone or different, as for any queued event; no state of the window manager is read from the old answer. (3) Paused: the check is wasted work, the paused test still comes first under the lock. BEHAVIOUR: events are classified up to one lock wait earlier. Gate needed: Windows e2e commands, config_reload, lifecycle, rules (rules_class, rules_exe), placement, scrolling, and the lock stress; the hold ranking of the next gate should lose the process_event holds of new windows. Verified: tools/fast-check Windows clippy -D warnings PASS; e2e not run.
This commit is contained in:
1 parent
f6ad736dca
commit
e24a446fe3
7 files changed
+97
-34
No files matched your search
@@ -131,8 +131,7 @@ impl AnimationEngine {
|
||||
ANIMATION_MANAGER.lock().start(key.as_str());
|
||||
}
|
||||
|
||||
let target_frame_time =
|
||||
Duration::from_millis(1000 / crate::settings::animation_fps());
|
||||
let target_frame_time = Duration::from_millis(1000 / crate::settings::animation_fps());
|
||||
let animation_start = Instant::now();
|
||||
|
||||
let mut stats = FrameStats::default();
|
||||
|
||||
@@ -15,13 +15,13 @@ use std::sync::atomic::Ordering;
|
||||
#[cfg(feature = "deadlock_detection")]
|
||||
use std::time::Duration;
|
||||
|
||||
use crate::animation::AnimationEngine;
|
||||
use crate::replace_env_in_path;
|
||||
use clap::Parser;
|
||||
use clap::ValueEnum;
|
||||
use color_eyre::eyre;
|
||||
use color_eyre::eyre::bail;
|
||||
use crossbeam_utils::Backoff;
|
||||
use crate::animation::AnimationEngine;
|
||||
use crate::replace_env_in_path;
|
||||
use parking_lot::Mutex;
|
||||
#[cfg(feature = "deadlock_detection")]
|
||||
use parking_lot::deadlock;
|
||||
|
||||
+6
-2
@@ -3,11 +3,11 @@
|
||||
#[macro_use]
|
||||
extern crate komorebi_collections;
|
||||
|
||||
pub mod daemon;
|
||||
pub mod animation;
|
||||
pub mod border_manager;
|
||||
pub mod cloaked;
|
||||
pub mod com;
|
||||
pub mod daemon;
|
||||
pub use komorebi_collections::ring;
|
||||
pub mod container;
|
||||
pub mod core;
|
||||
@@ -15,13 +15,13 @@ pub mod focus_manager;
|
||||
pub mod live_resize;
|
||||
pub use komorebi_collections::Lockable;
|
||||
pub use komorebi_collections::lockable_sequence;
|
||||
pub mod lock_holder;
|
||||
pub mod monitor;
|
||||
pub mod monitor_reconciliator;
|
||||
pub mod pan;
|
||||
pub mod process_command;
|
||||
pub mod process_event;
|
||||
pub mod process_movement;
|
||||
pub mod lock_holder;
|
||||
pub mod reaper;
|
||||
pub mod scrolling;
|
||||
pub mod set_window_position;
|
||||
@@ -268,6 +268,10 @@ pub static DEFAULT_RESIZE_DELTA: i32 = komorebi_settings::shared::DEFAULT_RESIZE
|
||||
|
||||
pub static DEFAULT_MOUSE_FOLLOWS_FOCUS: bool =
|
||||
komorebi_settings::shared::DEFAULT_MOUSE_FOLLOWS_FOCUS;
|
||||
/// Counts the commands the window manager has processed. An answer computed from the rule lists
|
||||
/// outside the lock (see `process_event::ManageCheck`) is only used if no command, which is what
|
||||
/// changes those lists, has run since.
|
||||
pub static COMMAND_GENERATION: AtomicU64 = AtomicU64::new(0);
|
||||
pub static INITIAL_CONFIGURATION_LOADED: AtomicBool = AtomicBool::new(false);
|
||||
pub static CUSTOM_FFM: AtomicBool = AtomicBool::new(false);
|
||||
pub static SESSION_ID: AtomicU32 = AtomicU32::new(0);
|
||||
|
||||
@@ -313,6 +313,15 @@ impl WindowManager {
|
||||
) -> eyre::Result<()> {
|
||||
let _held = crate::lock_holder::held(format!("command {message}"));
|
||||
let command_started = std::time::Instant::now();
|
||||
// when this command is over, an event answer computed before it is not used (see
|
||||
// `COMMAND_GENERATION`); the bump comes after the command has changed what it changes
|
||||
struct BumpGeneration;
|
||||
impl Drop for BumpGeneration {
|
||||
fn drop(&mut self) {
|
||||
crate::COMMAND_GENERATION.fetch_add(1, Ordering::SeqCst);
|
||||
}
|
||||
}
|
||||
let _bump_generation = BumpGeneration;
|
||||
if let Some(virtual_desktop_id) = &self.virtual_desktop_id
|
||||
&& let Some(id) = current_virtual_desktop()
|
||||
&& id != *virtual_desktop_id
|
||||
@@ -1443,10 +1452,12 @@ impl WindowManager {
|
||||
});
|
||||
}
|
||||
SocketMessage::Transparency(enable) => {
|
||||
crate::settings::settings().update(|settings| settings.shared.transparency.enabled = enable);
|
||||
crate::settings::settings()
|
||||
.update(|settings| settings.shared.transparency.enabled = enable);
|
||||
}
|
||||
SocketMessage::TransparencyAlpha(alpha) => {
|
||||
crate::settings::settings().update(|settings| settings.shared.transparency.alpha = alpha);
|
||||
crate::settings::settings()
|
||||
.update(|settings| settings.shared.transparency.alpha = alpha);
|
||||
}
|
||||
SocketMessage::StackbarMode(mode) => {
|
||||
STACKBAR_MODE.store(mode);
|
||||
|
||||
@@ -122,10 +122,14 @@ pub fn listen_for_events(wm: Arc<Mutex<WindowManager>>) {
|
||||
tracing::info!("listening");
|
||||
loop {
|
||||
if let Ok(event) = receiver.recv() {
|
||||
// Win32 calls on the event's window and rule matching need no window manager state, so
|
||||
// they are not done with the lock held (they took seconds for a window that had
|
||||
// just been created)
|
||||
let check = ManageCheck::compute(event);
|
||||
let waiting = std::time::Instant::now();
|
||||
let mut guard = crate::lock_holder::lock(&wm, "process_event");
|
||||
let handling = std::time::Instant::now();
|
||||
let result = guard.process_event(event);
|
||||
let result = guard.process_event_checked(event, check);
|
||||
let (waited, handled) = (handling.duration_since(waiting), handling.elapsed());
|
||||
if waited.max(handled) >= crate::lock_holder::SLOW {
|
||||
tracing::info!(
|
||||
@@ -157,23 +161,74 @@ pub fn listen_for_events(wm: Arc<Mutex<WindowManager>>) {
|
||||
});
|
||||
}
|
||||
|
||||
/// Whether an event's window is to be managed, found before the window manager is locked
|
||||
pub struct ManageCheck {
|
||||
should_manage: eyre::Result<bool>,
|
||||
rule_debug: RuleDebug,
|
||||
started: std::time::Instant,
|
||||
generation: u64,
|
||||
}
|
||||
|
||||
impl ManageCheck {
|
||||
/// Asks `Window::should_manage` for the window of the event. It reads the window, the rule
|
||||
/// lists (in their documented lock order) and settings, never the window manager.
|
||||
pub fn compute(event: WindowManagerEvent) -> Self {
|
||||
let generation = crate::COMMAND_GENERATION.load(std::sync::atomic::Ordering::SeqCst);
|
||||
let started = std::time::Instant::now();
|
||||
let mut rule_debug = RuleDebug::default();
|
||||
let should_manage = event.window().should_manage(Some(event), &mut rule_debug);
|
||||
tracing::debug!(
|
||||
"process_event: should_manage took {} ms",
|
||||
started.elapsed().as_millis()
|
||||
);
|
||||
|
||||
Self {
|
||||
should_manage,
|
||||
rule_debug,
|
||||
started,
|
||||
generation,
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether the answer can still be used: no command (which is what changes the rule lists)
|
||||
/// has been processed since it was computed
|
||||
fn is_current(&self) -> bool {
|
||||
self.generation == crate::COMMAND_GENERATION.load(std::sync::atomic::Ordering::SeqCst)
|
||||
}
|
||||
}
|
||||
|
||||
impl WindowManager {
|
||||
#[allow(clippy::too_many_lines, clippy::cognitive_complexity)]
|
||||
#[tracing::instrument(skip(self, event), fields(event = event.title(), winevent = event.winevent(), hwnd = event.hwnd()))]
|
||||
pub fn process_event(&mut self, event: WindowManagerEvent) -> eyre::Result<()> {
|
||||
self.process_event_checked(event, ManageCheck::compute(event))
|
||||
}
|
||||
|
||||
#[allow(clippy::too_many_lines, clippy::cognitive_complexity)]
|
||||
#[tracing::instrument(skip(self, event, check), fields(event = event.title(), winevent = event.winevent(), hwnd = event.hwnd()))]
|
||||
pub fn process_event_checked(
|
||||
&mut self,
|
||||
event: WindowManagerEvent,
|
||||
check: ManageCheck,
|
||||
) -> eyre::Result<()> {
|
||||
if self.is_paused {
|
||||
tracing::trace!("ignoring while paused");
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let mut rule_debug = RuleDebug::default();
|
||||
|
||||
let should_manage_started = std::time::Instant::now();
|
||||
let should_manage = event.window().should_manage(Some(event), &mut rule_debug)?;
|
||||
tracing::debug!(
|
||||
"process_event: should_manage took {} ms",
|
||||
should_manage_started.elapsed().as_millis()
|
||||
);
|
||||
// A command may have changed the rule lists while this event waited for the lock: the
|
||||
// answer is then asked again, here, as it used to be. The window itself may have changed
|
||||
// in the wait too; the handlers below already cope with a window that is gone or changed,
|
||||
// as they did for an event that sat in the queue before the lock was taken.
|
||||
let check = if check.is_current() {
|
||||
tracing::debug!(
|
||||
"process_event: should_manage answered {} ms before the lock was held",
|
||||
check.started.elapsed().as_millis()
|
||||
);
|
||||
check
|
||||
} else {
|
||||
ManageCheck::compute(event)
|
||||
};
|
||||
let rule_debug = check.rule_debug;
|
||||
let should_manage = check.should_manage?;
|
||||
|
||||
// All event handlers below this point should only be processed if the event is
|
||||
// related to a window that should be managed by the WindowManager.
|
||||
|
||||
@@ -311,12 +311,8 @@ impl From<&WindowManager> for StaticConfig {
|
||||
border_offset: Option::from(border_manager::BORDER_OFFSET.load(Ordering::SeqCst)),
|
||||
border: Option::from(border_manager::BORDER_ENABLED.load(Ordering::SeqCst)),
|
||||
border_colours,
|
||||
transparency: Option::from(
|
||||
crate::settings::transparency_enabled(),
|
||||
),
|
||||
transparency_alpha: Option::from(
|
||||
crate::settings::transparency_alpha(),
|
||||
),
|
||||
transparency: Option::from(crate::settings::transparency_enabled()),
|
||||
transparency_alpha: Option::from(crate::settings::transparency_alpha()),
|
||||
transparency_ignore_rules: None,
|
||||
border_style: Option::from(STYLE.load()),
|
||||
#[allow(deprecated)]
|
||||
|
||||
@@ -404,7 +404,6 @@ impl Window {
|
||||
|
||||
// enabled, duration and style from one snapshot
|
||||
if let Some((duration, style)) = crate::settings::animation_for(GhostDispatcher::PREFIX) {
|
||||
|
||||
let render_dispatcher =
|
||||
GhostDispatcher::new(self.hwnd, window_rect, *layout, top, style);
|
||||
|
||||
@@ -585,8 +584,9 @@ impl Window {
|
||||
|
||||
pub fn transparent(self) -> eyre::Result<()> {
|
||||
// enabled, duration and style from one snapshot
|
||||
if let Some((duration, style)) = crate::settings::animation_for(TransparencyRenderDispatcher::PREFIX) {
|
||||
|
||||
if let Some((duration, style)) =
|
||||
crate::settings::animation_for(TransparencyRenderDispatcher::PREFIX)
|
||||
{
|
||||
let render_dispatcher = TransparencyRenderDispatcher::new(
|
||||
self.hwnd,
|
||||
false,
|
||||
@@ -600,17 +600,15 @@ impl Window {
|
||||
let mut ex_style = self.ex_style()?;
|
||||
ex_style.insert(ExtendedWindowStyle::LAYERED);
|
||||
self.update_ex_style(&ex_style)?;
|
||||
WindowsApi::set_transparent(
|
||||
self.hwnd,
|
||||
crate::settings::transparency_alpha(),
|
||||
)
|
||||
WindowsApi::set_transparent(self.hwnd, crate::settings::transparency_alpha())
|
||||
}
|
||||
}
|
||||
|
||||
pub fn opaque(self) -> eyre::Result<()> {
|
||||
// enabled, duration and style from one snapshot
|
||||
if let Some((duration, style)) = crate::settings::animation_for(TransparencyRenderDispatcher::PREFIX) {
|
||||
|
||||
if let Some((duration, style)) =
|
||||
crate::settings::animation_for(TransparencyRenderDispatcher::PREFIX)
|
||||
{
|
||||
let render_dispatcher = TransparencyRenderDispatcher::new(
|
||||
self.hwnd,
|
||||
true,
|
||||
|
||||
Reference in new issue
Block a user