summaryrefslogtreecommitdiffhomepage
path: root/pkg/sentry/fs/proc/meminfo.go
blob: 313c6a32b6b2f1c6e669aba17e3573fe58c6b6e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// Copyright 2018 The gVisor Authors.
//
// 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 proc

import (
	"bytes"
	"fmt"

	"gvisor.dev/gvisor/pkg/sentry/context"
	"gvisor.dev/gvisor/pkg/sentry/fs/proc/seqfile"
	"gvisor.dev/gvisor/pkg/sentry/kernel"
	"gvisor.dev/gvisor/pkg/sentry/usage"
	"gvisor.dev/gvisor/pkg/sentry/usermem"
)

// LINT.IfChange

// meminfoData backs /proc/meminfo.
//
// +stateify savable
type meminfoData struct {
	// k is the owning Kernel.
	k *kernel.Kernel
}

// NeedsUpdate implements seqfile.SeqSource.NeedsUpdate.
func (*meminfoData) NeedsUpdate(generation int64) bool {
	return true
}

// ReadSeqFileData implements seqfile.SeqSource.ReadSeqFileData.
func (d *meminfoData) ReadSeqFileData(ctx context.Context, h seqfile.SeqHandle) ([]seqfile.SeqData, int64) {
	if h != nil {
		return nil, 0
	}

	mf := d.k.MemoryFile()
	mf.UpdateUsage()
	snapshot, totalUsage := usage.MemoryAccounting.Copy()
	totalSize := usage.TotalMemory(mf.TotalSize(), totalUsage)
	anon := snapshot.Anonymous + snapshot.Tmpfs
	file := snapshot.PageCache + snapshot.Mapped
	// We don't actually have active/inactive LRUs, so just make up numbers.
	activeFile := (file / 2) &^ (usermem.PageSize - 1)
	inactiveFile := file - activeFile

	var buf bytes.Buffer
	fmt.Fprintf(&buf, "MemTotal:       %8d kB\n", totalSize/1024)
	memFree := (totalSize - totalUsage) / 1024
	// We use MemFree as MemAvailable because we don't swap.
	// TODO(rahat): When reclaim is implemented the value of MemAvailable
	// should change.
	fmt.Fprintf(&buf, "MemFree:        %8d kB\n", memFree)
	fmt.Fprintf(&buf, "MemAvailable:   %8d kB\n", memFree)
	fmt.Fprintf(&buf, "Buffers:               0 kB\n") // memory usage by block devices
	fmt.Fprintf(&buf, "Cached:         %8d kB\n", (file+snapshot.Tmpfs)/1024)
	// Emulate a system with no swap, which disables inactivation of anon pages.
	fmt.Fprintf(&buf, "SwapCache:             0 kB\n")
	fmt.Fprintf(&buf, "Active:         %8d kB\n", (anon+activeFile)/1024)
	fmt.Fprintf(&buf, "Inactive:       %8d kB\n", inactiveFile/1024)
	fmt.Fprintf(&buf, "Active(anon):   %8d kB\n", anon/1024)
	fmt.Fprintf(&buf, "Inactive(anon):        0 kB\n")
	fmt.Fprintf(&buf, "Active(file):   %8d kB\n", activeFile/1024)
	fmt.Fprintf(&buf, "Inactive(file): %8d kB\n", inactiveFile/1024)
	fmt.Fprintf(&buf, "Unevictable:           0 kB\n") // TODO(b/31823263)
	fmt.Fprintf(&buf, "Mlocked:               0 kB\n") // TODO(b/31823263)
	fmt.Fprintf(&buf, "SwapTotal:             0 kB\n")
	fmt.Fprintf(&buf, "SwapFree:              0 kB\n")
	fmt.Fprintf(&buf, "Dirty:                 0 kB\n")
	fmt.Fprintf(&buf, "Writeback:             0 kB\n")
	fmt.Fprintf(&buf, "AnonPages:      %8d kB\n", anon/1024)
	fmt.Fprintf(&buf, "Mapped:         %8d kB\n", file/1024) // doesn't count mapped tmpfs, which we don't know
	fmt.Fprintf(&buf, "Shmem:          %8d kB\n", snapshot.Tmpfs/1024)
	return []seqfile.SeqData{{Buf: buf.Bytes(), Handle: (*meminfoData)(nil)}}, 0
}

// LINT.ThenChange(../../fsimpl/proc/tasks_files.go)