forked from shirou/gopsutil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mem_linux.go
66 lines (56 loc) · 1.46 KB
/
mem_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
// +build linux
package gopsutil
import (
"strings"
"syscall"
)
func VirtualMemory() (*VirtualMemoryStat, error) {
filename := "/proc/meminfo"
lines, _ := readLines(filename)
ret := &VirtualMemoryStat{}
for _, line := range lines {
fields := strings.Split(line, ":")
if len(fields) != 2 {
continue
}
key := strings.TrimSpace(fields[0])
value := strings.TrimSpace(fields[1])
value = strings.Replace(value, " kB", "", -1)
switch key {
case "MemTotal":
ret.Total = mustParseUint64(value) * 1000
case "MemFree":
ret.Free = mustParseUint64(value) * 1000
case "Buffers":
ret.Buffers = mustParseUint64(value) * 1000
case "Cached":
ret.Cached = mustParseUint64(value) * 1000
case "Active":
ret.Active = mustParseUint64(value) * 1000
case "Inactive":
ret.Inactive = mustParseUint64(value) * 1000
}
}
ret.Available = ret.Free + ret.Buffers + ret.Cached
ret.Used = ret.Total - ret.Free
ret.UsedPercent = float64(ret.Total-ret.Available) / float64(ret.Total) * 100.0
return ret, nil
}
func SwapMemory() (*SwapMemoryStat, error) {
sysinfo := &syscall.Sysinfo_t{}
if err := syscall.Sysinfo(sysinfo); err != nil {
return nil, err
}
ret := &SwapMemoryStat{
Total: uint64(sysinfo.Totalswap),
Free: uint64(sysinfo.Freeswap),
}
ret.Used = ret.Total - ret.Free
//check Infinity
if ret.Total != 0 {
ret.UsedPercent = float64(ret.Total-ret.Free) / float64(ret.Total) * 100.0
} else {
ret.UsedPercent = 0
}
return ret, nil
}