Skip to content

perf(hibernate): drop clean caches and scale compression threads before snapshot - #11948

Open
ansonboby wants to merge 3 commits into
omacom:quattrofrom
ansonboby:perf/hibernate-drop-caches
Open

ansonboby wants to merge 3 commits into
omacom:quattrofrom
ansonboby:perf/hibernate-drop-caches

Conversation

@ansonboby

@ansonboby ansonboby commented Sep 15, 2026 •

Copy link
Copy Markdown

Summary

Hibernation with a browser-heavy working set stalls for over a minute on the snapshot allocation. Dropping clean caches before that allocation, and letting image compression use more than the default three threads, cut 15 s off the heavy path in single-run measurements on one machine.

Before: PM: hibernation: Allocated 6756924 kbytes in 72.66s (92.99 MB/s)
After: Allocated 6756564 kbytes in 57.15s (118.22 MB/s)

The remaining ~57 s is anonymous pages being evicted into zram, which dropping clean caches does not touch. Light hibernations are fine at 2.4 s already.

What these numbers do and don't show. Allocated ... in covers the snapshot allocation only, which is why it reflects the cache drop and cannot reflect the compression-threads change: compression runs later, when the image is written. The lines that would show it are the kernel's Using N thread(s) for lzo compression and Wrote ... kbytes in, which I have not collected across runs — so the thread change is claimed as making compression parallel, not as measured seconds. Both figures above are one before/after pair on one machine, not a series; I have not repeated them.

What changed

  • default/systemd/system-sleep/drop-caches (new, 0755) — pre hibernate only, guarded for suspend/s2idle and for the suspend half of suspend-then-hibernate. Syncs, sets hibernate_compression_threads when the knob exists, then drops caches.
    • Writing nproc asks for nproc - 1 compression threads: the kernel clamps to num_online_cpus() - 1. The write also replaces any lower value a hibernate_compression_threads= boot parameter set. The knob exists from Linux 6.19; older kernels skip it through the -w guard.
    • The drop is level 3, which also frees reclaimable dentries and inodes — costly to rebuild after resume, per Omarchy's own vm.vfs_cache_pressure note. Kept at 3 because that is the measured configuration; echo 1 (page cache only) is the obvious comparison if we want to shrink that cost, and post-resume responsiveness would be the thing to check.
    • On why it helps: not "the kernel discards clean caches anyway" — with Omarchy's vm.swappiness=150, reclaim favours anonymous pages over file cache. The plausible mechanism (reading mm/vmscan.c, not measured) is that the allocator has less clean cache to compress on the way into the image.
  • bin/omarchy-hibernation-setup — installs the hook alongside keyboard-backlight through install_root_file.
  • migrations/1790692395.sh (new) — installs it on machines that already hibernate, which is most of them: the ISO installer runs hibernation setup, and setup returns at the resume marker before reaching the install step, so neither a re-run nor --force could ever deliver it. The migration mirrors that same marker check and only supplies a hook that is missing.
  • bin/omarchy-hibernation-remove — removes the hook, so removing hibernation does not leave it behind.
  • docs/file-layout.md — names drop-caches with the other conditional system-sleep sources.

Ownership, stated correctly

The hook is not package-owned. install_root_file writes an unowned root-owned file, exactly like keyboard-backlight and force-igpu; only unmount-fuse comes from omarchy-settings. Consequences worth knowing:

  • pacman does not manage it, so a later edit to the hook source reaches installed machines only through another migration — the same trade those other two hooks already make.
  • The path sits under the /usr/lib/systemd/system-sleep directory that unowned-system-paths-test.sh allowlists as a whole; that test's own install assertions cover keyboard-backlight, not this hook.

No /etc writes for the thread knob. The sysfs write is conditional on -w, so kernels without hibernate_compression_threads no-op.

Measurements

HP 250 G9, 12th Gen i5-1235U (12 threads), 15 GiB RAM, zram0 zstd 15.2 GiB. Same ~11 GiB working set, journalctl -k | grep "Allocated.*kbytes in":

case allocated wall throughput
before (heavy) 6756924 kbytes 72.66 s 92.99 MB/s
after (heavy) 6756564 kbytes 57.15 s 118.22 MB/s
light baseline 6757352 kbytes 2.40 s 2815.56 MB/s

Single runs each. systemd-hibernate.service: Consumed 57.233s CPU over 58.628s wall says the remaining time is CPU-bound, consistent with anon-page eviction rather than disk. The zstd/lz4 comparison (810 MiB in 1.0 s vs 0.5 s, 3.5× worse ratio) said switching compression would save ~5–8 s while shrinking effective zram, so I dropped it.

