Skip to content

Add focus boost for the 16-inch M4 Max - #97

Open
AxxzyWasTaken wants to merge 3 commits into
omacom:mainfrom
AxxzyWasTaken:m4-max-focus-boost
Open

AxxzyWasTaken wants to merge 3 commits into
omacom:mainfrom
AxxzyWasTaken:m4-max-focus-boost

Conversation

@AxxzyWasTaken

@AxxzyWasTaken AxxzyWasTaken commented Nov 7, 2026 •

Copy link
Copy Markdown

What changes

Adds omarchy-mac-focus-boost, a service for the 16-inch M4 Max (J616C) only. For 3 seconds after keyboard, trackpad or mouse input or a window change, it sets uclamp.min to 822 on every process of the focused Hyprland app, then back to 0.

Any focused windowed or fullscreen app doing sustained heavy work (>= 0.75 CPU cores, or >= 0.25 CPU cores while the G17 GPU reports effort_full=true, with 0.50 / 0.15 exit hysteresis) keeps the boost automatically without input, while desktop, browsing, and hardware-decoded video (< 0.15 CPU cores, effort_full=false) stay untouched at uclamp.min = 0 with 10 s idle wakeups.

During active 3D workloads (heavy_focus && effort_full=true), if idle_off_delay_ms is below 16 ms (for example 2 ms on battery Balanced, which otherwise triggers ~137 GPU power-cycles/sec during 120 fps games), it temporarily lifts idle_off_delay_ms to 16 ms and restores the original value on window switch, workload end, Low Power switch, service stop, or startup crash recovery via /run/omarchy-mac-focus-boost-gpu-idle (leaving external sysfs overrides untouched). In the low-power platform profile (cached at 1 s), both CPU boost and GPU idle hold stay off.

A blanket uclamp.min costs power all the time, and with none, short bursts run about three times slower:

20 ms page job every 250 ms, battery Power Job time
uclamp.min 0 10.32 W 56.6 ms
uclamp.min 822 10.60 W 19.2 ms

Boosting only on input and sustained focused workloads keeps video cheap: YouTube in Brave, untouched, was 15.6 W with a focus boost that didn't wait for input, and 11.9 W with this one.

It runs as root, because reading input, raising uclamp.min, and updating idle_off_delay_ms need it. Input events are only timed, never stored. It changes only processes whose real and effective uid match the verified Hyprland session owner's.

Verification

  • Rebased cleanly onto omacom/main (8205063).
  • omarchy-mac/test/focus-boost-test.sh and omarchy-mac/test/platform-test.sh: all checks pass (3/3 consecutive runs), including fake-socket event-loop tests for:
    • switching between boosted apps, late-spawned worker reset on hold expiry, rejecting symlinked/mismatched-uid session paths and other users' PIDs, dead-socket fallback, and SIGTERM reset;
    • windowed CPU-only heavy workloads (keeps CPU boost past hold, leaves GPU idle_off_delay_ms at 2 ms);
    • windowed 3D workloads (effort_full=true: keeps CPU boost and raises idle_off_delay_ms 2 -> 16 ms);
    • low-power profile immediately dropping both CPU boost and GPU idle hold;
    • external sysfs override preservation (50 ms left intact) and startup recover_stale() after an unclean exit.
  • Live hardware verification on 16-inch M4 Max (J616C, battery Balanced): windowed and fullscreen 120 fps Cordial (0.34.0) automatically promoted to P-cores (uclamp_min=822) and 16 ms GPU hold, restoring cleanly to uclamp_min=0 and 2 ms (cores_powered=false, 7.7 W battery desktop) within 2 s of unfocusing.

Screenshots

No visible change: it's a background service.

@greptile-apps

greptile-apps Bot commented Nov 7, 2026 •

Copy link
Copy Markdown

Confidence Score: 5/5RetriggerView in Greptile

[Medium impact] The PR appears safe to merge; no new actionable issues were found.

Summary

Adds a focus boost service for the 16-inch M4 Max (apple,j616c).

  • Focused apps get a short CPU boost after interaction.
  • Active 3D work can hold GPU idle timing longer.

The earlier threads were checked against the current code. Owner checks, bounded socket reads, exit cleanup, immediate scans after app switches, live-socket fallback, and the corrected shutdown instructions are present. No new actionable issues were found. Tests were not run during this review.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart TD
  A[J616C and Apple Silicon checks] --> B[Find a live user session]
  B --> C[Watch input and focus changes]
  C --> D{Low Power mode?}
  D -->|Yes| E[Release CPU boost and GPU hold]
  D -->|No| F{Recent input or sustained work?}
  F -->|Yes| G[Boost focused app processes]
  F -->|No| E
  G --> H{Active 3D work?}
  H -->|Yes| I[Temporarily extend GPU idle delay]
  H -->|No| J[Restore GPU idle delay]
  G --> C
  I --> C
  J --> C
  E --> C
Loading

Reviews (5) · Last reviewed commit: "omarchy-mac: extend focus boost to windo..." · Reviewed by Greptile

Comment thread omarchy-mac/bin/omarchy-mac-focus-boost Resolved
Comment thread omarchy-mac/bin/omarchy-mac-focus-boost Resolved
Comment thread omarchy-mac/bin/omarchy-mac-focus-boost Resolved
Comment thread omarchy-mac/bin/omarchy-mac-focus-boost Resolved
Comment thread omarchy-mac/README.md Resolved
@AxxzyWasTaken

Copy link
Copy Markdown
Author

