Skip to content

Commit

Permalink
Parallelize MemoryFile save and kernel save.
Browse files Browse the repository at this point in the history
This compliments 39730b7 ("Load pgalloc.MemoryFile and kernel parallely
with compression=none mode.")

This is a performance optimization. Kernel and MemoryFile are saved
independently. The save can be done in parallel when using --compression=none
because the kernel and MemoryFile are being saved in different files.

PiperOrigin-RevId: 668128205
  • Loading branch information
ayushr2 authored and gvisor-bot committed Aug 27, 2024
1 parent 41cd09d commit 218f52a
Showing 1 changed file with 31 additions and 3 deletions.
34 changes: 31 additions & 3 deletions pkg/sentry/kernel/kernel.go
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,23 @@ func (k *Kernel) SaveTo(ctx context.Context, w, pagesMetadata io.Writer, pagesFi
mf.MarkSavable()
}

var (
mfSaveWg sync.WaitGroup
mfSaveErr error
)
parallelMfSave := pagesMetadata != nil && pagesFile != nil
if parallelMfSave {
// Parallelize MemoryFile save and kernel save. Both are independent.
mfSaveWg.Add(1)
go func() {
defer mfSaveWg.Done()
mfSaveErr = k.saveMemoryFiles(ctx, w, pagesMetadata, pagesFile, mfsToSave, mfOpts)
}()
// Defer a Wait() so we wait for k.saveMemoryFiles() to complete even if we
// error out without reaching the other Wait() below.
defer mfSaveWg.Wait()
}

// Save the CPUID FeatureSet before the rest of the kernel so we can
// verify its compatibility on restore before attempting to restore the
// entire kernel, which may fail on an incompatible machine.
Expand Down Expand Up @@ -690,6 +707,20 @@ func (k *Kernel) SaveTo(ctx context.Context, w, pagesMetadata io.Writer, pagesFi
log.Infof("Kernel save stats: %s", stats.String())
log.Infof("Kernel save took [%s].", time.Since(kernelStart))

if parallelMfSave {
mfSaveWg.Wait()
} else {
mfSaveErr = k.saveMemoryFiles(ctx, w, pagesMetadata, pagesFile, mfsToSave, mfOpts)
}
if mfSaveErr != nil {
return mfSaveErr
}

log.Infof("Overall save took [%s].", time.Since(saveStart))
return nil
}

func (k *Kernel) saveMemoryFiles(ctx context.Context, w, pagesMetadata io.Writer, pagesFile *fd.FD, mfsToSave map[string]*pgalloc.MemoryFile, mfOpts pgalloc.SaveOpts) error {
// Save the memory files' state.
memoryStart := time.Now()
pmw := w
Expand All @@ -707,9 +738,6 @@ func (k *Kernel) SaveTo(ctx context.Context, w, pagesMetadata io.Writer, pagesFi
return err
}
log.Infof("Memory files save took [%s].", time.Since(memoryStart))

log.Infof("Overall save took [%s].", time.Since(saveStart))

return nil
}

Expand Down

0 comments on commit 218f52a

Please sign in to comment.