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 ?
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.
'ulimit -l' is always 65536 no matter what i do. I also tried 'ulimit -Hl 131072'.
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.
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"
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With