Skip to content

ConvertCPUSharesToCgroupV2Value: improve #20

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

Merged
merged 2 commits into from
Jun 11, 2025

Conversation

kolyshkin
Copy link
Contributor

The old formula, while correctly converting the cgroup v1 range to cgroup v2 range, had a few issues:

  1. When cgroup v1 value is out of range, it returned invalid cgroup v2 value, leading to vague errors down the line (see 1).
  2. Default value cgroup v1 shares of 1024 is converted to 39, which is much less than the cgroup v2 default weight of 100 (see 2, 3).

The new conversion formula fixes both issues (see discussion in 2 for more details).

Amend the test case accordingly.

@iholder101
Copy link
Contributor

Thank you very much @kolyshkin!
/lgtm

@kolyshkin kolyshkin requested review from cyphar and AkihiroSuda June 10, 2025 20:24
Copy link
Member

@thaJeztah thaJeztah left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

overall LGTM, but left some comments / suggestions.

utils_test.go Outdated
2: 1, // Minimum.
1024: 100, // Default.
262144: 10000, // Maximum.
262145: 10000, // Above maximum (out of range).
}
for i, expected := range cases {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not directly related to this PR, but we should use a different name for the i var to reduce cognitive load; it's very easy to read i as index value for a slice;

Suggested change
for i, expected := range cases {
for input, expected := range cases {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done (as a separate patch).

utils.go Outdated
Comment on lines 428 to 431
if cpuShares < 2 {
return 1
}
if cpuShares > 262144 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really nitty, but these could be <= and >= to avoid doing the calculation

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Although this also results in formula not being used for boundary values, so we're no longer testing it giving min and max as inputs. In order to test that we now need to add sharesMin+1 and sharesMax-1 values to the test.

Added as well.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call, and thanks!

// Since the OCI spec is designed for cgroup v1, in some cases
// there is need to convert from the cgroup v1 configuration to cgroup v2
// the formula for cpuShares is y = (1 + ((x - 2) * 9999) / 262142)
// convert from [2-262144] to [1-10000]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps including the v1 range ([2-262144]) and v2 range ([1-10000]) is worth including in the documentation.

utils.go Outdated
Comment on lines 424 to 427
// Cgroup v1 CPU shares has a range of [2^1...2^18], i.e. [2...262144],
// and the default value is 1024.
// Cgroup v2 CPU weight has a range of [10^0...10^4], i.e. [1...10000],
// and the default value is 100.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess (see my other comment) this could be part of the GoDoc?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

The old formula, while correctly converting the cgroup v1 range to
cgroup v2 range, had a few issues:

1. When cgroup v1 value is out of range, it returned invalid cgroup v2
   value, leading to vague errors down the line (see [1]).
2. Default value cgroup v1 shares of 1024 is converted to 39, which is
   much less than the cgroup v2 default weight of 100 (see [2], [3]).

The new conversion formula fixes both issues (see discussion in [2] for
more details).

Amend the test case accordingly.

[1]: opencontainers/runc#4755
[2]: kubernetes/kubernetes#131216
[3]: opencontainers/runc#4772

Signed-off-by: Kir Kolyshkin <[email protected]>
Rename some variables, and simplify the error message using
the got/want pattern.

Signed-off-by: Kir Kolyshkin <[email protected]>
@kolyshkin
Copy link
Contributor Author

@thaJeztah addressed all your comments, PTAL

Copy link
Member

@thaJeztah thaJeztah left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!!

@thaJeztah thaJeztah merged commit 33e090f into opencontainers:main Jun 11, 2025
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants