Skip to content

Commit 837eec0

Browse files
committed
move resources from Config to HostConfig
Cgroup resources are host dependent, they should be in hostConfig. For backward compatibility, we just copy it to hostConfig, and leave it in Config for now, so there is no regressions, but the right way to use this throught json is to put it in HostConfig, like: { "Hostname": "", ... "HostConfig": { "CpuShares": 512, "Memory": 314572800, ... } } As we will add CpusetMems, CpusetCpus is definitely a better name, but some users are already using Cpuset in their http APIs, we also make it compatible. The main idea is keep using Cpuset in Config Struct, and make it has the same value as CpusetCpus, but not always, some scenarios: - Users use --cpuset in docker command, it can setup cpuset.cpus and can get Cpuset field from docker inspect or other http API which will get config info. - Users use --cpuset-cpus in docker command, ditto. - Users use Cpuset field in their http APIs, ditto. - Users use CpusetCpus field in their http APIs, they won't get Cpuset field in Config info, because by then, they should already know what happens to Cpuset. Signed-off-by: Qiang Huang <[email protected]>
1 parent 2772a47 commit 837eec0

12 files changed

Lines changed: 90 additions & 58 deletions

File tree

daemon/container.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -303,10 +303,10 @@ func populateCommand(c *Container, env []string) error {
303303
}
304304

305305
resources := &execdriver.Resources{
306-
Memory: c.Config.Memory,
307-
MemorySwap: c.Config.MemorySwap,
308-
CpuShares: c.Config.CpuShares,
309-
Cpuset: c.Config.Cpuset,
306+
Memory: c.hostConfig.Memory,
307+
MemorySwap: c.hostConfig.MemorySwap,
308+
CpuShares: c.hostConfig.CpuShares,
309+
CpusetCpus: c.hostConfig.CpusetCpus,
310310
Rlimits: rlimits,
311311
}
312312

