Skip to content

Instantly share code, notes, and snippets.

@rgs
Created April 30, 2014 10:40
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 rgs/f24d46abebd61a99b91e to your computer and use it in GitHub Desktop.
Save rgs/f24d46abebd61a99b91e to your computer and use it in GitHub Desktop.
A perl function to get the amount of wasted memory on a memcached server
sub wasted_memory {
my ($host, $port) = @_;
my @slabstats = `echo stats slabs | nc $host $port`;
my %perslab;
my $total_malloced = 0;
my $total_wasted = 0;
for (@slabstats) {
if (/STAT total_malloced ([0-9]+)/) {
$total_malloced = $1;
next;
}
/STAT ([0-9]+):(\w+) ([0-9]+)/ or next;
$perslab{$1}{$2} = $3;
}
for my $slabnum (keys %perslab) {
$total_wasted += $perslab{$slabnum}{wasted} = ( $perslab{$slabnum}{total_chunks} * $perslab{$slabnum}{chunk_size} ) - $perslab{$slabnum}{mem_requested};
}
return ($total_malloced, $total_wasted);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment