Skip to content

feat(memory): apply kernel memory tunables before memory-mode runs - #507

Merged
not-matthias merged 3 commits into
mainfrom
cod-3300-kernel-memory-tunables
Aug 27, 2026
Merged

feat(memory): apply kernel memory tunables before memory-mode runs#507
not-matthias merged 3 commits into
mainfrom
cod-3300-kernel-memory-tunables

Conversation

@not-matthias

@not-matthias not-matthias commented Aug 18, 2026

Copy link
Copy Markdown
Member

Benchmark repos running codspeed --mode memory currently need a hand-written CI step to stabilise kernel memory behaviour before measuring: disable THP, zero vm.compaction_proactiveness / vm.swappiness / kernel.numa_balancing, swapoff -a, and drop the page cache before each suite.

The runner now does this itself at the start of every memory-mode execution.

Behaviour

  • Applied only when a CI run environment is detected, so developer machines are not silently mutated. Skipped entirely when privileges cannot be elevated without a password prompt.
  • Best effort: a knob that cannot be applied is a warning, never fatal.
  • MemoryTunables::apply() returns a guard holding the previous value of every knob it changed, and restores it on drop — including on the error paths, where teardown() never runs. Only knobs that were not already at the target are captured, and THP is restored to the exact mode it held (madvise stays madvise).
  • swapoff -a is guarded: skipped on zram devices (it permanently resets their disksize) and whenever the swapped pages would not fit in MemAvailable. Restoring re-enables the recorded entries one by one, since swapon -a would miss a swap file absent from /etc/fstab.
  • The page cache drop has no previous state to restore; the node is a write-only trigger.
  • Applied in run() rather than setup(), so the page cache is dropped before every suite and --skip-setup does not bypass it. The sysctl/THP writes are no-ops on later suites since ensure_sysctl compares before writing.

Commits

  1. refactor(executor): move linux_sysctl into executor helpers — pure move, the sysctl primitive is no longer walltime-specific.
  2. feat(memory): apply kernel memory tunables before memory-mode runs

Verification

  • cargo clippy --all-targets clean, cargo test --lib (pre-existing failures on this host are the sudo-requiring tests, which write kernel.kptr_restrict and are identical on main).
  • Local non-CI run logs Not running in CI, skipping kernel memory tunables; host THP unchanged.
  • Privileged container: THP enabled/defrag become [never], vm.compaction_proactiveness = 0, vm.swappiness = 0, and the swap guard correctly logged Leaving swap enabled: swapped pages do not fit in available memory.

@codspeed-hq

codspeed-hq Bot commented Aug 18, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

⚠️ Unknown Walltime execution environment detected

Using the Walltime instrument on standard Hosted Runners will lead to inconsistent data.

For the most accurate results, we recommend using CodSpeed Macro Runners: bare-metal machines fine-tuned for performance measurement consistency.

✅ 17 untouched benchmarks


Comparing cod-3300-kernel-memory-tunables (e89df29) with main (8868af5)

Open in CodSpeed

@not-matthias
not-matthias force-pushed the cod-3300-kernel-memory-tunables branch from febfce2 to a219b76 Compare August 19, 2026 15:40
@not-matthias
not-matthias marked this pull request as ready for review August 19, 2026 15:45
@not-matthias
not-matthias force-pushed the cod-3300-kernel-memory-tunables branch from a219b76 to 3568cec Compare August 19, 2026 15:46
@greptile-apps

greptile-apps Bot commented Aug 19, 2026

Copy link
Copy Markdown

Greptile Summary

The PR adds best-effort Linux memory stabilization around memory-mode executions and restores reversible host settings after each run. It also centralizes Linux profiling sysctl handling and gives wall-time executors ownership of their restoration guards.

  • Adds THP, swap, and page-cache tuning for memory benchmarks.
  • Records successfully disabled swap areas and restores their paths and explicit priorities.
  • Moves profiling sysctl handling into shared executor helpers and restores changed values when the wall-time executor is dropped.

Confidence Score: 4/5

