You've already forked runc
mirror of
https://github.com/opencontainers/runc.git
synced 2025-07-30 17:43:06 +03:00
Handle memory swappiness as a pointer to handle default/unset case
This prior fix to set "-1" explicitly was lost, and it is simpler to use the same pointer type from the OCI spec to handle nil pointer == -1 == unset case. Also, as a nearly humorous aside, there was a test for MemorySwappiness that was actually setting Memory, and it was passing because of this bug (as it was always setting everyone's MemorySwappiness to zero!) Docker-DCO-1.1-Signed-off-by: Phil Estes <estesp@linux.vnet.ibm.com> (github: estesp)
This commit is contained in:
@ -86,14 +86,14 @@ func (s *MemoryGroup) Set(path string, cgroup *configs.Cgroup) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if cgroup.Resources.MemorySwappiness >= 0 && cgroup.Resources.MemorySwappiness <= 100 {
|
if cgroup.Resources.MemorySwappiness == nil || int64(*cgroup.Resources.MemorySwappiness) == -1 {
|
||||||
if err := writeFile(path, "memory.swappiness", strconv.FormatInt(cgroup.Resources.MemorySwappiness, 10)); err != nil {
|
return nil
|
||||||
|
} else if int64(*cgroup.Resources.MemorySwappiness) >= 0 && int64(*cgroup.Resources.MemorySwappiness) <= 100 {
|
||||||
|
if err := writeFile(path, "memory.swappiness", strconv.FormatInt(*cgroup.Resources.MemorySwappiness, 10)); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
} else if cgroup.Resources.MemorySwappiness == -1 {
|
|
||||||
return nil
|
|
||||||
} else {
|
} else {
|
||||||
return fmt.Errorf("invalid value:%d. valid memory swappiness range is 0-100", cgroup.Resources.MemorySwappiness)
|
return fmt.Errorf("invalid value:%d. valid memory swappiness range is 0-100", int64(*cgroup.Resources.MemorySwappiness))
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
@ -149,7 +149,7 @@ func memoryAssigned(cgroup *configs.Cgroup) bool {
|
|||||||
cgroup.Resources.MemorySwap > 0 ||
|
cgroup.Resources.MemorySwap > 0 ||
|
||||||
cgroup.Resources.KernelMemory > 0 ||
|
cgroup.Resources.KernelMemory > 0 ||
|
||||||
cgroup.Resources.OomKillDisable ||
|
cgroup.Resources.OomKillDisable ||
|
||||||
cgroup.Resources.MemorySwappiness != -1
|
(cgroup.Resources.MemorySwappiness != nil && *cgroup.Resources.MemorySwappiness != -1)
|
||||||
}
|
}
|
||||||
|
|
||||||
func getMemoryData(path, name string) (cgroups.MemoryData, error) {
|
func getMemoryData(path, name string) (cgroups.MemoryData, error) {
|
||||||
|
@ -118,16 +118,14 @@ func TestMemorySetMemorySwappinessDefault(t *testing.T) {
|
|||||||
helper := NewCgroupTestUtil("memory", t)
|
helper := NewCgroupTestUtil("memory", t)
|
||||||
defer helper.cleanup()
|
defer helper.cleanup()
|
||||||
|
|
||||||
const (
|
swappinessBefore := 60 //default is 60
|
||||||
swappinessBefore = 60 //deafult is 60
|
swappinessAfter := int64(0)
|
||||||
swappinessAfter = 0
|
|
||||||
)
|
|
||||||
|
|
||||||
helper.writeFileContents(map[string]string{
|
helper.writeFileContents(map[string]string{
|
||||||
"memory.swappiness": strconv.Itoa(swappinessBefore),
|
"memory.swappiness": strconv.Itoa(swappinessBefore),
|
||||||
})
|
})
|
||||||
|
|
||||||
helper.CgroupData.config.Resources.Memory = swappinessAfter
|
helper.CgroupData.config.Resources.MemorySwappiness = &swappinessAfter
|
||||||
memory := &MemoryGroup{}
|
memory := &MemoryGroup{}
|
||||||
if err := memory.Set(helper.CgroupPath, helper.CgroupData.config); err != nil {
|
if err := memory.Set(helper.CgroupPath, helper.CgroupData.config); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
@ -137,8 +135,8 @@ func TestMemorySetMemorySwappinessDefault(t *testing.T) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Failed to parse memory.swappiness - %s", err)
|
t.Fatalf("Failed to parse memory.swappiness - %s", err)
|
||||||
}
|
}
|
||||||
if value != swappinessAfter {
|
if int64(value) != swappinessAfter {
|
||||||
t.Fatal("Got the wrong value, set memory.swappiness failed.")
|
t.Fatalf("Got the wrong value (%d), set memory.swappiness = %d failed.", value, swappinessAfter)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -111,7 +111,7 @@ type Resources struct {
|
|||||||
OomKillDisable bool `json:"oom_kill_disable"`
|
OomKillDisable bool `json:"oom_kill_disable"`
|
||||||
|
|
||||||
// Tuning swappiness behaviour per cgroup
|
// Tuning swappiness behaviour per cgroup
|
||||||
MemorySwappiness int64 `json:"memory_swappiness"`
|
MemorySwappiness *int64 `json:"memory_swappiness"`
|
||||||
|
|
||||||
// Set priority of network traffic for container
|
// Set priority of network traffic for container
|
||||||
NetPrioIfpriomap []*IfPrioMap `json:"net_prio_ifpriomap"`
|
NetPrioIfpriomap []*IfPrioMap `json:"net_prio_ifpriomap"`
|
||||||
|
@ -48,7 +48,7 @@ func newTemplateConfig(rootfs string) *configs.Config {
|
|||||||
Cgroups: &configs.Cgroup{
|
Cgroups: &configs.Cgroup{
|
||||||
Path: "integration/test",
|
Path: "integration/test",
|
||||||
Resources: &configs.Resources{
|
Resources: &configs.Resources{
|
||||||
MemorySwappiness: -1,
|
MemorySwappiness: nil,
|
||||||
AllowAllDevices: false,
|
AllowAllDevices: false,
|
||||||
AllowedDevices: configs.DefaultAllowedDevices,
|
AllowedDevices: configs.DefaultAllowedDevices,
|
||||||
},
|
},
|
||||||
|
3
spec.go
3
spec.go
@ -398,7 +398,8 @@ func createCgroupConfig(name string, spec *specs.LinuxSpec) (*configs.Cgroup, er
|
|||||||
c.Resources.KernelMemory = int64(*r.Memory.Kernel)
|
c.Resources.KernelMemory = int64(*r.Memory.Kernel)
|
||||||
}
|
}
|
||||||
if r.Memory.Swappiness != nil {
|
if r.Memory.Swappiness != nil {
|
||||||
c.Resources.MemorySwappiness = int64(*r.Memory.Swappiness)
|
swappiness := int64(*r.Memory.Swappiness)
|
||||||
|
c.Resources.MemorySwappiness = &swappiness
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if r.CPU != nil {
|
if r.CPU != nil {
|
||||||
|
Reference in New Issue
Block a user