Skip to content

layercopy, executor: fewer path walks and overlay probes per exec - #14987

Closed
sipsma wants to merge 6 commits into
mainfrom
sipsma/path-walk-lookups
Closed

sipsma wants to merge 6 commits into
mainfrom
sipsma/path-walk-lookups

Conversation

@sipsma

@sipsma sipsma commented Oct 30, 2026

Copy link
Copy Markdown
Contributor

Every exec the engine runs pays for path resolution before it starts and after it ends. A per-package Go build runs thousands of small execs, each with deep mounts. In such a build, a few of these code paths were walking the same directories again and again, one lstat and a few allocations per path component. This PR removes most of that repeated work. It answers the same questions with fewer syscalls and allocations, and it changes no results except one cleanup fix, described below.

Measured one at a time, the changes cut about 250 MiB of engine allocations per cold first build of dagger's own CLI, and one-line edits rebuild about 0.1–0.2 s faster.

What changes

Resolving a path that holds no symlinks takes one syscall. Code that resolves a path inside a root (a snapshot, a container's rootfs) used continuity's fs.RootPath. RootPath walks the path from the root, one lstat per component, so that a symlink stays inside the root. Most paths hold no symlinks, and such a path resolves to itself. One openat2 with RESOLVE_NO_SYMLINKS (plus O_NOFOLLOW when the caller doesn't follow a final symlink) now settles that case. Success, or ENOENT, means every component that exists is a real directory, so the answer is the root joined with the path. Everything else goes to RootPath exactly as before:

  • a symlink anywhere in the path, or in the root's own path;
  • any other error, including ENOSYS on kernels before 5.6;
  • a .. component. RootPath applies .. after the symlinks before it, so cleaning the .. away first could change the answer.

The fast path first lands in layercopy's rootPath. A files mount calls it twice per file: once in the source snapshot's merged view, once in the mount's directory. It then moves to util/fsxutil as fsxutil.RootPath, a drop-in for RootPath, used by:

  • the exec's rootfs setup, working directory and secret scrubbing;
  • the mount-stub setup, the user lookup and read-only shared mounts;
  • Directory.stat and File mounts.

The result is RootPath's in every case. The only other caller left on the hot path is containerd's own mount.Mount.

Layercopy reads ancestors' overlay layers only to copy a directory. For each source path, source.selectBase computed the lowest overlay layer the path's ancestors leave visible (overlayAncestorMinLayer). That took an lstat, plus xattr reads, of every ancestor in every layer. Only a directory's walk needs that bound, to skip lower layers an ancestor hides (#13848). For anything else, the merged view already shows the file. So the topmost layer that holds it is the one the view shows, and no ancestor can hide that layer. Non-directories are now looked up from the top layer down, and a whiteout above them still stops the lookup. Layercopy never followed overlay redirects or metacopy, so nothing about them changes.

The opaque marker is read into a one-byte buffer. overlayEntryState reads trusted.overlay.opaque, then user.overlay.opaque, for every ancestor directory it probes, in every layer. The marker is the single byte y. So a one-byte stack buffer is enough: a longer value fails with ERANGE, and isn't the marker anyway. Before, each read allocated a buffer.

The mount-stub cleanup checks each parent once. Before an exec, the engine creates empty files and directories ("stubs") for mount targets that don't exist yet, together with any missing parents. After the exec, MountStubsCleaner removes the ones left empty and restores their parents' timestamps. A deep mount target such as a module under /go/pkg/mod leaves a chain of about 5 stubs. The cleanup resolved each stub, and then its parent, with a full RootPath walk, and read each stub directory to see whether it was empty. Now it checks each directory above a stub once per cleanup, with a memoized lstat that must find a real directory, not a symlink. A stub directory is removed only if rmdir succeeds; ENOTEMPTY means the exec wrote into it.

This makes the cleanup stricter. It now leaves alone a stub the exec replaced with a symlink, and anything below a directory the exec replaced with one. Before, it followed such a symlink and could remove an empty file or directory the exec had made elsewhere in its rootfs. Nothing outside the rootfs was at risk.

Numbers

Each change was measured on its own, against the same engine without it, building dagger's CLI with the per-package Go build (github.com/dagger/go) at GC target 100.

  • Machines: AWS c8gd.4xlarge spot instances (Graviton4, 16 vCPU, local NVMe) in us-east-1, with a fresh engine per run.
  • Series: ABBA-ordered paired rounds. Each run did a cold first build, an unchanged rebuild, and two one-line edits, each followed by a rebuild.
  • Allocations are exact runtime.MemStats totals from one profiled run per arm.
  • Base: each change was measured on the branch it was developed on. That branch carries other unmerged engine changes, so these aren't main's numbers, but the code paths are the same on main. Because each change was measured against a different base, the figures don't simply add up.
Change Allocations per cold first build Time
Ancestor layers only for directories (8 rounds) −117 MiB (−2.8%), −1.7 M objects Both edits −0.07 s, every round. Engine CPU −1.0 CPU-s, every round (−0.7 to −1.3).
One lookup in layercopy (4 rounds) −78 MiB (−2.0%), −1.2 M objects Edit of the CLI's own package −0.14 s, every round. Edit of a dependency −0.06 s, every round.
Mount-stub cleanup (4 rounds) the cleanup: 34.0 → 7.5 MiB, 0.65 → 0.09 M objects Engine CPU −0.25 CPU-s at the median, lower in all 4 rounds but within run-to-run spread.
One-byte opaque read about −20 MiB, −0.2 M objects Not timed on its own.
One lookup outside layercopy (4 rounds) −16 MiB (−0.5%), −0.65 M objects No resolvable change.
  • The cold first build's wall time (about 24 s) moves by less than the noise of these series for every change. The edits are where the gain shows, because each compile builds its files mount before it starts. That puts layercopy's path work on the critical path.
  • CPU and wall time: in a CPU profile, the one lookup in layercopy saves under 0.1 CPU-s per edit, less than its 0.14 s edit gain. The kernel's per-component lookups through overlay mounts may also wait, which a CPU profile doesn't count. That's a guess, not measured.
  • The one lookup outside layercopy saves allocations but little CPU, because the openat2 lookup costs kernel time of its own.
  • Peak memory and the unchanged rebuild show no change for any of them.

Tests

  • TestRootPathResolvesAsContinuity, in both util/layercopy and util/fsxutil, compares the fast path with RootPath, answer and error.
    • The paths include relative, absolute and escaping symlinks, final and dangling symlinks, missing components, a file used as a directory, and .. after a symlink and after a file.
    • The roots include absolute, unclean, relative and empty roots, and a root reached through a symlink.
  • TestRootPathWithoutSymlinksTakesOneLookup, in both packages, checks that a 5-component path costs at most 3 allocations. It takes 2, against 56 without the fast path. It assumes a kernel with openat2, as CI's kernels have.
  • TestCopyFileFromOverlayLinksTheVisibleLayer checks that a file is hard-linked from the layer the merged view shows. It covers a file above a layer with a stale copy and a file below an upper layer that lacks it. It also checks that no ancestor's layers are read.
  • TestOverlayEntryStateOpaqueMarker checks that y marks a directory opaque, while n, yy, yes and no value don't. The longer values reach the one-byte read's ERANGE path.
  • TestMountStubsCleaner checks:
    • that stubs, including ones two mounts share, are removed, and their parents' times restored;
    • that what the exec wrote is kept;
    • that nothing is removed through a symlink, which fails on the old code.

@sipsma
sipsma force-pushed the sipsma/path-walk-lookups branch from a4ae4f3 to dad5315 Compare October 30, 2026 19:02
overlayEntryState read each opaque attribute through continuity's
LGetxattr, which allocates a 128-byte buffer per call, for every
directory. The marker is the single byte "y", so the attribute is now
read into a one-byte buffer: a longer value doesn't fit, and isn't the
marker.

Signed-off-by: Erik Sipsma <[email protected]>
…ectory

Each file copied from an overlay, as an exec's files mount copies each
of its files, first found the lowest layer its ancestors leave visible:
an lstat, and for directories two xattr reads, of every ancestor in
every layer down to one that hides the layers below it. Only a
directory's walk needs that bound. The view shows a non-directory, so
the topmost layer holding it is the one the view shows, and no ancestor
hides it. A non-directory is now looked up from the top layer down, and
the ancestors' layers aren't read.

In a cold first build of dagger's CLI that was 0.60 CPU-s and 111 MiB
of allocations, most of the files mounts' cost.

Signed-off-by: Erik Sipsma <[email protected]>
rootPath resolved every path through continuity's RootPath, which
lstats each component from the root, and allocates for each, to follow
symlinks within the root. An exec's files mount resolves each of its
files that way twice: its path in the source snapshot's view, and its
name in the mount's directory. Most paths hold no symlinks, and such a
path resolves to itself. One openat2 that refuses symlinks now settles
that case; a path it refuses, or can't settle, is walked as before.
A path that is already clean is no longer cleaned again, which
allocated too.

Signed-off-by: Erik Sipsma <[email protected]>
After each exec, the mount-stub cleanup resolved every stub path, and
then its parent, from the rootfs root with continuity's RootPath, one
lstat and several allocations per path component. A mount at a deep
path such as a Go module's directory leaves a chain of stubs, so the
same prefixes were walked again for every stub and its parent. It also
read each stub directory to see whether it was empty.

Now each directory above a stub is checked once per cleanup, with an
lstat that must find a real directory, not a symlink, and a directory
stub is removed only if rmdir finds it empty. A stub whose path the exec
replaced with a symlink is left alone, as are stubs below a parent it
replaced with one: before, the cleanup followed the symlink and could
remove an empty directory or file the exec had made elsewhere.

Signed-off-by: Erik Sipsma <[email protected]>
layercopy settles a path that holds no symlinks with one openat2 that
refuses symlinks, where continuity's RootPath lstats each component from
the root and allocates for each. The engine still walked every path that
way elsewhere on each exec: its rootfs setup and working directory, its
mount-stub setup, its user lookup and read-only mounts, and the
Directory.stat and File mounts that builds run between execs.

fsxutil.RootPath is continuity's RootPath with that lookup first, and
the lookup moves there from layercopy. A path it refuses, or can't
settle, is walked by continuity as before, so the result is continuity's
in every case. That includes a path with a ".." component: continuity
takes a ".." after the symlinks before it, so the lookup leaves such a
path to continuity rather than cleaning the ".." away first.

Signed-off-by: Erik Sipsma <[email protected]>
@sipsma

sipsma commented Oct 30, 2026

Copy link
Copy Markdown
Contributor Author

Superseded by #15007, which carries these commits unchanged.

@sipsma sipsma closed this Oct 30, 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