Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for specifying cpu_cfs_period in the Docker driver #4462

Merged
merged 6 commits into from
Jul 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion client/driver/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ type DockerDriverConfig struct {
ReadonlyRootfs bool `mapstructure:"readonly_rootfs"` // Mount the container’s root filesystem as read only
AdvertiseIPv6Address bool `mapstructure:"advertise_ipv6_address"` // Flag to use the GlobalIPv6Address from the container as the detected IP
CPUHardLimit bool `mapstructure:"cpu_hard_limit"` // Enforce CPU hard limit.
CPUCFSPeriod int64 `mapstructure:"cpu_cfs_period"` // Set the period for the CFS scheduler for the cgroup.
PidsLimit int64 `mapstructure:"pids_limit"` // Enforce Docker Pids limit
}

Expand Down Expand Up @@ -741,6 +742,9 @@ func (d *DockerDriver) Validate(config map[string]interface{}) error {
"cpu_hard_limit": {
Type: fields.TypeBool,
},
"cpu_cfs_period": {
Type: fields.TypeInt,
},
"pids_limit": {
Type: fields.TypeInt,
},
Expand Down Expand Up @@ -1238,7 +1242,14 @@ func (d *DockerDriver) createContainerConfig(ctx *ExecContext, task *structs.Tas
if driverConfig.CPUHardLimit {
numCores := runtime.NumCPU()
percentTicks := float64(task.Resources.CPU) / float64(d.node.Resources.CPU)
hostConfig.CPUQuota = int64(percentTicks*defaultCFSPeriodUS) * int64(numCores)
if driverConfig.CPUCFSPeriod < 0 || driverConfig.CPUCFSPeriod > 1000000 {
return c, fmt.Errorf("invalid value for cpu_cfs_period")
}
if driverConfig.CPUCFSPeriod == 0 {
driverConfig.CPUCFSPeriod = defaultCFSPeriodUS
}
hostConfig.CPUPeriod = driverConfig.CPUCFSPeriod
hostConfig.CPUQuota = int64(percentTicks*float64(driverConfig.CPUCFSPeriod)) * int64(numCores)
}

// Windows does not support MemorySwap/MemorySwappiness #2193
Expand Down
22 changes: 22 additions & 0 deletions client/driver/docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2524,3 +2524,25 @@ func TestDockerImageRef(t *testing.T) {
})
}
}

func TestDockerDriver_CPUCFSPeriod(t *testing.T) {
if !tu.IsTravis() {
t.Parallel()
}
if !testutil.DockerIsConnected(t) {
t.Skip("Docker not connected")
}

task, _, _ := dockerTask(t)
task.Config["cpu_hard_limit"] = true
task.Config["cpu_cfs_period"] = 1000000

client, handle, cleanup := dockerSetup(t, task)
defer cleanup()

waitForExist(t, client, handle)

container, err := client.InspectContainer(handle.ContainerID())
assert.Nil(t, err, "Error inspecting container: %v", err)
assert.Equal(t, int64(1000000), container.HostConfig.CPUPeriod, "cpu_cfs_period option incorrectly set")
}
15 changes: 10 additions & 5 deletions website/source/docs/drivers/docker.html.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ The `docker` driver supports the following configuration in the job spec. Only
command = "my-command"
}
```

* `dns_search_domains` - (Optional) A list of DNS search domains for the container
to use.

Expand Down Expand Up @@ -361,7 +361,12 @@ The `docker` driver supports the following configuration in the job spec. Only
soft limiting is used and containers are able to burst above their CPU limit
when there is idle capacity.

* `advertise_ipv6_address` - (Optional) `true` or `false` (default). Use the container's
* `cpu_cfs_period` - (Optional) An integer value that specifies the duration in microseconds of the period
during which the CPU usage quota is measured. The default is 100000 (0.1 second) and the maximum allowed
value is 1000000 (1 second). See [here](https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/6/html/resource_management_guide/sec-cpu#sect-cfs)
for more details.

* `advertise_ipv6_address` - (Optional) `true` or `false` (default). Use the container's
IPv6 address (GlobalIPv6Address in Docker) when registering services and checks.
See [IPv6 Docker containers](/docs/job-specification/service.html#IPv6 Docker containers) for details.

Expand Down Expand Up @@ -639,10 +644,10 @@ options](/docs/agent/configuration/client.html#options):

* `docker.caps.whitelist`: A list of allowed Linux capabilities. Defaults to
`"CHOWN,DAC_OVERRIDE,FSETID,FOWNER,MKNOD,NET_RAW,SETGID,SETUID,SETFCAP,SETPCAP,NET_BIND_SERVICE,SYS_CHROOT,KILL,AUDIT_WRITE"`,
which is the list of capabilities allowed by docker by default, as
which is the list of capabilities allowed by docker by default, as
[defined here](https://docs.docker.com/engine/reference/run/#runtime-privilege-and-linux-capabilities).
Allows the operator to control which capabilities can be obtained by
tasks using `cap_add` and `cap_drop` options. Supports the value `"ALL"` as a
Allows the operator to control which capabilities can be obtained by
tasks using `cap_add` and `cap_drop` options. Supports the value `"ALL"` as a
shortcut for whitelisting all capabilities.

Note: When testing or using the `-dev` flag you can use `DOCKER_HOST`,
Expand Down