Skip to content

Port omarchy-powerd daemon to C99 with native sd-bus - #231

Merged
henrikklee merged 6 commits into
OrchardLinux:mainfrom
tcballard:perf-powerd-c
Dec 6, 2026
Merged

henrikklee merged 6 commits into
OrchardLinux:mainfrom
tcballard:perf-powerd-c

Conversation

@tcballard

Copy link
Copy Markdown
Contributor

Replaces the 1,330-line Python 3 omarchy-powerd daemon (plus its resident busctl monitor, systemd-inhibit, and sleep infinity helpers) with a single C99 binary (desktop/configuration/powerd.c, ~84 KB ELF, linked with libsystemd, -lpthread, -lm).

  • Replaces 4 resident processes (~20 MiB total RSS/Swap) with 1 C99 process (~1.5 MiB RSS).
  • Uses native sd-bus directly for PrepareForSleep, NameOwnerChanged, Inhibit() delay lock fd, CanSuspend, BlockInhibited, and systemd-suspend.service ActiveState.
  • Preserves the complete state machine (including low-battery sleep protection), evdev key/cover/combo handling, netlink power-supply uevents, smoothstep backlight fades, brightness persistence, locked USB authorization control, and /run/omarchy-powerd.sock protocol.
  • Updated tests/test_powerd.py and tests/test_low_battery.py to compile powerd.c and run the exhaustive state-space walk, unit tests, low-battery tests, and real C99 EventLoop tests against the binary.

Tested: 112/112 pass (test_powerd.py, test_low_battery.py, test_package_staging.py, test_package.py).

tcballard and others added 4 commits December 2, 2026 11:25
Replaces the 1,330-line Python 3 system/overlay/usr/libexec/omarchy-powerd
daemon (plus its resident busctl monitor, systemd-inhibit, and sleep
helper processes) with a single C99 binary (desktop/configuration/powerd.c,
~84 KB ELF, linked with libsystemd, pthread, and libm).

Key improvements:
- Replaces 4 resident processes (~20 MiB total RSS/Swap) with 1 C99
  process (~1.5 MiB RSS).
- Uses native sd-bus directly for logind PrepareForSleep and
  NameOwnerChanged signals, Inhibit() delay lock fd, CanSuspend,
  BlockInhibited, and systemd-suspend.service ActiveState polling.
- Preserves the complete state machine (including low-battery sleep
  protection), evdev key/cover/combo handling, netlink power-supply
  uevents, smoothstep backlight fades, brightness persistence, locked
  USB authorization control, and /run/omarchy-powerd.sock protocol.

Assisted-by: LLM
Makefile always builds omarchy-powerd with -DHAVE_SD_BUS and libsystemd,
while the unit test harnesses compile without HAVE_SD_BUS on minimal CI
runners that lack libsystemd-dev.

Assisted-by: LLM
powerd.c includes linux/input.h and netlink headers, which macOS lacks;
the repository's suite is green on both, and a test that needs what a Mac
lacks skips with its reason (AGENTS.md).

Assisted-by: LLM
@henrikklee

Copy link
Copy Markdown
Contributor

@tcballard one thing to fix before this can land: the power daemon needs systemd's library headers to build, but the package recipe doesn't declare them as a build dependency, so it only builds where the container happens to have them. Its suspend/sleep path is also skipped in the tests when the headers are missing, so CI doesn't cover it. Could you add the build dependency and make the tests cover that path? The other helper PRs (#222, #227–#230) can then go in one after another.

The loop tests ran the daemon with OMARCHY_POWERD_NO_DBUS, so the code that
takes logind's delay inhibitor, follows PrepareForSleep and asks CanSuspend
over sd-bus never ran in CI, and a machine without libsystemd's headers
built the tests without it.

The new LogindOverDbus tests start a private dbus-daemon and a small fake
logind (sd-bus, compiled from the test file) and check that the daemon
holds the inhibitor, releases it for a suspend from elsewhere after
darkening, asks CanSuspend over the bus before its own suspend, ends that
sleep on logind's signal, and takes the inhibitor again when logind
restarts. CI installs libsystemd's headers and sets
OMARCHY_TEST_REQUIRE_SD_BUS, so these tests can't silently skip there.

Assisted-by: LLM
# Conflicts:
#	CHANGELOG.md
#	desktop/install-tree
henrikklee added a commit to ozz1ee-dev/packages that referenced this pull request Dec 6, 2026
omarchy-a's power daemon (omarchy-powerd, OrchardLinux/omarchy-a#231) is
C built against libsystemd's sd-bus. Its headers and pkg-config file are
in systemd-libs; name it in makedepends instead of relying on the build
container happening to have it.

Assisted-by: LLM
@henrikklee
henrikklee merged commit a986d32 into OrchardLinux:main Dec 6, 2026
11 checks passed
@henrikklee

Copy link
Copy Markdown
Contributor

Merged, thank you Tom! The power daemon in C is a big win on the 1 GB boards.

What we added on your branch, as Henrik asked: the D-Bus side now has its own tests. They start a private bus with a small fake logind and check that powerd holds its sleep inhibitor, lets go of it for a suspend from elsewhere only after the screen is dark, asks CanSuspend over the bus before its own suspend, ends that sleep on logind's signal, and takes the inhibitor again when logind restarts. CI installs libsystemd's headers and fails instead of skipping if they're missing. The package recipe now names systemd-libs as a build dependency (OrchardLinux/packages#41). We also merged main in and wrote the changelog line.

Next: #222, #227, #228, #229 and #230, one after another.

henrikklee pushed a commit to grierj/omarchy-a that referenced this pull request Dec 6, 2026
At desktop idle the shell was spawning 8 processes per second because
panels/power/Status.qml ran a 1-second omarchy-battery-status timer
(which also forked awk) across four live instances (bar, lock screen,
control center, and power panel). RotationLock.qml likewise ran four
separate 2-second polling timers and four startup busctl probes.

- Compute omarchy-battery-status size and rate in pure bash without awk
  (guarding optional sysfs reads, octal leading zeros, and integer width),
  keeping the charge_full -> mAh fallback for charge-reporting gauges.
- Share one UPower-signal-driven BatteryStatus singleton (30 s fallback,
  coalesced concurrent refreshes, on-demand refresh when Control Center
  or Power panel opens).
- Share one RotationLockState singleton watching the config directory
  (and its parent for directory replacement) with one busctl probe and a
  shared setLocked() helper used by both Control Center and Display
  settings.

[Orchard: cut to the battery and rotation-lock readers, with both of the
PR's commits folded into one; the auto-brightness and omarchy-powerd
wakeup changes are left out, since those daemons are being ported to C
(OrchardLinux#230, OrchardLinux#231), where the same fixes belong.]

Assisted-by: LLM
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.

2 participants