Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LINUX: How to lock the pages of a process in memory

Tags:

I have a LINUX server running a process with a large memory footprint (some sort of a database engine). The memory allocated by this process is so large that part of it needs to be swapped (paged) out.

What I would like to do is to lock the memory pages of all the other processes (or a subset of the running processes) in memory, so that only the pages of the database process get swapped out. For example I would like to make sure that i can continue to connect remotely and monitor the machine without having the processes impacted by swapping. I.e. I want sshd, X, top, vmstat, etc to have all pages memory resident.

On linux there are the mlock(), mlockall() system calls that seem to offer the right knob to do the pinning. Unfortunately, it seems to me that I need to make an explicit call inside every process and cannot invoke mlock() from a different process or from the parent (mlock() is not inherited after fork() or evecve()).

Any help is greatly appreciated. Virtual pizza & beer offered :-).

like image 307
pol lol Avatar asked Sep 20 '12 20:09

pol lol


People also ask

What is memory lock?

Memory locking is one way to ensure that a process stays in main memory and is exempt from paging. In a realtime environment, a system must be able to guarantee that it will lock a process in memory to reduce latency for data access, instruction fetches, buffer passing between processes, and so forth.

How do I use Mlock in Linux?

mlock(), mlock2(), and munlock() mlock() locks pages in the address range starting at addr and continuing for len bytes. All pages that contain a part of the specified address range are guaranteed to be resident in RAM when the call returns successfully; the pages are guaranteed to stay in RAM until later unlocked.

What is Mlockall?

The function mlockall() causes all of the pages mapped by the address space of a process to be memory resident until unlocked or until the process exits or execs another process image.

What is Max locked memory?

max locked memory (kbytes, -l) The maximum size that may be locked into memory. Memory locking ensures the memory is always in RAM and never moved to the swap disk. https://stackoverflow.com/questions/9818755/why-would-we-need-to-lock-a-processs-address-space-in-ram.


2 Answers

It has been a while since I've done this so I may have missed a few steps.

Make a GDB command file that contains something like this:

call mlockall(3)
detach

Then on the command line, find the PID of the process you want to mlock. Type:
gdb --pid [PID] --batch -x [command file]

If you get fancy with pgrep that could be:
gdb --pid $(pgrep sshd) --batch -x [command file]

like image 166
Zan Lynx Avatar answered Sep 30 '22 19:09

Zan Lynx


Actually locking the pages of most of the stuff on your system seems a bit crude/drastic, not to mention being such an abuse of the mechanism it seems bound to cause some other unanticipated problems.

Ideally, what you probably actually want is to control the "swappiness" of groups of processes so the database is first in line to be swapped while essential system admin tools are the last, and there is a way of doing this.

like image 31
timday Avatar answered Sep 30 '22 19:09

timday