Resume is unaffected. suspend (s2idle) is unaffected — the hook exits early when sleep_action != hibernate.

Testing

  • test/shell.d/hibernate-drop-caches-migration-test.sh (new) — runs a retargeted copy of the migration with a stubbed sudo: installs the shipped file at 0755 through the absolute privileged installer, is idempotent, leaves an existing hook alone, and does nothing without the resume hook line. It also asserts the production paths stay fixed literals so a caller cannot choose what root writes, and that setup, the migration and hibernation removal are the hook's only owners. Mutants that drop either guard fail it.
  • Repeated systemctl hibernate with a heavy browser session, comparing journalctl --since Allocated lines before and after.
  • Verified no-op on suspend: PM: suspend entry (s2idle) path still immediate, drop_caches not triggered.
  • Verified the knob guard: cat /sys/power/hibernate_compression_threads reads back after pre-hibernate, no error on kernels without the file.
  • bash -n on hook, setup and migration; bin-style-test, manifest-entrypoints-test, migrate-scope-test, migrate-wrapper-test, system-sleep-ownership-migration-test pass.

Compound Engineering
Pi

@omarchybot omarchybot added the enhancement New feature or request label Sep 27, 2026
@llstrk

llstrk commented Sep 28, 2026

Copy link
Copy Markdown

Automated AI review

Community review: Independent automated community review, unaffiliated with the Omarchy team, intended to help prepare PRs for their review.

Verified: the new drop-caches hook acts only on the pre phase of a hibernation, including the hibernate step of suspend-then-hibernate, and does nothing for suspend or post. A fresh omarchy-hibernation-setup run installs it at mode 755. The main gap is delivery: machines that already have hibernation configured never receive the hook, because setup exits before the new install step and the PR adds no migration. A few statements in the hook comment and PR description also overstate what the hook does or how it is managed.

Verified hook behavior (a copy of the hook with /sys and /proc paths redirected to private files, sync and nproc stubbed, nproc = 12, knob starting at 3):

$1 $2 SYSTEMD_SLEEP_ACTION Compression threads after drop_caches written
pre hibernate hibernate or unset 12 3
pre suspend-then-hibernate hibernate 12 3
pre suspend-then-hibernate suspend 3 no
pre suspend suspend 3 no
post hibernate hibernate 3 no
pre hibernate, knob file absent or read-only hibernate unchanged 3, exit 0

The head also merges cleanly with the current quattro tip, and the existing unowned-system-paths-test.sh, config-test.sh and system-sleep-ownership-migration-test.sh pass on it.

Existing hibernation setups never receive the hook

bin/omarchy-hibernation-setup returns early when hibernation is already configured, before the new install_root_file call:

omarchy-hibernation-setup (with or without --force)
├─ :63  omarchy_resume.conf contains HOOKS+=(resume)?
│    └─ yes → "Hibernation is already set up", exit 0   ← already configured machines stop here
└─ :118 install_root_file ... keyboard-backlight
   :123 install_root_file ... drop-caches                ← only reached on a fresh setup

A copy of the setup script with a stubbed sudo confirmed this: with the resume marker present it prints "Hibernation is already set up", makes no sudo calls and installs no hooks. --force does not change this, because the marker check runs before --force is used. No file under migrations/ installs the new hook.

Impact: the Omarchy ISO installer runs hibernation setup by default, so machines installed from it on supported hardware already have the marker. They get the updated source under /usr/share/omarchy, but /usr/lib/systemd/system-sleep/drop-caches is never created by omarchy update or by re-running omarchy hibernation setup. Only new installs, or a full omarchy hibernation remove followed by setup (which recreates the swapfile), get the change. A factory reset restores the installer's snapshot, so it includes the hook only on machines installed with this change.

Suggested change: either add a migration that checks for the resume marker and installs the hook when it is absent (the staging helper in migrations/1788662350.sh can be reused, but that migration itself returns early when a hook is missing, so its flow cannot be copied as is), or ship the hook from the omarchy-settings package the way unmount-fuse is shipped. The hook does nothing outside hibernation, so installing it everywhere is harmless, and package ownership would also let later edits to the hook reach installed copies.

Install and removal claims in the PR description

  • "Package-owned under /usr/lib, matching the existing sleep hooks": install_root_file writes a file that no package owns, like keyboard-backlight and force-igpu. Only unmount-fuse is package-owned. Because pacman ignores the file, a later change to the hook source will not reach machines that already have it unless a migration copies it again.
  • "Removed by omarchy-hibernation-remove": bin/omarchy-hibernation-remove does not touch /usr/lib/systemd/system-sleep, so the hook stays after hibernation is removed. It still only acts on hibernate, so the leftover is harmless.
  • "Already allowlisted in unowned-system-paths-test.sh": true, since the whole /usr/lib/systemd/system-sleep directory is allowlisted. That test's explicit install assertions only check keyboard-backlight.

