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
This commit is contained in:
Mahmood Ali 2021-03-30 16:33:55 -04:00
parent 85502c1739
commit bf1c0dcf17

View file

@ -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)
}