-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Deal with when cpu.cfs_quota_us
is negative
#8428
Merged
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
4087010
ws-daemon: Add a simple unit test for cputlimit.
660cfd9
Deal with when cpu.cfs_quota_us is negative
14b5a47
Unify the values of each cgroup for units when reading them.
2475ba5
Add some unit tests for the cpu subsystem of cgroup.
a33de54
Since `max` is not used in the cpu subsystem of cgroup, delete it.
119be45
When `cpu.cfs_quota_us` is negative, it is always set to the maximum …
78720fc
Clarify the size ot the cfs_period_us.
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
// Copyright (c) 2022 Gitpod GmbH. All rights reserved. | ||
// Licensed under the GNU Affero General Public License (AGPL). | ||
// See License-AGPL.txt in the project root for license information. | ||
|
||
package cpulimit | ||
|
||
import ( | ||
"math" | ||
"os" | ||
"path/filepath" | ||
"strconv" | ||
"testing" | ||
"time" | ||
|
||
"github.com/opencontainers/runc/libcontainer/cgroups" | ||
) | ||
|
||
func init() { | ||
cgroups.TestMode = true | ||
} | ||
|
||
func createTempDir(t *testing.T, subsystem string) string { | ||
path := filepath.Join(t.TempDir(), subsystem) | ||
if err := os.Mkdir(path, 0o755); err != nil { | ||
t.Fatal(err) | ||
} | ||
return path | ||
} | ||
|
||
func TestCfsSetLimit(t *testing.T) { | ||
type test struct { | ||
beforeCfsPeriodUs int | ||
beforeCfsQuotaUs int | ||
bandWidth Bandwidth | ||
cfsQuotaUs int | ||
changed bool | ||
} | ||
tests := []test{ | ||
{ | ||
beforeCfsPeriodUs: 10000, | ||
beforeCfsQuotaUs: -1, | ||
bandWidth: Bandwidth(6000), | ||
cfsQuotaUs: 60000, | ||
changed: true, | ||
}, | ||
{ | ||
beforeCfsPeriodUs: 5000, | ||
beforeCfsQuotaUs: -1, | ||
bandWidth: Bandwidth(6000), | ||
cfsQuotaUs: 30000, | ||
changed: true, | ||
}, | ||
{ | ||
beforeCfsPeriodUs: 10000, | ||
beforeCfsQuotaUs: 60000, | ||
bandWidth: Bandwidth(6000), | ||
cfsQuotaUs: 60000, | ||
changed: false, | ||
}, | ||
} | ||
for _, tc := range tests { | ||
tempdir := createTempDir(t, "cpu") | ||
err := cgroups.WriteFile(tempdir, "cpu.cfs_period_us", strconv.Itoa(tc.beforeCfsPeriodUs)) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
err = cgroups.WriteFile(tempdir, "cpu.cfs_quota_us", strconv.Itoa(tc.beforeCfsQuotaUs)) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
cfs := CgroupCFSController(tempdir) | ||
changed, err := cfs.SetLimit(tc.bandWidth) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if changed != tc.changed { | ||
t.Fatalf("unexpected error: changed is '%v' but expected '%v'", changed, tc.changed) | ||
} | ||
cfsQuotaUs, err := cgroups.ReadFile(tempdir, "cpu.cfs_quota_us") | ||
sagor999 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if cfsQuotaUs != strconv.Itoa(tc.cfsQuotaUs) { | ||
t.Fatalf("unexpected error: cfsQuotaUs is '%v' but expected '%v'", cfsQuotaUs, tc.cfsQuotaUs) | ||
} | ||
} | ||
} | ||
|
||
func TestReadCfsQuota(t *testing.T) { | ||
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.value)) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
cfs := CgroupCFSController(tempdir) | ||
v, err := cfs.readCfsQuota() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if v.Microseconds() != int64(tc.expect) { | ||
t.Fatalf("unexpected error: cfs quota is '%v' but expected '%v'", v, tc.expect) | ||
} | ||
} | ||
} | ||
|
||
func TestReadCfsPeriod(t *testing.T) { | ||
tests := []int{ | ||
10000, | ||
} | ||
for _, tc := range tests { | ||
tempdir := createTempDir(t, "cpu") | ||
err := cgroups.WriteFile(tempdir, "cpu.cfs_period_us", strconv.Itoa(tc)) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
cfs := CgroupCFSController(tempdir) | ||
v, err := cfs.readCfsPeriod() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if v.Microseconds() != int64(tc) { | ||
t.Fatalf("unexpected error: cfs period is '%v' but expected '%v'", v, tc) | ||
} | ||
} | ||
} | ||
|
||
func TestReadCpuUsage(t *testing.T) { | ||
tests := []int{ | ||
0, | ||
100000, | ||
} | ||
for _, tc := range tests { | ||
tempdir := createTempDir(t, "cpu") | ||
err := cgroups.WriteFile(tempdir, "cpuacct.usage", strconv.Itoa(tc)) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
cfs := CgroupCFSController(tempdir) | ||
v, err := cfs.readCpuUsage() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if v.Nanoseconds() != int64(tc) { | ||
t.Fatalf("unexpected error: cpu usage is '%v' but expected '%v'", v, tc) | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I couldn't find any documentation where
max
has any meaning 🤔https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/6/html/resource_management_guide/sec-cpu