Skip to content

Commit d306681

Browse files
committed
Compare three local checkpoint readers at the kernel decode boundary
1 parent 63d3761 commit d306681

6 files changed

Lines changed: 142 additions & 2 deletions

File tree

‎runsc/boot/controller.go‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -499,7 +499,12 @@ func (cm *containerManager) Restore(o *sentryapi.RestoreOpts, reply *sentryapi.R
499499
return fmt.Errorf("checkpoint taken with no-root-container=%t cannot be restored with no-root-container=%t", saved, cm.l.noRootContainer.enabled)
500500
}
501501
timer.Reached("restorer initialized")
502-
reply.KernelStarted, err = cm.restorer.restoreContainerInfo(cm.l, &cm.l.root)
502+
r := cm.restorer
503+
reply.KernelStarted, err = r.restoreContainerInfo(cm.l, &cm.l.root)
504+
if err == nil && reply.KernelStarted && o.TrialReader != "" {
505+
reply.TrialReader = o.TrialReader
506+
reply.TrialKernelNS = r.trialKernelNS
507+
}
503508
return err
504509
}
505510

@@ -534,7 +539,10 @@ func getRestoreReadersForLocalCheckpointFiles(o *sentryapi.RestoreOpts, cpus int
534539
if stat.Size == 0 {
535540
return nil, nil, nil, fmt.Errorf("statefile cannot be empty")
536541
}
537-
if stat.Mode&unix.S_IFMT == unix.S_IFREG {
542+
if o.TrialReader == "buffer" && stat.Mode&unix.S_IFMT != unix.S_IFREG {
543+
return nil, nil, nil, fmt.Errorf("buffered trial did not receive a regular file")
544+
}
545+
if stat.Mode&unix.S_IFMT == unix.S_IFREG && (o.TrialReader == "buffer" || o.TrialReader == "") {
538546
// Read the state file ahead of kernel decode, so that decode waits for
539547
// the host file less often. The buffer holds the whole file, up to
540548
// localStateFileMaxBufBytes. The reads use file offsets, so only a

‎runsc/boot/restore.go‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,8 @@ func newProcInternalData(conf *config.Config, spec *specs.Spec, containerName st
225225
// all containers and triggers the full sandbox restore after the last
226226
// container is restored.
227227
type restorer struct {
228+
trialKernelNS int64
229+
228230
mu sync.Mutex
229231

230232
// totalContainers is the number of containers expected to be restored in
@@ -552,6 +554,7 @@ func (r *restorer) restore(l *Loader) error {
552554
// Load the state.
553555
clocks := time.NewCalibratedClocks(shouldEnableClockMonotonicRaw(l.root.spec, l.root.conf))
554556
r.timer.Reached("loading kernel")
557+
trialStart := time2.Now()
555558
if r.extractRootFsMode {
556559
if err := l.k.ExtractRootfsUpperLayer(ctx, r.stateFile, r.asyncMFLoader, nil, clocks, r.rootFsOutputTar); err != nil {
557560
return fmt.Errorf("failed to extract rootfs upper layer: %w", err)
@@ -566,6 +569,7 @@ func (r *restorer) restore(l *Loader) error {
566569
if err := r.stateFile.Verify(); err != nil {
567570
return fmt.Errorf("statefile integrity check failed: %w", err)
568571
}
572+
r.trialKernelNS = time2.Since(trialStart).Nanoseconds()
569573
r.timer.Reached("kernel loaded")
570574
if oldNvidiaDriverVersion.Major() > 0 && !l.k.NvidiaDriverVersion.Equals(oldNvidiaDriverVersion) {
571575
return fmt.Errorf("nvidia driver version changed during restore: was %v, now %v", oldNvidiaDriverVersion, l.k.NvidiaDriverVersion)

‎runsc/boot/sentryapi/controller.go‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@ const ContMgrRestore = "containerManager.Restore"
112112

113113
// RestoreOpts contains options related to restoring a container's file system.
114114
type RestoreOpts struct {
115+
TrialReader string
116+
115117
// FilePayload contains, in order:
116118
// 1. checkpoint state file.
117119
// 2. optional checkpoint pages metadata file.
@@ -136,6 +138,9 @@ type RestoreOpts struct {
136138

137139
// RestoreReply is the reply of ContMgrRestore and ContMgrRestoreSubcontainer.
138140
type RestoreReply struct {
141+
TrialReader string
142+
TrialKernelNS int64
143+
139144
// KernelStarted is set when the call restored the last container, which
140145
// starts the kernel.
141146
KernelStarted bool

‎runsc/sandbox/sandbox.go‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -653,6 +653,9 @@ func (s *Sandbox) Restore(conf *config.Config, spec *specs.Spec, cid string, ima
653653
}
654654
return fmt.Errorf("restoring container %q: %w", cid, err)
655655
}
656+
if opt.TrialReader != "" {
657+
fmt.Fprintf(os.Stderr, "RESTORE-TRIAL %s %d\n", reply.TrialReader, reply.TrialKernelNS)
658+
}
656659
s.RegisteredMetrics = registeredMetrics
657660
s.Restored = true
658661
if !reply.KernelStarted {
@@ -736,6 +739,21 @@ func (s *Sandbox) setRestoreOptsForLocalCheckpointFiles(conf *config.Config, ima
736739
if err != nil {
737740
return fmt.Errorf("opening state file %q failed: %w", path.Join(imagePath, checkpointfiles.StateFileName), err)
738741
}
742+
// Fork-only experiment: both arms run the same executable, with the
743+
// same restore code after this point. The source image is a regular file.
744+
if mode, ok := os.LookupEnv("GVISOR_FADVISE_TRIAL"); ok {
745+
opt.TrialReader = mode
746+
switch mode {
747+
case "willneed":
748+
if err := unix.Fadvise(int(sf.Fd()), 0, 0, unix.FADV_WILLNEED); err != nil {
749+
sf.Close()
750+
return fmt.Errorf("advising checkpoint read: %w", err)
751+
}
752+
case "control", "buffer":
753+
default:
754+
panic(mode)
755+
}
756+
}
739757
opt.FilePayload.Files = append(opt.FilePayload.Files, sf)
740758

741759
// If either the pages metadata file or pages file exist, both must exist,

‎test/rbe/restore_advice/BUILD‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
load("//tools:defs.bzl", "go_binary")
2+
3+
go_binary(
4+
name = "guest",
5+
testonly = True,
6+
srcs = ["guest.go"],
7+
visibility = ["//visibility:public"],
8+
deps = ["@org_golang_x_sys//unix:go_default_library"],
9+
)

‎test/rbe/restore_advice/guest.go‎

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
// This qualification guest keeps application pages and bytes in kernel pipes
2+
// separate, so restoring the small kernel image cannot stand in for restoring
3+
// the application's memory.
4+
package main
5+
6+
import (
7+
"crypto/sha256"
8+
"fmt"
9+
"io"
10+
"math/rand"
11+
"net"
12+
"os"
13+
14+
"golang.org/x/sys/unix"
15+
)
16+
17+
func check(err error) {
18+
if err != nil {
19+
panic(err)
20+
}
21+
}
22+
23+
const address = "@restore-advice"
24+
const mib = 1024 * 1024
25+
26+
func serve() {
27+
payload := make([]byte, 512*mib)
28+
rng := rand.New(rand.NewSource(29))
29+
_, err := rng.Read(payload)
30+
check(err)
31+
var readers [8]*os.File
32+
for i := range readers {
33+
r, w, err := os.Pipe()
34+
check(err)
35+
capacity, err := unix.FcntlInt(w.Fd(), unix.F_SETPIPE_SZ, mib)
36+
check(err)
37+
if capacity < mib {
38+
panic(capacity)
39+
}
40+
b := payload[i*mib : (i+1)*mib]
41+
n, err := w.Write(b)
42+
check(err)
43+
if n != len(b) {
44+
panic(n)
45+
}
46+
check(w.Close())
47+
readers[i] = r
48+
}
49+
prefix := sha256.Sum256(payload[:len(readers)*mib])
50+
entire := sha256.Sum256(payload)
51+
listener, err := net.Listen("unix", address)
52+
check(err)
53+
for {
54+
c, err := listener.Accept()
55+
check(err)
56+
var op [1]byte
57+
_, err = io.ReadFull(c, op[:])
58+
check(err)
59+
switch op[0] {
60+
case 'r':
61+
_, err = fmt.Fprintf(c, "%x %x\n", entire, prefix)
62+
case 'v':
63+
h := sha256.New()
64+
for _, r := range readers {
65+
n, err := io.Copy(h, r)
66+
check(err)
67+
if n != mib {
68+
panic(n)
69+
}
70+
check(r.Close())
71+
}
72+
_, err = fmt.Fprintf(c, "%x %x\n", sha256.Sum256(payload), h.Sum(nil))
73+
default:
74+
panic(op)
75+
}
76+
check(err)
77+
check(c.Close())
78+
}
79+
}
80+
81+
func main() {
82+
if len(os.Args) == 1 {
83+
serve()
84+
return
85+
}
86+
if os.Args[1] != "ready" && os.Args[1] != "verify" {
87+
panic(os.Args[1])
88+
}
89+
c, err := net.Dial("unix", address)
90+
check(err)
91+
defer c.Close()
92+
_, err = c.Write([]byte{os.Args[1][0]})
93+
check(err)
94+
_, err = io.Copy(os.Stdout, c)
95+
check(err)
96+
}

0 commit comments

Comments
 (0)