summaryrefslogtreecommitdiffhomepage
path: root/pkg/sentry/usage
diff options
context:
space:
mode:
authorFabricio Voznika <fvoznika@google.com>2018-09-17 21:33:51 -0700
committerShentubot <shentubot@google.com>2018-09-17 21:34:37 -0700
commit5d9816be41a967fa1fa9bbbe0c638dd322c7c0b1 (patch)
tree69ce4df0c42bcd4f8788cd40238cecf903bede94 /pkg/sentry/usage
parent26b08e182cd08371f14dc58fd54ed7865452cea7 (diff)
Remove memory usage static init
panic() during init() can be hard to debug. Updates #100 PiperOrigin-RevId: 213391932 Change-Id: Ic103f1981c5b48f1e12da3b42e696e84ffac02a9
Diffstat (limited to 'pkg/sentry/usage')
-rw-r--r--pkg/sentry/usage/memory.go16
1 files changed, 9 insertions, 7 deletions
diff --git a/pkg/sentry/usage/memory.go b/pkg/sentry/usage/memory.go
index 4a1527b5f..f13a77779 100644
--- a/pkg/sentry/usage/memory.go
+++ b/pkg/sentry/usage/memory.go
@@ -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 {