Merge pull request #2213 from Zyqsempai/2166-convert-cpu-weight-poperly

Added conversion for cpu.weight v2
This commit is contained in:
Mrunal Patel 2020-02-17 07:49:39 -08:00 committed by GitHub
commit 81ef5024f8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 0 deletions

View File

@ -586,3 +586,12 @@ func isEINVAL(err error) bool {
return false return false
} }
} }
// 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]
// 262144 comes from Linux kernel definition "#define MAX_SHARES (1UL << 18)"
func ConvertCPUSharesToCgroupV2Value(cpuShares uint64) uint64 {
return (1 + ((cpuShares-2)*9999)/262142)
}

View File

@ -11,6 +11,8 @@ import (
"strings" "strings"
"time" "time"
"github.com/opencontainers/runc/libcontainer/cgroups"
"github.com/opencontainers/runc/libcontainer/configs" "github.com/opencontainers/runc/libcontainer/configs"
"github.com/opencontainers/runc/libcontainer/seccomp" "github.com/opencontainers/runc/libcontainer/seccomp"
libcontainerUtils "github.com/opencontainers/runc/libcontainer/utils" libcontainerUtils "github.com/opencontainers/runc/libcontainer/utils"
@ -405,6 +407,9 @@ func CreateCgroupConfig(opts *CreateOpts) (*configs.Cgroup, error) {
if r.CPU != nil { if r.CPU != nil {
if r.CPU.Shares != nil { if r.CPU.Shares != nil {
c.Resources.CpuShares = *r.CPU.Shares c.Resources.CpuShares = *r.CPU.Shares
//CpuWeight is used for cgroupv2 and should be converted
c.Resources.CpuWeight = cgroups.ConvertCPUSharesToCgroupV2Value(c.Resources.CpuShares)
} }
if r.CPU.Quota != nil { if r.CPU.Quota != nil {
c.Resources.CpuQuota = *r.CPU.Quota c.Resources.CpuQuota = *r.CPU.Quota

View File

@ -8,6 +8,8 @@ import (
"os" "os"
"strconv" "strconv"
"github.com/opencontainers/runc/libcontainer/cgroups"
"github.com/docker/go-units" "github.com/docker/go-units"
"github.com/opencontainers/runc/libcontainer/configs" "github.com/opencontainers/runc/libcontainer/configs"
"github.com/opencontainers/runc/libcontainer/intelrdt" "github.com/opencontainers/runc/libcontainer/intelrdt"
@ -253,6 +255,8 @@ other options are ignored.
config.Cgroups.Resources.CpuPeriod = *r.CPU.Period config.Cgroups.Resources.CpuPeriod = *r.CPU.Period
config.Cgroups.Resources.CpuQuota = *r.CPU.Quota config.Cgroups.Resources.CpuQuota = *r.CPU.Quota
config.Cgroups.Resources.CpuShares = *r.CPU.Shares config.Cgroups.Resources.CpuShares = *r.CPU.Shares
//CpuWeight is used for cgroupv2 and should be converted
config.Cgroups.Resources.CpuWeight = cgroups.ConvertCPUSharesToCgroupV2Value(*r.CPU.Shares)
config.Cgroups.Resources.CpuRtPeriod = *r.CPU.RealtimePeriod config.Cgroups.Resources.CpuRtPeriod = *r.CPU.RealtimePeriod
config.Cgroups.Resources.CpuRtRuntime = *r.CPU.RealtimeRuntime config.Cgroups.Resources.CpuRtRuntime = *r.CPU.RealtimeRuntime
config.Cgroups.Resources.CpusetCpus = r.CPU.Cpus config.Cgroups.Resources.CpusetCpus = r.CPU.Cpus