Linux/proc/pid/smaps比例交换(如Pss但交换)

Gio*_*hal 17 linux procfs mmu

看来(从查看Linux内核源代码),Swap:度量标准/proc/pid/smaps是给定pid可访问的总交换.

在涉及共享内存的情况下,这似乎是实际交换使用的过度近似.例如,当将父pid与其分叉子节点的交换使用相加时,如果它们在交换中具有共同的共享内存,则看起来该部分(交换的共享内存)被计数多次(每个pid一次).

我的问题是,是否有办法根据共享它的进程数量(类似于Pss:)计算出公平交换使用指标.

Jér*_*ler 1

您只需将Swap值除以共享此虚拟内存区域的进程数即可。

实际上,我没有找到如何获取共享VMA的进程数。然而,有时可以通过除以 来计算RSSPSS。当然,它只有在以下情况下才有效PSS != 0

最后,您可以使用此 perl 代码(传递smap文件作为参数):

#!/usr/bin/perl -w
my ($rss, $pss);
my $total = 0;

while(<>) {
  $rss = $1 if /Rss: *([0-9]*) kB/;
  $pss = $1 if /Pss: *([0-9]*) kB/;
  if (/Swap: *([0-9]*) kB/) {
    my $swap = $1;
    if ($swap != 0) {
      if ($pss == 0) {
        print "Cannot get number of process using this VMA\n";

      } else {
        my $swap = $swap * $rss / $pss;
        print "P-swap: $swap\n";
      }
      $total += $swap;
    }
  }
}
print "Total P-Swap: $total kB\n"
Run Code Online (Sandbox Code Playgroud)