Skip to content
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

koordlet: fix the cpu.shares conversion on cgroups-v2 #1521

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions pkg/koordlet/resourceexecutor/reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -576,19 +576,19 @@ func TestCgroupReader_ReadCPUShares(t *testing.T) {
args: args{
parentDir: "/kubepods.slice",
},
want: 10,
want: 2,
wantErr: false,
},
{
name: "parse v2 value successfully 1",
fields: fields{
UseCgroupsV2: true,
CPUWeightValue: "100",
CPUWeightValue: "39",
},
args: args{
parentDir: "/kubepods.slice",
},
want: 1024,
want: 998,
wantErr: false,
},
{
Expand Down
14 changes: 12 additions & 2 deletions pkg/koordlet/util/system/cgroup2.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,9 +286,15 @@ func ConvertCPUWeightToShares(v int64) (int64, error) {
if !isValid {
return -1, fmt.Errorf("invalid cpu.weight value, err: %s", msg)
}
s := v * 1024 / 100 // no overflow since v is in [1, 10000]
// Use the inverse conversion of the kubelet.
// https://github.com/kubernetes/enhancements/tree/master/keps/sig-node/2254-cgroup-v2
// Map weights [1, 10000] to shares [2, 262144]:
// shares = (weights - 1) * 262142 / 9999 + 2
s := (v-1)*262142/9999 + 2
if s < CPUSharesMinValue {
s = CPUSharesMinValue
} else if s > CPUSharesMaxValue {
s = CPUSharesMaxValue
}
return s, nil
}
Expand All @@ -299,7 +305,11 @@ func ConvertCPUSharesToWeight(s string) (int64, error) {
return -1, fmt.Errorf("invalid cpu.weight value, err: %s", msg)
}
v, _ := strconv.ParseInt(s, 10, 64) // the valid value must be an integer
w := v * 100 / 1024 // assert no overflow since in k8s v is no more than 1024*num_cpus << math.MaxInt64
// Use the same conversion of the kubelet.
// https://github.com/kubernetes/enhancements/tree/master/keps/sig-node/2254-cgroup-v2
// Map shares [2, 262144] to weights [1, 10000]:
// weights = 1 + ((shares - 2) * 9999) / 262142
w := 1 + ((v-2)*9999)/262142
if w < CPUWeightMinValue {
w = CPUWeightMinValue
} else if w > CPUWeightMaxValue {
Expand Down
28 changes: 16 additions & 12 deletions pkg/koordlet/util/system/cgroup2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,13 +340,13 @@ func TestConvertCPUWeightToShares(t *testing.T) {
wantErr bool
}{
{
input: 50,
expected: 512,
input: 1,
expected: 2,
wantErr: false,
},
{
input: 10000,
expected: 102400,
expected: 262144,
wantErr: false,
},
{
Expand All @@ -358,9 +358,8 @@ func TestConvertCPUWeightToShares(t *testing.T) {
wantErr: true,
},
{
input: -1,
expected: 2,
wantErr: true,
input: -1,
wantErr: true,
},
}

Expand Down Expand Up @@ -390,17 +389,22 @@ func TestConvertCPUSharesToWeight(t *testing.T) {
}{
{
input: "1000",
expected: 97, // 1000 * 100 / 1024 = 97
expected: 39, // 1 + (1000 - 2) * 9999 / 262142 = 39
wantErr: false,
},
{
input: "1024",
expected: 100, // 1024 * 100 / 1024 = 100
expected: 39, // 1 + (1024 - 2) * 9999 / 262142 = 39
wantErr: false,
},
{
input: "2",
expected: 1, // 1 + (2 - 2) * 9999 / 262142 = 1
wantErr: false,
},
{
input: "512",
expected: 50, // 512 * 100 / 1024 = 50
input: "262144",
expected: 10000, // 1 + (262144 - 2) * 9999 / 262142 = 10000
wantErr: false,
},
{
Expand All @@ -409,12 +413,12 @@ func TestConvertCPUSharesToWeight(t *testing.T) {
},
{
input: "2000",
expected: 195, // 2000 * 100 / 1024 = 195
expected: 77, // 1 + (77 - 2) * 9999 / 262142 = 77
wantErr: false,
},
{
input: "50000",
expected: 4882, // 50000 * 100 / 1024 = 4882
expected: 1908, // 1 + (50000 - 2) * 9999 / 262142 = 1908
wantErr: false,
},
{
Expand Down
3 changes: 2 additions & 1 deletion pkg/koordlet/util/system/cgroup_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ const (
CFSBasePeriodValue int64 = 100000
CFSQuotaMinValue int64 = 1000 // min value except `-1`
CPUSharesMinValue int64 = 2
CPUSharesMaxValue int64 = 262144
CPUWeightMinValue int64 = 1
CPUWeightMaxValue int64 = 10000

Expand Down Expand Up @@ -171,7 +172,7 @@ const (
var (
NaturalInt64Validator = &RangeValidator{min: 0, max: math.MaxInt64}

CPUSharesValidator = &RangeValidator{min: CPUSharesMinValue, max: math.MaxInt64}
CPUSharesValidator = &RangeValidator{min: CPUSharesMinValue, max: CPUSharesMaxValue}
CPUBurstValidator = &RangeValidator{min: 0, max: 100 * 10 * 100000}
CPUBvtWarpNsValidator = &RangeValidator{min: -1, max: 2}
CPUWeightValidator = &RangeValidator{min: CPUWeightMinValue, max: CPUWeightMaxValue}
Expand Down