How to set per process swapiness for linux?

/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.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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

Method 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"


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x