Repository navigation
runsc: read local state files ahead of kernel decode during restore - #16472
milantracy wants to merge 1 commit into
Conversation
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
left a comment
There was a problem hiding this comment.
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.
| return nil | ||
| } | ||
|
|
||
| // closeStateFile closes r.stateFile if it is open. |
| // regular file can use this reader. | ||
| fileBytes := (stat.Size + localStateFileReadBytes - 1) &^ (localStateFileReadBytes - 1) | ||
| bufBytes := min(fileBytes, localStateFileMaxBufBytes) | ||
| parallelReads := min(localStateFileMaxParallelReads, max(1, cpus)) |
There was a problem hiding this comment.
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.
| // 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) |
There was a problem hiding this comment.
? Not sure what quantity this is computing...
Sorry, I had not read that far into the benchmarks prior to doing the review above. Seems like the |
|
@EtiennePerot, @milantracy: I tried With all three input files warm, benchstat didn't distinguish the The 108 restores used the same executable on one 4-vCPU I also tried the six requested OCI quotas on a listening four-worker
Buffering shortened cold kernel load in all six pairs at both 4 and The Nginx state files were only 301–343 kB. The buffered reader used Posted on behalf of @tamird. |
|
@milantracy @EtiennePerot, I repeated the local-file comparison with the default graph decoder. For this checkpoint I would use the existing reader with These are the whole-command rows from benchstat (milliseconds, 18 restores per column). 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. |
Fixes #16209
For a regular local state file, restore now reads the file through a
stateio.BufReaderover astateio.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, becauseFDReaderreads 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-msis the kernel decode ("loading kernel" to "kernel loaded").rpc-to-vfs-msis from the restore RPC to "VFS restored"; the application can start after this step.restore-cli-msis the duration ofrunsc restore.A separate run measured the same 13 MB state file with a hot page cache in background mode (n=24):
Summary:
runsc restoreis 9% shorter. In foreground restore,runsc restoredoes not change, because it waits for the full page load.Testing
TestBufReaderFDReaderin//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.