Skip to content

Commit 6b63320

Browse files
nybidarigvisor-bot
authored andcommitted
Store the CPU time usage in the metadata during save operation.
PiperOrigin-RevId: 862781446
1 parent 5f31525 commit 6b63320

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

pkg/sentry/state/state.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"errors"
2020
"fmt"
2121
"io"
22+
"time"
2223

2324
"gvisor.dev/gvisor/pkg/abi/linux"
2425
"gvisor.dev/gvisor/pkg/context"
@@ -31,6 +32,8 @@ import (
3132

3233
var previousMetadata map[string]string
3334

35+
const gvisorCPUUsageKey = "gvisor_cpu_usage"
36+
3437
// SaveOpts contains save-related options.
3538
type SaveOpts struct {
3639
// Destination is the save target.
@@ -79,7 +82,10 @@ func (opts *SaveOpts) Close() error {
7982

8083
// Save saves the system state.
8184
func (opts *SaveOpts) Save(ctx context.Context, k *kernel.Kernel, w *watchdog.Watchdog) error {
82-
t, _ := CPUTime()
85+
t, err := CPUTime()
86+
if err != nil {
87+
log.Warningf("Error getting cpu time: %v", err)
88+
}
8389
log.Infof("Before save CPU usage: %s", t.String())
8490

8591
log.Infof("Sandbox save started, pausing all tasks.")
@@ -97,6 +103,14 @@ func (opts *SaveOpts) Save(ctx context.Context, k *kernel.Kernel, w *watchdog.Wa
97103
if opts.Metadata == nil {
98104
opts.Metadata = make(map[string]string)
99105
}
106+
if previousMetadata != nil {
107+
p, err := time.ParseDuration(previousMetadata[gvisorCPUUsageKey])
108+
if err != nil {
109+
log.Warningf("Error parsing previous runs' cpu time: %v", err)
110+
}
111+
t += p
112+
}
113+
opts.Metadata[gvisorCPUUsageKey] = t.String()
100114
addSaveMetadata(opts.Metadata)
101115

102116
// Open the statefile.

runsc/boot/controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -638,7 +638,7 @@ func (cm *containerManager) Restore(o *RestoreOpts, _ *struct{}) (retErr error)
638638

639639
containerSpecs, ok := metadata[ContainerSpecsKey]
640640
if !ok {
641-
return fmt.Errorf("container specs not found in metdata during restore")
641+
return fmt.Errorf("container specs not found in metadata during restore")
642642
}
643643
specs, err := specutils.GetSpecsFromString(containerSpecs)
644644
if err != nil {

0 commit comments

Comments
 (0)