-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmeminfo_linux.go
139 lines (117 loc) · 2.94 KB
/
meminfo_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
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
// +build linux
package oom
import (
"bufio"
"os"
"path/filepath"
"strconv"
"strings"
"sync"
"time"
)
var meminfo = &MemInfo{}
// MemoryUsage usage returns used to total memory ratio
func MemoryUsage() float64 {
meminfo.Update()
total := meminfo.Total()
if total == 0 { // don't divide by 0
return 0.0
}
return float64(meminfo.Used()) / float64(total)
}
// SetUpdateInteval updates the interval between MemInfo updates. By default
// Updates happen on every request
func SetUpdateInteval(i time.Duration) {
if i.Nanoseconds() == 0 {
return
}
meminfo.mutex.Lock()
meminfo.updateInterval = &i
meminfo.mutex.Unlock()
}
// Following is copied/adapted github.com/guillermo/go.procmeminfo
// MemInfo contains data feched from /proc/meminfo, mutex preventing races and
// update frequency related fields
type MemInfo struct {
mutex sync.RWMutex
lastUpdate time.Time // in milliseconds
updateInterval *time.Duration
Values map[string]uint64
}
// Update get fresh data from /proc/meminfo
func (m *MemInfo) Update() error {
m.mutex.Lock()
defer m.mutex.Unlock()
if m.updateInterval != nil {
now := time.Now()
if now.Before(m.lastUpdate.Add(*m.updateInterval)) {
return nil // no need to update yet
}
m.lastUpdate = now
}
if m.Values == nil {
m.Values = make(map[string]uint64)
}
var err error
path := filepath.Join("/proc/meminfo")
file, err := os.Open(path)
defer file.Close()
if err != nil {
return err
}
scanner := bufio.NewScanner(file)
for scanner.Scan() {
text := scanner.Text()
n := strings.Index(text, ":")
if n == -1 {
continue
}
key := text[:n] // metric
data := strings.Split(strings.Trim(text[(n+1):], " "), " ")
if len(data) == 1 {
value, err := strconv.ParseUint(data[0], 10, 64)
if err != nil {
continue
}
m.Values[key] = value
} else if len(data) == 2 {
if data[1] == "kB" {
value, err := strconv.ParseUint(data[0], 10, 64)
if err != nil {
continue
}
m.Values[key] = value * 1024
}
}
}
return nil
}
// Total returns total RAM of system
func (m *MemInfo) Total() uint64 {
m.mutex.RLock()
defer m.mutex.RUnlock()
total, _ := m.Values["MemTotal"]
return total
}
// Available returns the amount of still RAM available
// It uses new process mem available estimation - factors in reclaiming file
// buffers and cache.
// On Linux kernels 3.14+ it tens to err on the side of caution
// On Linux kernels older than 3.14 it's a tiny bit too optimistic, unless you
// are using next to no file buffers
func (m *MemInfo) Available() uint64 {
m.mutex.RLock()
defer m.mutex.RUnlock()
if av, ok := m.Values["MemAvailable"]; ok {
return av
}
// This will happen only on kernels < 3.14, will overestimate a tiny bit
fr, _ := m.Values["MemFree"]
buf, _ := m.Values["Buffers"]
ca, _ := m.Values["Cached"]
return fr + buf + ca
}
// Used returns the amount of non-reclaimable memory used
func (m *MemInfo) Used() uint64 {
return m.Total() - m.Available()
}