36

/proc/sys/vm/swappiness is nice, but I want a knob that is per process like /proc/$PID/oom_adj. So that I can make certain processes less likely than others to have any of their pages swapped out. Unlike memlock(), this doesn't prevent a program from being swapped out. And like nice, the user by default can't make their programs less likely, but only more likely to get swapped. I think I had to call this /proc/$PID/swappiness_adj.

2 Answers 2

35

You can configure swappiness per cgroup:

http://www.kernel.org/doc/Documentation/cgroup-v1/cgroups.txt

http://www.kernel.org/doc/Documentation/cgroup-v1/memory.txt

For an easier introduction to cgroups, with examples, see

https://access.redhat.com/site/documentation/en-US/Red_Hat_Enterprise_Linux/6/html/Resource_Management_Guide/ch01.html

1
  • 3
    Beware, that, according to the RedHat guide, enabling memory-cgroup uses additional memory, 40 byte per page. Consider hugepages to reduce the number of pages in this context, see wiki.debian.org/Hugepages
    – Tino
    Apr 9, 2016 at 13:35
2

I would like to extend Šimon Tóth's answer with a real life solution that I have developed. I think that it deserves a separate answer.

RedHat 7 - cgroups v1 and systemd

As only since RedHat 8 the cgroups v2 are supported, we need to use cgroups v1 here.

The official guide suggests adding a ExecStartPost that changes memory settings for your service, but it does not work until you make another change that will make /sys/fs/cgroup/memory/system.slice available.

So I ended up with adding this to my service unit file, assuming that the service name is example:

# the first entry is here only to make /sys/fs/cgroup/memory/system.slice path
# available for the next entry
MemoryLimit=64G
# this entry actually effectively disables swap for the service
ExecStartPost=/bin/bash -c \
              "echo 0 > /sys/fs/cgroup/memory/system.slice/example.service/memory.swappiness"

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .