Skip to content

Commit

Permalink
interpret "max" as math.MaxInt64
Browse files Browse the repository at this point in the history
  • Loading branch information
psnszsn committed Feb 13, 2025
1 parent 4822ec2 commit 86d5189
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
10 changes: 10 additions & 0 deletions plugins/inputs/cgroup/cgroup_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package cgroup

import (
"fmt"
"math"
"os"
"path"
"path/filepath"
Expand Down Expand Up @@ -275,6 +276,15 @@ func numberOrString(s string) interface{} {
if err == nil {
return i
}
if s == "max" {
return math.MaxInt64
}

// Care should be taken to always interpret each field as the same type on every cycle.
// *.pressure files follow the PSI format and contain numbers with fractional parts
// that always have a decimal separator, even when the fractional part is 0 (e.g., "0.00"),
// thus they will always be interpreted as floats.
// https://www.kernel.org/doc/Documentation/accounting/psi.txt
f, err := strconv.ParseFloat(s, 64)
if err == nil {
return f
Expand Down
23 changes: 12 additions & 11 deletions plugins/inputs/cgroup/cgroupv2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package cgroup

import (
"math"
"testing"
"time"

Expand Down Expand Up @@ -88,7 +89,7 @@ func TestCgroupV2Memory(t *testing.T) {
"memory.events.oom": int64(0),
"memory.events.oom_group_kill": int64(0),
"memory.events.oom_kill": int64(0),
"memory.high": "max",
"memory.high": int64(math.MaxInt64),
"memory.low": int64(0),
"memory.max": int64(103079215104),
"memory.min": int64(0),
Expand Down Expand Up @@ -207,7 +208,7 @@ func TestCgroupV2Memory(t *testing.T) {
"memory.swap.events.fail": int64(0),
"memory.swap.events.high": int64(0),
"memory.swap.events.max": int64(0),
"memory.swap.high": "max",
"memory.swap.high": int64(math.MaxInt64),
"memory.swap.max": int64(0),
},
time.Unix(0, 0),
Expand Down Expand Up @@ -268,27 +269,27 @@ func TestCgroupV2Hugetlb(t *testing.T) {
map[string]string{"path": `testdata/v2`},
map[string]interface{}{
"hugetlb.1GB.current": int64(0),
"hugetlb.1GB.events.0": "max",
"hugetlb.1GB.events.0": int64(math.MaxInt64),
"hugetlb.1GB.events.1": int64(0),
"hugetlb.1GB.events.local.0": "max",
"hugetlb.1GB.events.local.0": int64(math.MaxInt64),
"hugetlb.1GB.events.local.1": int64(0),
"hugetlb.1GB.max": "max",
"hugetlb.1GB.max": int64(math.MaxInt64),
"hugetlb.1GB.numa_stat.N0": int64(0),
"hugetlb.1GB.numa_stat.N1": int64(0),
"hugetlb.1GB.numa_stat.total": int64(0),
"hugetlb.1GB.rsvd.current": int64(0),
"hugetlb.1GB.rsvd.max": "max",
"hugetlb.1GB.rsvd.max": int64(math.MaxInt64),
"hugetlb.2MB.current": int64(0),
"hugetlb.2MB.events.0": "max",
"hugetlb.2MB.events.0": int64(math.MaxInt64),
"hugetlb.2MB.events.1": int64(0),
"hugetlb.2MB.events.local.0": "max",
"hugetlb.2MB.events.local.0": int64(math.MaxInt64),
"hugetlb.2MB.events.local.1": int64(0),
"hugetlb.2MB.max": "max",
"hugetlb.2MB.max": int64(math.MaxInt64),
"hugetlb.2MB.numa_stat.N0": int64(0),
"hugetlb.2MB.numa_stat.N1": int64(0),
"hugetlb.2MB.numa_stat.total": int64(0),
"hugetlb.2MB.rsvd.current": int64(0),
"hugetlb.2MB.rsvd.max": "max",
"hugetlb.2MB.rsvd.max": int64(math.MaxInt64),
},
time.Unix(0, 0),
),
Expand All @@ -312,7 +313,7 @@ func TestCgroupV2Pids(t *testing.T) {
map[string]string{"path": `testdata/v2`},
map[string]interface{}{
"pids.current": int64(592),
"pids.events.0": "max",
"pids.events.0": int64(math.MaxInt64),
"pids.events.1": int64(0),
"pids.max": int64(629145),
"pids.peak": int64(2438),
Expand Down

0 comments on commit 86d5189

Please sign in to comment.