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

Fix CPU share calculation #5324

Conversation

evgenyfedorov2
Copy link
Contributor

@evgenyfedorov2 evgenyfedorov2 commented Jul 31, 2024

Fixes #5323

Microsoft Reviewers: Open in CodeFlow

Copy link
Member

@RussKie RussKie left a comment

Choose a reason for hiding this comment

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

👍

cpuUnits = cpuPodShare;
// The formula to calculate CPU pod weight (measured in millicores) from CPU share:
// y = (1 + ((x - 2) * 9999) / 262142),
// where y is the CPU pod weight (e.g. cpuPodWeight) and x is the CPU share of cgroup v1 (e.g. cpuUnits).
Copy link
Member

Choose a reason for hiding this comment

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

Btw, since we're in V2 class, should the comment say "cgroup v2" too?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

CPU share is a setting from cgroups v1 only and it is not necessarily present in cgroups v2 files, though it might happen, so we are trying to take advantage of that and calculate CPU request based on the CPU share value (if it exists).

@RussKie
Copy link
Member

RussKie commented Aug 2, 2024

Also, is this comment still valid?
image

}

_ = GetNextNumber(cpuPodWeightBuffer, out long cpuPodWeight);

if (cpuPodWeight == -1)
{
Throw.InvalidOperationException($"Could not parse '{_cpuPodWeight}'. Expected to get an integer but got: '{cpuPodWeight}'.");
Throw.InvalidOperationException(
$"Could not parse '{_cpuPodWeight}' content. Expected to get an integer but got: '{cpuPodWeight}'.");
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't this include the buffer? The value here will always be -1.

Copy link
Member

Choose a reason for hiding this comment

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

Thank you @danmoseley, I totally missed this one.
This, however, reinforces the need to have tests that validate exception messages (see #5324 (comment)).

@evgenyfedorov2 if you could add tests - that'd be great. The best way to have such tests in my experience is to use Verify to assert blobs (e.g., text). This way we don't have to maintain/update test assertions manually whenever text changes.

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

Copy link
Member

Choose a reason for hiding this comment

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

Awesome, thank you

// where y is the CPU pod weight (e.g. cpuPodWeight) and x is the CPU share of cgroup v1 (e.g. cpuUnits).
// https://github.com/kubernetes/enhancements/tree/master/keps/sig-node/2254-cgroup-v2#phase-1-convert-from-cgroups-v1-settings-to-v2
// We invert the formula to calculate CPU share from CPU pod weight:
#pragma warning disable S109 // Magic numbers should not be used - using the formula, forgive.
Copy link
Member

Choose a reason for hiding this comment

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

You can just extract local consts for the values then you can name them and eliminate the warnings.

Copy link
Contributor Author

@evgenyfedorov2 evgenyfedorov2 Aug 2, 2024

Choose a reason for hiding this comment

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

But I deliberately don't want to do that, prefer to use the formula as is because it is more readable :)

@evgenyfedorov2
Copy link
Contributor Author

Also, is this comment still valid? image

Fixed it

@danespinosa
Copy link
Contributor

Also, is this comment still valid? image

I am sending a PR to fix this: #5334

cc @RussKie @evgenyfedorov2

@@ -533,6 +533,9 @@ private static bool TryGetCpuUnitsFromCgroups(IFileSystem fileSystem, out float

private static bool TryGetCgroupRequestCpu(IFileSystem fileSystem, out float cpuUnits)
{
const long CpuPodWeightPossibleMax = 10_000;
Copy link
Contributor

Choose a reason for hiding this comment

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

@evgenyfedorov2 I am being curious here, pod, for me is a K8s concept and as far as I understand this parser will calculate the utilization of resources on a cgroup on a container regardless of the use of k8s or not.

In the future would it be worth it to rename pod to container to not confuse that this class and logic is not K8s exclussive?

@evgenyfedorov2 evgenyfedorov2 merged commit ff546f3 into dotnet:main Aug 4, 2024
6 checks passed
@github-actions github-actions bot locked and limited conversation to collaborators Sep 4, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Incorrect calculation of CPU Share from cgroups v2 CPU weight
4 participants