在Linux下,如何发现哪个进程使用交换空间更多?


当前回答

给出使用交换的进程的总数和百分比

smem -t -p

来源:https://www.cyberciti.biz/faq/linux-which-process-is-using-swap/

其他回答

另一种在shell中避免循环的脚本变体:

#!/bin/bash
grep VmSwap /proc/[0-9]*/status | awk -F':' -v sort="$1" '
  {
    split($1,pid,"/") # Split first field on /
    split($3,swp," ") # Split third field on space
    cmdlinefile = "/proc/"pid[3]"/cmdline" # Build the cmdline filepath
    getline pname[pid[3]] < cmdlinefile # Get the command line from pid
    swap[pid[3]] = sprintf("%6i %s",swp[1],swp[2]) # Store the swap used (with unit to avoid rebuilding at print)
    sum+=swp[1] # Sum the swap
  }
  END {
    OFS="\t" # Change the output separator to tabulation
    print "Pid","Swap used","Command line" # Print header
    if(sort) {
      getline max_pid < "/proc/sys/kernel/pid_max"
      for(p=1;p<=max_pid;p++) {
        if(p in pname) print p,swap[p],pname[p] # print the values
      }
    } else {
      for(p in pname) { # Loop over all pids found
        print p,swap[p],pname[p] # print the values
      }
    }
    print "Total swap used:",sum # print the sum
  }'

标准用法是script.sh以随机顺序获取每个程序的使用情况(直到awk如何存储其哈希值)或script.sh 1以pid对输出进行排序。

我希望我已经对代码进行了足够的注释,以说明它的功能。

运行top,然后按OpEnter。现在进程应该根据它们的交换使用情况进行排序。

这是一个更新,因为我原来的答案没有提供一个确切的答案,在评论中指出的问题。从htop常见问题:

It is not possible to get the exact size of used swap space of a process. Top fakes this information by making SWAP = VIRT - RES, but that is not a good metric, because other stuff such as video memory counts on VIRT as well (for example: top says my X process is using 81M of swap, but it also reports my system as a whole is using only 2M of swap. Therefore, I will not add a similar Swap column to htop because I don't know a reliable way to get this information (actually, I don't think it's possible to get an exact number, because of shared pages).

还有两种变体:

因为top或htop不能安装在小型系统上,所以浏览/proc总是可能的。

即使在小型系统上,你也会发现一个壳…

一个shell变体!(不仅仅是bash)

这与loolotux脚本完全相同,但没有任何到grep, awk或ps的分支。这要快得多!

由于bash是性能最差的shell之一,我们做了一些工作,以确保该脚本在dash、busybox和其他一些环境下能够正常运行。然后,(感谢Stéphane Chazelas)再次变得更快!

#!/bin/sh 
# Get current swap usage for all running processes
# Felix Hauri 2016-08-05
# Rewritted without fork. Inspired by first stuff from
# Erik Ljungstrom 27/05/2011
# Modified by Mikko Rantalainen 2012-08-09
# Pipe the output to "sort -nk3" to get sorted output
# Modified by Marc Methot 2014-09-18
# removed the need for sudo

OVERALL=0
for FILE in /proc/[0-9]*/status ;do
    SUM=0
    while read FIELD VALUE;do
        case $FIELD in
            Pid )    PID=$VALUE         ;;
            Name )   PROGNAME="$VALUE"  ;;
            VmSwap ) SUM=${VALUE%% *} ; break ;;
        esac
    done <$FILE
    [ $SUM -gt 0 ] &&
        printf "PID: %9d  swapped: %11d KB (%s)\n" $PID $SUM "$PROGNAME"
    OVERALL=$((OVERALL+SUM))
done
printf "Total swapped memory: %14u KB\n" $OVERALL

别忘了双引号“$PROGNAME”!请看Stéphane Chazelas的评论:

read FIELD PROGNAME < <(
    perl -ne 'BEGIN{$0="/*/*/../../*/*"} print if /^Name/' /proc/self/status
)
echo $FIELD "$PROGNAME"

不要尝试在合理的系统上不带双引号的回显$PROGNAME,并在此之前准备好杀死当前shell !

还有一个perl版本

当这变成一个不那么简单的脚本,时间来编写一个专用的工具,使用更有效的语言。

#!/usr/bin/perl -w

use strict;
use Getopt::Std;
my ($tot,$mtot)=(0,0);
my %procs;

my %opts;
getopt('', \%opts);

sub sortres {
    return $a <=> $b                                          if $opts{'p'};
    return $procs{$a}->{'cmd'} cmp $procs{$b}->{'cmd'}        if $opts{'c'};
    return $procs{$a}->{'mswap'} <=> $procs{$b}->{'mswap'}    if $opts{'m'};
    return $procs{$a}->{'swap'} <=> $procs{$b}->{'swap'};
};

opendir my $dh,"/proc";

for my $pid (grep {/^\d+$/} readdir $dh) {
    if (open my $fh,"</proc/$pid/status") {
        my ($sum,$nam)=(0,"");
        while (<$fh>) {
            $sum+=$1 if /^VmSwap:\s+(\d+)\s/;
            $nam=$1 if /^Name:\s+(\S+)/;
        }
        if ($sum) {
            $tot+=$sum;
            $procs{$pid}->{'swap'}=$sum;
            $procs{$pid}->{'cmd'}=$nam;
            close $fh;
            if (open my $fh,"</proc/$pid/smaps") {
                $sum=0;
                while (<$fh>) {
                    $sum+=$1 if /^Swap:\s+(\d+)\s/;
                };
            };
            $mtot+=$sum;
            $procs{$pid}->{'mswap'}=$sum;
        } else { close $fh; };
    };
};
map {
    printf "PID: %9d  swapped: %11d (%11d) KB (%s)\n",
        $_, $procs{$_}->{'swap'}, $procs{$_}->{'mswap'}, $procs{$_}->{'cmd'};
} sort sortres keys %procs;
printf "Total swapped memory: %14u (%11u) KB\n", $tot,$mtot;

能不能带着一个跑

-c  sort by command name
-p  sort by pid
-m  sort by swap values
by default, output is sorted by status's vmsize

我使用这个,如果你只有/proc,没有其他有用的。只需要设置nr为你想看到的顶部交换器的数量,它会告诉你进程名,交换占用空间(MB),它是ps -ef的完整进程线:

Nr =10;对于pid在$(对于文件在/proc//状态;do awk '/ vmswwap |Name|^Pid/{printf $2 "" $3}END{print ""}' $file;做|分类3 - n - r - k - $ {nr} | |头awk{打印$ 2});做awk ' / VmSwap |名称| ^ Pid / {printf 2”、“3美元}{打印”“}”结束/proc/$ Pid /状态| awk的{打印1美元”“2”“3/1024美元“m”}”| sed - e ' s /。[0 - 9]/ / g’;ps ef | awk“2美元= = $ Pid{打印}”,回声;完成

不完全清楚您的意思是要找到交换出最多页的进程还是导致交换出最多页的进程。

对于前者,您可以运行top并按swap排序(按'Op'),对于后者,您可以运行vmstat并查找'so'的非零条目。