Rebased onto main (35dc7ed) as 9dd244e and dropped the omarchy-mac/version bump so check-version-bump passes on PR branches. check-scope, check-architecture-gates, check-version-bump, and omarchy-mac/test/focus-boost-test.sh (4/4) all pass. Ready for re-review!

Comment thread omarchy-mac/bin/omarchy-mac-focus-boost Resolved
Comment thread omarchy-mac/bin/omarchy-mac-focus-boost Resolved
@AxxzyWasTaken

Copy link
Copy Markdown
Author

Pushed 627c76b addressing both Greptile P1 findings:

  • Session & socket ownership: session() now derives the session UID from lstat(/run/user/<uid>) (rejecting symlinks, UID 0, and mismatched directory names), validates hypr, the instance directory, and .socket2.sock via lstat (S_IFDIR/S_IFSOCK and matching owner), and run()/focused() verify SO_PEERCRED peer UID and reject symlinks/wrong-owner paths before trusting Hyprland replies.
  • Late-spawned workers: release() rescans app_pids(cgroups) and unions with boosted on hold expiry, app switch, and exit so children spawned after the last scan do not keep an inherited boost.

Added test cases for symlinked/wrong-UID session paths, wrong socket owner, and late-spawned worker reset on hold expiry; omarchy-mac/test/focus-boost-test.sh (4/4), check-scope, check-architecture-gates, and check-version-bump all pass.

Comment thread omarchy-mac/bin/omarchy-mac-focus-boost Resolved
@AxxzyWasTaken

Copy link
Copy Markdown
Author

Pushed c117ecc addressing the dead-socket review finding:

  • Added sessions() (verified candidates sorted newest-first) and connect_events() (safe_path + connect timeout + SO_PEERCRED check + OSError handling), so session() and main() skip leftover sockets without a live listener and fall back to an older active session instead of failing on the dead socket.
  • Logged unavailable session sockets once per path (avoiding repeat log spam on rescan) and moved the initial clamp() inside the try: block in run() so the event socket is always closed on exit.
  • Expanded omarchy-mac/test/focus-boost-test.sh to cover skipping a newer dead socket, falling back when the newer listener closes, and returning False from run() on a dead socket (4/4 pass).

A short burst of page work on the M4 Max stays slow because schedutil
takes time to raise the clock, and a blanket uclamp.min costs power all
the time, video included. The new service sets uclamp.min to 822 on the
focused Hyprland app for 3 seconds after input or a window change, then
back to 0. A fullscreen app using at least 0.75 of a core keeps the
boost without input. It runs only on the J616C and reads input only to
time it.

Signed-off-by: AxxzyWasTaken <axxzytte@gmail.com>
- Change only processes whose real and effective uid in
  /proc/<pid>/status are the session owner's, for the focused PID
  Hyprland reports and for every process uclampset touches, so a
  replaced socket can't steer the root service at other users.
- Derive the session owner from lstat(/run/user/<uid>) (non-root,
  directory name matching st_uid), verify every hypr path component and
  socket with lstat (no symlinks, matching owner), verify SO_PEERCRED
  peer uid on both Hyprland sockets, and skip dead leftover sockets so a
  crashed newer session does not block an older live one.
- Rescan the focused app's cgroups on hold expiry, app switch, and exit
  so worker children spawned after the last scan do not keep an
  inherited boost.
- Give up on an activewindow reply over 64 KiB or 1 s, and drop an
  event line over 64 KiB instead of buffering it.
- Reset every boosted process on SIGTERM, SIGINT and any other exit;
  KillMode=mixed keeps systemd's SIGTERM off an in-flight uclampset.
- A switch to another app clears the scan state, so the new app is
  boosted at once instead of at the next rescan, and a pending focus
  change no longer waits out the select timeout.
- README and manual: mask, not disable, turns it off, since the
  vendor wants link under /usr/lib can't be disabled.

The test now drives the event loop against fake Hyprland sockets:
switching between two boosted apps, resetting a late-spawned worker on
hold expiry, rejecting symlinked/mismatched-uid session paths and wrong
socket owners, skipping dead sockets in favor of an older live session,
a window owned by another uid, and SIGTERM resetting the boost.

Signed-off-by: AxxzyWasTaken <axxzytte@gmail.com>
… and low-power guard

Drop the fullscreen-only gate on sustained workload detection so both windowed and fullscreen games/heavy focused apps stay on P-cores automatically without affecting desktop or hardware-decoded video battery life:

- Monitor CPU usage of any focused app cgroup with hysteresis (enter at >= 0.75 cores, or >= 0.25 cores while G17 GPU reports effort_full=true; exit at 0.50 / 0.15) while keeping 10 s idle sleep when inactive.
- During active 3D workloads (heavy_focus && gpu_full), raise a short GPU idle_off_delay_ms (< 16 ms) to 16 ms to avoid ~137/s GPU core power-cycling stutter on battery Balanced, and restore the saved value on window switch, workload exit, low-power switch, service stop, or startup crash recovery via /run/omarchy-mac-focus-boost-gpu-idle (preserving external sysfs overrides).
- Respect the system low-power platform profile (cached at 1 s to avoid sysfs overhead on input events), disabling both CPU boost and GPU idle hold in Low Power mode.
- Update focus-boost-test.sh with unit + live event-loop tests covering crash recovery, external sysfs override preservation, windowed CPU-only vs 3D workloads, low-power drop, and SIGTERM cleanup, plus README and manual docs.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant