Browse code

Add cpu cfs quota to build

Signed-off-by: Lei Jitang <leijitang@huawei.com>

Lei Jitang authored on 2015/04/24 09:54:08
Showing 8 changed files
... ...
@@ -55,6 +55,7 @@ func (cli *DockerCli) CmdBuild(args ...string) error {
55 55
 	flMemoryString := cmd.String([]string{"m", "-memory"}, "", "Memory limit")
56 56
 	flMemorySwap := cmd.String([]string{"-memory-swap"}, "", "Total memory (memory + swap), '-1' to disable swap")
57 57
 	flCPUShares := cmd.Int64([]string{"c", "-cpu-shares"}, 0, "CPU shares (relative weight)")
58
+	flCpuQuota := cmd.Int64([]string{"-cpu-quota"}, 0, "Limit the CPU CFS (Completely Fair Scheduler) quota")
58 59
 	flCPUSetCpus := cmd.String([]string{"-cpuset-cpus"}, "", "CPUs in which to allow execution (0-3, 0,1)")
59 60
 	flCPUSetMems := cmd.String([]string{"-cpuset-mems"}, "", "MEMs in which to allow execution (0-3, 0,1)")
60 61
 
... ...
@@ -281,6 +282,7 @@ func (cli *DockerCli) CmdBuild(args ...string) error {
281 281
 	v.Set("cpusetcpus", *flCPUSetCpus)
282 282
 	v.Set("cpusetmems", *flCPUSetMems)
283 283
 	v.Set("cpushares", strconv.FormatInt(*flCPUShares, 10))
284
+	v.Set("cpuquota", strconv.FormatInt(*flCpuQuota, 10))
284 285
 	v.Set("memory", strconv.FormatInt(memory, 10))
285 286
 	v.Set("memswap", strconv.FormatInt(memorySwap, 10))
286 287
 
... ...
@@ -1343,6 +1343,7 @@ func (s *Server) postBuild(eng *engine.Engine, version version.Version, w http.R
1343 1343
 	buildConfig.MemorySwap = int64Value(r, "memswap")
1344 1344
 	buildConfig.Memory = int64Value(r, "memory")
1345 1345
 	buildConfig.CpuShares = int64Value(r, "cpushares")
1346
+	buildConfig.CpuQuota = int64Value(r, "cpuquota")
1346 1347
 	buildConfig.CpuSetCpus = r.FormValue("cpusetcpus")
1347 1348
 	buildConfig.CpuSetMems = r.FormValue("cpusetmems")
1348 1349
 
... ...
@@ -124,6 +124,7 @@ type Builder struct {
124 124
 	cpuSetCpus string
125 125
 	cpuSetMems string
126 126
 	cpuShares  int64
127
+	cpuQuota   int64
127 128
 	memory     int64
128 129
 	memorySwap int64
129 130
 
... ...
@@ -547,6 +547,7 @@ func (b *Builder) create() (*daemon.Container, error) {
547 547
 
548 548
 	hostConfig := &runconfig.HostConfig{
549 549
 		CpuShares:  b.cpuShares,
550
+		CpuQuota:   b.cpuQuota,
550 551
 		CpusetCpus: b.cpuSetCpus,
551 552
 		CpusetMems: b.cpuSetMems,
552 553
 		Memory:     b.memory,
... ...
@@ -49,6 +49,7 @@ type Config struct {
49 49
 	Memory         int64
50 50
 	MemorySwap     int64
51 51
 	CpuShares      int64
52
+	CpuQuota       int64
52 53
 	CpuSetCpus     string
53 54
 	CpuSetMems     string
54 55
 	AuthConfig     *cliconfig.AuthConfig
... ...
@@ -169,6 +170,7 @@ func Build(d *daemon.Daemon, buildConfig *Config) error {
169 169
 		ConfigFile:      buildConfig.ConfigFile,
170 170
 		dockerfileName:  buildConfig.DockerfileName,
171 171
 		cpuShares:       buildConfig.CpuShares,
172
+		cpuQuota:        buildConfig.CpuQuota,
172 173
 		cpuSetCpus:      buildConfig.CpuSetCpus,
173 174
 		cpuSetMems:      buildConfig.CpuSetMems,
174 175
 		memory:          buildConfig.Memory,
... ...
@@ -279,7 +279,7 @@ _docker_build() {
279 279
 
280 280
 	case "$cur" in
281 281
 		-*)
282
-			COMPREPLY=( $( compgen -W "--cpu-shares -c --cpuset-cpus --file -f --force-rm --help --memory -m --memory-swap --no-cache --pull --quiet -q --rm --tag -t" -- "$cur" ) )
282
+			COMPREPLY=( $( compgen -W "--cpu-shares -c --cpuset-cpus --cpu-quota --file -f --force-rm --help --memory -m --memory-swap --no-cache --pull --quiet -q --rm --tag -t" -- "$cur" ) )
283 283
 			;;
284 284
 		*)
285 285
 			local counter="$(__docker_pos_first_nonflag '--tag|-t')"
... ...
@@ -17,6 +17,7 @@ docker-build - Build a new image from the source code at PATH
17 17
 [**-m**|**--memory**[=*MEMORY*]]
18 18
 [**--memory-swap**[=*MEMORY-SWAP*]]
19 19
 [**-c**|**--cpu-shares**[=*0*]]
20
+[**--cpu-quota**[=*0*]]
20 21
 [**--cpuset-cpus**[=*CPUSET-CPUS*]]
21 22
 
22 23
 PATH | URL | -
... ...
@@ -5371,7 +5371,7 @@ func (s *DockerSuite) TestBuildResourceConstraintsAreUsed(c *check.C) {
5371 5371
 		c.Fatal(err)
5372 5372
 	}
5373 5373
 
5374
-	cmd := exec.Command(dockerBinary, "build", "--no-cache", "--rm=false", "--memory=64m", "--memory-swap=-1", "--cpuset-cpus=0", "--cpuset-mems=0", "--cpu-shares=100", "-t", name, ".")
5374
+	cmd := exec.Command(dockerBinary, "build", "--no-cache", "--rm=false", "--memory=64m", "--memory-swap=-1", "--cpuset-cpus=0", "--cpuset-mems=0", "--cpu-shares=100", "--cpu-quota=8000", "-t", name, ".")
5375 5375
 	cmd.Dir = ctx.Dir
5376 5376
 
5377 5377
 	out, _, err := runCommandWithOutput(cmd)
... ...
@@ -5388,6 +5388,7 @@ func (s *DockerSuite) TestBuildResourceConstraintsAreUsed(c *check.C) {
5388 5388
 		CpusetCpus string
5389 5389
 		CpusetMems string
5390 5390
 		CpuShares  int64
5391
+		CpuQuota   int64
5391 5392
 	}
5392 5393
 
5393 5394
 	cfg, err := inspectFieldJSON(cID, "HostConfig")
... ...
@@ -5399,9 +5400,9 @@ func (s *DockerSuite) TestBuildResourceConstraintsAreUsed(c *check.C) {
5399 5399
 	if err := json.Unmarshal([]byte(cfg), &c1); err != nil {
5400 5400
 		c.Fatal(err, cfg)
5401 5401
 	}
5402
-	if c1.Memory != 67108864 || c1.MemorySwap != -1 || c1.CpusetCpus != "0" || c1.CpusetMems != "0" || c1.CpuShares != 100 {
5403
-		c.Fatalf("resource constraints not set properly:\nMemory: %d, MemSwap: %d, CpusetCpus: %s, CpusetMems: %s, CpuShares: %d",
5404
-			c1.Memory, c1.MemorySwap, c1.CpusetCpus, c1.CpusetMems, c1.CpuShares)
5402
+	if c1.Memory != 67108864 || c1.MemorySwap != -1 || c1.CpusetCpus != "0" || c1.CpusetMems != "0" || c1.CpuShares != 100 || c1.CpuQuota != 8000 {
5403
+		c.Fatalf("resource constraints not set properly:\nMemory: %d, MemSwap: %d, CpusetCpus: %s, CpusetMems: %s, CpuShares: %d, CpuQuota: %d",
5404
+			c1.Memory, c1.MemorySwap, c1.CpusetCpus, c1.CpusetMems, c1.CpuShares, c1.CpuQuota)
5405 5405
 	}
5406 5406
 
5407 5407
 	// Make sure constraints aren't saved to image
... ...
@@ -5415,9 +5416,9 @@ func (s *DockerSuite) TestBuildResourceConstraintsAreUsed(c *check.C) {
5415 5415
 	if err := json.Unmarshal([]byte(cfg), &c2); err != nil {
5416 5416
 		c.Fatal(err, cfg)
5417 5417
 	}
5418
-	if c2.Memory == 67108864 || c2.MemorySwap == -1 || c2.CpusetCpus == "0" || c2.CpusetMems == "0" || c2.CpuShares == 100 {
5419
-		c.Fatalf("resource constraints leaked from build:\nMemory: %d, MemSwap: %d, CpusetCpus: %s, CpusetMems: %s, CpuShares: %d",
5420
-			c2.Memory, c2.MemorySwap, c2.CpusetCpus, c2.CpusetMems, c2.CpuShares)
5418
+	if c2.Memory == 67108864 || c2.MemorySwap == -1 || c2.CpusetCpus == "0" || c2.CpusetMems == "0" || c2.CpuShares == 100 || c2.CpuQuota == 8000 {
5419
+		c.Fatalf("resource constraints leaked from build:\nMemory: %d, MemSwap: %d, CpusetCpus: %s, CpusetMems: %s, CpuShares: %d, CpuQuota: %d",
5420
+			c2.Memory, c2.MemorySwap, c2.CpusetCpus, c2.CpusetMems, c2.CpuShares, c2.CpuQuota)
5421 5421
 	}
5422 5422
 
5423 5423
 }