Skip to content

Skip clamshell poll syncs unless monitor state changed - #17115

Open
RandintN wants to merge 1 commit into
omacom:quattrofrom
RandintN:reduce-clamshell-poll-cpu
Open

RandintN wants to merge 1 commit into
omacom:quattrofrom
RandintN:reduce-clamshell-poll-cpu

Conversation

@RandintN

@RandintN RandintN commented Oct 30, 2026 •

Copy link
Copy Markdown

What

The docked reconciliation poll in omarchy-hyprland-monitor-watch runs a full sync_clamshell every 2s, forking a hyprctl + jq pass and clamshell children each tick, even when nothing moved.

The only clamshell input with no event is the lid. Monitors arrive on the socket the watcher already reads, and the switch:*:Lid Switch binds cover the lid on hardware whose switch Hyprland can read. So the poll now compares the lid from the ACPI button file, which costs a builtin read and no fork, and runs a sync on a change, plus one a minute for what no comparison can see:

  • the logind HandleLidSwitchDocked policy omarchy-system-lid-inhibit reads
  • drift that leaves both the lid and the monitor list untouched

On a machine with no ACPI lid (Apple Silicon) it falls back to omarchy-hw-laptop-closed, the same helper clamshell decides with.

The lid is recorded only after a sync succeeds, so a correction that failed, or that found the lock taken by another sync, is retried on the next tick. sync_clamshell now reports a failure from either helper, and reports the lock as taken rather than done, which is what that retry reads.

Measured

Docked laptop, idle, 70s window:

Before After
Jiffies 160 per 30s 21 per 70s
% of one core 5.40 0.30
Jiffies per minute 320 18
Syncs at idle 30 per minute 1 per minute

The cost of one tick, measured against the real binaries: hyprctl monitors all -j | jq | cksum is 0.70 jiffies, the ACPI lid read is 0.00, the lid helper fork 0.18, sleep 2 0.10, and a full clamshell sync 8.0.

Behaviour, against the watcher with stubbed hyprctl, lid, clamshell and lock:

Scenario Syncs
lid closed 1
monitor removed 3 (the retry chain)
nothing changed, 5s 0
clamshell exits 1 one attempt per tick

Alternatives measured

Built and measured in separate worktrees, same harness, same machine:

Design Idle, docked Lid reaction Verdict
poll removed, events only ~0% none rejected: the switch bind does not fire on hardware where Hyprland cannot read the switch
fingerprint of lid + monitor fields, 60s backstop 0.80% 1 sync what an earlier revision of this PR did
full fingerprint with 2s to 60s backoff ~0.2% up to 60s late rejected: lid latency
tick folded into the event loop with read -t 2 0.14% 1 sync best number, larger diff: rewrites the loop and removes sync_poll_state
lid comparison only (this PR) 0.30% 1 sync keeps the existing structure, 16 added lines

Verified on real hardware

Samsung Galaxy Book (Intel, Hyprland 0.57.0), docked to a DisplayPort monitor at 2560x1440@240, with the default lid binds removed so the poll is the only path that reconciles clamshell:

19:30:36 systemd-logind: Lid closed.
19:30:38 internal-monitor-scale written   <- clamshell ran, 2.8s after the lid
19:30:38 Hyprland: Disabling output eDP-1
19:30:51 systemd-logind: Lid opened.
19:30:51 Hyprland: Modesetting eDP-1 with [email protected]

Suspend and resume, docked and undocked, and undocking with the lid shut were exercised on the same machine; the watcher and its poll kept their PIDs across a suspend, and the monitorremoved handler re-enabled the internal panel before logind suspended.

Tests

  • bash test/shell.d/lid-inhibit-test.sh passes, including "the watcher keeps one inhibitor across hotplugs, retries and its poll".
  • bash test/shell.d/monitor-modeless-test.sh passes.
  • bash test/shell.d/lid-close-test.sh passes.
  • bash -n clean.

@greptile-apps

greptile-apps Bot commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] No new blocking issue was found.

Summary

Reduces repeated clamshell work while a laptop is docked.

  • The docked poll syncs on lid changes and once a minute.

No new findings were accepted. Earlier unnumbered threads remain review context and were not reposted.

Reviews (7) · Last reviewed commit: "Skip clamshell poll syncs unless the lid..." · Reviewed by Greptile

Comment thread bin/omarchy-hyprland-monitor-watch Resolved
Comment thread bin/omarchy-hyprland-monitor-watch Resolved
@RandintN
RandintN force-pushed the reduce-clamshell-poll-cpu branch 2 times, most recently from 3b90b6c to 70dfc09 Compare October 30, 2026 22:39
@RandintN

Copy link
Copy Markdown
Author

On the "failed corrections stop retrying" finding: fixed in 70dfc09.

The fingerprint is now recorded only when sync_clamshell reports success, and sync_clamshell reports a failure from either helper instead of the status of whichever ran last:

