Skip to content

engine: size overlay snapshots without allocating per file - #14984

Closed
sipsma wants to merge 3 commits into
mainfrom
sipsma/overlay-size-walk
Closed

sipsma wants to merge 3 commits into
mainfrom
sipsma/overlay-size-walk

Conversation

@sipsma

@sipsma sipsma commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor

Every snapshot commit makes the engine walk the snapshot's whole upper directory to record its size, and the walk allocates two objects per file. This PR keeps the walk and its result but drops those per-file allocations. That cuts about 40 MiB of the engine's allocations from a cold build of dagger's CLI.

Why

containerd's overlay snapshotter stores each committed snapshot's size, so it walks the upper directory at every Commit, and again for every Usage of an active snapshot. It walks with continuity's fs.DiskUsage, which goes through filepath.Walk: an os.Lstat per entry, which allocates a FileInfo, and a filepath.Join per entry for its path. In a cold build of dagger's CLI with dagger.io/go, most commits are exec outputs and extracted Go modules. There the walk allocated 54 MiB, about 1.5% of everything the engine allocated during the build. Every byte the engine allocates is work for its garbage collector.

How

The engine wraps the overlay snapshotter it builds (in engine/server/snapshotter.go) with its own Commit and Usage:

  • The walk opens each directory and reads its entries with getdents into a pooled 32 KiB buffer. It fstatats each entry relative to the directory's descriptor, into one reused Stat_t. An entry now costs only its name.
  • The result is the same as fs.DiskUsage's: the allocated blocks of each distinct inode, the root's included, and the number of distinct inodes. Only non-directories with more than one link are tracked for de-duplication, since an inode with a single link, or a directory, can't appear twice in one tree.
  • Commit is containerd's own otherwise: the same write transaction on the engine's snapshotter store, the same storage.CommitActive with the computed usage. It checks for cancellation before the walk and at each entry, as continuity does.
  • Usage of a committed snapshot still returns the stored size; only an active snapshot is walked.
  • Cleanup is passed through to the overlay, since containerd's metadata store calls it to remove deleted snapshots' directories under asynchronous removal.

The system calls stay: they are most of the walk's CPU.

Numbers

Measured on one 16-core arm64 machine (AWS c8gd.4xlarge). Each run is a cold build of dagger's CLI with dagger.io/go on a fresh engine. Engines were built with and without this change from the same source, 4 runs each in alternating order.

  • The walk's allocations, from heap profiles taken before and after the cold build: 54.3 MiB without the change, 14.2 MiB with it. What remains is each entry's name (9 MiB) and the pooled buffers, which the pool drops at each collection (5 MiB).
  • Wall time and engine CPU don't change beyond run-to-run noise: the cold build's median was 23.19 s without and 23.15 s with; the engine's CPU 33.4 and 33.1 CPU-s. Edits and unchanged rebuilds don't change either.

Tests

  • TestOverlaySnapshotterUsage builds the overlay snapshotter through newSnapshotter and fills an active snapshot with:

    • nested and empty directories, and a directory bigger than one read of its entries;
    • a file hard-linked twice in the tree, and one linked from outside it;
    • a sparse file, symlinks and a fifo.

    It checks that the size, while active and once committed, equals fs.DiskUsage's. It also checks that sizing and committing a second such snapshot allocate under a quarter of what fs.DiskUsage does for the same tree, which fails if the wrapper isn't wired in.

  • TestOverlaySnapshotterUsageCanceled checks that sizing or committing an empty snapshot under a canceled context fails and commits nothing.

containerd's overlay snapshotter walks a snapshot's whole upper directory
at every commit, to store its size, and on every Usage of an active
snapshot. It walks with continuity's fs.DiskUsage, which allocates a
FileInfo and a joined path for each entry: 58-70 MiB in a cold build of
dagger's CLI, from exec outputs and module extraction.

The engine now wraps the overlay snapshotter with its own Commit and
Usage, which size the upper directory with the same result (each distinct
inode's allocated blocks, and the number of distinct inodes) by reading
each directory's entries into a reused buffer and stating them relative
to the directory's descriptor. Commit is otherwise containerd's: the same
transaction on the engine's snapshotter store, storing the usage with the
snapshot. The wrapper passes Cleanup through, which the asynchronous
removal depends on.

The system calls stay: they are most of the walk's CPU.

Signed-off-by: Erik Sipsma <[email protected]>
…, and test the walk through the snapshotter

Review found the walk checked the context only on entering a directory, so a
canceled commit could finish a large directory's scan and commit; continuity
checks at each entry, and so does this walk now. The allocation check also
called the walk directly, so it passed with the wrapper unwired; it now
measures an active snapshot's Usage and a Commit through the snapshotter the
engine builds.

Signed-off-by: Erik Sipsma <[email protected]>
…ading the root

Review found that with the check at each entry only, an empty snapshot was
sized and committed under a canceled context. continuity checks before the
root too, so this walk now does; a test commits an empty snapshot under a
canceled context.

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 as part of the engine performance batch.

@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