
# Extract bargraph data from lmbench results.
# Usage: getbg file file file....
#
# Hacked into existence by Larry McVoy (lm@sun.com now lm@sgi.com).
# Copyright (c) 1994 Larry McVoy.  GPLed software.
# $Id: getbg,v 1.1 1994/11/23 07:16:48 lm Exp $
eval "exec perl -Ss $0 $*"
	if 0;

foreach $file (@ARGV) {
	push(@files, $file);
	open(FD, $file) || die "$0: can't open $file";
	warn "$0: doing $file\n";
	while (<FD>) {
		chop;
		if (/^\[lmbench/) {
			split;
			if ($_[3] eq "SunOS") {
				$_[3] .= "-$_[5]";
			}
			push(@uname, "@_");
		}
		if (/Mhz/) {
			split;
			push(@misc_mhz, $_[0]);
		}
		if (/^Null syscall:/) {
			split;
			push(@lat_nullsys, $_[2]);
		}
		if (/^Pipe latency:/) {
			split;
			push(@lat_pipe, $_[2]);
		}
		if (/UDP latency using localhost:/) {
			split;
			push(@lat_udp_local, $_[4]);
		}
		if (/TCP latency using localhost/) {
			split;
			push(@lat_tcp_local, $_[4]);
		}
		if (/RPC.udp latency using localhost/) {
			split;
			push(@lat_rpc_udp_local, $_[4]);
		}
		if (/RPC.tcp latency using localhost/) {
			split;
			push(@lat_rpc_tcp_local, $_[4]);
		}
		if (/^Process fork.exit/) {
			split;
			push(@lat_nullproc, $_[2]);
		}
		if (/^Process fork.execve/) {
			split;
			push(@lat_simpleproc, $_[2]);
		}
		if (/^Process fork..bin.sh/) {
			split;
			push(@lat_shproc, $_[3]);
		}
		if (/size=0 ovr=/) {
			while (<FD>) {
				next unless /^2/;
				split;
				push(@lat_ctx, $_[1]);
			    	last;
			}
			while (<FD>) {
				next unless /^8/;
				split;
				push(@lat_ctx8, $_[1]);
			    	last;
			}
		}
		if (/^Pipe bandwidth/) {
			split;
			push(@bw_pipe, $_[2]);
		}
		if (/^Socket bandwidth using localhost/) {
			split;
			push(@bw_tcp_local, $_[4]);
		}
		if (/^File .* write bandwidth/) {
			split;
			$bw = sprintf("%.2f", $_[4] / 1024.);
			push(@bw_file, $bw);
		}
		if (/^"mappings/) {
			$done = 0;
			while (<FD>) {
				last if /^\s*$/;
				$save = $_ if /^\d\./;
				next unless /^8.00/;
				split;
				push(@lat_mappings, $_[1]);
				$done++;
			    	last;
			}
			unless ($done) {
				$_ = $save;
				split;
				push(@lat_mappings, $_[1]);
				warn "$file: No 8MB found for " .
				    "memory mapping timing, using $_[0]\n";
			}
		}
		if (/^"read bandwidth/) {
			$done = 0;
			while (<FD>) {
				last if /^\s*$/;
				$save = $_ if /^\d\./;
				next unless /^8.00/;
				split;
				push(@bw_reread, $_[1]);
				$done++;
			    	last;
			}
			unless ($done) {
				$_ = $save;
				split;
				push(@bw_reread, $_[1]);
				warn "$file: No 8MB found for " .
				    "reread timing, using $_[0]\n";
			}
		}
		if (/^"Mmap read bandwidth/) {
			$done = 0;
			while (<FD>) {
				last if /^\s*$/;
				$save = $_ if /^\d\./;
				next unless /^8.00/;
				split;
				push(@bw_mmap, $_[1]);
				$done++;
			    	last;
			}
			unless ($done) {
				$_ = $save;
				split;
				push(@bw_mmap, $_[1]);
				warn "$file: No 8MB found for " .
				    "mmap reread timing, using $_[0]\n";
			}
		}
		if (/^"libc bcopy unaligned/) {
			while (<FD>) {
				next unless /^8.00/;
				split;
				push(@bw_bcopy_libc, $_[1]);
			    	last;
			}
		}
		if (/^"unrolled bcopy unaligned/) {
			while (<FD>) {
				next unless /^8.00/;
				split;
				push(@bw_bcopy_unrolled, $_[1]);
			    	last;
			}
		}
		if (/^Memory read/) {
			while (<FD>) {
				next unless /^8.00/;
				split;
				push(@bw_mem_rdsum, $_[1]);
			    	last;
			}
		}
		if (/^Memory write/) {
			while (<FD>) {
				next unless /^8.00/;
				split;
				push(@bw_mem_wr, $_[1]);
			    	last;
			}
		}
	}
}

&bg("lat_nullsys", "usecs", "Null system calls per second");
&bg("lat_nullproc", "usecs", "Process forks/exits per second");
&bg("lat_simpleproc", "usecs", "Simple process creates (/bin/echo foo) per second");
&bg("lat_shproc", "usecs", "Simple process creates via /bin/sh per second");
&bg("lat_ctx", "usecs", "Context switches per second, 2 small processes");
&bg("lat_ctx8", "usecs", "Context switches per second, 8 small processes");

&bg("lat_pipe", "usecs", "Pipe transactions per second");
&bg("lat_tcp_local", "usecs", "Local TCP socket transactions per second");
&bg("lat_udp_local", "usecs", "Local UDP socket transactions per second");
&bg("lat_rpc_udp_local", "usecs", 
    "Local RPC/UDP socket transactions per second");
&bg("lat_rpc_tcp_local", "usecs", 
    "Local RPC/TCP socket transactions per second");
&bg("lat_mappings", "usecs", "Memory mappings per second");

&bg("misc_mhz", "mhz", "Processor clock rate");
&bg("bw_pipe", "MB", "Pipe bandwidth in MB / second");
&bg("bw_tcp_local", "MB", "Local TCP socket bandwidth in MB / second");
&bg("bw_file", "MB", "File write bandwidth in MB / second");
&bg("bw_reread", "MB", "(Re)Read in MB / second");
&bg("bw_mmap", "MB", "(Re)Read via mmap bandwidth in MB / second");
&bg("bw_bcopy_libc", "MB", "Libc bcopy bandwidth in MB / second");
&bg("bw_bcopy_unrolled", "MB", "Unrolled bcopy bandwidth in MB / second");
&bg("bw_mem_rdsum", "MB", "Memory read & sum bandwidth in MB / second");
&bg("bw_mem_wr", "MB", "Memory write bandwidth in MB / second");

exit 0;


sub bg
{
	local($graph, $units, $title) = @_;
	local($persec, $value);

	open(FD, "|sort -nr > tmp/$graph.bg");
	for ($i = 0; $i <= $#uname; $i++) {
		@info = &getinfo($uname[$i], $misc_mhz[$i]);
		eval "\$value = \$$graph[$i];";
		if ($units eq "usecs") {
			if ($value == 0) {
				warn
				"$ARGV[$i] $graph $info[$#info]: value is 0\n";
				$persec = 0;
			} else {
				$persec = 1000000 / $value;
			}
			printf FD
			    "%.0f\t$info[3] $info[$#info] $value\\ $units\n",
			    $persec;
		} elsif ($units eq "MB") {
			printf FD "$value\t$info[3] $info[$#info]\n";
		} elsif ($units eq "mhz") {
			printf FD "$value\t$info[3] $info[$#info]\n";
		} else {
			die "Unknown units: $units";
		}
	}
	print FD "%Title n $title\n";
	close(FD);
}

# Try and create sensible names from uname -a output
sub getinfo
{
	local(@info);
	local($name);
	local($mhz) = sprintf("%.0f", $_[1]);

	@info = split(/\s+/, $_[0]);
	$name = pop(@info);
	chop($name);
	if ($name eq "mips") {
		$name = "$info[$#info]@$mhz";
	} elsif ($_[0] =~ /HP-UX/) {
		$name = "$info[7]@$mhz";
	} elsif ($_[0] =~ /SunOS/) {
		$name = "$info[7]@$mhz";
	} elsif ($_[0] =~ /AIX/) {
		$name = "?@$mhz";
	} else {
		$name .= "@$mhz";
	}
	push(@info, $name);
	@info;
}