sync_clamshell() {
  (
    local status=0

    flock -n 9 || exit 0
    omarchy-hyprland-monitor-clamshell || status=1
    omarchy-system-lid-inhibit || status=1
    exit "$status"
  ) 9>"$LOCK"
}

With a clamshell stub that exits 1, the poll now runs one attempt per tick instead of one total: 3 attempts in a 6s window, versus 0 before the fix. With the stub succeeding, a lid flip and a monitor change still run exactly one sync and idle runs none.

Comment thread bin/omarchy-hyprland-monitor-watch Resolved
@RandintN
RandintN force-pushed the reduce-clamshell-poll-cpu branch from 70dfc09 to a1a432d Compare October 30, 2026 22:48
@RandintN

Copy link
Copy Markdown
Author

Second finding covered in a1a432d: a sync that found the lock taken was reported as done.

flock -n 9 || exit 0 meant a lid change landing while another sync held the lock recorded its fingerprint and went quiet, so the correction was lost. Measured with the lock held across a lid flip: 0 syncs after the lock freed. It is now exit 1, and the same harness runs the correction as soon as the lock frees:

syncs before the lid moved: 5
syncs after the lock freed: 6
corrections run: 1 (expect 1)

The poll retries every tick while the compositor stops answering, and a lid flip after it returns still lands (verified with a hyprctl stub that fails for five seconds).

Comment thread bin/omarchy-hyprland-monitor-watch Resolved
@RandintN
RandintN force-pushed the reduce-clamshell-poll-cpu branch from 0749217 to 49a18cd Compare October 30, 2026 22:54
@RandintN

Copy link
Copy Markdown
Author

All three findings, on 49a18cd:

Opened laptop screen stays off (P1) — the lid is in the fingerprint, which is what 70dfc09 added after that review (it ran against 8391aa0, the monitor-only version). The lid is read through omarchy-hw-laptop-closed, the same helper clamshell decides with.

Failed corrections stop retrying (P2) — fixed in a1a432d: the fingerprint is recorded only after sync_clamshell succeeds, and sync_clamshell now surfaces a failure from either helper instead of the status of whichever ran last. With a clamshell stub that exits 1, the poll attempts once per tick instead of once total.

Lid policy changes are ignored (P2) — valid and fixed in 49a18cd. The gate skipped omarchy-system-lid-inhibit's HandleLidSwitchDocked read along with everything else, so a policy change went unnoticed until a monitor or lid change. The poll now runs one full sync a minute regardless of the fingerprint, which also covers any drift that leaves the monitor list untouched. Measured cost of that: 0.17% of one core, against 5.4% before the change.

syncs while idle for 70s: 1 (expect 1, the 60s backstop)
syncs after a lid flip: 1 (expect 1)

The docked reconciliation poll ran a full sync every 2s even when nothing
moved. Its only input with no event is the lid: monitors arrive on the
socket, and the switch binds cover the lid only where Hyprland can read
the switch. Compare the lid from the ACPI button file (a builtin read, no
fork) and sync on a change, plus one a minute for what no comparison can
see: the logind lid policy omarchy-system-lid-inhibit reads, and drift
that leaves both the lid and the monitor list untouched.

The lid is recorded only after a sync succeeds, so a correction that
failed, or that found the lock taken by another sync, is retried on the
next tick. sync_clamshell now reports a failure from either helper, and
reports the lock as taken rather than done, which is what that retry
reads.

Measured on a docked laptop at idle: 5.4% -> 0.30% of one core, 49 to 21
jiffies per minute, with the sync still running on a lid or monitor
change.
@RandintN
RandintN force-pushed the reduce-clamshell-poll-cpu branch from 49a18cd to df29182 Compare October 30, 2026 23:59
@RandintN

Copy link
Copy Markdown
Author

df29182 replaces the fingerprint with a comparison of the one input that has no event.

Monitors already arrive on the socket this watcher reads, so hashing their fields every 2s was work for a state the loop is told about anyway. The lid is the only clamshell input with no event on hardware whose switch Hyprland cannot read, and it is a builtin read of the ACPI button file with no fork. The minute backstop stays for the logind policy and for drift neither the lid nor the monitor list would show.

The earlier revision, which hashed the lid plus six monitor fields, measured 0.80% of one core docked. This one measures 0.30%, with the same behaviour: one sync on a lid change, the retry chain on a monitor event, none at idle.

I also measured two designs that go further, in separate worktrees on the same machine:

Design Idle, docked Lid reaction
tick folded into the event loop (read -t 2), poll process removed 0.14% 1 sync
poll removed entirely, events only ~0% none, the bind does not fire here

The read -t 2 version is cheaper and drops a process, at the cost of rewriting the loop and removing sync_poll_state, which two earlier commits added on purpose. I kept this one at 16 added lines so the review stays about the comparison, and can send the other as a follow-up if you want the extra 0.16 points.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants