Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to assign a "memory priority" to a linux process?

I am running tor on a small OpenWRT router, where swapping can't be avoided due to the limited amount of ram available (32mb).

Most of the time the router doesn't do anything else, however from time to time a postgresql database also running on the router is accessed. Due to tor constantly running, postgresql is completly swapped out and the first few accesses have very high latency, which is bad because it is a interactively used system.

I've already assigned a nice value of -15 to postgres and +15 to tor, but it doesn't seem to affect memory management very much. Setting swappiness=1 globally doesn't change things too, because swapping can not be avoided, and because postgresql isn't running most of the time, it is swapped out anyway.

Is there any way to a something like a memory priority to a Linux process? I had a look at cgroup specific swappiness, however the only description I found was that it affects the decision page-cache vs swap.

What I am looking for is a parameter to tell the linux kernel to not swap out postgresql as aggresivly as the other processes (but I don't want to mlock the whole process). Or would assigning swappiness=80 system-wide and swapiness=1 for postgresql keep postgresql in memory while swaping out everything else when required?

like image 485
user2923748 Avatar asked Aug 20 '14 12:08

user2923748


People also ask

How do I reserve memory for a process in Linux?

With cgroups, you can limit the amount of resources certain processes can use, including memory. So in your case, you would create at least 2 cgroups. One would limit the memory access to all processes on the system to maybe 90% of your total RAM. Then the second one would have access to all the RAM.

How do I change memory priority?

Prioritize RAM UsageOpen Task Manager and right-click the application you want to prioritize, then select Go to details. This opens the Details tab of the Task Manager. Right-click the process and choose Set priority.

What is memory priority?

The memory priority of a thread or process serves as a hint to the memory manager when it trims pages from the working set. Other factors being equal, pages with lower memory priority are trimmed before pages with higher memory priority.


1 Answers

Strictly speaking, in Linux you can't prevent a process from swapping out. You can avoid swapping totally using swapoff -a (and adding some RAM) but this can lead the system to instability.

But in this case Linux is doing a good job: a process used "from time to time" must be swapped out, no matter how many free RAM you have. Maybe you are using a wrong configuration. Can you put postgres on another host, maybe with a faster hard disk?

BTW: if you want to prevent the swapping out of postgres process in you current configuration, I think that you can try to use something like a keep alive: let a deamon (or a simple bash script) to periodically send some query to let the system see that the process active.

I.E. you can do something like:

#!/bin/bash
DBHOST=localhost
DBPORT=5432
DBNAME=theDBname
DBUSER=theUserName
THEQUERY="SELECT 1"

psql -h $DBHOST -p $DBPORT -d $DBNAME -U $DBUSER  -c "$THEQUERY"

And let the cron call it each minute or so.

If you wants something more sophisticated, you can create a daemon to send some "real" query and cache the results, so postgres can swap out while you already have cached results.

like image 116
Stefano Falsetto Avatar answered Oct 07 '22 00:10

Stefano Falsetto