#!/bin/sh
#
# Copyright (c) 1997 The President and Fellows of Harvard College.
# All rights reserved.
# Copyright (c) 1997 Aaron B. Brown.
#
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program, in the file COPYING in this distribution;
#   if not, write to the Free Software Foundation, Inc., 675 Mass Ave,
#   Cambridge, MA 02139, USA.
#
# Results obtained from this benchmark may be published only under the
# name "HBench-OS".

#
# gen-latgraph
#
# This script takes a data directory generated by the HBench-OS 
# lat_mem_rd benchmark and constructs a graph of the memory hierarchy
# latency, similar to those produced by lmbench. It spits out a file
# into the data directory that can be fed to gnuplot to view the graph;
# it also spits out one data file per stride size measured.
#
# Adding the -ps option will cause gnuplot to be invoked automatically
# to generate a postscript version of the graph.
#
# $Id: gen-latgraph,v 1.1.1.1 2003/02/28 17:19:54 fedorova Exp $

if [ $# -ne 1 -a $# -ne 2 ]; then
    echo "Usage: $0 [-ps] <lat_mem_rd-resultdir>"
    exit 1
fi

if [ $1 = "-ps" ]; then
    PS="Y"
    DIR=$2
else
    PS="N"
    DIR=$1
fi

if [ -d $DIR ]; then
    cd $DIR
else
    echo "Unable to change to directory $DIR"
    exit 1
fi

# Step 0: Figure out echo.
if [ `echo -n "foo" | wc -l` -eq 0 ]; then
    ECHON="-n"; ECHOC=
else
    ECHON= ; ECHOC='\c'
fi

# Step 2: Extract the parameters from each filename and get the data
#         from the file, then put into the output file in proper format.
STRIDES=`ls rd_* | sed 's/rd_[0-9]*_//g' | sort -n -r | uniq`

YMAX="0"
for stride in $STRIDES
do
    echo "Generating data for stride size ${stride}..."
    outfile="plot.stride${stride}"
    rm -f $outfile
    touch $outfile
    echo "\# lat_mem_rd, stride=${stride}" >> outfile
    
    for file in rd_*_${stride}
    do
        size=`echo $file |sed "s/^rd_//" | sed "s/_.*$//"`
	size=`echo $size | awk '{print $1/1024;}'`
	VAL=`/usr/pkg/libexec/hbench/stats-single $file`
	if [ `echo "$VAL $YMAX" | awk '{print ($1 > $2);}'` -gt 0 ]; then
	    YMAX=$VAL
        fi
	echo "$size $VAL" >> $outfile
    done
done

#YKEY=`echo ${YMAX} | awk '{print 0.90*$1;}'`
YKEY=$YMAX

# Step 3: Spit out gnuplot header and configuration file
echo "Generating gnuplot file..."
rm -f gnuplot.in
touch gnuplot.in

# Get hostname and platform type for title
# Assume system information is available in ".."
SYSNAME=`grep "System name:" ../sysconf | sed 's/System name: //'`
PLATFORM=`grep "System type:" ../sysconf | sed 's/System type: //'`

# Write header
echo "# GNUPLOT driver file for lat_mem_rd data" >> gnuplot.in
echo "#" >> gnuplot.in
echo "# System name: $SYSNAME" >> gnuplot.in
echo "# System type: $PLATFORM" >> gnuplot.in
echo "#" >> gnuplot.in

# Set the title and axis labels
echo "set title \"Memory Hierarchy Latencies for $SYSNAME (${PLATFORM})\"" >> gnuplot.in
echo "set xlabel \"Memory Range (KB, log2 scale)\"" >> gnuplot.in
echo "set ylabel \"Access Time (ns)\"" >> gnuplot.in
echo "set logscale x 2" >> gnuplot.in
echo "set nologscale y" >> gnuplot.in
echo "set xrange [0.25:16384]" >> gnuplot.in
echo "set xtics (0.25, 0.5, 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384)" >> gnuplot.in
echo "set key 2,${YKEY}" >> gnuplot.in

echo ${ECHON} "plot ${ECHOC}" >> gnuplot.in
FIRST=Y
for stride in $STRIDES
do
    if [ $FIRST = N ]; then
	echo ", \\" >> gnuplot.in
    fi
    FIRST=N
    echo ${ECHON} "\"plot.stride${stride}\" title \"stride=${stride}\" with lines${ECHOC}" >> gnuplot.in
done
echo >> gnuplot.in

if [ $PS = Y ]; then
    echo "Generating postscript..."
    rm -f _gptmp.xxx
    touch _gptmp.xxx
    echo "set terminal postscript" >> _gptmp.xxx
    echo "set output \"plot.ps\"" >> _gptmp.xxx
    cat gnuplot.in >> _gptmp.xxx
    gnuplot < _gptmp.xxx
    rm _gptmp.xxx
fi

