开发者

How much memory locked in a process

开发者 https://www.devze.com 2023-02-28 00:38 出处:网络
Using getrlimit(RLIMIT_MEMLOCK), one can get the allowed amount of locked memory a process can allocate (mlock() or mlockall()).

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 begi开发者_运维问答nning 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 ?


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"
}
0

精彩评论

暂无评论...
验证码 换一张
取 消