forked from DataDog/datadog-agent
-
Notifications
You must be signed in to change notification settings - Fork 2
/
process_model.go
300 lines (275 loc) · 7.71 KB
/
process_model.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
package procutil
import (
"github.com/DataDog/gopsutil/cpu"
"github.com/DataDog/gopsutil/process"
)
// Process holds all relevant metadata and metrics for a process
type Process struct {
Pid int32
Ppid int32
NsPid int32 // process namespaced PID
Name string
Cwd string
Exe string
Cmdline []string
Username string // (Windows only)
Uids []int32
Gids []int32
Stats *Stats
}
// DeepCopy creates a deep copy of Process
func (p *Process) DeepCopy() *Process {
copy := &Process{
Pid: p.Pid,
Ppid: p.Ppid,
NsPid: p.NsPid,
Name: p.Name,
Cwd: p.Cwd,
Exe: p.Exe,
Username: p.Username,
}
copy.Cmdline = make([]string, len(p.Cmdline))
for i := range p.Cmdline {
copy.Cmdline[i] = p.Cmdline[i]
}
copy.Uids = make([]int32, len(p.Uids))
for i := range p.Uids {
copy.Uids[i] = p.Uids[i]
}
copy.Gids = make([]int32, len(p.Gids))
for i := range p.Gids {
copy.Gids[i] = p.Gids[i]
}
if p.Stats != nil {
copy.Stats = p.Stats.DeepCopy()
}
return copy
}
// Stats holds all relevant stats metrics of a process
type Stats struct {
CreateTime int64
// Status returns the process status.
// Return value could be one of these.
// R: Running S: Sleep T: Stop I: Idle
// Z: Zombie W: Wait L: Lock
// The character is the same within all supported platforms.
Status string
Nice int32
OpenFdCount int32
NumThreads int32
CPUPercent *CPUPercentStat
CPUTime *CPUTimesStat
MemInfo *MemoryInfoStat
MemInfoEx *MemoryInfoExStat
IOStat *IOCountersStat
IORateStat *IOCountersRateStat
CtxSwitches *NumCtxSwitchesStat
}
// DeepCopy creates a deep copy of Stats
func (s *Stats) DeepCopy() *Stats {
copy := &Stats{
CreateTime: s.CreateTime,
Status: s.Status,
Nice: s.Nice,
OpenFdCount: s.OpenFdCount,
NumThreads: s.NumThreads,
}
if s.CPUTime != nil {
copy.CPUTime = &CPUTimesStat{}
*copy.CPUTime = *s.CPUTime
}
if s.CPUPercent != nil {
copy.CPUPercent = &CPUPercentStat{}
*copy.CPUPercent = *s.CPUPercent
}
if s.MemInfo != nil {
copy.MemInfo = &MemoryInfoStat{}
*copy.MemInfo = *s.MemInfo
}
if s.MemInfoEx != nil {
copy.MemInfoEx = &MemoryInfoExStat{}
*copy.MemInfoEx = *s.MemInfoEx
}
if s.IOStat != nil {
copy.IOStat = &IOCountersStat{}
*copy.IOStat = *s.IOStat
}
if s.IORateStat != nil {
copy.IORateStat = &IOCountersRateStat{}
*copy.IORateStat = *s.IORateStat
}
if s.CtxSwitches != nil {
copy.CtxSwitches = &NumCtxSwitchesStat{}
*copy.CtxSwitches = *s.CtxSwitches
}
return copy
}
// StatsWithPerm is a collection of stats that require elevated permission to collect in linux
type StatsWithPerm struct {
OpenFdCount int32
IOStat *IOCountersStat
}
// CPUTimesStat holds CPU stat metrics of a process
type CPUTimesStat struct {
User float64
System float64
Idle float64
Nice float64
Iowait float64
Irq float64
Softirq float64
Steal float64
Guest float64
GuestNice float64
Stolen float64
Timestamp int64
}
// Total returns the total number of seconds in a CPUTimesStat
func (c *CPUTimesStat) Total() float64 {
total := c.User + c.System + c.Nice + c.Iowait + c.Irq + c.Softirq + c.Steal + c.Guest + c.GuestNice + c.Idle + c.Stolen
return total
}
// CPUPercentStat holds CPU stat metrics of a process as CPU usage percent
type CPUPercentStat struct {
UserPct float64
SystemPct float64
}
// MemoryInfoStat holds commonly used memory metrics for a process
type MemoryInfoStat struct {
RSS uint64 // bytes
VMS uint64 // bytes
Swap uint64 // bytes
}
// MemoryInfoExStat holds all memory metrics for a process
type MemoryInfoExStat struct {
RSS uint64 // bytes
VMS uint64 // bytes
Shared uint64 // bytes
Text uint64 // bytes
Lib uint64 // bytes
Data uint64 // bytes
Dirty uint64 // bytes
}
// IOCountersStat holds IO metrics for a process
type IOCountersStat struct {
ReadCount int64
WriteCount int64
ReadBytes int64
WriteBytes int64
}
// IsZeroValue checks whether all fields are 0 in value for IOCountersStat
func (i *IOCountersStat) IsZeroValue() bool {
return i.ReadCount == 0 && i.WriteCount == 0 && i.ReadBytes == 0 && i.WriteBytes == 0
}
// IOCountersRateStat holds IO metrics for a process represented as rates (/sec)
type IOCountersRateStat struct {
ReadRate float64
WriteRate float64
ReadBytesRate float64
WriteBytesRate float64
}
// NumCtxSwitchesStat holds context switch metrics for a process
type NumCtxSwitchesStat struct {
Voluntary int64
Involuntary int64
}
// ConvertAllFilledProcesses takes a group of FilledProcess objects and convert them into Process
func ConvertAllFilledProcesses(processes map[int32]*process.FilledProcess) map[int32]*Process {
result := make(map[int32]*Process, len(processes))
for pid, p := range processes {
result[pid] = ConvertFromFilledProcess(p)
}
return result
}
// ConvertAllFilledProcessesToStats takes a group of FilledProcess objects and convert them into Stats
func ConvertAllFilledProcessesToStats(processes map[int32]*process.FilledProcess) map[int32]*Stats {
stats := make(map[int32]*Stats, len(processes))
for pid, p := range processes {
stats[pid] = ConvertFilledProcessesToStats(p)
}
return stats
}
// ConvertFilledProcessesToStats takes a group of FilledProcess objects and convert them into Stats
func ConvertFilledProcessesToStats(p *process.FilledProcess) *Stats {
return &Stats{
CreateTime: p.CreateTime,
Status: p.Status,
Nice: p.Nice,
OpenFdCount: p.OpenFdCount,
NumThreads: p.NumThreads,
CPUTime: ConvertFromCPUStat(p.CpuTime),
MemInfo: ConvertFromMemInfo(p.MemInfo),
MemInfoEx: ConvertFromMemInfoEx(p.MemInfoEx),
IOStat: ConvertFromIOStats(p.IOStat),
CtxSwitches: ConvertFromCtxSwitches(p.CtxSwitches),
}
}
// ConvertFromFilledProcess takes a FilledProcess object and convert it into Process
func ConvertFromFilledProcess(p *process.FilledProcess) *Process {
return &Process{
Pid: p.Pid,
Ppid: p.Ppid,
NsPid: p.NsPid,
Name: p.Name,
Cwd: p.Cwd,
Exe: p.Exe,
Cmdline: p.Cmdline,
Username: p.Username,
Uids: p.Uids,
Gids: p.Gids,
Stats: ConvertFilledProcessesToStats(p),
}
}
// ConvertFromCPUStat converts gopsutil TimesStat object to CPUTimesStat in procutil
func ConvertFromCPUStat(s cpu.TimesStat) *CPUTimesStat {
return &CPUTimesStat{
User: s.User,
System: s.System,
Idle: s.Idle,
Nice: s.Nice,
Iowait: s.Iowait,
Irq: s.Irq,
Softirq: s.Softirq,
Steal: s.Steal,
Guest: s.Guest,
GuestNice: s.GuestNice,
Stolen: s.Stolen,
Timestamp: s.Timestamp,
}
}
// ConvertFromMemInfo converts gopsutil MemoryInfoStat object to MemoryInfoStat in procutil
func ConvertFromMemInfo(s *process.MemoryInfoStat) *MemoryInfoStat {
return &MemoryInfoStat{
RSS: s.RSS,
VMS: s.VMS,
Swap: s.Swap,
}
}
// ConvertFromMemInfoEx converts gopsutil MemoryInfoExStat object to MemoryInfoExStat in procutil
func ConvertFromMemInfoEx(s *process.MemoryInfoExStat) *MemoryInfoExStat {
return &MemoryInfoExStat{
RSS: s.RSS,
VMS: s.VMS,
Shared: s.Shared,
Text: s.Text,
Lib: s.Lib,
Data: s.Data,
Dirty: s.Dirty,
}
}
// ConvertFromIOStats converts gopsutil IOCountersStat object to IOCounterStat in procutil
func ConvertFromIOStats(s *process.IOCountersStat) *IOCountersStat {
return &IOCountersStat{
ReadCount: int64(s.ReadCount),
WriteCount: int64(s.WriteCount),
ReadBytes: int64(s.ReadBytes),
WriteBytes: int64(s.WriteBytes),
}
}
// ConvertFromCtxSwitches converts gopsutil NumCtxSwitchesStat object to NumCtxSwitchesStat in procutil
func ConvertFromCtxSwitches(s *process.NumCtxSwitchesStat) *NumCtxSwitchesStat {
return &NumCtxSwitchesStat{
Voluntary: s.Voluntary,
Involuntary: s.Involuntary,
}
}