forked from mongodb/grip
/
process_info.go
248 lines (209 loc) · 6.63 KB
/
process_info.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
package message
import (
"fmt"
"os"
"sync"
"github.com/mongodb/grip/level"
"github.com/shirou/gopsutil/cpu"
"github.com/shirou/gopsutil/net"
"github.com/shirou/gopsutil/process"
)
// ProcessInfo holds the data for per-process statistics (e.g. cpu,
// memory, io). The Process info composers produce messages in this
// form.
type ProcessInfo struct {
Message string `json:"message,omitempty" bson:"message,omitempty"`
Pid int32 `json:"pid" bson:"pid"`
Parent int32 `json:"parentPid,omitempty" bson:"parentPid,omitempty"`
Threads int `json:"numThreads,omitempty" bson:"numThreads,omitempty"`
Command string `json:"command,omitempty" bson:"command,omitempty"`
CPU cpu.TimesStat `json:"cpu,omitempty" bson:"cpu,omitempty"`
IoStat process.IOCountersStat `json:"io,omitempty" bson:"io,omitempty"`
NetStat []net.IOCountersStat `json:"net,omitempty" bson:"net,omitempty"`
Memory process.MemoryInfoStat `json:"mem,omitempty" bson:"mem,omitempty"`
MemoryPlatform process.MemoryInfoExStat `json:"memExtra,omitempty" bson:"memExtra,omitempty"`
Errors []string `json:"errors,omitempty" bson:"errors,omitempty"`
Base `json:"metadata,omitempty" bson:"metadata,omitempty"`
loggable bool
rendered string
}
///////////////////////////////////////////////////////////////////////////
//
// Constructors
//
///////////////////////////////////////////////////////////////////////////
// CollectProcessInfo returns a populated ProcessInfo message.Composer
// instance for the specified pid.
func CollectProcessInfo(pid int32) Composer {
return NewProcessInfo(level.Trace, pid, "")
}
// CollectProcessInfoSelf returns a populated ProcessInfo message.Composer
// for the pid of the current process.
func CollectProcessInfoSelf() Composer {
return NewProcessInfo(level.Trace, int32(os.Getpid()), "")
}
// CollectProcessInfoSelfWithChildren returns a slice of populated
// ProcessInfo message.Composer instances for the current process and
// all children processes.
func CollectProcessInfoSelfWithChildren() []Composer {
return CollectProcessInfoWithChildren(int32(os.Getpid()))
}
// CollectProcessInfoWithChildren returns a slice of populated
// ProcessInfo message.Composer instances for the process with the
// specified pid and all children processes for that process.
func CollectProcessInfoWithChildren(pid int32) []Composer {
var results []Composer
parent, err := process.NewProcess(pid)
if err != nil {
return results
}
parentMsg := &ProcessInfo{}
parentMsg.loggable = true
parentMsg.populate(parent)
results = append(results, parentMsg)
for _, child := range getChildrenRecursively(parent) {
cm := &ProcessInfo{}
cm.loggable = true
cm.populate(child)
results = append(results, cm)
}
return results
}
// CollectAllProcesses returns a slice of populated ProcessInfo
// message.Composer interfaces for all processes currently running on
// a system.
func CollectAllProcesses() []Composer {
numThreads := 32
procs, err := process.Processes()
if err != nil {
return []Composer{}
}
if len(procs) < numThreads {
numThreads = len(procs)
}
results := []Composer{}
procChan := make(chan *process.Process, len(procs))
for _, p := range procs {
procChan <- p
}
close(procChan)
wg := sync.WaitGroup{}
wg.Add(numThreads)
infoChan := make(chan *ProcessInfo, len(procs))
for i := 0; i < numThreads; i++ {
go func() {
defer wg.Done()
for p := range procChan {
cm := &ProcessInfo{}
cm.loggable = true
cm.populate(p)
infoChan <- cm
}
}()
}
wg.Wait()
close(infoChan)
for p := range infoChan {
results = append(results, p)
}
return results
}
func getChildrenRecursively(proc *process.Process) []*process.Process {
var out []*process.Process
children, err := proc.Children()
if len(children) == 0 || err != nil {
return out
}
for _, p := range children {
out = append(out, p)
out = append(out, getChildrenRecursively(p)...)
}
return out
}
// NewProcessInfo constructs a fully configured and populated
// Processinfo message.Composer instance for the specified process.
func NewProcessInfo(priority level.Priority, pid int32, message string) Composer {
p := &ProcessInfo{
Message: message,
Pid: pid,
}
if err := p.SetPriority(priority); err != nil {
p.saveError("priority", err)
return p
}
proc, err := process.NewProcess(pid)
p.saveError("process", err)
if err != nil {
return p
}
p.loggable = true
p.populate(proc)
return p
}
///////////////////////////////////////////////////////////////////////////
//
// message.Composer implementation
//
///////////////////////////////////////////////////////////////////////////
// Loggable returns true when the Processinfo structure has been
// populated.
func (p *ProcessInfo) Loggable() bool { return p.loggable }
// Raw always returns the ProcessInfo object, however it will call the
// Collect method of the base operation first.
func (p *ProcessInfo) Raw() interface{} { _ = p.Collect(); return p }
// String returns a string representation of the message, lazily
// rendering the message, and caching it privately.
func (p *ProcessInfo) String() string {
if p.rendered == "" {
p.rendered = renderStatsString(p.Message, p)
}
return p.rendered
}
///////////////////////////////////////////////////////////////////////////
//
// Internal Methods for collecting data
//
///////////////////////////////////////////////////////////////////////////
func (p *ProcessInfo) populate(proc *process.Process) {
var err error
if p.Pid == 0 {
p.Pid = proc.Pid
}
parentPid, err := proc.Ppid()
p.saveError("parent_pid", err)
if err == nil {
p.Parent = parentPid
}
memInfo, err := proc.MemoryInfo()
p.saveError("meminfo", err)
if err == nil && memInfo != nil {
p.Memory = *memInfo
}
memInfoEx, err := proc.MemoryInfoEx()
p.saveError("meminfo_extended", err)
if err == nil && memInfoEx != nil {
p.MemoryPlatform = *memInfoEx
}
threads, err := proc.NumThreads()
p.Threads = int(threads)
p.saveError("num_threads", err)
p.NetStat, err = proc.NetIOCounters(false)
p.saveError("netstat", err)
p.Command, err = proc.Cmdline()
p.saveError("cmd args", err)
cpuTimes, err := proc.Times()
p.saveError("cpu_times", err)
if err == nil && cpuTimes != nil {
p.CPU = *cpuTimes
}
ioStat, err := proc.IOCounters()
p.saveError("iostat", err)
if err == nil && ioStat != nil {
p.IoStat = *ioStat
}
}
func (p *ProcessInfo) saveError(stat string, err error) {
if shouldSaveError(err) {
p.Errors = append(p.Errors, fmt.Sprintf("%s: %v", stat, err))
}
}