|
5 | 5 | "errors" |
6 | 6 | "fmt" |
7 | 7 | "io" |
| 8 | + "math" |
8 | 9 | "os" |
9 | 10 | "path/filepath" |
10 | 11 | "strconv" |
@@ -413,16 +414,30 @@ func WriteCgroupProc(dir string, pid int) error { |
413 | 414 | return err |
414 | 415 | } |
415 | 416 |
|
416 | | -// Since the OCI spec is designed for cgroup v1, in some cases |
417 | | -// there is need to convert from the cgroup v1 configuration to cgroup v2 |
418 | | -// the formula for cpuShares is y = (1 + ((x - 2) * 9999) / 262142) |
419 | | -// convert from [2-262144] to [1-10000] |
420 | | -// 262144 comes from Linux kernel definition "#define MAX_SHARES (1UL << 18)" |
| 417 | +// ConvertCPUSharesToCgroupV2Value converts CPU shares, used by cgroup v1, |
| 418 | +// to CPU weight, used by cgroup v2. |
| 419 | +// |
| 420 | +// Cgroup v1 CPU shares has a range of [2^1...2^18], i.e. [2...262144], |
| 421 | +// and the default value is 1024. |
| 422 | +// |
| 423 | +// Cgroup v2 CPU weight has a range of [10^0...10^4], i.e. [1...10000], |
| 424 | +// and the default value is 100. |
421 | 425 | func ConvertCPUSharesToCgroupV2Value(cpuShares uint64) uint64 { |
| 426 | + // The value of 0 means "unset". |
422 | 427 | if cpuShares == 0 { |
423 | 428 | return 0 |
424 | 429 | } |
425 | | - return (1 + ((cpuShares-2)*9999)/262142) |
| 430 | + if cpuShares <= 2 { |
| 431 | + return 1 |
| 432 | + } |
| 433 | + if cpuShares >= 262144 { |
| 434 | + return 10000 |
| 435 | + } |
| 436 | + l := math.Log2(float64(cpuShares)) |
| 437 | + // Quadratic function which fits min, max, and default. |
| 438 | + exponent := (l*l+125*l)/612.0 - 7.0/34.0 |
| 439 | + |
| 440 | + return uint64(math.Ceil(math.Pow(10, exponent))) |
426 | 441 | } |
427 | 442 |
|
428 | 443 | // ConvertMemorySwapToCgroupV2Value converts MemorySwap value from OCI spec |
|
0 commit comments