Skip to content

Commit e676279

Browse files
committed
interpret "max" as math.MaxInt64
1 parent 4822ec2 commit e676279

File tree

2 files changed

+22
-11
lines changed

2 files changed

+22
-11
lines changed

plugins/inputs/cgroup/cgroup_linux.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package cgroup
44

55
import (
66
"fmt"
7+
"math"
78
"os"
89
"path"
910
"path/filepath"
@@ -275,6 +276,15 @@ func numberOrString(s string) interface{} {
275276
if err == nil {
276277
return i
277278
}
279+
if s == "max" {
280+
return int64(math.MaxInt64)
281+
}
282+
283+
// Care should be taken to always interpret each field as the same type on every cycle.
284+
// *.pressure files follow the PSI format and contain numbers with fractional parts
285+
// that always have a decimal separator, even when the fractional part is 0 (e.g., "0.00"),
286+
// thus they will always be interpreted as floats.
287+
// https://www.kernel.org/doc/Documentation/accounting/psi.txt
278288
f, err := strconv.ParseFloat(s, 64)
279289
if err == nil {
280290
return f

plugins/inputs/cgroup/cgroupv2_test.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
package cgroup
44

55
import (
6+
"math"
67
"testing"
78
"time"
89

@@ -88,7 +89,7 @@ func TestCgroupV2Memory(t *testing.T) {
8889
"memory.events.oom": int64(0),
8990
"memory.events.oom_group_kill": int64(0),
9091
"memory.events.oom_kill": int64(0),
91-
"memory.high": "max",
92+
"memory.high": int64(math.MaxInt64),
9293
"memory.low": int64(0),
9394
"memory.max": int64(103079215104),
9495
"memory.min": int64(0),
@@ -207,7 +208,7 @@ func TestCgroupV2Memory(t *testing.T) {
207208
"memory.swap.events.fail": int64(0),
208209
"memory.swap.events.high": int64(0),
209210
"memory.swap.events.max": int64(0),
210-
"memory.swap.high": "max",
211+
"memory.swap.high": int64(math.MaxInt64),
211212
"memory.swap.max": int64(0),
212213
},
213214
time.Unix(0, 0),
@@ -268,27 +269,27 @@ func TestCgroupV2Hugetlb(t *testing.T) {
268269
map[string]string{"path": `testdata/v2`},
269270
map[string]interface{}{
270271
"hugetlb.1GB.current": int64(0),
271-
"hugetlb.1GB.events.0": "max",
272+
"hugetlb.1GB.events.0": int64(math.MaxInt64),
272273
"hugetlb.1GB.events.1": int64(0),
273-
"hugetlb.1GB.events.local.0": "max",
274+
"hugetlb.1GB.events.local.0": int64(math.MaxInt64),
274275
"hugetlb.1GB.events.local.1": int64(0),
275-
"hugetlb.1GB.max": "max",
276+
"hugetlb.1GB.max": int64(math.MaxInt64),
276277
"hugetlb.1GB.numa_stat.N0": int64(0),
277278
"hugetlb.1GB.numa_stat.N1": int64(0),
278279
"hugetlb.1GB.numa_stat.total": int64(0),
279280
"hugetlb.1GB.rsvd.current": int64(0),
280-
"hugetlb.1GB.rsvd.max": "max",
281+
"hugetlb.1GB.rsvd.max": int64(math.MaxInt64),
281282
"hugetlb.2MB.current": int64(0),
282-
"hugetlb.2MB.events.0": "max",
283+
"hugetlb.2MB.events.0": int64(math.MaxInt64),
283284
"hugetlb.2MB.events.1": int64(0),
284-
"hugetlb.2MB.events.local.0": "max",
285+
"hugetlb.2MB.events.local.0": int64(math.MaxInt64),
285286
"hugetlb.2MB.events.local.1": int64(0),
286-
"hugetlb.2MB.max": "max",
287+
"hugetlb.2MB.max": int64(math.MaxInt64),
287288
"hugetlb.2MB.numa_stat.N0": int64(0),
288289
"hugetlb.2MB.numa_stat.N1": int64(0),
289290
"hugetlb.2MB.numa_stat.total": int64(0),
290291
"hugetlb.2MB.rsvd.current": int64(0),
291-
"hugetlb.2MB.rsvd.max": "max",
292+
"hugetlb.2MB.rsvd.max": int64(math.MaxInt64),
292293
},
293294
time.Unix(0, 0),
294295
),
@@ -312,7 +313,7 @@ func TestCgroupV2Pids(t *testing.T) {
312313
map[string]string{"path": `testdata/v2`},
313314
map[string]interface{}{
314315
"pids.current": int64(592),
315-
"pids.events.0": "max",
316+
"pids.events.0": int64(math.MaxInt64),
316317
"pids.events.1": int64(0),
317318
"pids.max": int64(629145),
318319
"pids.peak": int64(2438),

0 commit comments

Comments
 (0)