Skip to content

Commit

Permalink
Make readuint return maxInt when file has max value.
Browse files Browse the repository at this point in the history
This brings the behavior closer to getStatFileContentUint64
in cgroups1.

Signed-off-by: Manu Gupta <manugupt1@gmail.com>
  • Loading branch information
manugupt1 committed Apr 28, 2023
1 parent 62f3fd5 commit 466bd86
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
13 changes: 10 additions & 3 deletions cgroup1/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ package cgroup1

import (
"bufio"
"bytes"
"fmt"
"math"
"os"
"path/filepath"
"strconv"
Expand Down Expand Up @@ -137,13 +139,18 @@ func readUint(path string) (uint64, error) {
}
defer f.Close()

b := make([]byte, 32) // Chose 32 as some files have leading/trailing whitespaces and alignment
// We should only need 20 bytes for the max uint64, but for a nice power of 2
// lets use 32.
b := make([]byte, 32)
n, err := f.Read(b)
if err != nil {
return 0, err
}

return parseUint(strings.TrimSpace(string(b[:n])), 10, 64)
s := string(bytes.TrimSpace(b[:n]))
if s == "max" {
return math.MaxUint64, nil
}
return parseUint(s, 10, 64)
}

func parseUint(s string, base, bitSize int) (uint64, error) {
Expand Down
19 changes: 19 additions & 0 deletions cgroup1/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
package cgroup1

import (
"math"
"os"
"path/filepath"
"testing"
)

Expand All @@ -30,3 +33,19 @@ func BenchmarkReaduint64(b *testing.B) {
}
}
}

func TestReadUint(t *testing.T) {
tDir := t.TempDir()
pidsmax := filepath.Join(tDir, "pids.max")
err := os.WriteFile(pidsmax, []byte("max"), 0644)
if err != nil {
t.Fatal(err)
}
max, err := readUint(pidsmax)
if err != nil {
t.Fatal(err)
}
if max != math.MaxUint64 {
t.Fail()
}
}

0 comments on commit 466bd86

Please sign in to comment.