/
proc_linux.go
160 lines (151 loc) · 3.99 KB
/
proc_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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package main
import (
"bufio"
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
"path"
"strconv"
"strings"
)
const (
// max length of /proc/$PID/comm
CommMax = 16
// from ps_mem - average error due to truncation in the kernel
// pss calculations
PssAdjust = .5
pageSize = 4096
mapDetailLen = len("Size: 4 kB")
)
var (
tyVmFlags = []byte("VmFlags:")
tyPss = []byte("Pss:")
tySwap = []byte("Swap:")
tyPrivateClean = []byte("Private_Clean:")
tyPrivateDirty = []byte("Private_Dirty:")
kb = []byte(" kB")
)
// pidList returns a list of the process-IDs of every currently
// running process on the local system.
func pidList() ([]int, error) {
procLs, err := ioutil.ReadDir("/proc")
if err != nil {
return nil, fmt.Errorf("ReadDir(/proc): %s", err)
}
pids := make([]int, 0, len(procLs))
for _, pInfo := range procLs {
if !isDigit(pInfo.Name()[0]) || !pInfo.IsDir() {
continue
}
pidInt, err := strconv.Atoi(pInfo.Name())
if err != nil {
return nil, fmt.Errorf("Atoi(%s): %s", pInfo.Name(), err)
}
pids = append(pids, pidInt)
}
return pids, nil
}
// procName gets the process name for a worker. It first checks the
// value of /proc/$PID/cmdline. If setproctitle(3) has been called,
// it will use this. Otherwise it uses the value of
// path.Base(/proc/$PID/exe), which has info on whether the executable
// has changed since the process was exec'ed.
func procName(pid int) (string, error) {
p, err := os.Readlink(fmt.Sprintf("/proc/%d/exe", pid))
// this would return an error if the PID doesn't
// exist, or if the PID refers to a kernel thread.
if err != nil {
return "", nil
}
// cmdline is the null separated list of command line
// arguments for the process, unless setproctitle(3)
// has been called, in which case it is the
argsB, err := ioutil.ReadFile(fmt.Sprintf("/proc/%d/cmdline", pid))
if err != nil {
return "", fmt.Errorf("ReadFile(%s): %s", fmt.Sprintf("/proc/%d/cmdline", pid), err)
}
args := strings.Split(string(argsB), "\000")
n := args[0]
nTrunc := n
if len(n) > CommMax {
nTrunc = n[:CommMax]
}
if strings.HasPrefix(p, nTrunc) {
n = path.Base(p)
}
return n, nil
}
// procMem returns the amount of Pss, shared, and swapped out memory
// used. The swapped out amount refers to anonymous pages only.
func procMem(pid int) (pss, shared, heap, swap float64, err error) {
fPath := fmt.Sprintf("/proc/%d/smaps", pid)
f, err := os.Open(fPath)
if err != nil {
err = fmt.Errorf("os.Open(%s): %s", fPath, err)
return
}
var priv float64
var curr MapInfo
r := bufio.NewReaderSize(f, pageSize)
for {
var l []byte
var isPrefix bool
l, isPrefix, err = r.ReadLine()
// this should never happen, so take the easy way out.
if isPrefix {
err = fmt.Errorf("ReadLine(%s): isPrefix", fPath)
}
if err != nil {
// if we've got EOF, then we're simply done
// processing smaps.
if err == io.EOF {
err = nil
break
}
// otherwise error out
err = fmt.Errorf("ReadLine(%s): %s", fPath, err)
return
}
if len(l) != mapDetailLen && !bytes.HasSuffix(l, kb) {
if !bytes.HasPrefix(l, tyVmFlags) {
curr = NewMapInfo(l)
}
continue
}
pieces := splitSpaces(l)
ty := pieces[0]
var v uint64
if bytes.Equal(ty, tyPss) {
v, err = ParseUint(pieces[1], 10, 64)
if err != nil {
err = fmt.Errorf("Atoi(%s): %s", string(pieces[1]), err)
return
}
m := float64(v)
pss += m + PssAdjust
if curr.Name == "[heap]" {
// we don't nead PssAdjust because
// heap is private and anonymous.
heap = m
}
} else if bytes.Equal(ty, tyPrivateClean) || bytes.Equal(ty, tyPrivateDirty) {
v, err = ParseUint(pieces[1], 10, 64)
if err != nil {
err = fmt.Errorf("Atoi(%s): %s", string(pieces[1]), err)
return
}
priv += float64(v)
} else if bytes.Equal(ty, tySwap) {
v, err = ParseUint(pieces[1], 10, 64)
if err != nil {
err = fmt.Errorf("Atoi(%s): %s", string(pieces[1]), err)
return
}
swap += float64(v)
}
}
shared = pss - priv
return
}