我试图找到c
程序的virtual set size
和resident set size
。我编写了一个内核模块来遍历vm_areas
并计算vss
和rss
。我还编写了一个c
程序来验证vss
和rss
的变化。
// sample test program
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define N 10000000
int main() {
// setup...
int *arg1 = malloc(5*sizeof(int));
char *arg2 = malloc(sizeof(char)*1024);
pid_t _pid = getpid();
int rss , prev_rss , vss, prev_vss;
printf("pid of this process = %dn",_pid);
//...
// first observatrion
arg1[0] = (int)(_pid);
long res = syscall(333,arg1,&arg2);
vss = prev_vss = arg1[1]; // agr1[1] stores the vss from the kernel module
rss = prev_rss = arg1[2]; // agr1[2] stores the rss from the kernel module
printf("vss = %d rss = %dn",vss,rss);
unsigned int *ptr = malloc(1<<21); // 2 MB
printf("ptr = %pn",ptr);
// second observatrion
arg1[0] = (int)(_pid);
res = syscall(333,arg1,&arg2);
vss = arg1[1];
rss = arg1[2];
printf("vss = %d rss = %dn",vss,rss);
if(vss - prev_vss > 0) {
printf("chnage in vss = %dn", vss - prev_vss);
}
if(rss - prev_rss > 0) {
printf("chnage in rss = %dn", rss - prev_rss);
}
prev_vss = vss;
prev_rss = rss;
// ...
return 0;
}
上述程序的输出:
pid of this process = 12964
vss = 4332 rss = 1308
ptr = 0x7f4077464010
vss = 6384 rss = 1312
chnage in vss = 2052
chnage in rss = 4
以下是dmesg
输出: 第一个观察:
[11374.065527] 1 = [0000000000400000-0000000000401000] RSS=4KB sample
[11374.065529] 2 = [0000000000600000-0000000000601000] RSS=4KB sample
[11374.065530] 3 = [0000000000601000-0000000000602000] RSS=4KB sample
[11374.065532] 4 = [0000000000c94000-0000000000cb5000] RSS=4KB
[11374.065539] 5 = [00007f4077665000-00007f407781f000] RSS=1064KB libc-2.19.so
[11374.065546] 6 = [00007f407781f000-00007f4077a1f000] RSS=0KB libc-2.19.so
[11374.065547] 7 = [00007f4077a1f000-00007f4077a23000] RSS=16KB libc-2.19.so
[11374.065549] 8 = [00007f4077a23000-00007f4077a25000] RSS=8KB libc-2.19.so
[11374.065551] 9 = [00007f4077a25000-00007f4077a2a000] RSS=16KB
[11374.065553] 10 = [00007f4077a2a000-00007f4077a4d000] RSS=140KB ld-2.19.so
[11374.065554] 11 = [00007f4077c33000-00007f4077c36000] RSS=12KB
[11374.065556] 12 = [00007f4077c49000-00007f4077c4c000] RSS=12KB
[11374.065557] 13 = [00007f4077c4c000-00007f4077c4d000] RSS=4KB ld-2.19.so
[11374.065559] 14 = [00007f4077c4d000-00007f4077c4e000] RSS=4KB ld-2.19.so
[11374.065561] 15 = [00007f4077c4e000-00007f4077c4f000] RSS=4KB
[11374.065563] 16 = [00007ffcdf974000-00007ffcdf995000] RSS=8KB
[11374.065565] 17 = [00007ffcdf9c3000-00007ffcdf9c6000] RSS=0KB
[11374.065566] 18 = [00007ffcdf9c6000-00007ffcdf9c8000] RSS=4KB
第二点意见:
[11374.065655] 1 = [0000000000400000-0000000000401000] RSS=4KB sample
[11374.065657] 2 = [0000000000600000-0000000000601000] RSS=4KB sample
[11374.065658] 3 = [0000000000601000-0000000000602000] RSS=4KB sample
[11374.065660] 4 = [0000000000c94000-0000000000cb5000] RSS=4KB
[11374.065667] 5 = [00007f4077464000-00007f4077665000] RSS=4KB
[11374.065673] 6 = [00007f4077665000-00007f407781f000] RSS=1064KB libc-2.19.so
[11374.065679] 7 = [00007f407781f000-00007f4077a1f000] RSS=0KB libc-2.19.so
[11374.065681] 8 = [00007f4077a1f000-00007f4077a23000] RSS=16KB libc-2.19.so
[11374.065683] 9 = [00007f4077a23000-00007f4077a25000] RSS=8KB libc-2.19.so
[11374.065685] 10 = [00007f4077a25000-00007f4077a2a000] RSS=16KB
[11374.065687] 11 = [00007f4077a2a000-00007f4077a4d000] RSS=140KB ld-2.19.so
[11374.065688] 12 = [00007f4077c33000-00007f4077c36000] RSS=12KB
[11374.065690] 13 = [00007f4077c49000-00007f4077c4c000] RSS=12KB
[11374.065691] 14 = [00007f4077c4c000-00007f4077c4d000] RSS=4KB ld-2.19.so
[11374.065693] 15 = [00007f4077c4d000-00007f4077c4e000] RSS=4KB ld-2.19.so
[11374.065695] 16 = [00007f4077c4e000-00007f4077c4f000] RSS=4KB
[11374.065697] 17 = [00007ffcdf974000-00007ffcdf995000] RSS=8KB
[11374.065699] 18 = [00007ffcdf9c3000-00007ffcdf9c6000] RSS=0KB
[11374.065701] 19 = [00007ffcdf9c6000-00007ffcdf9c8000] RSS=4KB
ptr
的虚拟地址是:ptr = 0x7f4077464010
对应于第二次观测中的第5
个vm_area
。
[00007f4077464000-00007f4077665000] VSS=2052KB // shown from the VSS outputs
我的问题是:
为什么所需的
malloc
大小(2048 KB
)与 2052 KB)
5
vm_area (
vss
输出之间存在差异?我们尚未访问
ptr
指向的内存区域。那么,为什么分配一个物理页面,如第5
个vm_area
的 seocnd 观察结果rss
所示?(可能是因为新vm_area_struct
吗?
谢谢!
> malloc(xxx) 不会完全分配 xxx 大小的内存。 malloc 不是系统调用,而是库函数。
一般来说,malloc 有以下步骤。
- 通过 BRK 扩展堆空间(如果需要)
- 执行 mmap 以将虚拟地址与物理地址映射
- 分配一些元数据(用于管理堆空间,通常是链表)。
在步骤 3 中,将访问一个页面。 这意味着访问一个物理页面,并导致 RSS 大小增加 4KB(页面大小)。