forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cgroup.go
202 lines (184 loc) · 4.81 KB
/
cgroup.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
package process
import (
"strconv"
"github.com/elastic/beats/libbeat/common"
"github.com/elastic/gosigar/cgroup"
)
// cgroupStatsToMap returns a MapStr containing the data from the stats object.
// If stats is nil then nil is returned.
func cgroupStatsToMap(stats *cgroup.Stats) common.MapStr {
if stats == nil {
return nil
}
cgroup := common.MapStr{}
// id and path are only available when all subsystems share a common path.
if stats.ID != "" {
cgroup["id"] = stats.ID
}
if stats.Path != "" {
cgroup["path"] = stats.Path
}
if cpu := cgroupCPUToMapStr(stats.CPU); cpu != nil {
cgroup["cpu"] = cpu
}
if cpuacct := cgroupCPUAccountingToMapStr(stats.CPUAccounting); cpuacct != nil {
cgroup["cpuacct"] = cpuacct
}
if memory := cgroupMemoryToMapStr(stats.Memory); memory != nil {
cgroup["memory"] = memory
}
if blkio := cgroupBlockIOToMapStr(stats.BlockIO); blkio != nil {
cgroup["blkio"] = blkio
}
return cgroup
}
// cgroupCPUToMapStr returns a MapStr containing CPUSubsystem data. If the
// cpu parameter is nil then nil is returned.
func cgroupCPUToMapStr(cpu *cgroup.CPUSubsystem) common.MapStr {
if cpu == nil {
return nil
}
return common.MapStr{
"id": cpu.ID,
"path": cpu.Path,
"cfs": common.MapStr{
"period": common.MapStr{
"us": cpu.CFS.PeriodMicros,
},
"quota": common.MapStr{
"us": cpu.CFS.QuotaMicros,
},
"shares": cpu.CFS.Shares,
},
"rt": common.MapStr{
"period": common.MapStr{
"us": cpu.RT.PeriodMicros,
},
"runtime": common.MapStr{
"us": cpu.RT.RuntimeMicros,
},
},
"stats": common.MapStr{
"periods": cpu.Stats.Periods,
"throttled": common.MapStr{
"periods": cpu.Stats.ThrottledPeriods,
"ns": cpu.Stats.ThrottledTimeNanos,
},
},
}
}
// cgroupCPUAccountingToMapStr returns a MapStr containing
// CPUAccountingSubsystem data. If the cpuacct parameter is nil then nil is
// returned.
func cgroupCPUAccountingToMapStr(cpuacct *cgroup.CPUAccountingSubsystem) common.MapStr {
if cpuacct == nil {
return nil
}
perCPUUsage := common.MapStr{}
for i, usage := range cpuacct.UsagePerCPU {
perCPUUsage[strconv.Itoa(i+1)] = usage
}
return common.MapStr{
"id": cpuacct.ID,
"path": cpuacct.Path,
"total": common.MapStr{
"ns": cpuacct.TotalNanos,
},
"percpu": perCPUUsage,
"stats": common.MapStr{
"system": common.MapStr{
"ns": cpuacct.Stats.SystemNanos,
},
"user": common.MapStr{
"ns": cpuacct.Stats.UserNanos,
},
},
}
}
// cgroupMemoryToMapStr returns a MapStr containing MemorySubsystem data. If the
// memory parameter is nil then nil is returned.
func cgroupMemoryToMapStr(memory *cgroup.MemorySubsystem) common.MapStr {
if memory == nil {
return nil
}
addMemData := func(key string, m common.MapStr, data cgroup.MemoryData) {
m[key] = common.MapStr{
"failures": memory.Mem.FailCount,
"limit": common.MapStr{
"bytes": memory.Mem.Limit,
},
"usage": common.MapStr{
"bytes": memory.Mem.Usage,
"max": common.MapStr{
"bytes": memory.Mem.MaxUsage,
},
},
}
}
memMap := common.MapStr{
"id": memory.ID,
"path": memory.Path,
}
addMemData("mem", memMap, memory.Mem)
addMemData("memsw", memMap, memory.MemSwap)
addMemData("kmem", memMap, memory.Kernel)
addMemData("kmem_tcp", memMap, memory.KernelTCP)
memMap["stats"] = common.MapStr{
"active_anon": common.MapStr{
"bytes": memory.Stats.ActiveAnon,
},
"active_file": common.MapStr{
"bytes": memory.Stats.ActiveFile,
},
"cache": common.MapStr{
"bytes": memory.Stats.Cache,
},
"hierarchical_memory_limit": common.MapStr{
"bytes": memory.Stats.HierarchicalMemoryLimit,
},
"hierarchical_memsw_limit": common.MapStr{
"bytes": memory.Stats.HierarchicalMemswLimit,
},
"inactive_anon": common.MapStr{
"bytes": memory.Stats.InactiveAnon,
},
"inactive_file": common.MapStr{
"bytes": memory.Stats.InactiveFile,
},
"mapped_file": common.MapStr{
"bytes": memory.Stats.MappedFile,
},
"page_faults": memory.Stats.PageFaults,
"major_page_faults": memory.Stats.MajorPageFaults,
"pages_in": memory.Stats.PagesIn,
"pages_out": memory.Stats.PagesOut,
"rss": common.MapStr{
"bytes": memory.Stats.RSS,
},
"rss_huge": common.MapStr{
"bytes": memory.Stats.RSSHuge,
},
"swap": common.MapStr{
"bytes": memory.Stats.Swap,
},
"unevictable": common.MapStr{
"bytes": memory.Stats.Unevictable,
},
}
return memMap
}
// cgroupBlockIOToMapStr returns a MapStr containing BlockIOSubsystem data.
// If the blockIO parameter is nil then nil is returned.
func cgroupBlockIOToMapStr(blockIO *cgroup.BlockIOSubsystem) common.MapStr {
if blockIO == nil {
return nil
}
return common.MapStr{
"id": blockIO.ID,
"path": blockIO.Path,
"total": common.MapStr{
"bytes": blockIO.Throttle.TotalBytes,
"ios": blockIO.Throttle.TotalIOs,
},
}
}