Created
October 20, 2017 00:12
-
-
Save WGH-/91260f6d65db88be2c847053c49be5ae to your computer and use it in GitHub Desktop.
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 | |
use strict; | |
sub deswappify { | |
my $pid = shift; | |
my $fh = undef; | |
my $start_addr, $end_addr; | |
if(open F, "/proc/$pid/smaps") { | |
while(<F>) { | |
if(m/^([0-9a-f]+)-([0-9a-f]+) /si){ | |
$start_addr=hex($1); | |
$end_addr=hex($2); | |
} elsif( m/^Swap:\s*(\d\d+) *kB/s ){ | |
if ($fh == undef) { | |
if (!open($fh, "< :raw :bytes", "/proc/$pid/mem")) { | |
print STDERR "failed to open /proc/$pid/mem\n"; | |
continue; | |
} | |
print STDERR "Deswappifying $pid...\n"; | |
} | |
printf STDERR "%x - %x\n", $start_addr, $end_addr; | |
seek($fh, $start_addr, SEEK_SET); | |
while ($start_addr < $end_addr) { | |
read($fh, $_, 4096); | |
$start_addr += 4096; | |
} | |
} | |
} | |
close $fh if $fh != undef; | |
} else { | |
print STDERR "failed to open /proc/$pid/smaps\n" | |
} | |
} | |
for(`ps -e -o pid,args`) { | |
if(m/^ *(\d+) *(.{0,40})/) { | |
$pid=$1; | |
$desc=$2; | |
deswappify($pid); | |
} | |
} |
See https://github.com/wiedemannc/deswappify-auto for an automatic python daemon performing deswapping based on the ideas of this script.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For anyone getting errors such as
I've fixed the script to work properly with strict mode :)
https://gist.github.com/Someguy123/b221986a279e1dff5f1c139562f9ca73