2016-04-28 22:30:50 +08:00
|
|
|
// +build linux
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2020-05-17 08:20:44 +08:00
|
|
|
"errors"
|
2016-04-28 22:30:50 +08:00
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"strconv"
|
|
|
|
|
2020-01-16 00:33:15 +08:00
|
|
|
"github.com/opencontainers/runc/libcontainer/cgroups"
|
|
|
|
|
2016-05-30 18:56:10 +08:00
|
|
|
"github.com/docker/go-units"
|
2017-09-20 01:33:35 +08:00
|
|
|
"github.com/opencontainers/runc/libcontainer/configs"
|
|
|
|
"github.com/opencontainers/runc/libcontainer/intelrdt"
|
2016-04-28 22:30:50 +08:00
|
|
|
"github.com/opencontainers/runtime-spec/specs-go"
|
2016-06-07 02:45:46 +08:00
|
|
|
"github.com/urfave/cli"
|
2016-04-28 22:30:50 +08:00
|
|
|
)
|
|
|
|
|
2017-03-15 00:36:38 +08:00
|
|
|
func i64Ptr(i int64) *int64 { return &i }
|
2016-04-28 22:30:50 +08:00
|
|
|
func u64Ptr(i uint64) *uint64 { return &i }
|
|
|
|
func u16Ptr(i uint16) *uint16 { return &i }
|
|
|
|
|
|
|
|
var updateCommand = cli.Command{
|
|
|
|
Name: "update",
|
|
|
|
Usage: "update container resource constraints",
|
2016-05-09 19:04:25 +08:00
|
|
|
ArgsUsage: `<container-id>`,
|
2016-04-28 22:30:50 +08:00
|
|
|
Flags: []cli.Flag{
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "resources, r",
|
|
|
|
Value: "",
|
2016-05-28 13:33:57 +08:00
|
|
|
Usage: `path to the file containing the resources to update or '-' to read from the standard input
|
2016-04-28 22:30:50 +08:00
|
|
|
|
|
|
|
The accepted format is as follow (unchanged values can be omitted):
|
|
|
|
|
|
|
|
{
|
|
|
|
"memory": {
|
|
|
|
"limit": 0,
|
|
|
|
"reservation": 0,
|
|
|
|
"swap": 0,
|
2016-05-25 16:01:03 +08:00
|
|
|
"kernel": 0,
|
|
|
|
"kernelTCP": 0
|
2016-04-28 22:30:50 +08:00
|
|
|
},
|
|
|
|
"cpu": {
|
|
|
|
"shares": 0,
|
|
|
|
"quota": 0,
|
|
|
|
"period": 0,
|
2016-11-04 16:50:42 +08:00
|
|
|
"realtimeRuntime": 0,
|
|
|
|
"realtimePeriod": 0,
|
2016-04-28 22:30:50 +08:00
|
|
|
"cpus": "",
|
|
|
|
"mems": ""
|
|
|
|
},
|
|
|
|
"blockIO": {
|
2017-06-02 07:17:21 +08:00
|
|
|
"weight": 0
|
2016-10-29 01:28:08 +08:00
|
|
|
}
|
2016-04-28 22:30:50 +08:00
|
|
|
}
|
|
|
|
|
2016-05-09 19:04:25 +08:00
|
|
|
Note: if data is to be read from a file or the standard input, all
|
2016-04-28 22:30:50 +08:00
|
|
|
other options are ignored.
|
|
|
|
`,
|
|
|
|
},
|
|
|
|
|
|
|
|
cli.IntFlag{
|
|
|
|
Name: "blkio-weight",
|
2016-05-28 13:33:57 +08:00
|
|
|
Usage: "Specifies per cgroup weight, range is from 10 to 1000",
|
2016-04-28 22:30:50 +08:00
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "cpu-period",
|
2016-11-04 16:50:42 +08:00
|
|
|
Usage: "CPU CFS period to be used for hardcapping (in usecs). 0 to use system default",
|
2016-04-28 22:30:50 +08:00
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "cpu-quota",
|
2016-11-04 16:50:42 +08:00
|
|
|
Usage: "CPU CFS hardcap limit (in usecs). Allowed cpu time in a given period",
|
2016-04-28 22:30:50 +08:00
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "cpu-share",
|
|
|
|
Usage: "CPU shares (relative weight vs. other containers)",
|
|
|
|
},
|
2016-11-04 16:50:42 +08:00
|
|
|
cli.StringFlag{
|
|
|
|
Name: "cpu-rt-period",
|
|
|
|
Usage: "CPU realtime period to be used for hardcapping (in usecs). 0 to use system default",
|
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "cpu-rt-runtime",
|
|
|
|
Usage: "CPU realtime hardcap limit (in usecs). Allowed cpu time in a given period",
|
|
|
|
},
|
2016-04-28 22:30:50 +08:00
|
|
|
cli.StringFlag{
|
|
|
|
Name: "cpuset-cpus",
|
|
|
|
Usage: "CPU(s) to use",
|
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "cpuset-mems",
|
|
|
|
Usage: "Memory node(s) to use",
|
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "kernel-memory",
|
2016-05-09 23:14:29 +08:00
|
|
|
Usage: "Kernel memory limit (in bytes)",
|
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "kernel-memory-tcp",
|
2016-04-28 22:30:50 +08:00
|
|
|
Usage: "Kernel memory limit (in bytes) for tcp buffer",
|
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "memory",
|
|
|
|
Usage: "Memory limit (in bytes)",
|
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "memory-reservation",
|
|
|
|
Usage: "Memory reservation or soft_limit (in bytes)",
|
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "memory-swap",
|
2016-05-28 10:22:06 +08:00
|
|
|
Usage: "Total memory usage (memory + swap); set '-1' to enable unlimited swap",
|
2016-04-28 22:30:50 +08:00
|
|
|
},
|
2017-04-27 00:53:20 +08:00
|
|
|
cli.IntFlag{
|
|
|
|
Name: "pids-limit",
|
|
|
|
Usage: "Maximum number of pids allowed in the container",
|
|
|
|
},
|
2017-09-20 01:33:35 +08:00
|
|
|
cli.StringFlag{
|
|
|
|
Name: "l3-cache-schema",
|
|
|
|
Usage: "The string of Intel RDT/CAT L3 cache schema",
|
|
|
|
},
|
2018-10-16 12:37:50 +08:00
|
|
|
cli.StringFlag{
|
|
|
|
Name: "mem-bw-schema",
|
|
|
|
Usage: "The string of Intel RDT/MBA memory bandwidth schema",
|
|
|
|
},
|
2016-04-28 22:30:50 +08:00
|
|
|
},
|
2016-05-10 13:58:09 +08:00
|
|
|
Action: func(context *cli.Context) error {
|
2016-10-28 23:43:10 +08:00
|
|
|
if err := checkArgs(context, 1, exactArgs); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-04-28 22:30:50 +08:00
|
|
|
container, err := getContainer(context)
|
|
|
|
if err != nil {
|
2016-05-10 13:58:09 +08:00
|
|
|
return err
|
2016-04-28 22:30:50 +08:00
|
|
|
}
|
|
|
|
|
2016-12-17 13:01:53 +08:00
|
|
|
r := specs.LinuxResources{
|
|
|
|
Memory: &specs.LinuxMemory{
|
2017-06-24 08:17:00 +08:00
|
|
|
Limit: i64Ptr(0),
|
|
|
|
Reservation: i64Ptr(0),
|
|
|
|
Swap: i64Ptr(0),
|
|
|
|
Kernel: i64Ptr(0),
|
|
|
|
KernelTCP: i64Ptr(0),
|
2016-04-28 22:30:50 +08:00
|
|
|
},
|
2016-12-17 13:01:53 +08:00
|
|
|
CPU: &specs.LinuxCPU{
|
2016-11-04 16:50:42 +08:00
|
|
|
Shares: u64Ptr(0),
|
2017-03-15 00:36:38 +08:00
|
|
|
Quota: i64Ptr(0),
|
2016-11-04 16:50:42 +08:00
|
|
|
Period: u64Ptr(0),
|
2017-03-15 00:36:38 +08:00
|
|
|
RealtimeRuntime: i64Ptr(0),
|
2016-11-04 16:50:42 +08:00
|
|
|
RealtimePeriod: u64Ptr(0),
|
2017-03-15 00:36:38 +08:00
|
|
|
Cpus: "",
|
|
|
|
Mems: "",
|
2016-04-28 22:30:50 +08:00
|
|
|
},
|
2016-12-17 13:01:53 +08:00
|
|
|
BlockIO: &specs.LinuxBlockIO{
|
2016-04-28 22:30:50 +08:00
|
|
|
Weight: u16Ptr(0),
|
|
|
|
},
|
2017-04-27 00:53:20 +08:00
|
|
|
Pids: &specs.LinuxPids{
|
|
|
|
Limit: 0,
|
|
|
|
},
|
2016-04-28 22:30:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
config := container.Config()
|
|
|
|
|
|
|
|
if in := context.String("resources"); in != "" {
|
|
|
|
var (
|
|
|
|
f *os.File
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
switch in {
|
|
|
|
case "-":
|
|
|
|
f = os.Stdin
|
|
|
|
default:
|
|
|
|
f, err = os.Open(in)
|
|
|
|
if err != nil {
|
2016-05-10 13:58:09 +08:00
|
|
|
return err
|
2016-04-28 22:30:50 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
err = json.NewDecoder(f).Decode(&r)
|
|
|
|
if err != nil {
|
2016-05-10 13:58:09 +08:00
|
|
|
return err
|
2016-04-28 22:30:50 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if val := context.Int("blkio-weight"); val != 0 {
|
|
|
|
r.BlockIO.Weight = u16Ptr(uint16(val))
|
|
|
|
}
|
|
|
|
if val := context.String("cpuset-cpus"); val != "" {
|
2017-03-15 00:36:38 +08:00
|
|
|
r.CPU.Cpus = val
|
2016-04-28 22:30:50 +08:00
|
|
|
}
|
|
|
|
if val := context.String("cpuset-mems"); val != "" {
|
2017-03-15 00:36:38 +08:00
|
|
|
r.CPU.Mems = val
|
2016-04-28 22:30:50 +08:00
|
|
|
}
|
|
|
|
|
2016-09-27 23:36:57 +08:00
|
|
|
for _, pair := range []struct {
|
|
|
|
opt string
|
|
|
|
dest *uint64
|
|
|
|
}{
|
|
|
|
|
|
|
|
{"cpu-period", r.CPU.Period},
|
2016-11-04 16:50:42 +08:00
|
|
|
{"cpu-rt-period", r.CPU.RealtimePeriod},
|
2016-09-27 23:36:57 +08:00
|
|
|
{"cpu-share", r.CPU.Shares},
|
2016-05-30 18:56:10 +08:00
|
|
|
} {
|
2016-09-27 23:36:57 +08:00
|
|
|
if val := context.String(pair.opt); val != "" {
|
2016-05-30 18:56:10 +08:00
|
|
|
var err error
|
2016-09-27 23:36:57 +08:00
|
|
|
*pair.dest, err = strconv.ParseUint(val, 10, 64)
|
2016-05-30 18:56:10 +08:00
|
|
|
if err != nil {
|
2016-09-27 23:36:57 +08:00
|
|
|
return fmt.Errorf("invalid value for %s: %s", pair.opt, err)
|
2016-05-30 18:56:10 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-03-15 00:36:38 +08:00
|
|
|
for _, pair := range []struct {
|
|
|
|
opt string
|
|
|
|
dest *int64
|
|
|
|
}{
|
|
|
|
|
|
|
|
{"cpu-quota", r.CPU.Quota},
|
|
|
|
{"cpu-rt-runtime", r.CPU.RealtimeRuntime},
|
|
|
|
} {
|
|
|
|
if val := context.String(pair.opt); val != "" {
|
|
|
|
var err error
|
|
|
|
*pair.dest, err = strconv.ParseInt(val, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("invalid value for %s: %s", pair.opt, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-09-27 23:36:57 +08:00
|
|
|
for _, pair := range []struct {
|
|
|
|
opt string
|
2017-06-24 08:17:00 +08:00
|
|
|
dest *int64
|
2016-09-27 23:36:57 +08:00
|
|
|
}{
|
2016-10-19 18:15:55 +08:00
|
|
|
{"memory", r.Memory.Limit},
|
|
|
|
{"memory-swap", r.Memory.Swap},
|
2016-09-27 23:36:57 +08:00
|
|
|
{"kernel-memory", r.Memory.Kernel},
|
|
|
|
{"kernel-memory-tcp", r.Memory.KernelTCP},
|
|
|
|
{"memory-reservation", r.Memory.Reservation},
|
2016-04-28 22:30:50 +08:00
|
|
|
} {
|
2016-09-27 23:36:57 +08:00
|
|
|
if val := context.String(pair.opt); val != "" {
|
2016-10-19 18:15:55 +08:00
|
|
|
var v int64
|
|
|
|
|
|
|
|
if val != "-1" {
|
|
|
|
v, err = units.RAMInBytes(val)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("invalid value for %s: %s", pair.opt, err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
v = -1
|
2016-04-28 22:30:50 +08:00
|
|
|
}
|
2017-06-24 08:17:00 +08:00
|
|
|
*pair.dest = v
|
2016-04-28 22:30:50 +08:00
|
|
|
}
|
|
|
|
}
|
2017-04-27 00:53:20 +08:00
|
|
|
r.Pids.Limit = int64(context.Int("pids-limit"))
|
2016-04-28 22:30:50 +08:00
|
|
|
}
|
|
|
|
|
2020-06-05 07:48:58 +08:00
|
|
|
// Update the values
|
2016-04-28 22:30:50 +08:00
|
|
|
config.Cgroups.Resources.BlkioWeight = *r.BlockIO.Weight
|
2020-06-05 07:48:58 +08:00
|
|
|
|
|
|
|
// Seting CPU quota and period independently does not make much sense,
|
|
|
|
// but historically runc allowed it and this needs to be supported
|
|
|
|
// to not break compatibility.
|
|
|
|
//
|
|
|
|
// For systemd cgroup drivers to set CPU quota/period correctly,
|
|
|
|
// it needs to know both values. For fs2 cgroup driver to be compatible
|
|
|
|
// with the fs driver, it also needs to know both values.
|
|
|
|
//
|
|
|
|
// Here in update, previously set values are available from config.
|
|
|
|
// If only one of {quota,period} is set and the other is not, leave
|
|
|
|
// the unset parameter at the old value (don't overwrite config).
|
|
|
|
p, q := *r.CPU.Period, *r.CPU.Quota
|
|
|
|
if (p == 0 && q == 0) || (p != 0 && q != 0) {
|
|
|
|
// both values are either set or unset (0)
|
|
|
|
config.Cgroups.Resources.CpuPeriod = p
|
|
|
|
config.Cgroups.Resources.CpuQuota = q
|
|
|
|
} else {
|
|
|
|
// one is set and the other is not
|
|
|
|
if p != 0 {
|
|
|
|
// set new period, leave quota at old value
|
|
|
|
config.Cgroups.Resources.CpuPeriod = p
|
|
|
|
} else if q != 0 {
|
|
|
|
// set new quota, leave period at old value
|
|
|
|
config.Cgroups.Resources.CpuQuota = q
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-20 18:51:39 +08:00
|
|
|
config.Cgroups.Resources.CpuShares = *r.CPU.Shares
|
2020-01-16 00:33:15 +08:00
|
|
|
//CpuWeight is used for cgroupv2 and should be converted
|
|
|
|
config.Cgroups.Resources.CpuWeight = cgroups.ConvertCPUSharesToCgroupV2Value(*r.CPU.Shares)
|
2017-03-20 18:51:39 +08:00
|
|
|
config.Cgroups.Resources.CpuRtPeriod = *r.CPU.RealtimePeriod
|
|
|
|
config.Cgroups.Resources.CpuRtRuntime = *r.CPU.RealtimeRuntime
|
2017-03-15 00:36:38 +08:00
|
|
|
config.Cgroups.Resources.CpusetCpus = r.CPU.Cpus
|
|
|
|
config.Cgroups.Resources.CpusetMems = r.CPU.Mems
|
2017-03-20 18:51:39 +08:00
|
|
|
config.Cgroups.Resources.KernelMemory = *r.Memory.Kernel
|
|
|
|
config.Cgroups.Resources.KernelMemoryTCP = *r.Memory.KernelTCP
|
|
|
|
config.Cgroups.Resources.Memory = *r.Memory.Limit
|
|
|
|
config.Cgroups.Resources.MemoryReservation = *r.Memory.Reservation
|
|
|
|
config.Cgroups.Resources.MemorySwap = *r.Memory.Swap
|
2017-04-27 00:53:20 +08:00
|
|
|
config.Cgroups.Resources.PidsLimit = r.Pids.Limit
|
2016-04-28 22:30:50 +08:00
|
|
|
|
2018-10-16 12:37:50 +08:00
|
|
|
// Update Intel RDT
|
|
|
|
l3CacheSchema := context.String("l3-cache-schema")
|
|
|
|
memBwSchema := context.String("mem-bw-schema")
|
|
|
|
if l3CacheSchema != "" && !intelrdt.IsCatEnabled() {
|
2020-05-17 08:20:44 +08:00
|
|
|
return errors.New("Intel RDT/CAT: l3 cache schema is not enabled")
|
2018-10-16 12:37:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if memBwSchema != "" && !intelrdt.IsMbaEnabled() {
|
2020-05-17 08:20:44 +08:00
|
|
|
return errors.New("Intel RDT/MBA: memory bandwidth schema is not enabled")
|
2018-10-16 12:37:50 +08:00
|
|
|
}
|
2017-09-20 01:33:35 +08:00
|
|
|
|
2018-10-16 12:37:50 +08:00
|
|
|
if l3CacheSchema != "" || memBwSchema != "" {
|
2017-09-20 01:33:35 +08:00
|
|
|
// If intelRdt is not specified in original configuration, we just don't
|
|
|
|
// Apply() to create intelRdt group or attach tasks for this container.
|
|
|
|
// In update command, we could re-enable through IntelRdtManager.Apply()
|
|
|
|
// and then update intelrdt constraint.
|
|
|
|
if config.IntelRdt == nil {
|
|
|
|
state, err := container.State()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
config.IntelRdt = &configs.IntelRdt{}
|
|
|
|
intelRdtManager := intelrdt.IntelRdtManager{
|
|
|
|
Config: &config,
|
|
|
|
Id: container.ID(),
|
|
|
|
Path: state.IntelRdtPath,
|
|
|
|
}
|
|
|
|
if err := intelRdtManager.Apply(state.InitProcessPid); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2018-10-16 12:37:50 +08:00
|
|
|
config.IntelRdt.L3CacheSchema = l3CacheSchema
|
|
|
|
config.IntelRdt.MemBwSchema = memBwSchema
|
2017-09-20 01:33:35 +08:00
|
|
|
}
|
|
|
|
|
2017-01-07 07:57:31 +08:00
|
|
|
return container.Set(config)
|
2016-04-28 22:30:50 +08:00
|
|
|
},
|
|
|
|
}
|