Suggested change: correct the description. If the hook stays setup-installed, remove it in omarchy-hibernation-remove and add it to the list of conditional hooks in docs/file-layout.md:142.

Hook comment and measurements overstate the effect

  • The hook comment says the cache drop "turns a ~70s CPU-bound stall into an instant shrink". The PR's own measurement is 72.66 s before and 57.15 s after, with the remaining time attributed to anonymous-page eviction into zram.
  • The comment's reasoning, "the kernel discards clean caches anyway", may not hold under Omarchy's vm.swappiness=150, where reclaim favors anonymous pages over file cache. A plausible explanation for the gain is that dropping the cache up front saves compressing memory into zram. This is a reading of the kernel source (mm/vmscan.c), not a measurement.
  • The Allocated ... in line cannot show any effect of hibernate_compression_threads, because image compression happens later, when the image is written. The kernel's Using N thread(s) for lzo compression and Wrote ... kbytes in lines would show it. The timing also leaves out the hook's own sync and cache drop, and the PR reports only a single before/after pair.
  • Writing nproc gives nproc − 1 compression threads (the kernel uses clamp(num_online_cpus() - 1, 1, value)), so 11 on the 12-thread test machine, not 12 as the comment implies. The write also replaces any lower value set with the hibernate_compression_threads= boot parameter. The knob exists from Linux 6.19; on older kernels the -w guard skips it as intended.

Suggested change: correct the comment, and report a few runs of total hibernation entry time together with the Wrote and Using N thread(s) kernel lines.

Optional: echo 3 drops clean, unused page cache plus reclaimable dentries and inodes on every hibernation, including light ones, whose snapshot allocation the author measured at 2.40 s without the hook. Anything dropped that is used again after resume has to be read from disk, and Omarchy's own sysctl comment for vm.vfs_cache_pressure calls the dentry and inode cache costly to rebuild. Comparing echo 1 (page cache only), and measuring responsiveness after resume, would show whether the full drop is worth that cost.


Review information

Test scope: reviewed head 257aa423. The hook and setup script were exercised only as path-redirected copies with stubbed sync, nproc and sudo in an isolated sandbox; no real hibernation, cache drop or sysfs write was performed. The performance claims were assessed from Linux 7.2 and systemd 262 source only, not measured on hardware.

AI process: Opus 5.5 Medium coordination and synthesis, Opus 5.5 Xhigh technical review and final fact check, GPT 6 Sol Xhigh search for related issues, Opus 5.5 Medium editorial check.

Opt out: To stop receiving these reviews, reply to this comment saying so.

ansonboby and others added 3 commits September 29, 2026 20:02
…re snapshot

Hibernation entry stalls for 70s+ under heavy memory because the
single-threaded snapshot allocation has to reclaim clean page cache
inside the shrinker walk. The kernel discards those caches anyway when
it needs memory for the image — doing it up front with
`echo 3 > /proc/sys/vm/drop_caches` turns the storm into an instant
shrink. Measured on HP 250 G9 (i5-1235U, 15G RAM, zram zstd):

  Allocated 6756924 kbytes in 72.66s (92.99 MB/s) -> 6756564 kbytes in 57.15s (118.22 MB/s)
  15.5s faster (21%) heavy-memory hibernate, 2.4s light case unchanged.
  systemd-hibernate.service: 57.233s CPU over 58.628s wall (confirming
  CPU-bound reclaim).

Also drive `/sys/power/hibernate_compression_threads` to nproc at
hibernate-entry time (default 3, this machine 12) so image compression
scales with cores without a boot-time tmpfiles rule. Guarded to no-op
on suspend and older kernels without the knob.
The hook comment claimed the drop "turns a ~70s CPU-bound stall into an
instant shrink". It does not: the measurement was 72.66s to 57.15s, with the
remaining entry time being anonymous pages evicted into zram, and the
explanation offered for the gain ("the kernel discards clean caches anyway")
does not hold under Omarchy's vm.swappiness=150, where reclaim favours
anonymous pages over file cache. What the comment now says is what was
measured, and separately what reads out of mm/vmscan.c.

Also corrected: writing nproc asks for nproc-1 compression threads rather
than nproc (the kernel clamps to num_online_cpus()-1), it replaces any lower
value a hibernate_compression_threads= boot parameter set, and the knob is
Linux 6.19 and later. Level 3 also frees reclaimable dentries and inodes that
cost something to rebuild after resume; that trade-off is now stated instead
of implied, and the level stays at the measured configuration.

