mirror of
https://github.com/netbirdio/gvisor.git
synced 2026-05-22 17:12:49 -07:00
sentry: capture CPU usage metadata for save.
PiperOrigin-RevId: 195466647 Change-Id: Ib5ca815f7b64a4881441e58567adedf344b206f1
This commit is contained in:
@@ -7,10 +7,12 @@ go_library(
|
||||
srcs = [
|
||||
"state.go",
|
||||
"state_metadata.go",
|
||||
"state_unsafe.go",
|
||||
],
|
||||
importpath = "gvisor.googlesource.com/gvisor/pkg/sentry/state",
|
||||
visibility = ["//pkg/sentry:internal"],
|
||||
deps = [
|
||||
"//pkg/abi/linux",
|
||||
"//pkg/log",
|
||||
"//pkg/sentry/inet",
|
||||
"//pkg/sentry/kernel",
|
||||
|
||||
@@ -27,6 +27,8 @@ import (
|
||||
"gvisor.googlesource.com/gvisor/pkg/state/statefile"
|
||||
)
|
||||
|
||||
var previousMetadata map[string]string
|
||||
|
||||
// ErrStateFile is returned when the state file cannot be opened.
|
||||
type ErrStateFile struct {
|
||||
err error
|
||||
@@ -103,11 +105,13 @@ type LoadOpts struct {
|
||||
// Load loads the given kernel, setting the provided platform and stack.
|
||||
func (opts LoadOpts) Load(k *kernel.Kernel, p platform.Platform, n inet.Stack) error {
|
||||
// Open the file.
|
||||
r, _, err := statefile.NewReader(opts.Source, opts.Key)
|
||||
r, m, err := statefile.NewReader(opts.Source, opts.Key)
|
||||
if err != nil {
|
||||
return ErrStateFile{err}
|
||||
}
|
||||
|
||||
previousMetadata = m
|
||||
|
||||
// Restore the Kernel object graph.
|
||||
return k.LoadFrom(r, p, n)
|
||||
}
|
||||
|
||||
@@ -17,13 +17,29 @@ package state
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"gvisor.googlesource.com/gvisor/pkg/log"
|
||||
)
|
||||
|
||||
// The save metadata keys for timestamp.
|
||||
const (
|
||||
cpuUsage = "cpu_usage"
|
||||
metadataTimestamp = "timestamp"
|
||||
)
|
||||
|
||||
func addSaveMetadata(m map[string]string) {
|
||||
t, err := cpuTime()
|
||||
if err != nil {
|
||||
log.Warningf("Error getting cpu time: %v", err)
|
||||
}
|
||||
if previousMetadata != nil {
|
||||
p, err := time.ParseDuration(previousMetadata[cpuUsage])
|
||||
if err != nil {
|
||||
log.Warningf("Error parsing previous runs' cpu time: %v", err)
|
||||
}
|
||||
t += p
|
||||
}
|
||||
m[cpuUsage] = t.String()
|
||||
|
||||
m[metadataTimestamp] = fmt.Sprintf("%v", time.Now())
|
||||
}
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
// Copyright 2018 Google Inc.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package state
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"syscall"
|
||||
"time"
|
||||
"unsafe"
|
||||
|
||||
"gvisor.googlesource.com/gvisor/pkg/abi/linux"
|
||||
)
|
||||
|
||||
func cpuTime() (time.Duration, error) {
|
||||
var ts syscall.Timespec
|
||||
_, _, errno := syscall.RawSyscall(syscall.SYS_CLOCK_GETTIME, uintptr(linux.CLOCK_PROCESS_CPUTIME_ID), uintptr(unsafe.Pointer(&ts)), 0)
|
||||
if errno != 0 {
|
||||
return 0, fmt.Errorf("failed calling clock_gettime(CLOCK_PROCESS_CPUTIME_ID): errno=%d", errno)
|
||||
}
|
||||
return time.Duration(ts.Nano()), nil
|
||||
}
|
||||
Reference in New Issue
Block a user