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

client: do not disable memory swappiness if kernel does not support it #17625

Merged
merged 3 commits into from
Jun 22, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
3 changes: 3 additions & 0 deletions .changelog/17625.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
client: Fixed a bug where Nomad incorrectly write to memory swappiness cgroup on old kernels
shoenig marked this conversation as resolved.
Show resolved Hide resolved
```
24 changes: 24 additions & 0 deletions client/lib/cgutil/cgutil_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"path/filepath"

"github.com/hashicorp/go-hclog"
"github.com/hashicorp/nomad/helper/pointer"
"github.com/hashicorp/nomad/helper/uuid"
"github.com/opencontainers/runc/libcontainer/cgroups"
lcc "github.com/opencontainers/runc/libcontainer/configs"
Expand Down Expand Up @@ -148,3 +149,26 @@ func CopyCpuset(source, destination string) error {

return nil
}

// MaybeDisableMemorySwappiness will disable memory swappiness, if that controller
// is available. Always the case for cgroups v2, but is not always the case on
// very old kernels with cgroups v1.
func MaybeDisableMemorySwappiness() *uint64 {
bypass := (*uint64)(nil)
zero := pointer.Of[uint64](0)

// cgroups v2 always set zero
if UseV2 {
return zero
}

// cgroups v1 detect if swappiness is supported by attempting to write to
// the nomad parent cgroup swappiness interface
e := &editor{fromRoot: "memory/nomad"}
err := e.write("memory.swappiness", "0")
if err != nil {
return bypass
}

return zero
}
9 changes: 9 additions & 0 deletions client/lib/cgutil/cgutil_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,12 @@ func TestUtil_CopyCpuset(t *testing.T) {
require.Equal(t, "0-1", strings.TrimSpace(value))
})
}

func TestUtil_MaybeDisableMemorySwappiness(t *testing.T) {
ci.Parallel(t)

// will return 0 on any reasonable kernel (both cgroups v1 and v2)
value := MaybeDisableMemorySwappiness()
must.NotNil(t, value)
must.Eq(t, 0, *value)
}
5 changes: 2 additions & 3 deletions drivers/shared/executor/executor_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -689,9 +689,8 @@ func configureCgroups(cfg *lconfigs.Config, command *ExecCommand) error {
cfg.Cgroups.Resources.Memory = memHard * 1024 * 1024
cfg.Cgroups.Resources.MemoryReservation = memSoft * 1024 * 1024

// Disable swap to avoid issues on the machine
var memSwappiness uint64
cfg.Cgroups.Resources.MemorySwappiness = &memSwappiness
// Disable swap if possible, to avoid issues on the machine
cfg.Cgroups.Resources.MemorySwappiness = cgutil.MaybeDisableMemorySwappiness()
}

cpuShares := res.Cpu.CpuShares
Expand Down