bin: +psswap
authorJan Kratochvil <jan.kratochvil@redhat.com>
Wed, 25 May 2016 16:44:37 +0000 (18:44 +0200)
committerJan Kratochvil <jan.kratochvil@redhat.com>
Wed, 25 May 2016 16:44:37 +0000 (18:44 +0200)
bin/psswap [new file with mode: 0755]

diff --git a/bin/psswap b/bin/psswap
new file mode 100755 (executable)
index 0000000..615a07b
--- /dev/null
@@ -0,0 +1,46 @@
+#! /usr/bin/perl
+use strict;
+use warnings;
+local *F;
+my $pscmd="ps axwl";
+open F,"$pscmd|" or die "$pscmd: $!";
+my %pid;
+my $rsstotal=0;
+local $_;
+while (<F>) {
+  /^(?:\S+\s+){2}(\S+)\s+(?:\S+\s+){4}(\S+)/ or die $_;
+  die if $pid{$1};
+  $pid{$1}=$_;
+  $rsstotal+=$2 if $2 ne "RSS";
+}
+close F or die "$pscmd: $!";
+my $head=delete $pid{"PID"} or die;
+my %swap;
+for my $pid (keys(%pid)) {
+  my $line=$pid{$pid};
+  my $kb=0;
+  my $fn="/proc/$pid/smaps";
+  if (!open F,$fn) {
+    warn "$fn: $line: $!" if $line!~/ $pscmd$/;
+    next;
+  }
+  while (<F>) {
+    $kb+=$1 if /^Swap:\s+(\d+) kB\n$/;
+  }
+  close F or die "$fn: $!";
+  push @{$swap{$kb}},$line;
+}
+sub printfmt($$) {
+  my($kb,$line)=@_;
+  printf "%10s %s",$kb,$line;
+}
+printfmt "SWAP",$head;
+my $swaptotal=0;
+for my $kb (sort { $b<=>$a; } keys(%swap)) {
+  for my $line (@{$swap{$kb}}) {
+    printfmt $kb,$line;
+    $swaptotal+=$kb;
+  }
+}
+print "SWAP total: $swaptotal\n";
+print "RSS total: $rsstotal\n";