Windows: Fix scaling of NanoCPUs (--cpus) on Hyper-V containers #32608
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.
Signed-off-by: Darren Stahl [email protected]
- What I did
In current Docker versions, when calculating the result of setting NanoCPUs, it is assumed that all of the host's CPUs are available to the container. This assumption does not hold true for Hyper-V containers on Windows. Since Hyper-V containers are backed by Hyper-V technology, they require an explicit CPU count at creation.
This PR uses NanoCPUs to calculate the number of CPUs to expose to the container, as well as a percentage scaling to apply in order to reach the desired NanoCPUs target.
- How I did it
If NanoCPUs is set on a Hyper-V container, it is rounded up to the next whole CPUCount, and that many cores are enabled in the container. If the NanoCPUs value is not evenly divisible by 1000000000 (one whole CPU), a CPUPercent limit is applied to limit the available CPU resources to match the requested NanoCPUs.
Note that in current Windows releases, Hyper-V container CpuLimit (Percent) does not have the same effect on CPU resources as expected by NanoCPUs. This would result in odd scaling, so in current Windows releases NanoCPUs on Hyper-V containers is implemented with either CpuPercent or CpuCount, but not both (unless NanoCPUs is less than 1000000000). If the provided NanoCPUs value is being rounded, a warning will be provided to the user.
- How to verify it
Manual verification.
- Description for the changelog
Windows: Fix scaling of NanoCPUs (--cpus) on Hyper-V containers.