Remove memory usage static init

panic() during init() can be hard to debug.

Updates #100

PiperOrigin-RevId: 213391932
Change-Id: Ic103f1981c5b48f1e12da3b42e696e84ffac02a9
This commit is contained in:
Fabricio Voznika 2018-09-17 21:33:51 -07:00 committed by Shentubot
parent 26b08e182c
commit 5d9816be41
2 changed files with 13 additions and 7 deletions

View File

@ -117,15 +117,16 @@ type MemoryLocked struct {
File *os.File
}
func newMemoryLocked() MemoryLocked {
name := "memory-usage"
// Init initializes global 'MemoryAccounting'.
func Init() error {
const name = "memory-usage"
fd, err := memutil.CreateMemFD(name, 0)
if err != nil {
panic("error creating usage file: " + err.Error())
return fmt.Errorf("error creating usage file: %v", err)
}
file := os.NewFile(uintptr(fd), name)
if err := file.Truncate(int64(RTMemoryStatsSize)); err != nil {
panic("error truncating usage file: " + err.Error())
return fmt.Errorf("error truncating usage file: %v", err)
}
// Note: We rely on the returned page being initially zeroed. This will
// always be the case for a newly mapped page from /dev/shm. If we obtain
@ -133,13 +134,14 @@ func newMemoryLocked() MemoryLocked {
// explicitly zero the page.
mmap, err := syscall.Mmap(int(file.Fd()), 0, int(RTMemoryStatsSize), syscall.PROT_READ|syscall.PROT_WRITE, syscall.MAP_SHARED)
if err != nil {
panic("error mapping usage file: " + err.Error())
return fmt.Errorf("error mapping usage file: %v", err)
}
return MemoryLocked{
MemoryAccounting = &MemoryLocked{
File: file,
RTMemoryStats: RTMemoryStatsPointer(mmap),
}
return nil
}
// MemoryAccounting is the global memory stats.
@ -147,7 +149,7 @@ func newMemoryLocked() MemoryLocked {
// There is no need to save or restore the global memory accounting object,
// because individual frame kinds are saved and charged only when they become
// resident.
var MemoryAccounting = newMemoryLocked()
var MemoryAccounting *MemoryLocked
func (m *MemoryLocked) incLocked(val uint64, kind MemoryKind) {
switch kind {

View File

@ -42,6 +42,7 @@ import (
"gvisor.googlesource.com/gvisor/pkg/sentry/sighandling"
slinux "gvisor.googlesource.com/gvisor/pkg/sentry/syscalls/linux"
"gvisor.googlesource.com/gvisor/pkg/sentry/time"
"gvisor.googlesource.com/gvisor/pkg/sentry/usage"
"gvisor.googlesource.com/gvisor/pkg/sentry/watchdog"
"gvisor.googlesource.com/gvisor/pkg/tcpip"
"gvisor.googlesource.com/gvisor/pkg/tcpip/link/sniffer"
@ -143,6 +144,9 @@ func init() {
// New initializes a new kernel loader configured by spec.
// New also handles setting up a kernel for restoring a container.
func New(spec *specs.Spec, conf *Config, controllerFD, deviceFD int, goferFDs []int, console bool) (*Loader, error) {
if err := usage.Init(); err != nil {
return nil, fmt.Errorf("Error setting up memory usage: %v", err)
}
// Create kernel and platform.
p, err := createPlatform(conf, deviceFD)
if err != nil {