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

时间:2014-12-02 11:16:59

标签: linux procfs mmu

看来(从查看Linux内核sourceSwap:中的/proc/pid/smaps指标是给定pid可访问的总交换。

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

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

3 个答案:

答案 0 :(得分:1)

您可以从http://northernmost.org/blog/find-out-what-is-using-your-swap/

调整此脚本
#!/bin/bash
# Get current swap usage for all running processes
# Erik Ljungstrom 27/05/2011
#
## I've made some modifications to match my purposes.
## PIDs that don't use swap are printed to STDERR and not STDOUT...

OVERALL=0
PROGLIST=$(ps axw -o pid,args --no-headers)

while read PID ARGS; do
    SUM=0
    if [ -f "/proc/$PID/smaps" ]; then
        for SWAP in $(fgrep 'Swap' /proc/$PID/smaps 2>/dev/null | awk '{ print $2 }') ; do
            let SUM=$SUM+$SWAP
        done
    fi
    if [[ $SUM > 0 ]]; then
        printf "PID: %-6s | Swap used: %-6s KB   => %s\n" $PID $SUM "$ARGS"
    else
        printf "Not using Swap, PID: %-6s => %s\n" $PID "$ARGS" 1>/dev/stderr
    fi
    let OVERALL=$OVERALL+$SUM

done <<<"$PROGLIST"

echo "Overall swap used: $OVERALL"
exit 0;

This link could be helpful too.

答案 1 :(得分:1)

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

实际上,我还没有找到如何让进程共享VMA的数量。但是,有时可以通过将RSS除以PSS来计算它。当然,它只适用于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"

答案 2 :(得分:0)

你可以使用工具涂层的输出。它有多个输出和过滤选项。

相关问题