The PR is not yet safe to merge because local privileged memory runs still mutate host-global memory settings despite the stated CI-only boundary.

MemoryExecutor invokes the tunables unconditionally, and MemoryTunables gates only on passwordless elevation, so a local root or passwordless-sudo run still disables THP and eligible swap and irreversibly drops the local page cache.

Files Needing Attention: src/executor/memory/executor.rs, src/executor/memory/tunables.rs

Important Files Changed

Filename Overview
src/executor/memory/tunables.rs Implements best-effort THP, swap, and page-cache tuning with drop-based restoration.
src/executor/memory/executor.rs Applies the new tunables guard for the lifetime of each memory-mode execution.
src/executor/helpers/linux_sysctl.rs Moves Linux sysctl handling into a shared helper and adds restoration guards.
src/executor/wall_time/executor.rs Retains changed profiling sysctls on the executor so their original values can be restored.
src/executor/tests.rs Updates wall-time test setup so each executor owns its sysctl restoration guard.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
  A[MemoryExecutor run] --> B[Apply memory tunables]
  B --> C[Disable THP]
  B --> D[Disable eligible swap]
  B --> E[Drop page cache]
  C --> F[Run memtrack benchmark]
  D --> F
  E --> F
  F --> G[Drop MemoryTunables guard]
  G --> H[Restore swap entries]
  G --> I[Restore THP mode]
Loading

Reviews (5): Last reviewed commit: "fix(walltime): restore profiling sysctls" | Re-trigger Greptile

Comment thread src/run_environment/mod.rs Outdated
Comment thread src/executor/memory/tunables.rs Outdated
Comment thread src/executor/memory/tunables.rs
Comment thread src/executor/memory/tunables.rs
Comment thread src/executor/memory/tunables.rs Outdated
Comment thread src/executor/memory/tunables.rs Outdated
Comment thread src/executor/memory/tunables.rs Outdated
@not-matthias
not-matthias force-pushed the cod-3300-kernel-memory-tunables branch 3 times, most recently from 77ad6fc to 87a02e9 Compare August 24, 2026 17:30

@art049 art049 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know it was the case before but it hurts readability a bit

Comment thread src/executor/helpers/linux_sysctl.rs
Comment thread src/executor/memory/tunables.rs
Comment thread src/executor/memory/tunables.rs
@not-matthias
not-matthias force-pushed the cod-3300-kernel-memory-tunables branch 2 times, most recently from dcd2e8f to 758c564 Compare August 26, 2026 13:54
@not-matthias

Copy link
Copy Markdown
Member Author

@greptileai review again

Comment thread src/executor/memory/tunables.rs Outdated
@not-matthias
not-matthias force-pushed the cod-3300-kernel-memory-tunables branch from 758c564 to fc2fe71 Compare August 26, 2026 14:34
@not-matthias
not-matthias requested a review from art049 August 26, 2026 14:36
@not-matthias
not-matthias force-pushed the cod-3300-kernel-memory-tunables branch from fc2fe71 to 5619273 Compare August 27, 2026 15:53
The sysctl read-then-write-with-sudo primitive is not walltime-specific;
memory mode needs it too.
Disables transparent huge pages, sets vm.compaction_proactiveness,
vm.swappiness and kernel.numa_balancing to 0, disables swap and drops the
page cache, so benchmark repos no longer need a hand-written CI step.

Applied only in CI, best-effort: a knob that cannot be set is a warning.
swapoff is skipped on zram devices and whenever the swapped pages would not
fit in available memory.
Keep the original profiler sysctl values in the walltime executor and restore them when it is dropped. This prevents a local or containerized run from leaving host-global profiling access enabled.
@not-matthias
not-matthias force-pushed the cod-3300-kernel-memory-tunables branch from 5619273 to e89df29 Compare August 27, 2026 16:18
@not-matthias
not-matthias merged commit e89df29 into main Aug 27, 2026
48 checks passed
@not-matthias
not-matthias deleted the cod-3300-kernel-memory-tunables branch August 27, 2026 16:34
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.

3 participants