malloc()调用时分配的一个物理页面

时间:2018-01-31 12:39:03

标签: linux memory-management linux-kernel

我试图找到virtual set size计划的resident set sizec。我编写了一个内核模块来遍历vm_areas并计算vssrss。我还编写了一个c程序来验证vssrss中的更改。

// 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 = %d\n",_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 = %d\n",vss,rss);


    unsigned int *ptr = malloc(1<<21); // 2 MB 
    printf("ptr = %p\n",ptr);


    // second observatrion
    arg1[0] = (int)(_pid);  
    res = syscall(333,arg1,&arg2);
    vss = arg1[1];
    rss = arg1[2];
    printf("vss = %d rss = %d\n",vss,rss);
    if(vss - prev_vss > 0) {
        printf("chnage in vss = %d\n", vss - prev_vss);
    }
    if(rss - prev_rss > 0) {
        printf("chnage in rss = %d\n", 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

我的问题是:

  1. 为什么期望的malloc尺寸(2048 KB}与vss 5 vm_area (输出之间存在差异} 2052 KB )

  2. 我们还没有访问ptr指向的内存区域。那么为什么要分配一个物理页面,如rss 5的{​​{1}} seocnd观察的vm_area结果所示? (可能是因为新的vm_area_struct?)

  3. 谢谢!

1 个答案:

答案 0 :(得分:0)

malloc(xxx)并不完全分配xxx大小的内存。 malloc不是系统调用,而是库函数。

通常,malloc有以下步骤。

  1. 通过brk扩展堆空间(如果需要)
  2. 执行mmap以使用物理地址映射虚拟地址
  3. 分配一些元数据(用于管理堆空间,通常是链表)。
  4. 在第3步中,将访问一页。这意味着访问一个物理页面并导致RSS大小增加4KB(页面大小)。