From 4190e5a920e365ee899faf7c9ed854950eda144a Mon Sep 17 00:00:00 2001 From: Kenfe-Mickael Laventure Date: Thu, 28 Apr 2016 07:30:50 -0700 Subject: [PATCH] Add new `update` command to runc. This command allow users to update some of a container cgroups parameters. Signed-off-by: Kenfe-Mickael Laventure --- main.go | 5 +- tests/integration/update.bats | 205 ++++++++++++++++++++++++++++++++++ update.go | 186 ++++++++++++++++++++++++++++++ 3 files changed, 394 insertions(+), 2 deletions(-) create mode 100644 tests/integration/update.bats create mode 100644 update.go diff --git a/main.go b/main.go index 51b346488..1ceac4468 100644 --- a/main.go +++ b/main.go @@ -18,7 +18,7 @@ const ( version = "0.1.1" specConfig = "config.json" usage = `Open Container Initiative runtime - + runc is a command line client for running applications packaged according to the Open Container Initiative (OCI) format and is a compliant implementation of the Open Container Initiative specification. @@ -30,7 +30,7 @@ direct child of the process supervisor. Containers are configured using bundles. A bundle for a container is a directory that includes a specification file named "` + specConfig + `" and a root filesystem. -The root filesystem contains the contents of the container. +The root filesystem contains the contents of the container. To start a new instance of a container: @@ -99,6 +99,7 @@ func main() { specCommand, startCommand, stateCommand, + updateCommand, } app.Before = func(context *cli.Context) error { if context.GlobalBool("debug") { diff --git a/tests/integration/update.bats b/tests/integration/update.bats new file mode 100644 index 000000000..9bda96fcc --- /dev/null +++ b/tests/integration/update.bats @@ -0,0 +1,205 @@ +#!/usr/bin/env bats + +load helpers + +UPDATE_TEST_RUNC_ROOT="$BATS_TMPDIR/runc-update-integration-test" + +CGROUP_MEMORY="" +CGROUP_CPUSET="" +CGROUP_CPU="" +CGROUP_BLKIO="" + +function init_cgroup_path() { + for g in MEMORY CPUSET CPU BLKIO; do + base_path=$(grep "rw," /proc/self/mountinfo | grep -i -m 1 "$g\$" | cut -d ' ' -f 5) + eval CGROUP_${g}="${base_path}/runc-update-integration-test" + done +} + +function teardown() { + rm -f $BATS_TMPDIR/runc-update-integration-test.json + teardown_running_container_inroot test_update $UPDATE_TEST_RUNC_ROOT + teardown_busybox +} + +function setup() { + teardown + setup_busybox + + # Add cgroup path + sed -i 's/\("linux": {\)/\1\n "cgroupsPath": "runc-update-integration-test",/' ${BUSYBOX_BUNDLE}/config.json + + # Set some initial known values + DATA=$(cat < $BATS_TMPDIR/runc-update-integration-test.json + + run "$RUNC" --root $UPDATE_TEST_RUNC_ROOT update -r $BATS_TMPDIR/runc-update-integration-test.json test_update + [ "$status" -eq 0 ] + check_cgroup_value $CGROUP_BLKIO "blkio.weight" 1000 + check_cgroup_value $CGROUP_CPU "cpu.cfs_period_us" 1000000 + check_cgroup_value $CGROUP_CPU "cpu.cfs_quota_us" 500000 + check_cgroup_value $CGROUP_CPU "cpu.shares" 100 + check_cgroup_value $CGROUP_CPUSET "cpuset.cpus" 0 + check_cgroup_value $CGROUP_MEMORY "memory.kmem.limit_in_bytes" 16777216 + check_cgroup_value $CGROUP_MEMORY "memory.limit_in_bytes" 33554432 + check_cgroup_value $CGROUP_MEMORY "memory.soft_limit_in_bytes" 25165824 +} diff --git a/update.go b/update.go new file mode 100644 index 000000000..bbfc97c43 --- /dev/null +++ b/update.go @@ -0,0 +1,186 @@ +// +build linux + +package main + +import ( + "encoding/json" + "fmt" + "os" + "strconv" + + "github.com/codegangsta/cli" + "github.com/opencontainers/runtime-spec/specs-go" +) + +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", + ArgsUsage: ` `, + Flags: []cli.Flag{ + cli.StringFlag{ + Name: "resources, r", + Value: "", + Usage: `path to the file containing the resources to update or '-' to read from the standard input. + +The accepted format is as follow (unchanged values can be omitted): + +{ + "memory": { + "limit": 0, + "reservation": 0, + "swap": 0, + "kernel": 0 + }, + "cpu": { + "shares": 0, + "quota": 0, + "period": 0, + "cpus": "", + "mems": "" + }, + "blockIO": { + "blkioWeight": 0 + }, +} + +Note: if data is to be read from a file or the standard output all +other options are ignored. +`, + }, + + cli.IntFlag{ + Name: "blkio-weight", + Usage: "Specifies per cgroup weight, range is from 10 to 1000.", + }, + cli.StringFlag{ + Name: "cpu-period", + Usage: "CPU period to be used for hardcapping (in usecs). 0 to use system default.", + }, + cli.StringFlag{ + Name: "cpu-quota", + Usage: "CPU hardcap limit (in usecs). Allowed cpu time in a given period.", + }, + cli.StringFlag{ + Name: "cpu-share", + Usage: "CPU shares (relative weight vs. other containers)", + }, + 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", + 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", + Usage: "Total memory usage (memory + swap); set `-1` to enable unlimited swap", + }, + }, + Action: func(context *cli.Context) { + container, err := getContainer(context) + if err != nil { + fatal(err) + } + + r := specs.Resources{ + Memory: &specs.Memory{ + Limit: u64Ptr(0), + Reservation: u64Ptr(0), + Swap: u64Ptr(0), + Kernel: u64Ptr(0), + }, + CPU: &specs.CPU{ + Shares: u64Ptr(0), + Quota: u64Ptr(0), + Period: u64Ptr(0), + Cpus: sPtr(""), + Mems: sPtr(""), + }, + BlockIO: &specs.BlockIO{ + Weight: u16Ptr(0), + }, + } + + 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 { + fatal(err) + } + } + err = json.NewDecoder(f).Decode(&r) + if err != nil { + fatal(err) + } + } else { + if val := context.Int("blkio-weight"); val != 0 { + r.BlockIO.Weight = u16Ptr(uint16(val)) + } + if val := context.String("cpuset-cpus"); val != "" { + r.CPU.Cpus = &val + } + if val := context.String("cpuset-mems"); val != "" { + r.CPU.Mems = &val + } + + for opt, dest := range map[string]*uint64{ + "cpu-period": r.CPU.Period, + "cpu-quota": r.CPU.Quota, + "cpu-share": r.CPU.Shares, + "kernel-memory": r.Memory.Kernel, + "memory": r.Memory.Limit, + "memory-reservation": r.Memory.Reservation, + "memory-swap": r.Memory.Swap, + } { + if val := context.String(opt); val != "" { + var err error + *dest, err = strconv.ParseUint(val, 10, 64) + if err != nil { + fatal(fmt.Errorf("invalid value for %s: %s", opt, err)) + } + } + } + } + + // Update the value + config.Cgroups.Resources.BlkioWeight = *r.BlockIO.Weight + config.Cgroups.Resources.CpuPeriod = int64(*r.CPU.Period) + config.Cgroups.Resources.CpuQuota = int64(*r.CPU.Quota) + config.Cgroups.Resources.CpuShares = int64(*r.CPU.Shares) + config.Cgroups.Resources.CpusetCpus = *r.CPU.Cpus + config.Cgroups.Resources.CpusetMems = *r.CPU.Mems + config.Cgroups.Resources.KernelMemory = int64(*r.Memory.Kernel) + config.Cgroups.Resources.Memory = int64(*r.Memory.Limit) + config.Cgroups.Resources.MemoryReservation = int64(*r.Memory.Reservation) + config.Cgroups.Resources.MemorySwap = int64(*r.Memory.Swap) + + if err := container.Set(config); err != nil { + fatal(err) + } + }, +}