Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How much memory locked in a process

Tags:

c

linux

unix

posix

bsd

Using getrlimit(RLIMIT_MEMLOCK), one can get the allowed amount of locked memory a process can allocate (mlock() or mlockall()).

But how to retrieve the currently locked memory amount ?

For example, there's no information returned by getrusage().

Under Linux, it is possible to read /proc/self/status and extract the amount of locked memory from the line beginning with VmLck.

Is there a portable way to retrieve the amount of locked memory which would work on Linux, *BSD and other POSIX compatible systems ?

like image 808
Yann Droneaud Avatar asked Apr 23 '11 05:04

Yann Droneaud


People also ask

What is locked in memory?

Locking memory is one of the most important issues for real-time applications. In a real-time environment, a process must be able to guarantee continuous memory residence to reduce latency and to prevent paging and swapping.

What is Max locked memory in Ulimit?

'ulimit -l' is always 65536 no matter what i do. I also tried 'ulimit -Hl 131072'.

What is Mlock system call?

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.


1 Answers

You will probably need to check for each system and implement it accordingly. On Linux:

cat /proc/$PID/status | grep VmLck

You will probably need to do the same in C (read /proc line by line and search for VmLck), as this information is created in the function task_mem (in array.c) which I don't think you can access directly. Something like:

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>

char cwd[PATH_MAX];
sprintf(cwd, "/proc/%d/status", getpid());

FILE* fp = fopen(cwd, "r");
if(!fp) {
    exit(EXIT_FAILURE);
}

while((read = getline(&line, &len, fp)) != -1) {
    // search for line starting by "VmLck"
}
like image 147
Giovanni Funchal Avatar answered Oct 07 '22 20:10

Giovanni Funchal