-
Notifications
You must be signed in to change notification settings - Fork 0
/
free_linux.go
107 lines (97 loc) · 2.75 KB
/
free_linux.go
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
// Copyright 2018 the u-root Authors. All rights reserved
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"bytes"
"fmt"
"io/ioutil"
"strconv"
)
const meminfoFile = "/proc/meminfo"
// meminfo returns a mapping that represents the fields contained in
// /proc/meminfo
func meminfo() (meminfomap, error) {
buf, err := ioutil.ReadFile(meminfoFile)
if err != nil {
return nil, err
}
return meminfoFromBytes(buf)
}
// meminfoFromBytes returns a mapping that represents the fields contained in a
// byte stream with a content compatible with /proc/meminfo
func meminfoFromBytes(buf []byte) (meminfomap, error) {
ret := make(meminfomap)
for _, line := range bytes.Split(buf, []byte{'\n'}) {
kv := bytes.SplitN(line, []byte{':'}, 2)
if len(kv) != 2 {
// invalid line?
continue
}
key := string(kv[0])
tokens := bytes.SplitN(bytes.TrimSpace(kv[1]), []byte{' '}, 2)
if len(tokens) > 0 {
value, err := strconv.ParseUint(string(tokens[0]), 10, 64)
if err != nil {
return nil, err
}
ret[key] = value
}
}
return ret, nil
}
// getMainMemInfo prints the physical memory information in the specified units. Only
// the relevant fields will be used from the input map.
func getMainMemInfo(m meminfomap, config *FreeConfig) (*mainMemInfo, error) {
fields := []string{
"MemTotal",
"MemFree",
"Buffers",
"Cached",
"Shmem",
"SReclaimable",
"MemAvailable",
}
if missingRequiredFields(m, fields) {
return nil, fmt.Errorf("missing required fields from meminfo")
}
// These values are expressed in kibibytes, convert to the desired unit
memTotal := m["MemTotal"] << KB
memFree := m["MemFree"] << KB
memShared := m["Shmem"] << KB
memCached := (m["Cached"] + m["SReclaimable"]) << KB
memBuffers := (m["Buffers"]) << KB
memUsed := memTotal - memFree - memCached - memBuffers
memAvailable := m["MemAvailable"] << KB
mmi := mainMemInfo{
Total: memTotal,
Used: memUsed,
Free: memFree,
Shared: memShared,
Cached: memCached,
Buffers: memBuffers,
Available: memAvailable,
}
return &mmi, nil
}
// getSwapInfo prints the swap space information in the specified units. Only the
// relevant fields will be used from the input map.
func getSwapInfo(m meminfomap, config *FreeConfig) (*swapInfo, error) {
fields := []string{
"SwapTotal",
"SwapFree",
}
if missingRequiredFields(m, fields) {
return nil, fmt.Errorf("missing required fields from meminfo")
}
// These values are expressed in kibibytes, convert to the desired unit
swapTotal := m["SwapTotal"] << KB
swapUsed := (m["SwapTotal"] - m["SwapFree"]) << KB
swapFree := m["SwapFree"] << KB
si := swapInfo{
Total: swapTotal,
Used: swapUsed,
Free: swapFree,
}
return &si, nil
}