From 43549b46fc490e2cfbe72f6589bbb7f213d57aaf Mon Sep 17 00:00:00 2001 From: Mahmood Ali Date: Tue, 30 Mar 2021 16:33:55 -0400 Subject: [PATCH] driver/exec: set soft memory limit Linux offers soft memory limit: https://www.kernel.org/doc/html/latest/admin-guide/cgroup-v1/memory.html#soft-limits , and https://www.kernel.org/doc/html/latest/admin-guide/cgroup-v2.html?highlight=memory.low . We can set soft memory limits through libcontainer `Resources.MemoryReservation`: https://pkg.go.dev/github.com/opencontainers/runc@v0.1.1/libcontainer/configs#Resources --- drivers/shared/executor/executor_linux.go | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/drivers/shared/executor/executor_linux.go b/drivers/shared/executor/executor_linux.go index 500d2e91b..3255493c3 100644 --- a/drivers/shared/executor/executor_linux.go +++ b/drivers/shared/executor/executor_linux.go @@ -683,21 +683,23 @@ func configureCgroups(cfg *lconfigs.Config, command *ExecCommand) error { } // Total amount of memory allowed to consume - var memoryLimit int64 - if res := command.Resources.NomadResources.Memory; res.MemoryMaxMB > 0 { - memoryLimit = res.MemoryMaxMB * 1024 * 1024 - } else if res.MemoryMB > 0 { - memoryLimit = res.MemoryMB * 1024 * 1024 + res := command.Resources.NomadResources + memHard, memSoft := res.Memory.MemoryMaxMB, res.Memory.MemoryMB + if memHard <= 0 { + memHard = res.Memory.MemoryMB + memSoft = 0 } - if memoryLimit > 0 { - cfg.Cgroups.Resources.Memory = memoryLimit + + if memHard > 0 { + 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 } - cpuShares := command.Resources.NomadResources.Cpu.CpuShares + cpuShares := res.Cpu.CpuShares if cpuShares < 2 { return fmt.Errorf("resources.Cpu.CpuShares must be equal to or greater than 2: %v", cpuShares) }