Skip to content

core: withExec(transparentHugePages) opts an exec in to transparent huge pages - #14981

Closed
sipsma wants to merge 9 commits into
mainfrom
sipsma/thp-opt-in
Closed

sipsma wants to merge 9 commits into
mainfrom
sipsma/thp-opt-in

Conversation

@sipsma

@sipsma sipsma commented Oct 30, 2026

Copy link
Copy Markdown
Contributor

Adds withExec(transparentHugePages: true), which lets a command use the Linux kernel's transparent huge pages. Every other command now starts with them off, except for memory its own program asks huge pages for.

Why

Large-memory programs such as the Go compiler spend a lot of their time in page faults. With 2 MiB huge pages, a cold first build of the dagger CLI, built one package per exec, ran 2.6 s faster (24.7 s → 22.1 s). An edit-and-rebuild ran 0.8 s faster. Measured on an AWS c8gd.4xlarge (Graviton4, 16 cores), Linux 7.0, medians of 4 runs. The machine's system CPU over the first build fell from 48 to 33 CPU-seconds.

The kernel can't turn huge pages on per process. A process can only turn them off for itself (PR_SET_THP_DISABLE), and the processes it starts inherit that. So a host set to always gives them to every container, including programs that don't benefit and pay for them in memory. This PR makes them an opt-in per exec: on a host set to always, an exec gets them only if it asks.

What changes

  • Engine: around each runtime start, Run for an exec and Exec for a command in a running container, the engine sets its own mode under a lock. Execs that didn't opt in get "off except where advised" (PR_THP_DISABLE_EXCEPT_ADVISED, Linux 6.18). Execs that did get the mode cleared. Once the runtime has started, the engine puts its own mode back. The runtime and the container keep the mode they started with.
  • API: withExec(transparentHugePages:), in views from v1.0.0-beta.22.
  • Cache key: the setting can't change what an exec produces, so it isn't part of the exec's cache key. An exec that differs from a cached one only in this setting reuses the cached result, whichever setting produced it. For this, dagql gets volatile arguments: a field's schema can mark an argument volatile, and its value then reaches the resolver but never enters the call. It isn't in the cache key, the ID or telemetry. The flag comes from the schema, so a client can't use it to widen what counts as the same call. The resolver keeps the value in the exec's lazy state, so it applies when the exec runs.

Who sees a difference

  • Hosts with the usual default settings (madvise, and no page size under /sys/kernel/mm/transparent_hugepage/hugepages-* set to always): nothing changes. Measured on the same machine, the first build and both edits were within 0.3 s with and without the change.
  • Hosts set to always (or with a page size set to always), Linux 6.18 or later: execs no longer get huge pages unless they opt in. Programs that ask for them themselves (madvise(MADV_HUGEPAGE)) still get them.
  • Kernels before 6.18: nothing changes. The engine leaves execs alone, and the setting has no effect.

With the host at always, opting in only the Go compiles and links got the same build time as giving every exec huge pages (within 0.15 s).

Tests

  • TestVolatileArg (dagql): calls that differ only in a volatile argument run once and share the result. The resolver sees the value it ran with, and the ID doesn't carry the argument. A dynamic-input hook keeps the value, and a non-volatile argument still tells calls apart.
  • TestStartWithTHP (engine/engineutil): the runtime starts with the mode its exec asked for, the pid reaches the caller, and the engine's own mode is put back, also when the start fails. It skips on kernels before 6.18.
  • TestContainer/TestTransparentHugePages (core/integration): an exec and its children read back mode 0 with the opt-in, and 3 without it on 6.18+. The same command with and without the setting runs once.
  • TestBaseSchemaAllowlist: the argument isn't in older views.

@sipsma
sipsma requested review from a team as code owners October 30, 2026 04:29
@sipsma
sipsma force-pushed the sipsma/thp-opt-in branch from d509142 to 4c57214 Compare October 30, 2026 19:02
…e pages

The kernel can switch transparent huge pages off for a process, never on,
and a process passes the setting on to the processes it starts. So on a host
set to "always", every exec now starts with them off except where its
program asks for them (PR_THP_DISABLE_EXCEPT_ADVISED, Linux 6.18), as on a
host set to "madvise", unless the exec sets transparentHugePages. The
engine sets the mode on itself while the runtime starts, under a lock, and
puts its own mode back once the runtime has started. Kernels before 6.18
lack the mode; there nothing changes.

Signed-off-by: Erik Sipsma <[email protected]>
A field's schema can mark an argument volatile: its value goes to the
resolver, but not into the call's cache identity, its ID or its telemetry.
Calls that differ only in it are the same call, and share whichever result
was made first. It is for arguments that cannot change the result, such as
a performance hint, and needs a default.

Signed-off-by: Erik Sipsma <[email protected]>
The setting cannot change what an exec produces, so an exec that differs
from a cached one only in it reuses the cached result.

Signed-off-by: Erik Sipsma <[email protected]>
…, and says what the default keeps

Older views keep the schema they had. The documentation said commands get
no huge pages without the setting; they still get them for memory their
own program asks huge pages for, and a host's per-size settings count as
well as its top-level one.

Signed-off-by: Erik Sipsma <[email protected]>
@sipsma
sipsma force-pushed the sipsma/thp-opt-in branch from 4c57214 to 58052d9 Compare October 30, 2026 21:19
@sipsma

sipsma commented Nov 2, 2026

Copy link
Copy Markdown
Contributor Author

Its commits moved into #15007, which now combines the engine's pending performance changes, to save CI runs.

@sipsma sipsma closed this Nov 2, 2026
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