18

I would like to turn off swapping for only one process. Swap should work as usual for the other processes.

How I can implement this with cgroups?

Mat
  • 52,586
iNode
  • 191

1 Answers1

11

From the kernel documentation concerning memory.swappiness:

5.3 swappiness

Similar to /proc/sys/vm/swappiness, but affecting a hierarchy of groups only.

Following cgroups' swappiness can't be changed.
- root cgroup (uses /proc/sys/vm/swappiness).
- a cgroup which uses hierarchy and it has other cgroup(s) below it.
- a cgroup which uses hierarchy and not the root of hierarchy.

From Red Hat Customer Portal:

memory.swappiness

sets the tendency of the kernel to swap out process memory used by tasks in this cgroup instead of reclaiming pages from the page cache. This is the same tendency, calculated the same way, as set in /proc/sys/vm/swappiness for the system as a whole. The default value is 60. Values lower than 60 decrease the kernel's tendency to swap out process memory, values greater than 60 increase the kernel's tendency to swap out process memory, and values greater than 100 permit the kernel to swap out pages that are part of the address space of the processes in this cgroup.

Note that a value of 0 does not prevent process memory being swapped out; swap out might still happen when there is a shortage of system memory because the global virtual memory management logic does not read the cgroup value. To lock pages completely, use mlock() instead of cgroups.

:: You cannot change the swappiness of the following groups:
* the root cgroup, which uses the swappiness set in /proc/sys/vm/swappiness.
* a cgroup that has child groups below it.

EDIT: If you're interested in knowing exactly how to set it, you set it exactly like most other cgroup attributes:

 # cd /mnt/cgroup/<cgroupName>
 # cat memory.swappiness
 60
 # echo "59 " > memory.swappiness
 # cat memory.swappiness
 59
phemmer
  • 71,831
Bratchley
  • 16,824
  • 14
  • 67
  • 103
  • What about just for files inside /tmp mounted as tmpfs? http://unix.stackexchange.com/q/146490/56970 – CMCDragonkai Jul 25 '14 at 06:24
  • It's a good thing that you quoted the relevant part of the kernel documentation here - the link is broken. – mgarey Sep 15 '17 at 17:32
  • The link to the linux kernel documentation is broken. Use this one instead https://www.kernel.org/doc/Documentation/cgroup-v1/memory.txt – Andrew Eisenberg Nov 07 '17 at 19:36
  • @AndrewEisenberg Feel free to edit the answer if you want – Bratchley Nov 09 '17 at 19:11
  • Hmmm...won't let me. Says that the edit must be larger than 6 characters. Probably not enough reputation. – Andrew Eisenberg Nov 09 '17 at 21:17
  • This does not work with cgroups: Note that a value of 0 does not prevent process memory being swapped out; swap out might still happen when there is a shortage of system memory (...) To lock pages completely, use mlock() instead of cgroups. – Tim Visee Jun 29 '21 at 13:38