Skip to content

runsc: read local state files ahead of kernel decode during restore - #16472

Open
milantracy wants to merge 1 commit into
gvisor:masterfrom
milantracy:state-readahead
Open

milantracy wants to merge 1 commit into
gvisor:masterfrom
milantracy:state-readahead

Conversation

@milantracy

@milantracy milantracy commented Oct 27, 2026 •

Copy link
Copy Markdown
Member

Fixes #16209

For a regular local state file, restore now reads the file through a stateio.BufReader over a stateio.FDReader, as the checkpoint-gofer path already does. Worker goroutines read up to 8 MiB of the file ahead of kernel decode, so decode waits for the host file less often. The number of parallel reads follows the CPU limit of the restore readers (min(8, max(1, cpus)), the same input as #16405). Non-regular files (for example FIFOs, see #15553) use the old path, because FDReader reads at file offsets.

Restore now also closes the state file when it fails. Before this change, the state file was closed only after a successful restore.

Benchmark

systrap, GCP n2-standard-32 (32 vCPU, 125 GiB), local disk. Master and the change run in alternate order. Results from benchstat: median ± 95% confidence interval, Mann-Whitney U p-value. The benchmark ran before the rebase onto #16405; with 32 CPUs, the new sizing gives the same 8 parallel reads.

Each run starts a Python workload with 512 MiB of random data, does a checkpoint, and then restores it.

  • p8: the workload also fills 8 pipes of 1 MiB, so the state file is 13 MB. small: the state file is 1 MB.
  • warmbin: the page cache is dropped, then the runsc and sentry binaries are read back. coldbin: the page cache is dropped. hot: the page cache is not dropped.
  • foreground: runsc restore. background: runsc restore --background.

Metrics: kernel-load-ms is the kernel decode ("loading kernel" to "kernel loaded"). rpc-to-vfs-ms is from the restore RPC to "VFS restored"; the application can start after this step. restore-cli-ms is the duration of runsc restore.

                                 │    master      │                change                  │
                                 │ kernel-load-ms │ kernel-load-ms  vs base                │
Restore/p8-warmbin-foreground          891.9 ± 0%       371.8 ± 2%  -58.32% (p=0.000 n=32)
Restore/p8-coldbin-foreground         1183.4 ± 1%       653.0 ± 2%  -44.82% (p=0.000 n=32)
Restore/p8-warmbin-background          413.2 ± 1%       369.7 ± 3%  -10.52% (p=0.000 n=32)
Restore/p8-hot-foreground              385.0 ± 1%       398.1 ± 2%   +3.40% (p=0.000 n=32)
Restore/small-warmbin-background       12.69 ± 2%       12.23 ± 4%   -3.65% (p=0.012 n=32)
Restore/small-hot-background           12.68 ± 4%       12.75 ± 4%        ~ (p=0.697 n=32)
geomean                                173.2            133.2       -23.14%

                                 │    master     │                change                 │
                                 │ rpc-to-vfs-ms │ rpc-to-vfs-ms  vs base                │
Restore/p8-warmbin-foreground         902.7 ± 0%      384.8 ± 2%  -57.38% (p=0.000 n=32)
Restore/p8-coldbin-foreground        1201.2 ± 1%      684.8 ± 3%  -42.99% (p=0.000 n=32)
Restore/p8-warmbin-background         423.2 ± 1%      381.6 ± 3%   -9.83% (p=0.000 n=32)
Restore/p8-hot-foreground             397.9 ± 0%      411.7 ± 2%   +3.47% (p=0.000 n=32)
Restore/small-warmbin-background      23.05 ± 1%      22.64 ± 1%   -1.81% (p=0.014 n=32)
Restore/small-hot-background          22.88 ± 3%      23.10 ± 5%        ~ (p=0.179 n=32)
geomean                               214.1           166.9       -22.03%

                                 │    master      │                change                 │
                                 │ restore-cli-ms │ restore-cli-ms  vs base               │
Restore/p8-warmbin-foreground         1.225k ± 0%      1.224k ± 0%       ~ (p=0.103 n=32)
Restore/p8-coldbin-foreground         1.426k ± 0%      1.426k ± 0%       ~ (p=0.920 n=32)
Restore/p8-warmbin-background          456.9 ± 1%       415.4 ± 3%  -9.09% (p=0.000 n=32)
Restore/p8-hot-foreground              425.4 ± 0%       441.3 ± 1%  +3.73% (p=0.000 n=32)
Restore/small-warmbin-background       51.46 ± 1%       51.10 ± 1%       ~ (p=0.300 n=32)
Restore/small-hot-background           49.95 ± 3%       50.94 ± 3%       ~ (p=0.103 n=32)
geomean                                309.1            306.7       -0.78%

A separate run measured the same 13 MB state file with a hot page cache in background mode (n=24):

                          │    master      │                change                 │
                          │ kernel-load-ms │ kernel-load-ms  vs base               │
Restore/p8-hot-background       367.2 ± 1%       386.5 ± 2%  +5.27% (p=0.000 n=24)

                          │    master      │                change                 │
                          │ restore-cli-ms │ restore-cli-ms  vs base               │
Restore/p8-hot-background       404.4 ± 1%       425.9 ± 2%  +5.32% (p=0.000 n=24)

Summary:

  • When the state file is read from disk, kernel decode is 45–58% shorter in foreground restore and 11% shorter in background restore. In background restore, runsc restore is 9% shorter. In foreground restore, runsc restore does not change, because it waits for the full page load.
  • A small state file does not change.
  • When a large state file is already in the page cache, restore is 3–5% (13–21 ms) longer. A test with the same binary layout and read-ahead disabled at run time shows no difference, so the cause is the read-ahead, not the code layout. Pinning to one NUMA node does not change it.

Testing

  • New TestBufReaderFDReader in //pkg/sentry/state/stateio.
  • //runsc/boot:boot_test, //pkg/sentry/state/stateio:stateio_test, nogo, and the systrap checkpoint/restore container tests (including compressed and multi-container restore) pass.

For a regular local state file, restore now reads the file through a
stateio.BufReader over a stateio.FDReader. Worker goroutines read up to
8 MiB of the file ahead of kernel decode, so decode waits for the host
file less often. The number of parallel reads follows the CPU limit of
the restore readers, up to 8. Non-regular files use the old path.

Restore now also closes the state file when it fails.

Fixes gvisor#16209

Signed-off-by: Jing Chen <[email protected]>
@EtiennePerot
EtiennePerot self-requested a review October 27, 2026 00:56

@EtiennePerot EtiennePerot 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.

Overall this is valuable when the checkpoint is cold, but in Substrate I think it will often be warm (in page cache) so this may actually slow things down. I recommend benchmarking both.

If it is slower in the warm-page-cache case, please also explore if there's an approach that doesn't have this regression, for example posix_fadvise(POSIX_FADV_WILLNEED) which will tell the host kernel to read ahead on its own. AFAIK that's essentially doing what this is doing already, except the readahead happens in kernel-space rather than user-space, and it avoids duplicating memory in the likely case of a warm page cache.

Comment thread runsc/boot/restore.go
return nil
}

