MCPcopy Create free account
hub / github.com/containerd/cgroups / ConvertCPUSharesToCgroupV2Value

Function ConvertCPUSharesToCgroupV2Value

cgroup2/utils.go:160–176  ·  view source on GitHub ↗

ConvertCPUSharesToCgroupV2Value converts CPU shares, used by cgroup v1, to CPU weight, used by cgroup v2. 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 i

(cpuShares uint64)

Source from the content-addressed store, hash-verified

158// Taken from https://github.com/opencontainers/cgroups/blob/v0.0.5/utils.go#L417-L441
159// (Apache License 2.0)
160func ConvertCPUSharesToCgroupV2Value(cpuShares uint64) uint64 {
161 // The value of 0 means "unset".
162 if cpuShares == 0 {
163 return 0
164 }
165 if cpuShares <= 2 {
166 return 1
167 }
168 if cpuShares >= 262144 {
169 return 10000
170 }
171 l := math.Log2(float64(cpuShares))
172 // Quadratic function which fits min, max, and default.
173 exponent := (l*l+125*l)/612.0 - 7.0/34.0
174
175 return uint64(math.Ceil(math.Pow(10, exponent)))
176}
177
178// ToResources converts the oci LinuxResources struct into a
179// v2 Resources type for use with this package.

Callers 3

TestToResourcesFunction · 0.85
ToResourcesFunction · 0.85

Calls

no outgoing calls

Tested by 2

TestToResourcesFunction · 0.68

Used in the wild real call sites across dependent graphs

searching dependent graphs…