Skip to content

Commit

Permalink
Use int64 for cgroup v1 parsing (open-telemetry#6443)
Browse files Browse the repository at this point in the history
  • Loading branch information
perher committed Oct 29, 2022
1 parent 3e91f3d commit ceb7109
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 6 deletions.
4 changes: 2 additions & 2 deletions internal/cgroups/cgroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,10 @@ func (cg *CGroup) readFirstLine(param string) (string, error) {
}

// readInt parses the first line from a cgroup param file as int.
func (cg *CGroup) readInt(param string) (int, error) {
func (cg *CGroup) readInt(param string) (int64, error) {
text, err := cg.readFirstLine(param)
if err != nil {
return 0, err
}
return strconv.Atoi(text)
return strconv.ParseInt(text, 10, 64)
}
2 changes: 1 addition & 1 deletion internal/cgroups/cgroup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func TestCGroupReadInt(t *testing.T) {
testTable := []struct {
name string
paramName string
expectedValue int
expectedValue int64
shouldHaveError bool
}{
{
Expand Down
2 changes: 1 addition & 1 deletion internal/cgroups/cgroups.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ func (cg CGroups) MemoryQuota() (int64, bool, error) {
if defined := memLimitBytes > 0; err != nil || !defined {
return -1, defined, err
}
return int64(memLimitBytes), true, nil
return memLimitBytes, true, nil
}

// IsCGroupV2 returns true if the system supports and uses cgroup2.
Expand Down
10 changes: 8 additions & 2 deletions internal/cgroups/cgroups_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func TestNewCGroupsWithErrors(t *testing.T) {
}
}

func TestCGroupsCPUQuota(t *testing.T) {
func TestCGroupsMemoryQuota(t *testing.T) {
testTable := []struct {
name string
expectedQuota int64
Expand All @@ -110,6 +110,12 @@ func TestCGroupsCPUQuota(t *testing.T) {
name: "undefined",
expectedQuota: int64(-1.0),
expectedDefined: false,
shouldHaveError: true,
},
{
name: "memory",
expectedQuota: int64(8796093018112),
expectedDefined: true,
shouldHaveError: false,
},
}
Expand All @@ -123,7 +129,7 @@ func TestCGroupsCPUQuota(t *testing.T) {

for _, tt := range testTable {
cgroupPath := filepath.Join(testDataCGroupsPath, tt.name)
cgroups[_cgroupSubsysCPU] = NewCGroup(cgroupPath)
cgroups[_cgroupSubsysMemory] = NewCGroup(cgroupPath)

quota, defined, err := cgroups.MemoryQuota()
assert.Equal(t, tt.expectedQuota, quota, tt.name)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
8796093018112

0 comments on commit ceb7109

Please sign in to comment.