// closeStateFile closes r.stateFile if it is open.

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.

Add checklocks

Comment thread runsc/boot/controller.go
// regular file can use this reader.
fileBytes := (stat.Size + localStateFileReadBytes - 1) &^ (localStateFileReadBytes - 1)
bufBytes := min(fileBytes, localStateFileMaxBufBytes)
parallelReads := min(localStateFileMaxParallelReads, max(1, cpus))

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.

If we keep the non-posix_fadvise approach, then this number should probably be computed similarly as we did for the pages file aio queue. Check #16405 which just changed how this is sized, as a higher number here will trash performance at low CPU quota. Make sure you are rebased past that commit, then please benchmark this on a listening nginx server checkpoint given a range of CPU quota options: 0.25 cores, 1 core, 2 cores, 4 cores, 16 cores, uncapped, and see what makes sense at each step.

Comment thread runsc/boot/controller.go
// the host file less often. The buffer holds the whole file, up to
// localStateFileMaxBufBytes. The reads use file offsets, so only a
// regular file can use this reader.
fileBytes := (stat.Size + localStateFileReadBytes - 1) &^ (localStateFileReadBytes - 1)

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.

? Not sure what quantity this is computing...

@EtiennePerot

Copy link
Copy Markdown
Member

When a large state file is already in the page cache, restore is 3–5% (13–21 ms) longer

Sorry, I had not read that far into the benchmarks prior to doing the review above. Seems like the posix_fadvise(POSIX_FADV_WILLNEED) approach is needed.

@tamird

tamird commented Nov 2, 2026

Copy link
Copy Markdown
Contributor

