Last active
November 19, 2020 18:58
-
-
Save nyamakawa2/7214593c00d4c7064ea475d1812aa04d to your computer and use it in GitHub Desktop.
swapstat by /proc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl -w | |
# Original : https://stackoverflow.com/a/38790442 | |
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'}; | |
}; | |
sub meminfo { | |
open(my $fh, "</proc/meminfo") or return; | |
my ($MemTotal, $MemFree) = (0, 0); | |
my ($SwapTotal, $SwapFree, $Slab) = (0, 0, 0); | |
while (<$fh>) { | |
$MemTotal=$1 if /^MemTotal:\s+(\d+)\s/o; | |
$MemFree=$1 if /^MemFree:\s+(\d+)\s/o; | |
$Slab=$1 if /^Slab:\s+(\d+)\s/o; | |
$SwapTotal=$1 if /^SwapTotal:\s+(\d+)\s/o; | |
$SwapFree=$1 if /^SwapFree:\s+(\d+)\s/o; | |
} | |
close($fh); | |
my $data = { | |
Slab => $Slab, | |
MemTotal => $MemTotal, | |
MemFree => $MemFree, | |
SwapTotal => $SwapTotal, | |
SwapFree => $SwapFree, | |
SwapUsed => $SwapTotal - $SwapFree, | |
}; | |
return $data; | |
} | |
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time); | |
printf("%04d/%02d/%02d %02d:%02d:%02d\n", $year+1900, $mon+1, $mday, $hour, $min, $sec); | |
my $meminfo = meminfo(); | |
if ($meminfo) { | |
printf "MemTotal: %11d MemFree: %11d Kernel: %11d SwapTotal: %11d SwapFree: %11d SwapUsed: %11d KB\n", | |
$meminfo->{MemTotal}, $meminfo->{MemFree}, $meminfo->{Slab}, $meminfo->{SwapTotal}, $meminfo->{SwapFree}, $meminfo->{SwapUsed}; | |
} | |
opendir my $dh,"/proc"; | |
for my $pid (grep {/^\d+$/} readdir $dh) { | |
if (open my $fh,"</proc/$pid/status") { | |
my ($sum,$nam)=(0,""); | |
my ($VmHWM, $VmRSS) = (0, 0); | |
while (<$fh>) { | |
$sum+=$1 if /^VmSwap:\s+(\d+)\s/o; | |
$nam=$1 if /^Name:\s+(\S+)/o; | |
$VmHWM=$1 if /^VmHWM:\s+(\d+)\s/o; | |
$VmRSS=$1 if /^VmRSS:\s+(\d+)\s/o; | |
} | |
close $fh; | |
if ($VmRSS) { | |
$tot+=$sum; | |
$procs{$pid}->{'VmHWM'}=$VmHWM; | |
$procs{$pid}->{'VmRSS'}=$VmRSS; | |
$procs{$pid}->{'swap'}=$sum; | |
$procs{$pid}->{'cmd'}=$nam; | |
if (open my $fh,"</proc/$pid/smaps") { | |
$sum=0; | |
while (<$fh>) { | |
$sum+=$1 if /^Swap:\s+(\d+)\s/; | |
}; | |
}; | |
if (open my $fh,"</proc/$pid/cmdline") { | |
my $cmd = <$fh>; | |
$cmd =~ s/\0+/ /g; | |
$procs{$pid}->{'cmd'} .= " " . $cmd; | |
}; | |
$mtot+=$sum; | |
$procs{$pid}->{'mswap'}=$sum; | |
} | |
}; | |
}; | |
close($dh); | |
map { | |
my $d = $procs{$_}; | |
printf "PID: %9d VmHWM: %11d VmRSS: %11d swapped: %11d (%11d) KB (%s)\n", | |
$_, $d->{'VmHWM'}, $d->{'VmRSS'}, $d->{'swap'}, $d->{'mswap'}, $d->{'cmd'}; | |
} sort sortres keys %procs; | |
printf "Total swapped memory: %14u (%11u) KB\n", $tot,$mtot; | |
printf "\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment