-
Notifications
You must be signed in to change notification settings - Fork 1
/
memory.go
48 lines (45 loc) · 1.14 KB
/
memory.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
package main
import (
"bufio"
"os"
"path/filepath"
"strconv"
"strings"
"sync"
"time"
)
func getMemory() {
go func() {
for {
start := time.Now()
fnames, _ := filepath.Glob("/proc/[0-9]*/statm")
ts := time.Now().Unix()
for _, fname := range fnames {
var rss, shared uint64
fd, err := os.Open(fname)
if err == nil {
bf := bufio.NewReader(fd)
line, isPrefix, err := bf.ReadLine()
if err == nil && !isPrefix {
data := strings.Fields(string(line))
rss, _ = strconv.ParseUint(data[1], 10, 64)
shared, _ = strconv.ParseUint(data[2], 10, 64)
}
}
fd.Close()
cmd, _ := os.Open(strings.Join(append(strings.Split(fname, "statm")[0:1], "comm"), ""))
name, _, _ := bufio.NewReader(cmd).ReadLine()
cmd.Close()
pid, _ := strconv.Atoi(strings.Split(fname, "/")[2])
usage := float32(int(float64((rss-shared)*uint64(PageSize/1024))/float64(TotalMemory)*10000)) / 100
if usage > 0.01 {
ChanMem <- Memory{"memory", pid, string(name), usage, ts}
}
}
time.Sleep((1000000000 - time.Since(start)) * time.Nanosecond)
}
}()
var wg sync.WaitGroup
wg.Add(1)
wg.Wait()
}