Skip to content

Commit

Permalink
fix: use --cpus flag for resource limits
Browse files Browse the repository at this point in the history
Previously, we mistakenly were using this flag for the resource
reservations section. However, it turns out that this is only used for
Docker Swarm.

See: https://github.com/docker/compose/blob/6c06170eb0db72432e3e08e121d96f37c6d01dbc/pkg/compose/create.go#L685

Also, --cpu-quota is the "raw" form of --cpus. See:

https://docs.docker.com/engine/containers/resource_constraints/#cpu
  • Loading branch information
aksiksi committed Oct 7, 2024
1 parent 305a190 commit e141c9d
Show file tree
Hide file tree
Showing 21 changed files with 24 additions and 38 deletions.
8 changes: 3 additions & 5 deletions compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -623,17 +623,15 @@ func (g *Generator) buildNixContainer(service types.ServiceConfig, networkMap ma
}
// Name is misleading - this actually is the exact number passed in with "cpus".
if limits.NanoCPUs != 0 {
c.ExtraOptions = append(c.ExtraOptions, "--cpu-quota="+strconv.FormatFloat(float64(limits.NanoCPUs), 'f', -1, 32))
c.ExtraOptions = append(c.ExtraOptions, "--cpus="+strconv.FormatFloat(float64(limits.NanoCPUs), 'f', -1, 32))
}
}
if reservations := deploy.Resources.Reservations; reservations != nil {
if reservations.MemoryBytes != 0 {
c.ExtraOptions = append(c.ExtraOptions, fmt.Sprintf("--memory-reservation=%db", reservations.MemoryBytes))
}
// Name is misleading - this actually is the exact number passed in with "cpus".
if reservations.NanoCPUs != 0 {
c.ExtraOptions = append(c.ExtraOptions, "--cpus="+strconv.FormatFloat(float64(reservations.NanoCPUs), 'f', -1, 32))
}

// CPU reservation is a Docker Swarm option.

// CDI GPU support.
for _, device := range reservations.Devices {
Expand Down
3 changes: 1 addition & 2 deletions testdata/TestBasic.docker.nix
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@
];
log-driver = "journald";
extraOptions = [
"--cpu-quota=1.5"
"--cpus=1"
"--cpus=1.5"
"--dns=1.1.1.1"
"--health-cmd=curl -f http://localhost/\${POTATO}"
"--memory-reservation=524288000b"
Expand Down
3 changes: 1 addition & 2 deletions testdata/TestBasic.podman.nix
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@
];
log-driver = "journald";
extraOptions = [
"--cpu-quota=1.5"
"--cpus=1"
"--cpus=1.5"
"--dns=1.1.1.1"
"--health-cmd=curl -f http://localhost/\${POTATO}"
"--memory-reservation=524288000b"
Expand Down
3 changes: 1 addition & 2 deletions testdata/TestBasicAutoFormat.docker.nix
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@
dependsOn = [ "myproject-sabnzbd" ];
log-driver = "journald";
extraOptions = [
"--cpu-quota=1.5"
"--cpus=1"
"--cpus=1.5"
"--dns=1.1.1.1"
"--health-cmd=curl -f http://localhost/\${POTATO}"
"--memory-reservation=524288000b"
Expand Down
3 changes: 1 addition & 2 deletions testdata/TestBasicAutoFormat.podman.nix
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@
dependsOn = [ "myproject-sabnzbd" ];
log-driver = "journald";
extraOptions = [
"--cpu-quota=1.5"
"--cpus=1"
"--cpus=1.5"
"--dns=1.1.1.1"
"--health-cmd=curl -f http://localhost/\${POTATO}"
"--memory-reservation=524288000b"
Expand Down
2 changes: 2 additions & 0 deletions testdata/TestDeployDevices.compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,6 @@ services:
count: 1
capabilities:
- gpu
limits:
cpus: '0.50'

1 change: 1 addition & 0 deletions testdata/TestDeployDevices.docker.nix
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
log-driver = "journald";
autoStart = false;
extraOptions = [
"--cpus=0.5"
"--device=nvidia.com/gpu=all"
"--network-alias=deploy-nvidia"
"--network=test_default"
Expand Down
1 change: 1 addition & 0 deletions testdata/TestDeployDevices.podman.nix
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
log-driver = "journald";
autoStart = false;
extraOptions = [
"--cpus=0.5"
"--device=nvidia.com/gpu=all"
"--network-alias=deploy-nvidia"
"--network=test_default"
Expand Down
3 changes: 1 addition & 2 deletions testdata/TestNoWriteNixSetup.docker.nix
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@
log-driver = "journald";
autoStart = false;
extraOptions = [
"--cpu-quota=1.5"
"--cpus=1"
"--cpus=1.5"
"--dns=1.1.1.1"
"--health-cmd=curl -f http://localhost/\${POTATO}"
"--memory-reservation=524288000b"
Expand Down
3 changes: 1 addition & 2 deletions testdata/TestNoWriteNixSetup.podman.nix
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@
log-driver = "journald";
autoStart = false;
extraOptions = [
"--cpu-quota=1.5"
"--cpus=1"
"--cpus=1.5"
"--dns=1.1.1.1"
"--health-cmd=curl -f http://localhost/\${POTATO}"
"--memory-reservation=524288000b"
Expand Down
3 changes: 1 addition & 2 deletions testdata/TestOverrideSystemdStopTimeout.docker.nix
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@
log-driver = "journald";
autoStart = false;
extraOptions = [
"--cpu-quota=1.5"
"--cpus=1"
"--cpus=1.5"
"--dns=1.1.1.1"
"--health-cmd=curl -f http://localhost/\${POTATO}"
"--memory-reservation=524288000b"
Expand Down
3 changes: 1 addition & 2 deletions testdata/TestOverrideSystemdStopTimeout.podman.nix
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@
log-driver = "journald";
autoStart = false;
extraOptions = [
"--cpu-quota=1.5"
"--cpus=1"
"--cpus=1.5"
"--dns=1.1.1.1"
"--health-cmd=curl -f http://localhost/\${POTATO}"
"--memory-reservation=524288000b"
Expand Down
3 changes: 1 addition & 2 deletions testdata/TestProject.docker.nix
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@
log-driver = "journald";
autoStart = false;
extraOptions = [
"--cpu-quota=1.5"
"--cpus=1"
"--cpus=1.5"
"--dns=1.1.1.1"
"--health-cmd=curl -f http://localhost/\${POTATO}"
"--memory-reservation=524288000b"
Expand Down
3 changes: 1 addition & 2 deletions testdata/TestProject.podman.nix
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@
log-driver = "journald";
autoStart = false;
extraOptions = [
"--cpu-quota=1.5"
"--cpus=1"
"--cpus=1.5"
"--dns=1.1.1.1"
"--health-cmd=curl -f http://localhost/\${POTATO}"
"--memory-reservation=524288000b"
Expand Down
3 changes: 1 addition & 2 deletions testdata/TestRemoveVolumes.docker.nix
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@
log-driver = "journald";
autoStart = false;
extraOptions = [
"--cpu-quota=1.5"
"--cpus=1"
"--cpus=1.5"
"--dns=1.1.1.1"
"--health-cmd=curl -f http://localhost/\${POTATO}"
"--memory-reservation=524288000b"
Expand Down
3 changes: 1 addition & 2 deletions testdata/TestRemoveVolumes.podman.nix
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@
log-driver = "journald";
autoStart = false;
extraOptions = [
"--cpu-quota=1.5"
"--cpus=1"
"--cpus=1.5"
"--dns=1.1.1.1"
"--health-cmd=curl -f http://localhost/\${POTATO}"
"--memory-reservation=524288000b"
Expand Down
3 changes: 1 addition & 2 deletions testdata/TestSystemdMount.docker.nix
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@
log-driver = "journald";
autoStart = false;
extraOptions = [
"--cpu-quota=1.5"
"--cpus=1"
"--cpus=1.5"
"--dns=1.1.1.1"
"--health-cmd=curl -f http://localhost/\${POTATO}"
"--memory-reservation=524288000b"
Expand Down
3 changes: 1 addition & 2 deletions testdata/TestSystemdMount.podman.nix
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@
log-driver = "journald";
autoStart = false;
extraOptions = [
"--cpu-quota=1.5"
"--cpus=1"
"--cpus=1.5"
"--dns=1.1.1.1"
"--health-cmd=curl -f http://localhost/\${POTATO}"
"--memory-reservation=524288000b"
Expand Down
3 changes: 1 addition & 2 deletions testdata/TestUpheldBy.docker.nix
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@
log-driver = "journald";
autoStart = false;
extraOptions = [
"--cpu-quota=1.5"
"--cpus=1"
"--cpus=1.5"
"--dns=1.1.1.1"
"--health-cmd=curl -f http://localhost/\${POTATO}"
"--memory-reservation=524288000b"
Expand Down
3 changes: 1 addition & 2 deletions testdata/TestUpheldBy.podman.nix
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@
log-driver = "journald";
autoStart = false;
extraOptions = [
"--cpu-quota=1.5"
"--cpus=1"
"--cpus=1.5"
"--dns=1.1.1.1"
"--health-cmd=curl -f http://localhost/\${POTATO}"
"--memory-reservation=524288000b"
Expand Down
2 changes: 1 addition & 1 deletion testdata/compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ services:
cpus: 1.5
memory: 1000M
reservations:
cpus: 1.0
cpus: 1.0 # ignored
memory: 500M
logging:
driver: "json-file"
Expand Down

0 comments on commit e141c9d

Please sign in to comment.