-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmeminfo.go
105 lines (92 loc) · 3.16 KB
/
meminfo.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
package proc
import (
"io/ioutil"
"reflect"
"strconv"
"strings"
)
func (self *MemInfo) Total() uint64 {
return self.MemTotal
}
func (self *MemInfo) Free() uint64 {
return self.MemFree
}
type MemInfo struct {
MemTotal uint64 `json:"mem_total"`
MemFree uint64 `json:"mem_free"`
MemAvailable uint64 `json:"mem_available"`
Buffers uint64 `json:"buffers"`
Cached uint64 `json:"cached"`
SwapCached uint64 `json:"swap_cached"`
Active uint64 `json:"active"`
Inactive uint64 `json:"inactive"`
ActiveAnon uint64 `json:"active_anon" field:"Active(anon)"`
InactiveAnon uint64 `json:"inactive_anon" field:"Inactive(anon)"`
ActiveFile uint64 `json:"active_file" field:"Active(file)"`
InactiveFile uint64 `json:"inactive_file" field:"Inactive(file)"`
Unevictable uint64 `json:"unevictable"`
Mlocked uint64 `json:"mlocked"`
SwapTotal uint64 `json:"swap_total"`
SwapFree uint64 `json:"swap_free"`
Dirty uint64 `json:"dirty"`
Writeback uint64 `json:"write_back"`
AnonPages uint64 `json:"anon_pages"`
Mapped uint64 `json:"mapped"`
Shmem uint64 `json:"shmem"`
Slab uint64 `json:"slab"`
SReclaimable uint64 `json:"s_reclaimable"`
SUnreclaim uint64 `json:"s_unclaim"`
KernelStack uint64 `json:"kernel_stack"`
PageTables uint64 `json:"page_tables"`
NFS_Unstable uint64 `json:"nfs_unstable"`
Bounce uint64 `json:"bounce"`
WritebackTmp uint64 `json:"writeback_tmp"`
CommitLimit uint64 `json:"commit_limit"`
Committed_AS uint64 `json:"committed_as"`
VmallocTotal uint64 `json:"vmalloc_total"`
VmallocUsed uint64 `json:"vmalloc_used"`
VmallocChunk uint64 `json:"vmalloc_chunk"`
HardwareCorrupted uint64 `json:"hardware_corrupted"`
AnonHugePages uint64 `json:"anon_huge_pages"`
HugePages_Total uint64 `json:"huge_pages_total"`
HugePages_Free uint64 `json:"huge_pages_free"`
HugePages_Rsvd uint64 `json:"huge_pages_rsvd"`
HugePages_Surp uint64 `json:"huge_pages_surp"`
Hugepagesize uint64 `json:"hugepagesize"`
DirectMap4k uint64 `json:"direct_map_4k"`
DirectMap2M uint64 `json:"direct_map_2M"`
DirectMap1G uint64 `json:"direct_map_1G"`
}
func ReadMemInfo(path string) (*MemInfo, error) {
data, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
lines := strings.Split(string(data), "\n")
// Maps a meminfo metric to its value (i.e. MemTotal --> 100000)
statMap := make(map[string]uint64)
var info = MemInfo{}
for _, line := range lines {
fields := strings.SplitN(line, ":", 2)
if len(fields) < 2 {
continue
}
valFields := strings.Fields(fields[1])
val, _ := strconv.ParseUint(valFields[0], 10, 64)
statMap[fields[0]] = val
}
elem := reflect.ValueOf(&info).Elem()
typeOfElem := elem.Type()
for i := 0; i < elem.NumField(); i++ {
val, ok := statMap[typeOfElem.Field(i).Name]
if ok {
elem.Field(i).SetUint(val)
continue
}
val, ok = statMap[typeOfElem.Field(i).Tag.Get("field")]
if ok {
elem.Field(i).SetUint(val)
}
}
return &info, nil
}