#!perl -w
#
# Quickly put together by Anders Rayner-Karlsson <tak@redhat.com>
# after discussion with Neil Horman in Red Hat Engineering
# 
# GPL - Red Hat © 2009
# 
# The script simply gives you an estimate of what size your vmcore
# would be if you ran "makedumpfile -d31" as your core collector.
# If you change *anything* on your running system, the size will
# likely have changed. Compression can reduce your vmcore size even
# further, but it depends on what you have in your RAM.
#
# I make no warranties as to the scripts correctness, and it should
# simply be seen as an educationary tool, nothing more.

use strict;
use warnings;

my @iomem;
my @slabinfo;
my $memusage=0;

### grab iomem
open(FH, "< /proc/iomem") or die $!;
@iomem = <FH>;
close(FH);

### grab slabinfo
open(FH, "< /proc/slabinfo") or die $!;
@slabinfo = <FH>;
close(FH);

### iomem calculation
### values are in HEX, at least on F11
### look for Kernel code/data/bss
### todo: work out how to do this on Xen

foreach ( @iomem ) {
	m/ (\w+)-(\w+) : Kernel code/ && do {
		my $start = hex($1);
		my $end   = hex($2);
		my $diff  = $end - $start;
		$memusage += $diff;
	};
	m/ (\w+)-(\w+) : Kernel data/ && do {
		my $start = hex($1);
		my $end   = hex($2);
		my $diff  = $end - $start;
		$memusage += $diff;
	};
	m/ (\w+)-(\w+) : Kernel bss/ && do {
		my $start = hex($1);
		my $end   = hex($2);
		my $diff  = $end - $start;
		$memusage += $diff;
	};
};
### iomem done

### slabinfo calculation
### for now, assume column 2 & 3 (index from 0)
### todo: check the "header" line and decide column there

foreach ( @slabinfo ) {
	my @row = split(' ', $_);

	if( $row[2] =~ /^(\d+\.?\d*|\.\d+)$/ and $row[3] =~ /^(\d+\.?\d*|\.\d+)$/ ) {
		my $diff = $row[2] * $row[3];
		$memusage += $diff;
	};
};
### slabinfo done

### print how many MB (bytes) we use
print "vmcore likely to be " . sprintf("%0.2f", $memusage / 1024**2) . " MB (" . $memusage . " bytes)\n";

### EOF
