Skip to content

Instantly share code, notes, and snippets.

@hilbix
Forked from Someguy123/deswappify.pl
Last active December 18, 2020 21:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hilbix/fac46e27b6bc13b0294257558b940b50 to your computer and use it in GitHub Desktop.
Save hilbix/fac46e27b6bc13b0294257558b940b50 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl
use strict;
use Fcntl qw(SEEK_SET);
sub deswappify {
my $pid = shift;
my $fh = undef;
my $start_addr;
my $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+) *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`) {
my $pid;
if(m/^ *(\d+) *(.{0,40})/) {
$pid=$1;
deswappify($pid);
}
}
@hilbix
Copy link
Author

hilbix commented Dec 18, 2020

With a single digit it now it works for me. (At my side I had nearly a million of single 4 KB pages swapped out.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment