-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathprocmeminfo.go
108 lines (98 loc) · 2.8 KB
/
procmeminfo.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
108
package bt
import (
"bufio"
"fmt"
"io"
"log"
"os"
"strconv"
"strings"
)
const (
memPath = "/proc/meminfo"
procPath = "/proc/self/status"
)
var (
paths = []string{memPath, procPath}
mapper = map[string]string{
"MemTotal": "system.memory.total",
"MemFree": "system.memory.free",
"MemAvailable": "system.memory.available",
"Buffers": "system.memory.buffers",
"Cached": "system.memory.cached",
"SwapCached": "system.memory.swap.cached",
"Active": "system.memory.active",
"Inactive": "system.memory.inactive",
"SwapTotal": "system.memory.swap.total",
"SwapFree": "system.memory.swap.free",
"Dirty": "system.memory.dirty",
"Writeback": "system.memory.writeback",
"Slab": "system.memory.slab",
"VmallocTotal": "system.memory.vmalloc.total",
"VmallocUsed": "system.memory.vmalloc.used",
"VmallocChunk": "system.memory.vmalloc.chunk",
"nonvoluntary_ctxt_switches": "sched.cs.involuntary",
"voluntary_ctxt_switches": "sched.cs.voluntary",
"FDSize": "descriptor.count",
"VmData": "vm.data.size",
"VmLck": "vm.locked.size",
"VmPTE": "vm.pte.size",
"VmHWM": "vm.rss.peak",
"VmRSS": "vm.rss.size",
"VmLib": "vm.shared.size",
"VmStk": "vm.stack.size",
"VmSwap": "vm.swap.size",
"VmPeak": "vm.vma.peak",
"VmSize": "vm.vma.size",
}
)
func readMemProcInfo() {
for _, path := range paths {
readFile(path)
}
}
func readFile(path string) {
file, err := os.Open(path)
if err != nil {
log.Fatal(err)
}
defer file.Close()
reader := bufio.NewReader(file)
for {
l, _, err := reader.ReadLine()
if err != nil {
if err == io.EOF {
break
} else {
if Options.DebugBacktrace {
log.Printf("readFile err: %v", err)
}
break
}
}
values := strings.Split(string(l), ":")
if len(values) == 2 {
if attr, exists := mapper[values[0]]; exists {
value, err := getValue(values[1])
if err != nil {
continue
}
Options.Attributes[attr] = value
}
}
}
}
func getValue(value string) (string, error) {
value = strings.TrimSpace(value)
if strings.HasSuffix(value, "kB") {
value = strings.TrimSuffix(value, " kB")
atoi, err := strconv.ParseInt(value, 10, 64)
if err != nil && Options.DebugBacktrace {
log.Printf("readFile err: %v", err)
return "", err
}
atoi *= 1024
return fmt.Sprintf("%d", atoi), err
}
return value, nil
}