Skip to content

Commit b2d4294

Browse files
authored
Merge pull request #4462 from omame/omame/cpu_cfs_period
Add support for specifying cpu_cfs_period in the Docker driver
2 parents a7b5823 + a4771a4 commit b2d4294

File tree

3 files changed

+44
-6
lines changed

3 files changed

+44
-6
lines changed

client/driver/docker.go

+12-1
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,7 @@ type DockerDriverConfig struct {
235235
ReadonlyRootfs bool `mapstructure:"readonly_rootfs"` // Mount the container’s root filesystem as read only
236236
AdvertiseIPv6Address bool `mapstructure:"advertise_ipv6_address"` // Flag to use the GlobalIPv6Address from the container as the detected IP
237237
CPUHardLimit bool `mapstructure:"cpu_hard_limit"` // Enforce CPU hard limit.
238+
CPUCFSPeriod int64 `mapstructure:"cpu_cfs_period"` // Set the period for the CFS scheduler for the cgroup.
238239
PidsLimit int64 `mapstructure:"pids_limit"` // Enforce Docker Pids limit
239240
}
240241

@@ -741,6 +742,9 @@ func (d *DockerDriver) Validate(config map[string]interface{}) error {
741742
"cpu_hard_limit": {
742743
Type: fields.TypeBool,
743744
},
745+
"cpu_cfs_period": {
746+
Type: fields.TypeInt,
747+
},
744748
"pids_limit": {
745749
Type: fields.TypeInt,
746750
},
@@ -1238,7 +1242,14 @@ func (d *DockerDriver) createContainerConfig(ctx *ExecContext, task *structs.Tas
12381242
if driverConfig.CPUHardLimit {
12391243
numCores := runtime.NumCPU()
12401244
percentTicks := float64(task.Resources.CPU) / float64(d.node.Resources.CPU)
1241-
hostConfig.CPUQuota = int64(percentTicks*defaultCFSPeriodUS) * int64(numCores)
1245+
if driverConfig.CPUCFSPeriod < 0 || driverConfig.CPUCFSPeriod > 1000000 {
1246+
return c, fmt.Errorf("invalid value for cpu_cfs_period")
1247+
}
1248+
if driverConfig.CPUCFSPeriod == 0 {
1249+
driverConfig.CPUCFSPeriod = defaultCFSPeriodUS
1250+
}
1251+
hostConfig.CPUPeriod = driverConfig.CPUCFSPeriod
1252+
hostConfig.CPUQuota = int64(percentTicks*float64(driverConfig.CPUCFSPeriod)) * int64(numCores)
12421253
}
12431254

12441255
// Windows does not support MemorySwap/MemorySwappiness #2193

client/driver/docker_test.go

+22
Original file line numberDiff line numberDiff line change
@@ -2524,3 +2524,25 @@ func TestDockerImageRef(t *testing.T) {
25242524
})
25252525
}
25262526
}
2527+
2528+
func TestDockerDriver_CPUCFSPeriod(t *testing.T) {
2529+
if !tu.IsTravis() {
2530+
t.Parallel()
2531+
}
2532+
if !testutil.DockerIsConnected(t) {
2533+
t.Skip("Docker not connected")
2534+
}
2535+
2536+
task, _, _ := dockerTask(t)
2537+
task.Config["cpu_hard_limit"] = true
2538+
task.Config["cpu_cfs_period"] = 1000000
2539+
2540+
client, handle, cleanup := dockerSetup(t, task)
2541+
defer cleanup()
2542+
2543+
waitForExist(t, client, handle)
2544+
2545+
container, err := client.InspectContainer(handle.ContainerID())
2546+
assert.Nil(t, err, "Error inspecting container: %v", err)
2547+
assert.Equal(t, int64(1000000), container.HostConfig.CPUPeriod, "cpu_cfs_period option incorrectly set")
2548+
}

website/source/docs/drivers/docker.html.md

+10-5
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ The `docker` driver supports the following configuration in the job spec. Only
7474
command = "my-command"
7575
}
7676
```
77-
77+
7878
* `dns_search_domains` - (Optional) A list of DNS search domains for the container
7979
to use.
8080
@@ -361,7 +361,12 @@ The `docker` driver supports the following configuration in the job spec. Only
361361
soft limiting is used and containers are able to burst above their CPU limit
362362
when there is idle capacity.
363363
364-
* `advertise_ipv6_address` - (Optional) `true` or `false` (default). Use the container's
364+
* `cpu_cfs_period` - (Optional) An integer value that specifies the duration in microseconds of the period
365+
during which the CPU usage quota is measured. The default is 100000 (0.1 second) and the maximum allowed
366+
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)
367+
for more details.
368+
369+
* `advertise_ipv6_address` - (Optional) `true` or `false` (default). Use the container's
365370
IPv6 address (GlobalIPv6Address in Docker) when registering services and checks.
366371
See [IPv6 Docker containers](/docs/job-specification/service.html#IPv6 Docker containers) for details.
367372
@@ -639,10 +644,10 @@ options](/docs/agent/configuration/client.html#options):
639644

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

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

0 commit comments

Comments
 (0)