forked from adamlaska/containerd
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebug.go
More file actions
56 lines (53 loc) · 1.01 KB
/
debug.go
File metadata and controls
56 lines (53 loc) · 1.01 KB
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
package debug
import (
"bufio"
"fmt"
"os"
"strconv"
"strings"
)
// Smaps prints the smaps to a file
func Smaps(note, file string) error {
smaps, err := getMaps(os.Getpid())
if err != nil {
return err
}
f, err := os.OpenFile(file, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
if err != nil {
return err
}
defer f.Close()
fmt.Fprintf(f, "%s: rss %d\n", note, smaps["rss"])
fmt.Fprintf(f, "%s: pss %d\n", note, smaps["pss"])
return nil
}
func getMaps(pid int) (map[string]int, error) {
f, err := os.Open(fmt.Sprintf("/proc/%d/smaps", pid))
if err != nil {
return nil, err
}
defer f.Close()
var (
smaps = make(map[string]int)
s = bufio.NewScanner(f)
)
for s.Scan() {
var (
fields = strings.Fields(s.Text())
name = fields[0]
)
name = strings.TrimSuffix(strings.ToLower(name), ":")
if len(fields) < 2 {
continue
}
n, err := strconv.Atoi(fields[1])
if err != nil {
continue
}
smaps[name] += n
}
if err := s.Err(); err != nil {
return nil, err
}
return smaps, nil
}