summaryrefslogtreecommitdiffhomepage
path: root/pkg/sentry/fsimpl/proc/meminfo.go
blob: 9a827cd66c3fa89fc9e01c41b7dd55f5c6e370f7 (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
// Copyright 2019 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/kernel"
	"gvisor.dev/gvisor/pkg/sentry/usage"
	"gvisor.dev/gvisor/pkg/sentry/usermem"
	"gvisor.dev/gvisor/pkg/sentry/vfs"
)

// meminfoData implements vfs.DynamicBytesSource for /proc/meminfo.
//
// +stateify savable
type meminfoData struct {
	// k is the owning Kernel.
	k *kernel.Kernel
}

var _ vfs.DynamicBytesSource = (*meminfoData)(nil)

// Generate implements vfs.DynamicBytesSource.Generate.
func (d *meminfoData) Generate(ctx context.Context, buf *bytes.Buffer) error {
	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

	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 nil
}