#! /usr/bin/perl

@seq    = ( "sequenced", "nonsequenced" );
@ibises = ( "tcp", "net.bytes.gen.tcp_blk", "panda", "panda/inline", "net.gm" );
@nodes  = ( "8", "16", "32" );

while ( <> ) {
    chomp;
    # printf "current $_\n";
    if ($_ =~ /(\S+) ser (\S+)/) {
	$seq = $1;
	$ibis = $2;
	# printf "ibis $ibis; ser $ser\n";
    }
    if ($_ =~ /Run on (\S+) hosts/) {
	$node = $1;
	$is_all = 0;
    }
    if ($_ =~ /Kick-force/) {
    	$is_all = 1;
    }
    if ($_ =~ /Latency: ([0-9]+) calls took [0-9.]+ s, time\/call = ([0-9.]+) us/) {
	$size = $1;
	$lat = $2;

	if ( $seq eq "sequenced" && $ibis ne "panda" ) {
	    $node -= 1;
	}

	# printf "ibis $ibis; seq $seq; lat $lat node $node\n";

	$ix = $ibis . "/" . $seq . "@" . $node . "all=" . $is_all;

	$average{ $ix } += $lat;
	$n { $ix } ++;
	if ($min_lat { $ix } == 0 || $lat < $min_lat { $ix } ) {
	    $min_lat { $ix } = $lat;
	}
    }
}

foreach ( ( "0", "1" ) ) {
    $is_all = $_;
    if ( $is_all == "0" ) {
	printf("Broadcast except to self\n");
    } else {
	printf("Broadcast including to self\n");
    }
    printf("%-4s %-24s %-16s %8s %8s %8s\n",
	    "N", "Ibis", "order", "hosts", "min(us)", "av.(us)");
    foreach ( @nodes ) {
	$node = $_;
	foreach ( @ibises ) {
	    $ibis = $_;
	    foreach ( @seq ) {
		$s = $_;
		$ix = $ibis . "/" . $s . "@" . $node . "all=" . $is_all;
		if ( $n { $ix } > 0) {
		    printf("%-4d %-24s %-16s %8d %8.1f %8.1f\n",
			    $n { $ix },
			    "$ibis", "$s", 
			    $node,
			    $min_lat { $ix },
			    $average{ $ix } / $n { $ix });
		}
	    }
	}
    }
}

# foreach ( keys %average ) {
# 	print "$_ : ";
# 	printf "Average ($n{ $_ }) = ";
# 	printf $average{ $_ } / $n { $_ };
# 	printf " us\n";
# }
