Linux /proc/pid/smaps 比例交换(类似于Pss但用于交换)

17

通过查看Linux内核源代码,似乎/proc/pid/smaps中的Swap:指标是给定pid可访问的总交换空间。

在涉及共享内存的情况下,这似乎是实际交换使用量的一个过度近似。例如,当总结父进程与其派生子进程的交换使用情况时,如果它们在交换中有共同的共享内存,则此部分(交换的共享内存)会被计算多次(每个pid一次)。

我的问题是,是否有一种方法可以根据共享它的进程数量(类似于Pss:)来确定公平的交换使用指标。


你认为解析tophtop的输出对你有用吗?它们似乎有很多关于swap和共享内存使用的选项... - Hastur
我认为top或htop提供不了我需要的特定信息(比例交换)。 - Giovanni Funchal
请尝试在http://superuser.com/上发表此问题。 - o.z
试试这个:https://dev59.com/OnRB5IYBdhLWcg3w4bEo#61675746 - Mikko Rantalainen
3个回答

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"

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;

这个链接也可能很有帮助。


请检查这个链接:https://dev59.com/OnRB5IYBdhLWcg3w4bEo - eddy85br

0

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


我看了一下,我认为smem只报告驻留(在内存中)的比例集大小,而不是我所要求的比例交换(在磁盘上)。 - Giovanni Funchal

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接