@@ -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
3233var previousMetadata map [string ]string
3334
35+ const gvisorCPUUsageKey = "gvisor_cpu_usage"
36+
3437// SaveOpts contains save-related options.
3538type SaveOpts struct {
3639 // Destination is the save target.
@@ -79,7 +82,10 @@ func (opts *SaveOpts) Close() error {
7982
8083// Save saves the system state.
8184func (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.
0 commit comments