@EtiennePerot, @milantracy: I tried POSIX_FADV_WILLNEED on the local
state file with the existing reader. On a cold checkpoint it saved about
3% of kernel load time in paired rounds; buffering saved 41%. Here are
the cold rows from benchstat (milliseconds, 18 restores per column):

             │    direct    │               advice                │                buffer                │
             │ kernel-ms/op │ kernel-ms/op  vs base               │ kernel-ms/op  vs base                │
Restore/cold     777.3 ± 4%     751.0 ± 3%       ~ (p=0.091 n=18)     450.5 ± 3%  -42.04% (p=0.000 n=18)

             │   direct   │              advice               │               buffer               │
             │ cli-ms/op  │ cli-ms/op   vs base               │ cli-ms/op   vs base                │
Restore/cold   809.4 ± 4%   784.1 ± 2%       ~ (p=0.091 n=18)   489.2 ± 3%  -39.56% (p=0.000 n=18)

With all three input files warm, benchstat didn't distinguish the
readers. Comparing buffered with direct within each round, though,
the restore command was slower in 15 of 18 rounds: median +2.2%
(95% interval +0.3% to +5.0%). The kernel-load difference was less
clear: +1.3% (−0.6% to +4.9%). This is consistent with the hot CLI
regression in the PR; it doesn't establish a hot kernel-load regression.

The 108 restores used the same executable on one 4-vCPU
Linux worker, in balanced order, with a 12.2 MiB state file and a
513 MiB pages file. CLI time ends when the background restore command
returns. Memory and pipe contents were checked afterward.

I also tried the six requested OCI quotas on a listening four-worker
Nginx. Each quota had its own checkpoint, and all three readers restored
the same bytes within each quota. Here are the medians of the six
paired percentage changes for buffering versus direct reads. HTTP
time runs from starting restore through the first verified 1 KiB
response to a guest curl launched with runsc exec.

Application CPU quota Cold kernel load Cold first HTTP Warm first HTTP
0.25 +0.2% +7.9% +3.0%
1 −2.2% +0.2% −0.4%
2 −3.4% +1.3% +0.4%
4 −8.8% −1.1% +0.7%
16 −9.4% −1.4% −3.0%
Uncapped (19 CPUs) −9.7% −1.0% −0.7%

Buffering shortened cold kernel load in all six pairs at both 4 and
16 CPUs. It reached the first HTTP response sooner in three and four
of those pairs, respectively. Across the 72 pairs, buffering reached
HTTP sooner in 33 and WILLNEED in 36. At 0.25 CPU, five of six cold
buffered restores reached HTTP later. Individual differences ranged
from −19% to +20%. All 216 restores served the expected bytes on the
first request and on a subsequent 16-request burst. Quota results

The Nginx state files were only 301–343 kB. The buffered reader used
two 512 KiB chunks, so the whole state file fit in its first data read.
This sweep cannot choose a queue depth for larger files. It used the
default creation and lifecycle CPU policies. The background reader
uses the application CPU count, and the test verified each sandbox’s
application quota after restore. Reader sizing · CPU policies

Posted on behalf of @tamird.

@tamird

tamird commented Nov 5, 2026

Copy link
Copy Markdown
Contributor

@milantracy @EtiennePerot, I repeated the local-file comparison with the default graph decoder. For this checkpoint I would use the existing reader with POSIX_FADV_WILLNEED on checkpoint.img. It shortened the cold restore command. The async reader made the warm command slower.

These are the whole-command rows from benchstat (milliseconds, 18 restores per column).

             │    direct    │               advice                │               buffer               │
             │  cli-ms/op   │  cli-ms/op   vs base                │  cli-ms/op   vs base               │
Restore/warm   58.63 ±   7%   56.79 ± 14%        ~ (p=0.767 n=18)   64.10 ±  8%  +9.33% (p=0.004 n=18)
Restore/cold   86.72 ± 160%   60.04 ± 47%  -30.77% (p=0.003 n=18)   69.08 ± 40%       ~ (p=0.126 n=18)

Both approaches shortened the cold kernel-load phase. The command as a whole varied more, especially with cold files and the direct reader. This was one 8.06 MiB uncompressed state file with separate memory pages on a four-CPU AMD64 worker. Each trial checked residency of all three input files, then verified the guest memory and pipe contents after restore. Benchstat and all 108 samples

Posted on behalf of @tamird.

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.

Optimization: Read local state files through the async read-ahead reader during restore

3 participants