File: meminfo_test.go

package info (click to toggle)
snapd 2.73-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 81,460 kB
  • sloc: sh: 16,736; ansic: 16,652; python: 11,215; makefile: 1,966; exp: 190; awk: 58; xml: 22
file content (217 lines) | stat: -rw-r--r-- 6,084 bytes parent folder | download | duplicates (5)
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
// -*- Mode: Go; indent-tabs-mode: t -*-

/*
 * Copyright (C) 2021 Canonical Ltd
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 3 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

package osutil_test

import (
	"os"
	"path/filepath"

	. "gopkg.in/check.v1"

	"github.com/snapcore/snapd/osutil"
)

type meminfoSuite struct{}

var _ = Suite(&meminfoSuite{})

const meminfoExampleFromLiveSystem = `MemTotal:       32876680 kB
MemFree:         3478104 kB
MemAvailable:   20527364 kB
Buffers:         1584432 kB
Cached:         14550292 kB
SwapCached:            0 kB
Active:          8344864 kB
Inactive:       16209412 kB
Active(anon):     139828 kB
Inactive(anon):  8944656 kB
Active(file):    8205036 kB
Inactive(file):  7264756 kB
Unevictable:        2152 kB
Mlocked:            2152 kB
SwapTotal:             0 kB
SwapFree:              0 kB
Dirty:               804 kB
Writeback:             4 kB
AnonPages:       8201352 kB
Mapped:          1223272 kB
Shmem:            697812 kB
KReclaimable:    2043404 kB
Slab:            2423344 kB
SReclaimable:    2043404 kB
SUnreclaim:       379940 kB
KernelStack:       37392 kB
PageTables:        97644 kB
NFS_Unstable:          0 kB
Bounce:                0 kB
WritebackTmp:          0 kB
CommitLimit:    16438340 kB
Committed_AS:   30191756 kB
VmallocTotal:   34359738367 kB
VmallocUsed:      160500 kB
VmallocChunk:          0 kB
Percpu:            24256 kB
HardwareCorrupted:     0 kB
AnonHugePages:    120832 kB
ShmemHugePages:        0 kB
ShmemPmdMapped:        0 kB
FileHugePages:         0 kB
FilePmdMapped:         0 kB
CmaTotal:              0 kB
CmaFree:               0 kB
HugePages_Total:       0
HugePages_Free:        0
HugePages_Rsvd:        0
HugePages_Surp:        0
Hugepagesize:       2048 kB
Hugetlb:               0 kB
DirectMap4k:     3059376 kB
DirectMap2M:    23089152 kB
DirectMap1G:     8388608 kB
`

const meminfoExampleFromPi3 = `MemTotal:         929956 kB
MemFree:           83420 kB
MemAvailable:     676936 kB
Buffers:           84036 kB
Cached:           439980 kB
SwapCached:            0 kB
Active:           371964 kB
Inactive:         280064 kB
Active(anon):      86560 kB
Inactive(anon):     9152 kB
Active(file):     285404 kB
Inactive(file):   270912 kB
Unevictable:           0 kB
Mlocked:               0 kB
SwapTotal:             0 kB
SwapFree:              0 kB
Dirty:                 8 kB
Writeback:             0 kB
AnonPages:        128052 kB
Mapped:            53224 kB
Shmem:             13360 kB
KReclaimable:      52404 kB
Slab:             118608 kB
SReclaimable:      52404 kB
SUnreclaim:        66204 kB
KernelStack:        2928 kB
PageTables:         1552 kB
NFS_Unstable:          0 kB
Bounce:                0 kB
WritebackTmp:          0 kB
CommitLimit:      464976 kB
Committed_AS:     496260 kB
VmallocTotal:   135290159040 kB
VmallocUsed:       13700 kB
VmallocChunk:          0 kB
Percpu:             2768 kB
CmaTotal:         131072 kB
CmaFree:            8664 kB
`

func (s *meminfoSuite) TestMemInfoHappy(c *C) {
	p := filepath.Join(c.MkDir(), "meminfo")
	restore := osutil.MockProcMeminfo(p)
	defer restore()

	c.Assert(os.WriteFile(p, []byte(meminfoExampleFromLiveSystem), 0644), IsNil)

	mem, err := osutil.TotalUsableMemory()
	c.Assert(err, IsNil)
	c.Check(mem, Equals, uint64(32876680)*1024)

	c.Assert(os.WriteFile(p, []byte(`MemTotal:    1234 kB`), 0644), IsNil)

	mem, err = osutil.TotalUsableMemory()
	c.Assert(err, IsNil)
	c.Check(mem, Equals, uint64(1234)*1024)

	const meminfoReorderedWithEmptyLine = `MemAvailable:   20527370 kB

MemTotal:       32876699 kB
MemFree:         3478104 kB
`
	c.Assert(os.WriteFile(p, []byte(meminfoReorderedWithEmptyLine), 0644), IsNil)

	mem, err = osutil.TotalUsableMemory()
	c.Assert(err, IsNil)
	c.Check(mem, Equals, uint64(32876699)*1024)

	c.Assert(os.WriteFile(p, []byte(meminfoExampleFromPi3), 0644), IsNil)

	// CmaTotal is taken correctly into account
	mem, err = osutil.TotalUsableMemory()
	c.Assert(err, IsNil)
	c.Check(mem, Equals, uint64(929956-131072)*1024)
}

func (s *meminfoSuite) TestMemInfoFromHost(c *C) {
	mem, err := osutil.TotalUsableMemory()
	c.Assert(err, IsNil)
	c.Check(mem > uint64(32*1024*1024),
		Equals, true, Commentf("unexpected system memory %v", mem))
}

func (s *meminfoSuite) TestMemInfoUnhappy(c *C) {
	p := filepath.Join(c.MkDir(), "meminfo")
	restore := osutil.MockProcMeminfo(p)
	defer restore()

	const noTotalMem = `MemFree:         3478104 kB
MemAvailable:   20527364 kB
Buffers:         1584432 kB
Cached:         14550292 kB
`
	const notkBTotalMem = `MemTotal:         3478104 MB
`
	const missingFieldsTotalMem = `MemTotal:  1234
`
	const badTotalMem = `MemTotal:  abcdef kB
`
	const hexTotalMem = `MemTotal:  0xabcdef kB
`

	for _, tc := range []struct {
		content, err string
	}{
		{
			content: noTotalMem,
			err:     `cannot determine the total amount of memory in the system from .*/meminfo`,
		}, {
			content: notkBTotalMem,
			err:     `cannot process unexpected meminfo entry "MemTotal:         3478104 MB"`,
		}, {
			content: missingFieldsTotalMem,
			err:     `cannot process unexpected meminfo entry "MemTotal:  1234"`,
		}, {
			content: badTotalMem,
			err:     `cannot convert memory size value: strconv.ParseUint: parsing "abcdef": invalid syntax`,
		}, {
			content: hexTotalMem,
			err:     `cannot convert memory size value: strconv.ParseUint: parsing "0xabcdef": invalid syntax`,
		},
	} {
		c.Assert(os.WriteFile(p, []byte(tc.content), 0644), IsNil)
		mem, err := osutil.TotalUsableMemory()
		c.Assert(err, ErrorMatches, tc.err)
		c.Check(mem, Equals, uint64(0))
	}
}