Skip to content

Instantly share code, notes, and snippets.

@mrqwer88
Created April 25, 2017 10:34
Show Gist options
  • Save mrqwer88/edca2362e44eb259b2822338642019c8 to your computer and use it in GitHub Desktop.
Save mrqwer88/edca2362e44eb259b2822338642019c8 to your computer and use it in GitHub Desktop.
Script for parse /proc/buddyinfo
#!/usr/bin/perl
use strict;
use warnings;
my $page_size = `getconf PAGE_SIZE 2>&1`;
if ( $? ) {
die "Cannot get page_size with error - $page_size\n";
}
chomp $page_size;
#print "Our PAGE_SIZE - $page_size\n";
my @free_chunks;
open my $file_handler,"<", "/proc/buddyinfo" or die "Cannot open buddyinfo:$!\n";
while ( my $line = <$file_handler> ) {
chomp $line;
if ( $line !~ /^Node\s+(\d+),\s+zone\s+/ ) {
next;
}
#print "$line\n";
my @splited_line = split /\s+/, $line;
# Parsing line like -
# Node 0, zone Normal 3774 171738 357334 209691 112758 50274 23413 6561 639 7 0
shift @splited_line;
my $node = shift @splited_line;
$node =~ s/,//;
shift @splited_line;
my $zone = shift @splited_line;
next if $zone !~/Normal/i;
my $i = 0;
for my $count (@splited_line) {
chomp $count;
if ( defined $free_chunks[$i] ) {
$free_chunks[$i] += $count;
}
else {
$free_chunks[$i] = $count;
}
$i++;
}
#print "$node $zone\n";
}
close $file_handler;
my $all_avalible_memory = get_all_avalible_memory($page_size, @free_chunks);
my $all_avalible_memory_G = $all_avalible_memory/1024/1024/1024;
$all_avalible_memory_G = sprintf("%.2f",$all_avalible_memory_G);
print "All avalible: $all_avalible_memory - $all_avalible_memory_G G\n";
for (my $i = 0; $i < scalar @free_chunks; $i++ ) {
my $fragment_size = (2**$i) * $page_size;
my $size = $free_chunks[$i] * $fragment_size;
my $percent = ($size/$all_avalible_memory ) * 100;
$percent = sprintf("%00.2f", $percent);
my $size_G = $size/1024/1024/1024;
printf("%-10s - %5s %% - %5.2f G\n",$fragment_size, $percent, $size_G)
}
sub get_all_avalible_memory {
my $page_size = shift;
my @free_chunks = @_;
my $all = 0;
my $i;
for ($i = 0; $i < scalar @free_chunks ; $i++) {
#print "$i\n";
my $fragment_size = (2**$i) * $page_size;
#print "$fragment_size ", $free_chunks[$i], " ", $free_chunks[$i] * $fragment_size,"\n";
#$all += $free_chunks[$i] *2^($i*$page_size);
$all += $free_chunks[$i] * $fragment_size;
}
return $all;
}
@mrqwer88
Copy link
Author

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