Browse code

implement "-c" option to allocate a number of CPU shares to a container

Jérôme Petazzoni authored on 2013/05/08 03:16:30
Showing 7 changed files
... ...
@@ -413,6 +413,7 @@ func TestAttachDisconnect(t *testing.T) {
413 413
 	container, err := NewBuilder(runtime).Create(
414 414
 		&Config{
415 415
 			Image:     GetTestImage(runtime).Id,
416
+			CpuShares: 1024,
416 417
 			Memory:    33554432,
417 418
 			Cmd:       []string{"/bin/cat"},
418 419
 			OpenStdin: true,
... ...
@@ -56,6 +56,7 @@ type Config struct {
56 56
 	User         string
57 57
 	Memory       int64 // Memory limit (in bytes)
58 58
 	MemorySwap   int64 // Total memory usage (memory + swap); set `-1' to disable swap
59
+	CpuShares    int64 // CPU shares (relative weight vs. other containers)
59 60
 	AttachStdin  bool
60 61
 	AttachStdout bool
61 62
 	AttachStderr bool
... ...
@@ -91,6 +92,8 @@ func ParseRun(args []string, stdout io.Writer, capabilities *Capabilities) (*Con
91 91
 		*flMemory = 0
92 92
 	}
93 93
 
94
+	flCpuShares := cmd.Int64("c", 1024, "CPU shares (relative weight)")
95
+
94 96
 	var flPorts ListOpts
95 97
 	cmd.Var(&flPorts, "p", "Expose a container's port to the host (use 'docker port' to see the actual mapping)")
96 98
 
... ...
@@ -137,6 +140,7 @@ func ParseRun(args []string, stdout io.Writer, capabilities *Capabilities) (*Con
137 137
 		Tty:          *flTty,
138 138
 		OpenStdin:    *flStdin,
139 139
 		Memory:       *flMemory,
140
+		CpuShares:    *flCpuShares,
140 141
 		AttachStdin:  flAttach.Get("stdin"),
141 142
 		AttachStdout: flAttach.Get("stdout"),
142 143
 		AttachStderr: flAttach.Get("stderr"),
... ...
@@ -390,6 +390,7 @@ func TestStart(t *testing.T) {
390 390
 		&Config{
391 391
 			Image:     GetTestImage(runtime).Id,
392 392
 			Memory:    33554432,
393
+			CpuShares: 1000,
393 394
 			Cmd:       []string{"/bin/cat"},
394 395
 			OpenStdin: true,
395 396
 		},
... ...
@@ -1059,12 +1060,17 @@ func TestLXCConfig(t *testing.T) {
1059 1059
 	memMin := 33554432
1060 1060
 	memMax := 536870912
1061 1061
 	mem := memMin + rand.Intn(memMax-memMin)
1062
+	// CPU shares as well
1063
+	cpuMin := 100
1064
+	cpuMax := 10000
1065
+	cpu := cpuMin + rand.Intn(cpuMax-cpuMin)
1062 1066
 	container, err := NewBuilder(runtime).Create(&Config{
1063 1067
 		Image: GetTestImage(runtime).Id,
1064 1068
 		Cmd:   []string{"/bin/true"},
1065 1069
 
1066
-		Hostname: "foobar",
1067
-		Memory:   int64(mem),
1070
+		Hostname:  "foobar",
1071
+		Memory:    int64(mem),
1072
+		CpuShares: int64(cpu),
1068 1073
 	},
1069 1074
 	)
1070 1075
 	if err != nil {
... ...
@@ -16,6 +16,7 @@ Full -run example::
16 16
 
17 17
     {"Hostname": "",
18 18
      "User": "",
19
+     "CpuShares": 0,
19 20
      "Memory": 0,
20 21
      "MemorySwap": 0,
21 22
      "PortSpecs": ["22", "80", "443"],
... ...
@@ -9,6 +9,7 @@
9 9
     Run a command in a new container
10 10
 
11 11
       -a=map[]: Attach to stdin, stdout or stderr.
12
+      -c=1024: CPU shares (relative weight)
12 13
       -d=false: Detached mode: leave the container running in the background
13 14
       -e=[]: Set environment variables
14 15
       -h="": Container host name
... ...
@@ -96,6 +96,9 @@ lxc.cgroup.memory.soft_limit_in_bytes = {{.Config.Memory}}
96 96
 lxc.cgroup.memory.memsw.limit_in_bytes = {{$memSwap}}
97 97
 {{end}}
98 98
 {{end}}
99
+{{if .Config.CpuShares}}
100
+lxc.cgroup.cpu.shares = {{.Config.CpuShares}}
101
+{{end}}
99 102
 `
100 103
 
101 104
 var LxcTemplateCompiled *template.Template
... ...
@@ -486,6 +486,7 @@ func CompareConfig(a, b *Config) bool {
486 486
 		a.User != b.User ||
487 487
 		a.Memory != b.Memory ||
488 488
 		a.MemorySwap != b.MemorySwap ||
489
+		a.CpuShares != b.CpuShares ||
489 490
 		a.OpenStdin != b.OpenStdin ||
490 491
 		a.Tty != b.Tty {
491 492
 		return false