Skip to content

Commit ddd92ca

Browse files
committed
Add the memory swappiness tuning support to libcontainer
Signed-off-by: Raghavendra K T <[email protected]>
1 parent 2a3954f commit ddd92ca

File tree

4 files changed

+42
-0
lines changed

4 files changed

+42
-0
lines changed

cgroups/fs/memory.go

+5
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ func (s *MemoryGroup) Set(path string, cgroup *configs.Cgroup) error {
6262
return err
6363
}
6464
}
65+
if cgroup.MemorySwappiness >= 0 && cgroup.MemorySwappiness <= 100 {
66+
if err := writeFile(path, "memory.swappiness", strconv.FormatInt(cgroup.MemorySwappiness, 10)); err != nil {
67+
return err
68+
}
69+
}
6570

6671
return nil
6772
}

cgroups/fs/memory_test.go

+28
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,34 @@ func TestMemorySetKernelMemory(t *testing.T) {
113113
}
114114
}
115115

116+
func TestMemorySetMemorySwappinessDefault(t *testing.T) {
117+
helper := NewCgroupTestUtil("memory", t)
118+
defer helper.cleanup()
119+
120+
const (
121+
swappinessBefore = 60 //deafult is 60
122+
swappinessAfter = 0
123+
)
124+
125+
helper.writeFileContents(map[string]string{
126+
"memory.swappiness": strconv.Itoa(swappinessBefore),
127+
})
128+
129+
helper.CgroupData.c.Memory = swappinessAfter
130+
memory := &MemoryGroup{}
131+
if err := memory.Set(helper.CgroupPath, helper.CgroupData.c); err != nil {
132+
t.Fatal(err)
133+
}
134+
135+
value, err := getCgroupParamUint(helper.CgroupPath, "memory.swappiness")
136+
if err != nil {
137+
t.Fatalf("Failed to parse memory.swappiness - %s", err)
138+
}
139+
if value != swappinessAfter {
140+
t.Fatal("Got the wrong value, set memory.swappiness failed.")
141+
}
142+
}
143+
116144
func TestMemoryStats(t *testing.T) {
117145
helper := NewCgroupTestUtil("memory", t)
118146
defer helper.cleanup()

cgroups/systemd/apply_systemd.go

+6
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,12 @@ func joinMemory(c *configs.Cgroup, pid int) error {
482482
return err
483483
}
484484
}
485+
if c.MemorySwappiness >= 0 && c.MemorySwappiness <= 100 {
486+
err = writeFile(path, "memory.swappiness", strconv.FormatInt(c.MemorySwappiness, 10))
487+
if err != nil {
488+
return err
489+
}
490+
}
485491

486492
return nil
487493
}

configs/cgroup.go

+3
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,9 @@ type Cgroup struct {
8787
// Whether to disable OOM Killer
8888
OomKillDisable bool `json:"oom_kill_disable"`
8989

90+
// Tuning swappiness behaviour per cgroup
91+
MemorySwappiness int64 `json:"memory_swappiness"`
92+
9093
// Set priority of network traffic for container
9194
NetPrioIfpriomap []*IfPrioMap `json:"net_prio_ifpriomap"`
9295

0 commit comments

Comments
 (0)