Browse code

Merge pull request #16277 from runcom/add-oom-score-adj

Add OomScoreAdj

Arnaud Porterie authored on 2015/12/03 04:49:51
Showing 16 changed files
... ...
@@ -1389,6 +1389,7 @@ _docker_run() {
1389 1389
 		--memory-reservation
1390 1390
 		--name
1391 1391
 		--net
1392
+		--oom-score-adj
1392 1393
 		--pid
1393 1394
 		--publish -p
1394 1395
 		--restart
... ...
@@ -482,6 +482,7 @@ __docker_subcommand() {
482 482
         "($help)--name=[Container name]:name: "
483 483
         "($help)--net=[Connect a container to a network]:network mode:(bridge none container host)"
484 484
         "($help)--oom-kill-disable[Disable OOM Killer]"
485
+        "($help)--oom-score-adj[Tune the host's OOM preferences for containers (accepts -1000 to 1000)]"
485 486
         "($help -P --publish-all)"{-P,--publish-all}"[Publish all exposed ports]"
486 487
         "($help)*"{-p=,--publish=}"[Expose a container's port to the host]:port:_ports"
487 488
         "($help)--pid=[PID namespace to use]:PID: "
... ...
@@ -346,6 +346,7 @@ func (daemon *Daemon) populateCommand(c *Container, env []string) error {
346 346
 		GIDMapping:         gidMap,
347 347
 		GroupAdd:           c.hostConfig.GroupAdd,
348 348
 		Ipc:                ipc,
349
+		OomScoreAdj:        c.hostConfig.OomScoreAdj,
349 350
 		Pid:                pid,
350 351
 		ReadonlyRootfs:     c.hostConfig.ReadonlyRootfs,
351 352
 		RemappedRoot:       remappedRoot,
... ...
@@ -263,6 +263,9 @@ func verifyPlatformContainerSettings(daemon *Daemon, hostConfig *runconfig.HostC
263 263
 		return warnings, fmt.Errorf("Your kernel does not support oom kill disable.")
264 264
 	}
265 265
 
266
+	if hostConfig.OomScoreAdj < -1000 || hostConfig.OomScoreAdj > 1000 {
267
+		return warnings, fmt.Errorf("Invalid value %d, range for oom score adj is [-1000, 1000].", hostConfig.OomScoreAdj)
268
+	}
266 269
 	if sysInfo.IPv4ForwardingDisabled {
267 270
 		warnings = append(warnings, "IPv4 forwarding is disabled. Networking will not work.")
268 271
 		logrus.Warnf("IPv4 forwarding is disabled. Networking will not work")
... ...
@@ -114,6 +114,7 @@ type Command struct {
114 114
 	GIDMapping         []idtools.IDMap   `json:"gidmapping"`
115 115
 	GroupAdd           []string          `json:"group_add"`
116 116
 	Ipc                *Ipc              `json:"ipc"`
117
+	OomScoreAdj        int               `json:"oom_score_adj"`
117 118
 	Pid                *Pid              `json:"pid"`
118 119
 	ReadonlyRootfs     bool              `json:"readonly_rootfs"`
119 120
 	RemappedRoot       *User             `json:"remap_root"`
... ...
@@ -86,6 +86,8 @@ func (d *Driver) createContainer(c *execdriver.Command, hooks execdriver.Hooks)
86 86
 		return nil, err
87 87
 	}
88 88
 
89
+	container.OomScoreAdj = c.OomScoreAdj
90
+
89 91
 	if container.Readonlyfs {
90 92
 		for i := range container.Mounts {
91 93
 			switch container.Mounts[i].Destination {
... ...
@@ -108,12 +108,11 @@ This section lists each version from latest to oldest.  Each listing includes a
108 108
 * `POST /volumes/create` to create a volume.
109 109
 * `GET /volumes/(name)` get low-level information about a volume.
110 110
 * `DELETE /volumes/(name)`remove a volume with the specified name.
111
-* `VolumeDriver` has been moved from config to hostConfig to make the configuration portable.
112
-* `GET /images/(name)/json` now returns information about tags and digests of the image.
111
+* `VolumeDriver` was moved from `config` to `HostConfig` to make the configuration portable.
112
+* `GET /images/(name)/json` now returns information about an image's `RepoTags` and `RepoDigests`.
113 113
 * The `config` option now accepts the field `StopSignal`, which specifies the signal to use to kill a container.
114 114
 * `GET /containers/(id)/stats` will return networking information respectively for each interface.
115
-* The `hostConfig` option now accepts the field `DnsOptions`, which specifies a
116
-list of DNS options to be used in the container.
115
+* The `HostConfig` option now includes the `DnsOptions` field to configure the container's DNS options.
117 116
 * `POST /build` now optionally takes a serialized map of build-time variables.
118 117
 * `GET /events` now includes a `timenano` field, in addition to the existing `time` field.
119 118
 * `GET /events` now supports filtering by image and container labels.
... ...
@@ -131,6 +130,9 @@ list of DNS options to be used in the container.
131 131
   `NetworkSettings.Gateway`, `NetworkSettings.IPAddress`,
132 132
   `NetworkSettings.IPPrefixLen`, and `NetworkSettings.MacAddress` fields, which
133 133
   are still returned for backward-compatibility, but will be removed in a future version.
134
+* The `HostConfig` option now includes the `OomScoreAdj` field for adjusting the
135
+  badness heuristic. This heuristic selects which processes the OOM killer kills
136
+  under out-of-memory conditions.
134 137
 
135 138
 ### v1.20 API changes
136 139
 
... ...
@@ -218,7 +220,7 @@ container. Previously this was only available when starting a container.
218 218
 [Docker Remote API v1.14](docker_remote_api_v1.14.md) documentation
219 219
 
220 220
 * `DELETE /containers/(id)` when using `force`, the container will be immediately killed with SIGKILL.
221
-* `POST /containers/(id)/start` the `hostConfig` option accepts the field `CapAdd`, which specifies a list of capabilities
221
+* `POST /containers/(id)/start` the `HostConfig` option accepts the field `CapAdd`, which specifies a list of capabilities
222 222
 to add, and the field `CapDrop`, which specifies a list of capabilities to drop.
223 223
 * `POST /images/create` th `fromImage` and `repo` parameters support the
224 224
 `repo:tag` format. Consequently,  the `tag` parameter is now obsolete. Using the
... ...
@@ -190,6 +190,7 @@ Create a container
190 190
              "BlkioWeightDevice": [{}],
191 191
              "MemorySwappiness": 60,
192 192
              "OomKillDisable": false,
193
+             "OomScoreAdj": 500,
193 194
              "PortBindings": { "22/tcp": [{ "HostPort": "11022" }] },
194 195
              "PublishAllPorts": false,
195 196
              "Privileged": false,
... ...
@@ -243,9 +244,10 @@ Json Parameters:
243 243
 -   **CpusetCpus** - String value containing the `cgroups CpusetCpus` to use.
244 244
 -   **CpusetMems** - Memory nodes (MEMs) in which to allow execution (0-3, 0,1). Only effective on NUMA systems.
245 245
 -   **BlkioWeight** - Block IO weight (relative weight) accepts a weight value between 10 and 1000.
246
- -   **BlkioWeightDevice** - Block IO weight (relative device weight) in the form of:        `"BlkioWeightDevice": [{"Path": "device_path", "Weight": weight}]`
246
+-   **BlkioWeightDevice** - Block IO weight (relative device weight) in the form of:        `"BlkioWeightDevice": [{"Path": "device_path", "Weight": weight}]`
247 247
 -   **MemorySwappiness** - Tune a container's memory swappiness behavior. Accepts an integer between 0 and 100.
248 248
 -   **OomKillDisable** - Boolean value, whether to disable OOM Killer for the container or not.
249
+-   **OomScoreAdj** - An integer value containing the score given to the container in order to tune OOM killer preferences.
249 250
 -   **AttachStdin** - Boolean value, attaches to `stdin`.
250 251
 -   **AttachStdout** - Boolean value, attaches to `stdout`.
251 252
 -   **AttachStderr** - Boolean value, attaches to `stderr`.
... ...
@@ -416,6 +418,7 @@ Return low-level information on the container `id`
416 416
 			"MemoryReservation": 0,
417 417
 			"KernelMemory": 0,
418 418
 			"OomKillDisable": false,
419
+			"OomScoreAdj": 500,
419 420
 			"NetworkMode": "bridge",
420 421
 			"PortBindings": {},
421 422
 			"Privileged": false,
... ...
@@ -1950,6 +1953,7 @@ Display system-wide information
1950 1950
         "NoProxy": "9.81.1.160",
1951 1951
         "OomKillDisable": true,
1952 1952
         "OSType": "linux",
1953
+        "OomScoreAdj": 500,
1953 1954
         "OperatingSystem": "Boot2Docker",
1954 1955
         "RegistryConfig": {
1955 1956
             "IndexConfigs": {
... ...
@@ -58,6 +58,7 @@ Creates a new container.
58 58
       --name=""                     Assign a name to the container
59 59
       --net="default"               Set the Network mode for the container
60 60
       --oom-kill-disable=false      Whether to disable OOM Killer for the container or not
61
+      --oom-score-adj=0             Tune the host's OOM preferences for containers (accepts -1000 to 1000)
61 62
       -P, --publish-all=false       Publish all exposed ports to random ports
62 63
       -p, --publish=[]              Publish a container's port(s) to the host
63 64
       --pid=""                      PID namespace to use
... ...
@@ -62,6 +62,7 @@ parent = "smn_cli"
62 62
                                     'host': use the host network stack inside the container
63 63
                                     'NETWORK': connects the container to user-created network using `docker network create` command
64 64
       --oom-kill-disable=false      Whether to disable OOM Killer for the container or not
65
+      --oom-score-adj=0             Tune the host's OOM preferences for containers (accepts -1000 to 1000)
65 66
       -P, --publish-all=false       Publish all exposed ports to random ports
66 67
       -p, --publish=[]              Publish a container's port(s) to the host
67 68
       --pid=""                      PID namespace to use
... ...
@@ -1524,3 +1524,34 @@ func (s *DockerSuite) TestPostContainersCreateMemorySwappinessHostConfigOmitted(
1524 1524
 
1525 1525
 	c.Assert(*containerJSON.HostConfig.MemorySwappiness, check.Equals, int64(-1))
1526 1526
 }
1527
+
1528
+// check validation is done daemon side and not only in cli
1529
+func (s *DockerSuite) TestPostContainersCreateWithOomScoreAdjInvalidRange(c *check.C) {
1530
+	testRequires(c, DaemonIsLinux)
1531
+
1532
+	config := struct {
1533
+		Image       string
1534
+		OomScoreAdj int
1535
+	}{"busybox", 1001}
1536
+	name := "oomscoreadj-over"
1537
+	status, b, err := sockRequest("POST", "/containers/create?name="+name, config)
1538
+	c.Assert(err, check.IsNil)
1539
+	c.Assert(status, check.Equals, http.StatusInternalServerError)
1540
+	expected := "Invalid value 1001, range for oom score adj is [-1000, 1000]."
1541
+	if !strings.Contains(string(b), expected) {
1542
+		c.Fatalf("Expected output to contain %q, got %q", expected, string(b))
1543
+	}
1544
+
1545
+	config = struct {
1546
+		Image       string
1547
+		OomScoreAdj int
1548
+	}{"busybox", -1001}
1549
+	name = "oomscoreadj-low"
1550
+	status, b, err = sockRequest("POST", "/containers/create?name="+name, config)
1551
+	c.Assert(err, check.IsNil)
1552
+	c.Assert(status, check.Equals, http.StatusInternalServerError)
1553
+	expected = "Invalid value -1001, range for oom score adj is [-1000, 1000]."
1554
+	if !strings.Contains(string(b), expected) {
1555
+		c.Fatalf("Expected output to contain %q, got %q", expected, string(b))
1556
+	}
1557
+}
... ...
@@ -3761,3 +3761,31 @@ func (s *DockerSuite) TestRunInvalidReference(c *check.C) {
3761 3761
 		c.Fatalf(`Expected "invalid reference format" in output; got: %s`, out)
3762 3762
 	}
3763 3763
 }
3764
+
3765
+func (s *DockerSuite) TestRunWithOomScoreAdj(c *check.C) {
3766
+	testRequires(c, DaemonIsLinux)
3767
+
3768
+	expected := "642"
3769
+	out, _ := dockerCmd(c, "run", "--oom-score-adj", expected, "busybox", "cat", "/proc/self/oom_score_adj")
3770
+	oomScoreAdj := strings.TrimSpace(out)
3771
+	if oomScoreAdj != "642" {
3772
+		c.Fatalf("Expected oom_score_adj set to %q, got %q instead", expected, oomScoreAdj)
3773
+	}
3774
+}
3775
+
3776
+func (s *DockerSuite) TestRunWithOomScoreAdjInvalidRange(c *check.C) {
3777
+	testRequires(c, DaemonIsLinux)
3778
+
3779
+	out, _, err := dockerCmdWithError("run", "--oom-score-adj", "1001", "busybox", "true")
3780
+	c.Assert(err, check.NotNil)
3781
+	expected := "Invalid value 1001, range for oom score adj is [-1000, 1000]."
3782
+	if !strings.Contains(out, expected) {
3783
+		c.Fatalf("Expected output to contain %q, got %q instead", expected, out)
3784
+	}
3785
+	out, _, err = dockerCmdWithError("run", "--oom-score-adj", "-1001", "busybox", "true")
3786
+	c.Assert(err, check.NotNil)
3787
+	expected = "Invalid value -1001, range for oom score adj is [-1000, 1000]."
3788
+	if !strings.Contains(out, expected) {
3789
+		c.Fatalf("Expected output to contain %q, got %q instead", expected, out)
3790
+	}
3791
+}
... ...
@@ -47,6 +47,7 @@ docker-create - Create a new container
47 47
 [**--name**[=*NAME*]]
48 48
 [**--net**[=*"bridge"*]]
49 49
 [**--oom-kill-disable**[=*false*]]
50
+[**--oom-score-adj**[=*0*]]
50 51
 [**-P**|**--publish-all**[=*false*]]
51 52
 [**-p**|**--publish**[=*[]*]]
52 53
 [**--pid**[=*[]*]]
... ...
@@ -234,6 +235,9 @@ This value should always larger than **-m**, so you should always use this with
234 234
 **--oom-kill-disable**=*true*|*false*
235 235
 	Whether to disable OOM Killer for the container or not.
236 236
 
237
+**--oom-score-adj**=""
238
+    Tune the host's OOM preferences for containers (accepts -1000 to 1000)
239
+
237 240
 **-P**, **--publish-all**=*true*|*false*
238 241
    Publish all exposed ports to random ports on the host interfaces. The default is *false*.
239 242
 
... ...
@@ -48,6 +48,7 @@ docker-run - Run a command in a new container
48 48
 [**--name**[=*NAME*]]
49 49
 [**--net**[=*"bridge"*]]
50 50
 [**--oom-kill-disable**[=*false*]]
51
+[**--oom-score-adj**[=*0*]]
51 52
 [**-P**|**--publish-all**[=*false*]]
52 53
 [**-p**|**--publish**[=*[]*]]
53 54
 [**--pid**[=*[]*]]
... ...
@@ -346,6 +347,9 @@ and foreground Docker containers.
346 346
 **--oom-kill-disable**=*true*|*false*
347 347
    Whether to disable OOM Killer for the container or not.
348 348
 
349
+**--oom-score-adj**=""
350
+   Tune the host's OOM preferences for containers (accepts -1000 to 1000)
351
+
349 352
 **-P**, **--publish-all**=*true*|*false*
350 353
    Publish all exposed ports to random ports on the host interfaces. The default is *false*.
351 354
 
... ...
@@ -211,6 +211,7 @@ type HostConfig struct {
211 211
 	GroupAdd        []string              // List of additional groups that the container process will run as
212 212
 	IpcMode         IpcMode               // IPC namespace to use for the container
213 213
 	Links           []string              // List of links (in the name:alias form)
214
+	OomScoreAdj     int                   // Container preference for OOM-killing
214 215
 	OomKillDisable  bool                  // Whether to disable OOM Killer or not
215 216
 	PidMode         PidMode               // PID namespace to use for the container
216 217
 	Privileged      bool                  // Is the container in privileged mode
... ...
@@ -81,6 +81,7 @@ func Parse(cmd *flag.FlagSet, args []string) (*Config, *HostConfig, *flag.FlagSe
81 81
 		flStdin             = cmd.Bool([]string{"i", "-interactive"}, false, "Keep STDIN open even if not attached")
82 82
 		flTty               = cmd.Bool([]string{"t", "-tty"}, false, "Allocate a pseudo-TTY")
83 83
 		flOomKillDisable    = cmd.Bool([]string{"-oom-kill-disable"}, false, "Disable OOM Killer")
84
+		flOomScoreAdj       = cmd.Int([]string{"-oom-score-adj"}, 0, "Tune host's OOM preferences (-1000 to 1000)")
84 85
 		flContainerIDFile   = cmd.String([]string{"-cidfile"}, "", "Write the container ID to the file")
85 86
 		flEntrypoint        = cmd.String([]string{"-entrypoint"}, "", "Overwrite the default ENTRYPOINT of the image")
86 87
 		flHostname          = cmd.String([]string{"h", "-hostname"}, "", "Container host name")
... ...
@@ -96,7 +97,7 @@ func Parse(cmd *flag.FlagSet, args []string) (*Config, *HostConfig, *flag.FlagSe
96 96
 		flCpusetCpus        = cmd.String([]string{"-cpuset-cpus"}, "", "CPUs in which to allow execution (0-3, 0,1)")
97 97
 		flCpusetMems        = cmd.String([]string{"-cpuset-mems"}, "", "MEMs in which to allow execution (0-3, 0,1)")
98 98
 		flBlkioWeight       = cmd.Uint16([]string{"-blkio-weight"}, 0, "Block IO (relative weight), between 10 and 1000")
99
-		flSwappiness        = cmd.Int64([]string{"-memory-swappiness"}, -1, "Tuning container memory swappiness (0 to 100)")
99
+		flSwappiness        = cmd.Int64([]string{"-memory-swappiness"}, -1, "Tune container memory swappiness (0 to 100)")
100 100
 		flNetMode           = cmd.String([]string{"-net"}, "default", "Set the Network for the container")
101 101
 		flMacAddress        = cmd.String([]string{"-mac-address"}, "", "Container MAC address (e.g. 92:d0:c6:0a:29:33)")
102 102
 		flIpcMode           = cmd.String([]string{"-ipc"}, "", "IPC namespace to use")
... ...
@@ -382,6 +383,7 @@ func Parse(cmd *flag.FlagSet, args []string) (*Config, *HostConfig, *flag.FlagSe
382 382
 	hostConfig := &HostConfig{
383 383
 		Binds:           binds,
384 384
 		ContainerIDFile: *flContainerIDFile,
385
+		OomScoreAdj:     *flOomScoreAdj,
385 386
 		OomKillDisable:  *flOomKillDisable,
386 387
 		Privileged:      *flPrivileged,
387 388
 		PortBindings:    portBindings,