似乎(从查看 Linux 内核源代码)中的Swap:
度量标准/proc/pid/smaps
是给定 pid 可访问的总交换。
在涉及共享内存的情况下,这似乎是对实际交换使用的过度近似。例如,当将父 pid 与其分叉子代的交换使用量相加时,如果它们在交换中有共同的共享内存,那么这部分(交换的共享内存)似乎被计算了多次(每个 pid 一次)。
我的问题是,是否有一种方法可以根据共享它的进程数(类似于Pss:
)来计算出一个公平的交换使用指标。
您可以从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;
您只需将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"
您可以使用工具 smem 的输出。它有多个输出和过滤器选项。