/
cgroup_metrics.go
490 lines (458 loc) · 13.3 KB
/
cgroup_metrics.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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016-2020 Datadog, Inc.
// +build linux
package cgroup
import (
"bufio"
"fmt"
"math"
"os"
"runtime"
"strconv"
"strings"
"time"
"github.com/DataDog/datadog-agent/pkg/util/containers/metrics"
"github.com/DataDog/datadog-agent/pkg/util/log"
)
// NanoToUserHZDivisor holds the divisor to convert cpu.usage to the
// same unit as cpu.system (USER_HZ = 1/100)
// TODO: get USER_HZ from gopsutil? Needs to patch it
const NanoToUserHZDivisor float64 = 1e9 / 100
var (
numCPU float64 = float64(runtime.NumCPU())
)
// Mem returns the memory statistics for a Cgroup. If the cgroup file is not
// available then we return an empty stats file.
func (c ContainerCgroup) Mem() (*metrics.ContainerMemStats, error) {
ret := &metrics.ContainerMemStats{}
statfile := c.cgroupFilePath("memory", "memory.stat")
f, err := os.Open(statfile)
if os.IsNotExist(err) {
log.Debugf("Missing cgroup file: %s", statfile)
return ret, nil
} else if err != nil {
return nil, err
}
defer f.Close()
scanner := bufio.NewScanner(f)
for scanner.Scan() {
fields := strings.Split(scanner.Text(), " ")
v, err := strconv.ParseUint(fields[1], 10, 64)
if err != nil {
continue
}
switch fields[0] {
case "cache":
ret.Cache = v
case "swap":
ret.Swap = v
ret.SwapPresent = true
case "rss":
ret.RSS = v
case "rss_huge":
ret.RSSHuge = v
case "mapped_file":
ret.MappedFile = v
case "pgpgin":
ret.Pgpgin = v
case "pgpgout":
ret.Pgpgout = v
case "pgfault":
ret.Pgfault = v
case "pgmajfault":
ret.Pgmajfault = v
case "inactive_anon":
ret.InactiveAnon = v
case "active_anon":
ret.ActiveAnon = v
case "inactive_file":
ret.InactiveFile = v
case "active_file":
ret.ActiveFile = v
case "unevictable":
ret.Unevictable = v
case "hierarchical_memory_limit":
ret.HierarchicalMemoryLimit = v
case "hierarchical_memsw_limit":
ret.HierarchicalMemSWLimit = v
case "total_cache":
ret.TotalCache = v
case "total_rss":
ret.TotalRSS = v
case "total_rssHuge":
ret.TotalRSSHuge = v
case "total_mapped_file":
ret.TotalMappedFile = v
case "total_pgpgin":
ret.TotalPgpgIn = v
case "total_pgpgout":
ret.TotalPgpgOut = v
case "total_pgfault":
ret.TotalPgFault = v
case "total_pgmajfault":
ret.TotalPgMajFault = v
case "total_inactive_anon":
ret.TotalInactiveAnon = v
case "total_active_anon":
ret.TotalActiveAnon = v
case "total_inactive_file":
ret.TotalInactiveFile = v
case "total_active_file":
ret.TotalActiveFile = v
case "total_unevictable":
ret.TotalUnevictable = v
}
}
if err := scanner.Err(); err != nil {
return ret, fmt.Errorf("error reading %s: %s", statfile, err)
}
return ret, nil
}
// MemLimit returns the memory limit of the cgroup, if it exists. If the file does not
// exist or there is no limit then this will default to 0.
func (c ContainerCgroup) MemLimit() (uint64, error) {
v, err := c.ParseSingleStat("memory", "memory.limit_in_bytes")
if os.IsNotExist(err) {
log.Debugf("Missing cgroup file: %s",
c.cgroupFilePath("memory", "memory.limit_in_bytes"))
return 0, nil
} else if err != nil {
return 0, err
}
// limit_in_bytes is a special case here, it's possible that it shows a ridiculous number,
// in which case it represents unlimited, so return 0 here
if v > uint64(math.Pow(2, 60)) {
v = 0
}
return v, nil
}
// FailedMemoryCount returns the number of times this cgroup reached its memory limit, if it exists.
// If the file does not exist or there is no limit, then this will default to 0
func (c ContainerCgroup) FailedMemoryCount() (uint64, error) {
v, err := c.ParseSingleStat("memory", "memory.failcnt")
if os.IsNotExist(err) {
log.Debugf("Missing cgroup file: %s",
c.cgroupFilePath("memory", "memory.failcnt"))
return 0, nil
} else if err != nil {
return 0, err
}
return v, nil
}
// KernelMemoryUsage returns the number of bytes of kernel memory used by this cgroup, if it exists.
// If the file does not exist or there is an error, then this will default to 0
func (c ContainerCgroup) KernelMemoryUsage() (uint64, error) {
v, err := c.ParseSingleStat("memory", "memory.kmem.usage_in_bytes")
if os.IsNotExist(err) {
log.Debugf("Missing cgroup file: %s",
c.cgroupFilePath("memory", "memory.kmem.usage_in_bytes"))
return 0, nil
} else if err != nil {
return 0, err
}
return v, nil
}
// SoftMemLimit returns the soft memory limit of the cgroup, if it exists. If the file does not
// exist or there is no limit then this will default to 0.
func (c ContainerCgroup) SoftMemLimit() (uint64, error) {
v, err := c.ParseSingleStat("memory", "memory.soft_limit_in_bytes")
if os.IsNotExist(err) {
log.Debugf("Missing cgroup file: %s",
c.cgroupFilePath("memory", "memory.soft_limit_in_bytes"))
return 0, nil
} else if err != nil {
return 0, err
}
// limit_in_bytes is a special case here, it's possible that it shows a ridiculous number,
// in which case it represents unlimited, so return 0 here
if v > uint64(math.Pow(2, 60)) {
v = 0
}
return v, nil
}
// CPU returns the CPU status for this cgroup instance
// If the cgroup file does not exist then we just log debug return nothing.
func (c ContainerCgroup) CPU() (*metrics.ContainerCPUStats, error) {
ret := &metrics.ContainerCPUStats{}
statfile := c.cgroupFilePath("cpuacct", "cpuacct.stat")
f, err := os.Open(statfile)
if os.IsNotExist(err) {
log.Debugf("Missing cgroup file: %s", statfile)
return ret, nil
} else if err != nil {
return nil, err
}
defer f.Close()
scanner := bufio.NewScanner(f)
for scanner.Scan() {
fields := strings.Split(scanner.Text(), " ")
if fields[0] == "user" {
user, err := strconv.ParseUint(fields[1], 10, 64)
if err == nil {
ret.User = user
}
}
if fields[0] == "system" {
system, err := strconv.ParseUint(fields[1], 10, 64)
if err == nil {
ret.System = system
}
}
}
if err := scanner.Err(); err != nil {
return ret, fmt.Errorf("error reading %s: %s", statfile, err)
}
usage, err := c.ParseSingleStat("cpuacct", "cpuacct.usage")
ret.Timestsamp = time.Now()
if err == nil {
ret.UsageTotal = float64(usage) / NanoToUserHZDivisor
} else {
log.Debugf("Missing total cpu usage stat for %s: %s", c.ContainerID, err.Error())
}
shares, err := c.ParseSingleStat("cpu", "cpu.shares")
if err == nil {
ret.Shares = shares
} else {
log.Debugf("Missing cpu shares stat for %s: %s", c.ContainerID, err.Error())
}
return ret, nil
}
// CPUPeriods returns the number of times the cgroup has been
// throttle/limited because of CPU quota / limit
// If the cgroup file does not exist then we just log debug and return 0.
func (c ContainerCgroup) CPUPeriods() (throttledNr uint64, throttledTime float64, err error) {
statfile := c.cgroupFilePath("cpu", "cpu.stat")
f, err := os.Open(statfile)
if os.IsNotExist(err) {
log.Debugf("Missing cgroup file: %s", statfile)
return 0, 0, nil
} else if err != nil {
return 0, 0, err
}
defer f.Close()
scanner := bufio.NewScanner(f)
for scanner.Scan() {
fields := strings.Split(scanner.Text(), " ")
if fields[0] == "nr_throttled" {
throttledNr, err = strconv.ParseUint(fields[1], 10, 64)
if err != nil {
return 0, 0, err
}
}
if fields[0] == "throttled_time" {
throttledTime, err = strconv.ParseFloat(fields[1], 64)
if err != nil {
return 0, 0, err
}
}
}
return throttledNr, throttledTime / NanoToUserHZDivisor, nil
}
// CPULimit would show CPU limit for this cgroup.
// It does so by checking the cpu period and cpu quota config
// if a user does this:
//
// docker run --cpus='0.5' ubuntu:latest
//
// we should return 50% for that container.
//
// However cfs_period_us is per CPU, which means that
//
// docker run --cpu='2' ubuntu:latest
//
// Will yield 200% (cfs_period_us = 100000, cfs_quota_us = 200000)
//
// If the limits files aren't available (on older version) then
// we'll return the default value of numCPU * 100.
func (c ContainerCgroup) CPULimit() (float64, error) {
limit := numCPU * 100.0
periodFile := c.cgroupFilePath("cpu", "cpu.cfs_period_us")
quotaFile := c.cgroupFilePath("cpu", "cpu.cfs_quota_us")
plines, err := readLines(periodFile)
if os.IsNotExist(err) {
log.Debugf("Missing cgroup file: %s", periodFile)
return limit, nil
} else if err != nil {
return 0, err
}
qlines, err := readLines(quotaFile)
if os.IsNotExist(err) {
log.Debugf("Missing cgroup file: %s", quotaFile)
return limit, nil
} else if err != nil {
return 0, err
}
period, err := strconv.ParseFloat(plines[0], 64)
if err != nil {
return 0, err
}
quota, err := strconv.ParseFloat(qlines[0], 64)
if err != nil {
return 0, err
}
// If we don't have limit check on current cgroup, check parent
// -1 means no limit
// We ignore failures as we already have current cgroup values
if quota == -1 {
periodFile = c.cgroupParentFilePath("cpu", "cpu.cfs_period_us")
quotaFile = c.cgroupParentFilePath("cpu", "cpu.cfs_quota_us")
plines, err = readLines(periodFile)
if err == nil {
parentPeriod, err := strconv.ParseFloat(plines[0], 64)
if err == nil {
period = parentPeriod
}
}
qlines, err := readLines(quotaFile)
if err == nil {
parentQuota, err := strconv.ParseFloat(qlines[0], 64)
if err == nil {
quota = parentQuota
}
}
}
// default cpu limit is 100%
if (period > 0) && (quota > 0) {
limit = quota / period * 100.0
}
return limit, nil
}
// IO returns the disk read and write bytes stats for this cgroup.
// tested in DiskMappingTestSuite.TestContainerCgroupIO
// Format:
//
// 8:0 Read 49225728
// 8:0 Write 9850880
// 8:0 Sync 0
// 8:0 Async 59076608
// 8:0 Total 59076608
// 252:0 Read 49094656
// 252:0 Write 9850880
// 252:0 Sync 0
// 252:0 Async 58945536
// 252:0 Total 58945536
//
func (c ContainerCgroup) IO() (*metrics.ContainerIOStats, error) {
ret := &metrics.ContainerIOStats{
DeviceReadBytes: make(map[string]uint64),
DeviceWriteBytes: make(map[string]uint64),
}
statfile := c.cgroupFilePath("blkio", "blkio.throttle.io_service_bytes")
f, err := os.Open(statfile)
if os.IsNotExist(err) {
log.Debugf("Missing cgroup file: %s", statfile)
return ret, nil
} else if err != nil {
return nil, err
}
defer f.Close()
// Get device id->name mapping
var devices map[string]string
mapping, err := getDiskDeviceMapping()
if err != nil {
log.Debugf("Cannot get per-device stats: %s", err)
// devices will stay nil, lookups are safe in nil maps
} else {
devices = mapping.idToName
}
scanner := bufio.NewScanner(f)
for scanner.Scan() {
fields := strings.Split(scanner.Text(), " ")
if len(fields) < 3 {
continue
}
deviceName := devices[fields[0]]
if fields[1] == "Read" {
read, err := strconv.ParseUint(fields[2], 10, 64)
if err == nil {
ret.ReadBytes += read
if deviceName != "" {
ret.DeviceReadBytes[deviceName] = read
}
}
} else if fields[1] == "Write" {
write, err := strconv.ParseUint(fields[2], 10, 64)
if err == nil {
ret.WriteBytes += write
if deviceName != "" {
ret.DeviceWriteBytes[deviceName] = write
}
}
}
}
if err := scanner.Err(); err != nil {
return ret, fmt.Errorf("error reading %s: %s", statfile, err)
}
var fileDescCount uint64
for _, pid := range c.Pids {
fdCount, err := GetFileDescriptorLen(int(pid))
if err != nil {
log.Debugf("Failed to get file desc length for pid %d, container %s: %s", pid, c.ContainerID[:12], err)
continue
}
fileDescCount += uint64(fdCount)
}
ret.OpenFiles = fileDescCount
return ret, nil
}
// ThreadCount returns the number of threads in the pid cgroup
// linked to the container.
// ref: https://www.kernel.org/doc/Documentation/cgroup-v1/pids.txt
//
// Although the metric is called `pid.current`, it also tracks
// threads, and not only task-group-pids
func (c ContainerCgroup) ThreadCount() (uint64, error) {
v, err := c.ParseSingleStat("pids", "pids.current")
if os.IsNotExist(err) {
log.Debugf("Missing cgroup file: %s",
c.cgroupFilePath("pids", "pids.current"))
return 0, nil
} else if err != nil {
return 0, err
}
return v, nil
}
// ThreadLimit returns the thread count limit in the pid cgroup
// linked to the container.
// ref: https://www.kernel.org/doc/Documentation/cgroup-v1/pids.txt
//
// If `max` is found, the method returns 0 as-in "no limit"
func (c ContainerCgroup) ThreadLimit() (uint64, error) {
statFile := c.cgroupFilePath("pids", "pids.max")
lines, err := readLines(statFile)
if os.IsNotExist(err) {
log.Debugf("Missing cgroup file: %s", statFile)
return 0, nil
} else if err != nil {
return 0, err
}
if len(lines) != 1 {
return 0, fmt.Errorf("wrong file format: %s", statFile)
}
if lines[0] == "max" {
return 0, nil
}
value, err := strconv.ParseUint(lines[0], 10, 64)
if err != nil {
return 0, err
}
return value, nil
}
// ParseSingleStat reads and converts a single-value cgroup stat file content to uint64.
func (c ContainerCgroup) ParseSingleStat(target, file string) (uint64, error) {
statFile := c.cgroupFilePath(target, file)
lines, err := readLines(statFile)
if err != nil {
return 0, err
}
if len(lines) != 1 {
return 0, fmt.Errorf("wrong file format: %s", statFile)
}
value, err := strconv.ParseUint(lines[0], 10, 64)
if err != nil {
return 0, err
}
return value, nil
}