通过查看Linux内核源代码,似乎/proc/pid/smaps
中的Swap:
指标是给定pid可访问的总交换空间。
在涉及共享内存的情况下,这似乎是实际交换使用量的一个过度近似。例如,当总结父进程与其派生子进程的交换使用情况时,如果它们在交换中有共同的共享内存,则此部分(交换的共享内存)会被计算多次(每个pid一次)。
我的问题是,是否有一种方法可以根据共享它的进程数量(类似于Pss:
)来确定公平的交换使用指标。
Swap
值除以共享此虚拟内存区域的进程数量即可。RSS
除以PSS
来计算它。当然,这仅在PSS!= 0
时才有效。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"
你可以从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;
你可以使用工具smem的输出。它有多个输出和过滤选项。
top
或htop
的输出对你有用吗?它们似乎有很多关于swap
和共享内存使用的选项... - Hastur