Skip to content

Commit

Permalink
When cpu.cfs_quota_us is negative, it is always set to the maximum …
Browse files Browse the repository at this point in the history
  • Loading branch information
utam0k committed Feb 25, 2022
1 parent a33de54 commit 119be45
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
6 changes: 5 additions & 1 deletion components/ws-daemon/pkg/cpulimit/cfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package cpulimit

import (
"bufio"
"math"
"os"
"path/filepath"
"strconv"
Expand All @@ -20,7 +21,6 @@ type CgroupCFSController string

// Usage returns the cpuacct.usage value of the cgroup
func (basePath CgroupCFSController) Usage() (usage CPUTime, err error) {

cputime, err := basePath.readCpuUsage()
if err != nil {
return 0, xerrors.Errorf("cannot read cpuacct.usage: %w", err)
Expand Down Expand Up @@ -99,6 +99,10 @@ func (basePath CgroupCFSController) readCfsQuota() (time.Duration, error) {
if err != nil {
return 0, err
}

if p < 0 {
return time.Duration(math.MaxInt64), nil
}
return time.Duration(p) * time.Microsecond, nil
}

Expand Down
25 changes: 19 additions & 6 deletions components/ws-daemon/pkg/cpulimit/cfs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
package cpulimit

import (
"math"
"os"
"path/filepath"
"strconv"
"testing"
"time"

"github.com/opencontainers/runc/libcontainer/cgroups"
)
Expand Down Expand Up @@ -86,13 +88,24 @@ func TestCfsSetLimit(t *testing.T) {
}

func TestReadCfsQuota(t *testing.T) {
tests := []int{
100000,
-1,
type test struct {
value int
expect int
}
tests := []test{
{
value: 100000,
expect: 100000,
},
{
value: -1,
expect: int(time.Duration(math.MaxInt64).Microseconds()),
},
}

for _, tc := range tests {
tempdir := createTempDir(t, "cpu")
err := cgroups.WriteFile(tempdir, "cpu.cfs_quota_us", strconv.Itoa(tc))
err := cgroups.WriteFile(tempdir, "cpu.cfs_quota_us", strconv.Itoa(tc.value))
if err != nil {
t.Fatal(err)
}
Expand All @@ -102,8 +115,8 @@ func TestReadCfsQuota(t *testing.T) {
if err != nil {
t.Fatal(err)
}
if v.Microseconds() != int64(tc) {
t.Fatalf("unexpected error: cfs quota is '%v' but expected '%v'", v, tc)
if v.Microseconds() != int64(tc.expect) {
t.Fatalf("unexpected error: cfs quota is '%v' but expected '%v'", v, tc.expect)
}
}
}
Expand Down

0 comments on commit 119be45

Please sign in to comment.