From efd9becb78c82ddef07efb7e76e0100d7a712281 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Petazzoni?= Date: Tue, 7 May 2013 11:16:30 -0700 Subject: [PATCH 1/4] implement "-c" option to allocate a number of CPU shares to a container --- commands_test.go | 1 + container.go | 4 ++++ container_test.go | 10 ++++++++-- docs/sources/commandline/command/commit.rst | 1 + docs/sources/commandline/command/run.rst | 1 + lxc_template.go | 3 +++ utils.go | 1 + 7 files changed, 19 insertions(+), 2 deletions(-) diff --git a/commands_test.go b/commands_test.go index 307f90bf7f..6566bbec5d 100644 --- a/commands_test.go +++ b/commands_test.go @@ -413,6 +413,7 @@ func TestAttachDisconnect(t *testing.T) { container, err := NewBuilder(runtime).Create( &Config{ Image: GetTestImage(runtime).Id, + CpuShares: 1024, Memory: 33554432, Cmd: []string{"/bin/cat"}, OpenStdin: true, diff --git a/container.go b/container.go index a4bee6a6fe..1cf62c07e3 100644 --- a/container.go +++ b/container.go @@ -56,6 +56,7 @@ type Config struct { User string Memory int64 // Memory limit (in bytes) MemorySwap int64 // Total memory usage (memory + swap); set `-1' to disable swap + CpuShares int64 // CPU shares (relative weight vs. other containers) AttachStdin bool AttachStdout bool AttachStderr bool @@ -91,6 +92,8 @@ func ParseRun(args []string, stdout io.Writer, capabilities *Capabilities) (*Con *flMemory = 0 } + flCpuShares := cmd.Int64("c", 1024, "CPU shares (relative weight)") + var flPorts ListOpts cmd.Var(&flPorts, "p", "Expose a container's port to the host (use 'docker port' to see the actual mapping)") @@ -137,6 +140,7 @@ func ParseRun(args []string, stdout io.Writer, capabilities *Capabilities) (*Con Tty: *flTty, OpenStdin: *flStdin, Memory: *flMemory, + CpuShares: *flCpuShares, AttachStdin: flAttach.Get("stdin"), AttachStdout: flAttach.Get("stdout"), AttachStderr: flAttach.Get("stderr"), diff --git a/container_test.go b/container_test.go index 5b63b2a0e7..35fa82e659 100644 --- a/container_test.go +++ b/container_test.go @@ -390,6 +390,7 @@ func TestStart(t *testing.T) { &Config{ Image: GetTestImage(runtime).Id, Memory: 33554432, + CpuShares: 1000, Cmd: []string{"/bin/cat"}, OpenStdin: true, }, @@ -1059,12 +1060,17 @@ func TestLXCConfig(t *testing.T) { memMin := 33554432 memMax := 536870912 mem := memMin + rand.Intn(memMax-memMin) + // CPU shares as well + cpuMin := 100 + cpuMax := 10000 + cpu := cpuMin + rand.Intn(cpuMax-cpuMin) container, err := NewBuilder(runtime).Create(&Config{ Image: GetTestImage(runtime).Id, Cmd: []string{"/bin/true"}, - Hostname: "foobar", - Memory: int64(mem), + Hostname: "foobar", + Memory: int64(mem), + CpuShares: int64(cpu), }, ) if err != nil { diff --git a/docs/sources/commandline/command/commit.rst b/docs/sources/commandline/command/commit.rst index c73f8d1898..1d5c503414 100644 --- a/docs/sources/commandline/command/commit.rst +++ b/docs/sources/commandline/command/commit.rst @@ -16,6 +16,7 @@ Full -run example:: {"Hostname": "", "User": "", + "CpuShares": 0, "Memory": 0, "MemorySwap": 0, "PortSpecs": ["22", "80", "443"], diff --git a/docs/sources/commandline/command/run.rst b/docs/sources/commandline/command/run.rst index d5e571b41b..ec46039851 100644 --- a/docs/sources/commandline/command/run.rst +++ b/docs/sources/commandline/command/run.rst @@ -9,6 +9,7 @@ Run a command in a new container -a=map[]: Attach to stdin, stdout or stderr. + -c=1024: CPU shares (relative weight) -d=false: Detached mode: leave the container running in the background -e=[]: Set environment variables -h="": Container host name diff --git a/lxc_template.go b/lxc_template.go index e2be3f21cd..008a717bad 100644 --- a/lxc_template.go +++ b/lxc_template.go @@ -96,6 +96,9 @@ lxc.cgroup.memory.soft_limit_in_bytes = {{.Config.Memory}} lxc.cgroup.memory.memsw.limit_in_bytes = {{$memSwap}} {{end}} {{end}} +{{if .Config.CpuShares}} +lxc.cgroup.cpu.shares = {{.Config.CpuShares}} +{{end}} ` var LxcTemplateCompiled *template.Template diff --git a/utils.go b/utils.go index 2d54ef4c6a..832d89441b 100644 --- a/utils.go +++ b/utils.go @@ -486,6 +486,7 @@ func CompareConfig(a, b *Config) bool { a.User != b.User || a.Memory != b.Memory || a.MemorySwap != b.MemorySwap || + a.CpuShares != b.CpuShares || a.OpenStdin != b.OpenStdin || a.Tty != b.Tty { return false From e36752e03347bf46177d8ee810cdd5a019321dc9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Petazzoni?= Date: Tue, 7 May 2013 11:43:45 -0700 Subject: [PATCH 2/4] if -c is not specified, do not set cpu.shares (instead of using the default value of 1024) --- container.go | 2 +- docs/sources/commandline/command/run.rst | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/container.go b/container.go index 1cf62c07e3..9315d3347a 100644 --- a/container.go +++ b/container.go @@ -92,7 +92,7 @@ func ParseRun(args []string, stdout io.Writer, capabilities *Capabilities) (*Con *flMemory = 0 } - flCpuShares := cmd.Int64("c", 1024, "CPU shares (relative weight)") + flCpuShares := cmd.Int64("c", 0, "CPU shares (relative weight)") var flPorts ListOpts cmd.Var(&flPorts, "p", "Expose a container's port to the host (use 'docker port' to see the actual mapping)") diff --git a/docs/sources/commandline/command/run.rst b/docs/sources/commandline/command/run.rst index ec46039851..95fb208dd3 100644 --- a/docs/sources/commandline/command/run.rst +++ b/docs/sources/commandline/command/run.rst @@ -9,7 +9,7 @@ Run a command in a new container -a=map[]: Attach to stdin, stdout or stderr. - -c=1024: CPU shares (relative weight) + -c=0: CPU shares (relative weight) -d=false: Detached mode: leave the container running in the background -e=[]: Set environment variables -h="": Container host name From af9f559f2ebd6438f37fef890f2a25eca590f252 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Petazzoni?= Date: Tue, 7 May 2013 11:44:24 -0700 Subject: [PATCH 3/4] in the tests, use a non-default value for cpu.shares --- commands_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/commands_test.go b/commands_test.go index 6566bbec5d..999a241ce7 100644 --- a/commands_test.go +++ b/commands_test.go @@ -413,7 +413,7 @@ func TestAttachDisconnect(t *testing.T) { container, err := NewBuilder(runtime).Create( &Config{ Image: GetTestImage(runtime).Id, - CpuShares: 1024, + CpuShares: 1000, Memory: 33554432, Cmd: []string{"/bin/cat"}, OpenStdin: true, From eef8b0d406412ffe6622ed1f2c858540eb7f75f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Petazzoni?= Date: Tue, 7 May 2013 11:44:38 -0700 Subject: [PATCH 4/4] propagate CpuShares in mergeConfig --- builder.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/builder.go b/builder.go index e835c6d390..b22c853730 100644 --- a/builder.go +++ b/builder.go @@ -38,6 +38,9 @@ func (builder *Builder) mergeConfig(userConf, imageConf *Config) { if userConf.MemorySwap == 0 { userConf.MemorySwap = imageConf.MemorySwap } + if userConf.CpuShares == 0 { + userConf.CpuShares = imageConf.CpuShares + } if userConf.PortSpecs == nil || len(userConf.PortSpecs) == 0 { userConf.PortSpecs = imageConf.PortSpecs }