-
Notifications
You must be signed in to change notification settings - Fork 155
/
processes.go
222 lines (203 loc) · 5.45 KB
/
processes.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
// Copyright (c) 2020 Zededa, Inc.
// SPDX-License-Identifier: Apache-2.0
package domainmgr
// Periodically extract and publish information about the running processes
// and their memory, thread, FD, etc usage
import (
"fmt"
"os"
"path"
"strconv"
"strings"
"time"
"github.com/lf-edge/eve/pkg/pillar/types"
fileutils "github.com/lf-edge/eve/pkg/pillar/utils/file"
"github.com/shirou/gopsutil/process"
)
const (
maxStackStringLen = 1024 // Single stack
totalMaxStackLen = 8192 // All reported stacks
)
// Return a slice of all the ProcessMetric plus a map of the pids
// Excludes kernel-only processes
func gatherProcessMetricList(ctx *domainContext) ([]types.ProcessMetric, map[int32]bool) {
var ret []types.ProcessMetric
reportedPids := make(map[int32]bool)
watchedPids, err := getWatchedPids()
if err != nil {
log.Errorf("process.Processes failed: %s", err)
return ret, reportedPids
}
log.Tracef("watchedPids: %+v", watchedPids)
processes, err := process.Processes()
if err != nil {
log.Errorf("process.Processes failed: %s", err)
return ret, reportedPids
}
totalStacks := 0
for _, p := range processes {
pi, err := getProcessMetric(p)
if err != nil {
// Process might have just exited
log.Functionf("getProcessMetric failed: %s", err)
continue
}
if pi.UserProcess {
if _, ok := watchedPids[pi.Pid]; ok {
pi.Watched = true
} else if time.Since(pi.CreateTime) > time.Minute {
// Report stack for not watched processed which
// have been running for a while. This is
// a bit of belt and suspenders approach to
// catch hung processes which are not watched by
// the watchdog process to be able to see them
// even when we have no debug ssh access.
// We limit the total memory used for stacks for
// all the processes.
filename := fmt.Sprintf("/proc/%d/stack", pi.Pid)
b, err := fileutils.ReadWithMaxSize(log, filename, maxStackStringLen)
if err != nil {
log.Error(err)
continue
}
pi.Stack = string(b)
// Apply size limit
if pi.Stack != "" {
if totalStacks+len(pi.Stack) > totalMaxStackLen {
pi.Stack = "Omitted"
} else {
totalStacks += len(pi.Stack)
}
}
}
reportedPids[int32(pi.Pid)] = true
ret = append(ret, *pi)
}
}
return ret, reportedPids
}
const (
watchdogDirName = "/run/watchdog/pid/"
pidDirName = "/run"
)
// getWatchedPids returns a map will all the pids watched by watchdog
// based on /run/watchdog/pid/<foo> by reading the content of /run/<foo>
func getWatchedPids() (map[int32]bool, error) {
return getWatchedPidsFromDir(watchdogDirName, pidDirName)
}
func getWatchedPidsFromDir(wDirname string, pDirname string) (map[int32]bool, error) {
pids := make(map[int32]bool)
locations, err := os.ReadDir(wDirname)
if err != nil {
return pids, err
}
for _, location := range locations {
if location.IsDir() {
wsubdir := path.Join(wDirname, location.Name())
psubdir := path.Join(pDirname, location.Name())
p, err := getWatchedPidsFromDir(wsubdir, psubdir)
if err != nil {
continue
}
// copy map (entries)
for k, v := range p {
pids[k] = v
}
continue
}
pidFile := path.Join(pDirname, location.Name())
pidBytes, err := os.ReadFile(pidFile)
if err != nil {
log.Errorf("pidFile %s read error %v", pidFile, err)
continue
}
pidStr := string(pidBytes)
pidStr = strings.TrimSuffix(pidStr, "\n")
pidStr = strings.TrimSpace(pidStr)
pid, err := strconv.Atoi(pidStr)
if err != nil {
log.Errorf("pidFile %s with <%s> convert error %v",
pidFile, pidStr, err)
continue
}
pids[int32(pid)] = true
}
return pids, nil
}
// getProcessMetric gets the metrics for one process
func getProcessMetric(p *process.Process) (*types.ProcessMetric, error) {
n, err := p.Name()
if err != nil {
return nil, err
}
// Ensure we have a copy and not reference into slice
n = string([]byte(n))
_, err = p.Exe()
userProcess := (err == nil) // kernel or user-space?
c, err := p.CPUPercent()
if err != nil {
return nil, err
}
mp, err := p.MemoryPercent()
if err != nil {
return nil, err
}
ts, err := p.Times()
if err != nil {
return nil, err
}
// Requires permissions
nfd, err := p.NumFDs()
if err != nil {
return nil, err
}
nt, err := p.NumThreads()
if err != nil {
return nil, err
}
ct, err := p.CreateTime()
if err != nil {
return nil, err
}
ctSec := ct / 1000
ctNsec := (ct - ctSec*1000) * 1000000
createTime := time.Unix(ctSec, ctNsec)
m, err := p.MemoryInfo()
if err != nil {
return nil, err
}
return &types.ProcessMetric{
Pid: p.Pid,
Name: n,
UserProcess: userProcess,
CPUPercent: c,
MemoryPercent: mp,
NumFDs: nfd,
NumThreads: nt,
UserTime: ts.User,
SystemTime: ts.System,
CreateTime: createTime,
VMBytes: m.VMS,
RssBytes: m.RSS,
}, nil
}
// unpublishRemovedPids removes the old ones which are not in new
func unpublishRemovedPids(ctx *domainContext, oldPids, newPids map[int32]bool) {
for pid := range oldPids {
if _, ok := newPids[pid]; !ok {
unpublishProcessMetric(ctx, uint32(pid))
}
}
}
func publishProcessMetric(ctx *domainContext, status *types.ProcessMetric) {
key := status.Key()
log.Tracef("publishProcessMetric(%s)", key)
pub := ctx.pubProcessMetric
pub.Publish(key, *status)
}
func unpublishProcessMetric(ctx *domainContext, pid uint32) {
key := strconv.Itoa(int(pid))
log.Tracef("unpublishProcessMetric(%s)", key)
pub := ctx.pubProcessMetric
pub.Unpublish(key)
}