Browse code

Add cpuset cpus support for docker Docker-DCO-1.1-Signed-off-by: Michael Crosby <michael@crosbymichael.com> (github: crosbymichael)

Michael Crosby authored on 2014/05/13 09:44:57
Showing 8 changed files
... ...
@@ -215,6 +215,7 @@ func populateCommand(c *Container, env []string) error {
215 215
 		Memory:     c.Config.Memory,
216 216
 		MemorySwap: c.Config.MemorySwap,
217 217
 		CpuShares:  c.Config.CpuShares,
218
+		Cpuset:     c.Config.Cpuset,
218 219
 	}
219 220
 	c.command = &execdriver.Command{
220 221
 		ID:         c.ID,
... ...
@@ -103,9 +103,10 @@ type NetworkInterface struct {
103 103
 }
104 104
 
105 105
 type Resources struct {
106
-	Memory     int64 `json:"memory"`
107
-	MemorySwap int64 `json:"memory_swap"`
108
-	CpuShares  int64 `json:"cpu_shares"`
106
+	Memory     int64  `json:"memory"`
107
+	MemorySwap int64  `json:"memory_swap"`
108
+	CpuShares  int64  `json:"cpu_shares"`
109
+	Cpuset     string `json:"cpuset"`
109 110
 }
110 111
 
111 112
 type Mount struct {
... ...
@@ -127,6 +127,9 @@ lxc.cgroup.memory.memsw.limit_in_bytes = {{$memSwap}}
127 127
 {{if .Resources.CpuShares}}
128 128
 lxc.cgroup.cpu.shares = {{.Resources.CpuShares}}
129 129
 {{end}}
130
+{{if .Resources.Cpuset}}
131
+lxc.cgroup.cpuset.cpus = {{.Resources.Cpuset}}
132
+{{end}}
130 133
 {{end}}
131 134
 
132 135
 {{if .Config.lxc}}
... ...
@@ -117,6 +117,7 @@ func (d *driver) setupCgroups(container *libcontainer.Container, c *execdriver.C
117 117
 		container.Cgroups.Memory = c.Resources.Memory
118 118
 		container.Cgroups.MemoryReservation = c.Resources.Memory
119 119
 		container.Cgroups.MemorySwap = c.Resources.MemorySwap
120
+		container.Cgroups.CpusetCpus = c.Resources.Cpuset
120 121
 	}
121 122
 	return nil
122 123
 }
... ...
@@ -811,6 +811,7 @@ Run a command in a new container
811 811
 
812 812
     -a, --attach=[]            Attach to stdin, stdout or stderr.
813 813
     -c, --cpu-shares=0         CPU shares (relative weight)
814
+    --cpuset=""                CPUs in which to allow execution (0-3, 0,1)
814 815
     --cidfile=""               Write the container ID to the file
815 816
     -d, --detach=false         Detached mode: Run container in the background, print new container id
816 817
     --dns=[]                   Set custom dns servers
... ...
@@ -768,3 +768,14 @@ func TestProcWritableInPrivilegedContainers(t *testing.T) {
768 768
 
769 769
 	logDone("run - proc writable in privileged container")
770 770
 }
771
+
772
+func TestRunWithCpuset(t *testing.T) {
773
+	cmd := exec.Command(dockerBinary, "run", "--cpuset", "0", "busybox", "true")
774
+	if code, err := runCommand(cmd); err != nil || code != 0 {
775
+		t.Fatalf("container should run successfuly with cpuset of 0: %s", err)
776
+	}
777
+
778
+	deleteAllContainers()
779
+
780
+	logDone("run - cpuset 0")
781
+}
... ...
@@ -12,9 +12,10 @@ type Config struct {
12 12
 	Hostname        string
13 13
 	Domainname      string
14 14
 	User            string
15
-	Memory          int64 // Memory limit (in bytes)
16
-	MemorySwap      int64 // Total memory usage (memory + swap); set `-1' to disable swap
17
-	CpuShares       int64 // CPU shares (relative weight vs. other containers)
15
+	Memory          int64  // Memory limit (in bytes)
16
+	MemorySwap      int64  // Total memory usage (memory + swap); set `-1' to disable swap
17
+	CpuShares       int64  // CPU shares (relative weight vs. other containers)
18
+	Cpuset          string // Cpuset 0-2, 0,1
18 19
 	AttachStdin     bool
19 20
 	AttachStdout    bool
20 21
 	AttachStderr    bool
... ...
@@ -41,6 +42,7 @@ func ContainerConfigFromJob(job *engine.Job) *Config {
41 41
 		Memory:          job.GetenvInt64("Memory"),
42 42
 		MemorySwap:      job.GetenvInt64("MemorySwap"),
43 43
 		CpuShares:       job.GetenvInt64("CpuShares"),
44
+		Cpuset:          job.Getenv("Cpuset"),
44 45
 		AttachStdin:     job.GetenvBool("AttachStdin"),
45 46
 		AttachStdout:    job.GetenvBool("AttachStdout"),
46 47
 		AttachStderr:    job.GetenvBool("AttachStderr"),
... ...
@@ -63,6 +63,7 @@ func parseRun(cmd *flag.FlagSet, args []string, sysInfo *sysinfo.SysInfo) (*Conf
63 63
 		flUser            = cmd.String([]string{"u", "-user"}, "", "Username or UID")
64 64
 		flWorkingDir      = cmd.String([]string{"w", "-workdir"}, "", "Working directory inside the container")
65 65
 		flCpuShares       = cmd.Int64([]string{"c", "-cpu-shares"}, 0, "CPU shares (relative weight)")
66
+		flCpuset          = cmd.String([]string{"-cpuset"}, "", "CPUs in which to allow execution (0-3, 0,1)")
66 67
 		flNetMode         = cmd.String([]string{"-net"}, "bridge", "Set the Network mode for the container\n'bridge': creates a new network stack for the container on the docker bridge\n'none': no networking for this container\n'container:<name|id>': reuses another container network stack\n'host': use the host network stack inside the contaner")
67 68
 		// For documentation purpose
68 69
 		_ = cmd.Bool([]string{"#sig-proxy", "-sig-proxy"}, true, "Proxify all received signal to the process (even in non-tty mode)")
... ...
@@ -219,6 +220,7 @@ func parseRun(cmd *flag.FlagSet, args []string, sysInfo *sysinfo.SysInfo) (*Conf
219 219
 		OpenStdin:       *flStdin,
220 220
 		Memory:          flMemory,
221 221
 		CpuShares:       *flCpuShares,
222
+		Cpuset:          *flCpuset,
222 223
 		AttachStdin:     flAttach.Get("stdin"),
223 224
 		AttachStdout:    flAttach.Get("stdout"),
224 225
 		AttachStderr:    flAttach.Get("stderr"),