Comment-only: the hook's behavior is unchanged.
… hibernate

omarchy-hibernation-setup returns as soon as it sees the resume marker, and
that marker is what "already configured" means, so the hook added for new
setups never reached machines set up before it existed - including every
machine the ISO installer configured, since it runs hibernation setup by
default. Re-running setup could not help either: it exits at the same marker.

A migration installs the hook where the marker is present and the hook is
absent. It mirrors the literal check setup uses, so it decides on the same
condition, and it only supplies what is missing: a hook that is already there
belongs to whoever put it there. Its privileged paths stay fixed literals, as
the other migrations do, so a caller cannot choose what root writes.

Hibernation removal now removes the hook as well, rather than leaving it
behind as inert litter, and docs/file-layout.md names drop-caches with the
other conditional system-sleep sources and records the limit that none of them
are package-owned - a later edit reaches installed machines only through
another migration. That limit is also what the PR description now says,
instead of calling the hook package-owned.

Tested on a retargeted copy of the migration with a stubbed sudo: installs the
shipped file at 0755 through the absolute installer, is idempotent, leaves an
existing hook untouched, and does nothing without the resume hook line.
Mutants that drop either guard fail it.
@ansonboby
ansonboby force-pushed the perf/hibernate-drop-caches branch from 257aa42 to b7fcd59 Compare September 29, 2026 14:41
@ansonboby

Copy link
Copy Markdown
Author

Fixed at b7fcd590. All three sections accepted — and the delivery gap was the real one, since it meant the feature shipped to nobody.

Delivery. Added migrations/1790692395.sh: it mirrors the literal HOOKS+=(resume) marker check that omarchy-hibernation-setup uses, so it decides on exactly the condition that makes setup return early, and it installs only when the hook is absent — an existing hook belongs to whoever put it there. The privileged paths stay fixed literals (resume_conf=/etc/..., hook=/usr/lib/...), with the test asserting both literals and rejecting any OMARCHY_DROP_CACHES_*-style override of them, following legacy-power-udev-rules-migration-test.sh. You were right that 1788662350.sh's staging helper cannot be copied as-is: its repair loop returns early when a destination is missing, which is the opposite of what I needed. Test coverage: installs at 0755 through the absolute privileged installer (a PATH-resolved install stub fails the run), idempotent, leaves an existing hook untouched, does nothing without the resume hook line, and mutants dropping either guard fail it.

I went with the migration over omarchy-settings because this repo has no PKGBUILD — config-test.sh reaches for the checkout outside the tree — so package ownership is not something I can deliver from here. Your point about it stands and I said so in the description: the file is unowned, so a later edit to the hook reaches installed machines only through another migration. That is the durable fix, and I would not object to you moving it to packaging instead.

Ownership claims. All three corrected rather than defended. "Package-owned under /usr/lib" is gone; it now says the file is unowned like keyboard-backlight and force-igpu, that only unmount-fuse comes from omarchy-settings, and that the unowned-system-paths-test.sh allowlist covers the directory as a whole while its install assertions cover keyboard-backlight only. On your either/or about removal: I took "remove it in omarchy-hibernation-remove" rather than just rewording the description, since the hook does belong to hibernation and leaving it behind after removal was litter. omarchy-hibernation-remove now drops it, docs/file-layout.md names drop-caches with the other conditional sources and records that none of them are package-owned, and the test asserts setup, the migration and hibernation removal are the hook's only owners so a fourth does not appear unnoticed.

Hook comment and measurements. The "~70s CPU-bound stall into an instant shrink" claim is gone; it never matched the numbers I posted (72.66 s → 57.15 s, with the rest being anon eviction into zram). The comment now separates what was measured from what I read in mm/vmscan.c, and drops the "the kernel discards clean caches anyway" reasoning, since you are right that vm.swappiness=150 favours anonymous pages and that reasoning does not hold. The compression comment now says nproc - 1 threads (the clamp(num_online_cpus() - 1, ...) case), that the write overrides a lower boot-parameter value, and that the knob is 6.19+.

On measuring properly: I can't do repeated hibernations from here, so rather than imply a series I labelled both figures as single runs on one machine and spelled out that Allocated ... in cannot show the threads effect at all — compression happens when the image is written, so Using N thread(s) and Wrote ... kbytes in are the lines that would, and I haven't collected them. That is a real gap and the honest state of it.

Optional point: kept echo 3 because it is the measured configuration, and said in both the comment and the description that echo 1 is the comparison worth running, with post-resume responsiveness as the thing to check against vfs_cache_pressure's own note. If you'd rather land at echo 1 first, that is a one-character change plus a re-measure I'd need hardware for.

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.

3 participants