daemon/create.go

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,33 +18,28 @@ func (daemon *Daemon) ContainerCreate(job *engine.Job) engine.Status {
1818
} else if len(job.Args) > 1 {
1919
return job.Errorf("Usage: %s", job.Name)
2020
}
21+
2122
config := runconfig.ContainerConfigFromJob(job)
22-
if config.Memory != 0 && config.Memory < 4194304 {
23+
hostConfig := runconfig.ContainerHostConfigFromJob(job)
24+
25+
if hostConfig.Memory != 0 && hostConfig.Memory < 4194304 {
2326
return job.Errorf("Minimum memory limit allowed is 4MB")
2427
}
25-
if config.Memory > 0 && !daemon.SystemConfig().MemoryLimit {
28+
if hostConfig.Memory > 0 && !daemon.SystemConfig().MemoryLimit {
2629
job.Errorf("Your kernel does not support memory limit capabilities. Limitation discarded.\n")
27-
config.Memory = 0
30+
hostConfig.Memory = 0
2831
}
29-
if config.Memory > 0 && !daemon.SystemConfig().SwapLimit {
32+
if hostConfig.Memory > 0 && !daemon.SystemConfig().SwapLimit {
3033
job.Errorf("Your kernel does not support swap limit capabilities. Limitation discarded.\n")
31-
config.MemorySwap = -1
34+
hostConfig.MemorySwap = -1
3235
}
33-
if config.Memory > 0 && config.MemorySwap > 0 && config.MemorySwap < config.Memory {
36+
if hostConfig.Memory > 0 && hostConfig.MemorySwap > 0 && hostConfig.MemorySwap < hostConfig.Memory {
3437
return job.Errorf("Minimum memoryswap limit should be larger than memory limit, see usage.\n")
3538
}
36-
if config.Memory == 0 && config.MemorySwap > 0 {
39+
if hostConfig.Memory == 0 && hostConfig.MemorySwap > 0 {
3740
return job.Errorf("You should always set the Memory limit when using Memoryswap limit, see usage.\n")
3841
}
3942

40-
var hostConfig *runconfig.HostConfig
41-
if job.EnvExists("HostConfig") {
42-
hostConfig = runconfig.ContainerHostConfigFromJob(job)
43-
} else {
44-
// Older versions of the API don't provide a HostConfig.
45-
hostConfig = nil
46-
}
47-
4843
container, buildWarnings, err := daemon.Create(config, hostConfig, name)
4944
if err != nil {
5045
if daemon.Graph().IsNotExist(err) {

daemon/execdriver/driver.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ type Resources struct {
109109
Memory int64 `json:"memory"`
110110
MemorySwap int64 `json:"memory_swap"`
111111
CpuShares int64 `json:"cpu_shares"`
112-
Cpuset string `json:"cpuset"`
112+
CpusetCpus string `json:"cpuset_cpus"`
113113
Rlimits []*ulimit.Rlimit `json:"rlimits"`
114114
}
115115

@@ -198,7 +198,7 @@ func SetupCgroups(container *configs.Config, c *Command) error {
198198
container.Cgroups.Memory = c.Resources.Memory
199199
container.Cgroups.MemoryReservation = c.Resources.Memory
200200
container.Cgroups.MemorySwap = c.Resources.MemorySwap
201-
container.Cgroups.CpusetCpus = c.Resources.Cpuset
201+
container.Cgroups.CpusetCpus = c.Resources.CpusetCpus
202202
}
203203

204204
return nil

daemon/execdriver/lxc/lxc_template.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,8 @@ lxc.cgroup.memory.memsw.limit_in_bytes = {{$memSwap}}
107107
{{if .Resources.CpuShares}}
108108
lxc.cgroup.cpu.shares = {{.Resources.CpuShares}}
109109
{{end}}
110-
{{if .Resources.Cpuset}}
111-
lxc.cgroup.cpuset.cpus = {{.Resources.Cpuset}}
110+
{{if .Resources.CpusetCpus}}
111+
lxc.cgroup.cpuset.cpus = {{.Resources.CpusetCpus}}
112112
{{end}}
113113
{{end}}
114114

docs/man/docker-create.1.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ docker-create - Create a new container
1212
[**--cap-add**[=*[]*]]
1313
[**--cap-drop**[=*[]*]]
1414
[**--cidfile**[=*CIDFILE*]]
15-
[**--cpuset**[=*CPUSET*]]
15+
[**--cpuset-cpus**[=*CPUSET-CPUS*]]
1616
[**--device**[=*[]*]]
1717
[**--dns-search**[=*[]*]]
1818
[**--dns**[=*[]*]]
@@ -64,7 +64,7 @@ IMAGE [COMMAND] [ARG...]
6464
**--cidfile**=""
6565
Write the container ID to the file
6666

67-
**--cpuset**=""
67+
**--cpuset-cpus**=""
6868
CPUs in which to allow execution (0-3, 0,1)
6969

7070
**--device**=[]

docs/man/docker-run.1.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ docker-run - Run a command in a new container
1212
[**--cap-add**[=*[]*]]
1313
[**--cap-drop**[=*[]*]]
1414
[**--cidfile**[=*CIDFILE*]]
15-
[**--cpuset**[=*CPUSET*]]
15+
[**--cpuset-cpus**[=*CPUSET-CPUS*]]
1616
[**-d**|**--detach**[=*false*]]
1717
[**--device**[=*[]*]]
1818
[**--dns-search**[=*[]*]]
@@ -124,7 +124,7 @@ division of CPU shares:
124124
**--cidfile**=""
125125
Write the container ID to the file
126126

127-
**--cpuset**=""
127+
**--cpuset-cpus**=""
128128
CPUs in which to allow execution (0-3, 0,1)
129129

130130
**-d**, **--detach**=*true*|*false*

docs/sources/reference/api/docker_remote_api_v1.18.md

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,6 @@ Create a container
113113
"Hostname": "",
114114
"Domainname": "",
115115
"User": "",
116-
"Memory": 0,
117-
"MemorySwap": 0,
118-
"CpuShares": 512,
119-
"Cpuset": "0,1",
120116
"AttachStdin": false,
121117
"AttachStdout": true,
122118
"AttachStderr": true,
@@ -143,6 +139,10 @@ Create a container
143139
"Binds": ["/tmp:/tmp"],
144140
"Links": ["redis3:redis"],
145141
"LxcConf": {"lxc.utsname":"docker"},
142+
"Memory": 0,
143+
"MemorySwap": 0,
144+
"CpuShares": 512,
145+
"CpusetCpus": "0,1",
146146
"PortBindings": { "22/tcp": [{ "HostPort": "11022" }] },
147147
"PublishAllPorts": false,
148148
"Privileged": false,
@@ -182,7 +182,8 @@ Json Parameters:
182182
always use this with `memory`, and make the value larger than `memory`.
183183
- **CpuShares** - An integer value containing the CPU Shares for container
184184
(ie. the relative weight vs othercontainers).
185-
**CpuSet** - String value containg the cgroups Cpuset to use.
185+
- **Cpuset** - The same as CpusetCpus, but deprecated, please don't use.
186+
- **CpusetCpus** - String value containg the cgroups CpusetCpus to use.
186187
- **AttachStdin** - Boolean value, attaches to stdin.
187188
- **AttachStdout** - Boolean value, attaches to stdout.
188189
- **AttachStderr** - Boolean value, attaches to stderr.
@@ -195,7 +196,7 @@ Json Parameters:
195196
of strings
196197
- **Image** - String value containing the image name to use for the container
197198
- **Volumes** – An object mapping mountpoint paths (strings) inside the
198-
container to empty objects.
199+
container to empty objects.
199200
- **WorkingDir** - A string value containing the working dir for commands to
200201
run in.
201202
- **NetworkDisabled** - Boolean value, when true disables neworking for the
@@ -292,8 +293,6 @@ Return low-level information on the container `id`
292293
"-c",
293294
"exit 9"
294295
],
295-
"CpuShares": 0,
296-
"Cpuset": "",
297296
"Domainname": "",
298297
"Entrypoint": null,
299298
"Env": [
@@ -303,8 +302,6 @@ Return low-level information on the container `id`
303302
"Hostname": "ba033ac44011",
304303
"Image": "ubuntu",
305304
"MacAddress": "",
306-
"Memory": 0,
307-
"MemorySwap": 0,
308305
"NetworkDisabled": false,
309306
"OnBuild": null,
310307
"OpenStdin": false,
@@ -324,13 +321,17 @@ Return low-level information on the container `id`
324321
"CapAdd": null,
325322
"CapDrop": null,
326323
"ContainerIDFile": "",
324+
"CpusetCpus": "",
325+
"CpuShares": 0,
327326
"Devices": [],
328327
"Dns": null,
329328
"DnsSearch": null,
330329
"ExtraHosts": null,
331330
"IpcMode": "",
332331
"Links": null,
333332
"LxcConf": [],
333+
"Memory": 0,
334+
"MemorySwap": 0,
334335
"NetworkMode": "bridge",
335336
"PortBindings": {},
336337
"Privileged": false,
@@ -1173,8 +1174,6 @@ Return low-level information on the image `name`
11731174
{
11741175
"Hostname": "",
11751176
"User": "",
1176-
"Memory": 0,
1177-
"MemorySwap": 0,
11781177
"AttachStdin": false,
11791178
"AttachStdout": false,
11801179
"AttachStderr": false,
@@ -1539,10 +1538,6 @@ Create a new image from a container's changes
15391538
"Hostname": "",
15401539
"Domainname": "",
15411540
"User": "",
1542-
"Memory": 0,
1543-
"MemorySwap": 0,
1544-
"CpuShares": 512,
1545-
"Cpuset": "0,1",
15461541
"AttachStdin": false,
15471542
"AttachStdout": true,
15481543
"AttachStderr": true,
@@ -1896,10 +1891,6 @@ Return low-level information about the exec command `id`.
18961891
"Hostname" : "8f177a186b97",
18971892
"Domainname" : "",
18981893
"User" : "",
1899-
"Memory" : 0,
1900-
"MemorySwap" : 0,
1901-
"CpuShares" : 0,
1902-
"Cpuset" : "",
19031894
"AttachStdin" : false,
19041895
"AttachStdout" : false,
19051896
"AttachStderr" : false,

docs/sources/reference/commandline/cli.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -781,7 +781,7 @@ Creates a new container.
781781
--cap-add=[] Add Linux capabilities
782782
--cap-drop=[] Drop Linux capabilities
783783
--cidfile="" Write the container ID to the file
784-
--cpuset="" CPUs in which to allow execution (0-3, 0,1)
784+
--cpuset-cpus="" CPUs in which to allow execution (0-3, 0,1)
785785
--device=[] Add a host device to the container
786786
--dns=[] Set custom DNS servers
787787
--dns-search=[] Set custom DNS search domains
@@ -1638,7 +1638,7 @@ removed before the image is removed.
16381638
--cap-add=[] Add Linux capabilities
16391639
--cap-drop=[] Drop Linux capabilities
16401640
--cidfile="" Write the container ID to the file
1641-
--cpuset="" CPUs in which to allow execution (0-3, 0,1)
1641+
--cpuset-cpus="" CPUs in which to allow execution (0-3, 0,1)
16421642
-d, --detach=false Run container in background and print container ID
16431643
--device=[] Add a host device to the container
16441644
--dns=[] Set custom DNS servers

integration-cli/docker_cli_run_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1283,6 +1283,17 @@ func TestRunWithCpuset(t *testing.T) {
12831283
logDone("run - cpuset 0")
12841284
}
12851285

1286+
func TestRunWithCpusetCpus(t *testing.T) {
1287+
defer deleteAllContainers()
1288+
1289+
cmd := exec.Command(dockerBinary, "run", "--cpuset-cpus", "0", "busybox", "true")
1290+
if code, err := runCommand(cmd); err != nil || code != 0 {
1291+
t.Fatalf("container should run successfuly with cpuset-cpus of 0: %s", err)
1292+
}
1293+
1294+
logDone("run - cpuset-cpus 0")
1295+
}
1296+
12861297
func TestRunDeviceNumbers(t *testing.T) {
12871298
defer deleteAllContainers()
12881299

runconfig/config.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ type Config struct {
1212
Hostname string
1313
Domainname string
1414
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)
18-
Cpuset string // Cpuset 0-2, 0,1
15+
Memory int64 // FIXME: we keep it for backward compatibility, it has been moved to hostConfig.
16+
MemorySwap int64 // FIXME: it has been moved to hostConfig.
17+
CpuShares int64 // FIXME: it has been moved to hostConfig.
18+
Cpuset string // FIXME: it has been moved to hostConfig and renamed to CpusetCpus.
1919
AttachStdin bool
2020
AttachStdout bool
2121
AttachStderr bool

0 commit comments

Comments
 (0)