环境:gcc版本4.8.4(Ubuntu 4.8.4-2ubuntu1〜14.04)
3.16.0-30-generiC#40〜14.04.1-Ubuntu SMP Thu Jan 15 17:45:15 UTC 2015 i686 i686 i686 GNU

C代码a2.c:具有40MB的全局数组,并且每个项目都归为一类。

int b[10000000];//40M global array
void main() {
  int i = 0;
  for(i = 0; i<10000000; i++) {b[i]=i;}
  while(1);
}

我建立像gcc -o a2 a2.c
当我运行此代码并看到smap文件cat /proc/25739/smaps时,内容如下
08048000-08049000 r-xp 00000000 08:11 46930087   /home/jzd/test/a2
Size:                  4 kB
Rss:                   4 kB
Pss:                   4 kB
Shared_Clean:          0 kB
Shared_Dirty:          0 kB
Private_Clean:         4 kB
Private_Dirty:         0 kB
Referenced:            4 kB
Anonymous:             0 kB
AnonHugePages:         0 kB
Swap:                  0 kB
KernelPageSize:        4 kB
MMUPageSize:           4 kB
Locked:                0 kB
VmFlags: rd ex mr mw me dw
//here I hide some sections
0804b000-0a670000 rw-p 00000000 00:00 0
Size:              39060 kB
Rss:               39060 kB // the RSS is the global array's size
Pss:                2196 kB // the array is only used by the program
                            // why it's pss is not equal with rss
Shared_Clean:          0 kB // all shared size is 0
Shared_Dirty:          0 kB
Private_Clean:         0 kB
Private_Dirty:     39060 kB
Referenced:        39060 kB
Anonymous:         39060 kB
AnonHugePages:     36864 kB
Swap:                  0 kB
KernelPageSize:        4 kB
MMUPageSize:           4 kB
Locked:                0 kB
VmFlags: rd wr mr mw me ac
//here I hide other sections

为什么会这样呢?

最佳答案

您已启用对透明大页面(THP)的支持,并且可执行文件的BSS由大页面支持:

0804b000-0a670000 rw-p 00000000 00:00 0
Size:              39060 kB
Rss:               39060 kB
Pss:                2196 kB
Shared_Clean:          0 kB
Shared_Dirty:          0 kB
Private_Clean:         0 kB
Private_Dirty:     39060 kB
Referenced:        39060 kB
Anonymous:         39060 kB
AnonHugePages:     36864 kB <------
Swap:                  0 kB
KernelPageSize:        4 kB
MMUPageSize:           4 kB
Locked:                0 kB
VmFlags: rd wr mr mw me ac

如果仔细观察,报告的Pss值2196 KiB恰好是由常规4 KiB页支持的匿名内存映射的数量,即AnonymousAnonHugePages之间的差异。

我的猜测是PSS中的THP记帐在3.16.0-30中被打破。在您的内核版本和@Evan内核的版本之间,有几次提交会影响Linux内核中生成smaps文件(fs/proc/task_mmu.c)内容的部分,更具体地说,此更改between 3.18 and 3.19可能是固定的。

09-26 01:15