#!/usr/local/bin/gawk -f
#
#  alltrace -- a combination of blocktrace, alltrace, and memtrace
#   
#  (An executable awk script.)
#
# Make sure that DEBUG_TRACE is turned on in iopfs-general.
#
# usage:
#   dmcache 'parms...' |& alltrace | gnuplot
#
# Part of
#              The STARFISH Parallel file-system simulator
#        (Simulation Tool for Advanced Research in File Systems)
# 
#                               David Kotz
#                           Dartmouth College
#                              Version 3.0
#                              October 1996
#                          dfk@cs.dartmouth.edu

BEGIN {
    lpr = 1;	# set to 1 if you want them printed instead of on screen
    FALSE = 0; TRUE = 1;
    maptrace = FALSE;
    memtrace = FALSE;
    rectrace = FALSE;
    blocktrace = FALSE;

    print > "blocktrace.data";
    print > "maptrace.data1";
    print > "maptrace.data2";
    print > "memtrace.data";
    print > "rectrace.data";
}

# We ignore all lines except tracing lines.

/^alltrace/ {
    RecordSize = $2;
    Nio = $3;
    Ncomp = $4;
}

/^tracetitle/ {
    title = $2;
}

/^blocktrace/ {
    blocktrace = TRUE;
    block = $2;

    print block > "blocktrace.data";
}

/^memtrace/ {
    memtrace = TRUE;
    CPnum = $2;
    offset = $3;
    len = $4;

    print offset / RecordSize, CPnum > "memtrace.data";
    print (offset+len) / RecordSize, CPnum > "memtrace.data";
    print "" > "memtrace.data";
}

/^rectrace/ {
    rectrace = TRUE;
    CPnum = $2;
    record = $3;
    reclen = $4;

    print record, CPnum > "rectrace.data";
    print record + reclen - 1, CPnum > "rectrace.data";
    print "" > "rectrace.data";
}

/^maptrace/ {
    maptrace = TRUE;
    block = $2;
    chunkNum = $3;
    CPnum = $4;
    CPchunkNum = $5;

    print chunkNum, CPnum > "maptrace.data1";
    print chunkNum, CPchunkNum > "maptrace.data2";
}

END {
    # issue gnuplot commands to stdout
    print "set nokey"
    print "set noxzeroaxis"
    print "set time"

    if (lpr) {
        print "set term postscript"
        print "set output \"|lpr\""
    }

# blocktrace
    if (blocktrace) {
        print "! sort -n blocktrace.data > blocktrace.sorted"
        print "set title \"", title, Nio, Ncomp, "blocktrace\""
        print "set xlabel \"order\""
        print "set ylabel \"file block\""
        print "set autoscale y";
    	if (!lpr)   # these are pointless to print out
            print "plot \"blocktrace.sorted\" with points 1 2";
        print "pause 0 \"data is in blocktrace.data and blocktrace.sorted\""
        if (!lpr)
            print "pause 2 \"You have 2 seconds to look at this...\""
    } else
        print "pause 0 \"there is no blocktrace data\""

# maptrace
    if (maptrace) {
        print "set title \"", title, Nio, Ncomp, "maptrace\""
        print "set xlabel \"file chunkNum\""
        print "set ylabel \"CPnum\""
        print "set offset 10, 10, .5, .5"
        print "set ytics 0,1"
        print "plot \"maptrace.data1\" with points 1 2";
        print "pause 0 \"data is in maptrace.data1\""
        if (!lpr)
            print "pause 4 \"You have 4 seconds to look at this...\""

        print "set ylabel \"CPchunkNum\""
        print "set autoscale y";
        print "set ytics";
        print "set offset 10, 10, 5, 5"
        print "plot \"maptrace.data2\" with points 1 2";
        print "pause 0 \"data is in maptrace.data2\""
        if (!lpr)
            print "pause 4 \"You have 4 seconds to look at this...\""
    } else
        print "pause 0 \"there is no maptrace data\""

# memtrace
    if (memtrace) {
        print "set title \"", title, Nio, Ncomp, "memtrace\""
        print "set xlabel \"memory offset address (record number)\""
        print "set ylabel \"CPnum\""
        print "set offset 0, 0, .5, .5"
#       print "set xtics 0,1024"
        print "set ytics 0,1"
        print "plot \"memtrace.data\" with lines";
        print "pause 0 \"data is in memtrace.data\""
        if (!lpr)
            print "pause 4 \"You have 4 seconds to look at this...\""
    } else
        print "pause 0 \"there is no memtrace data\""

# rectrace
    if (rectrace) {
        print "set title \"", title, Nio, Ncomp, "rectrace\""
        print "set xlabel \"file record number\""
        print "set ylabel \"CPnum\""
        print "set offset 0, 0, .5, .5"
#       print "set xtics 0,1024"
        print "set ytics 0,1"
        print "plot \"rectrace.data\" with lines";
        print "pause 0 \"data is in rectrace.data\""
        if (!lpr)
            print "pause 4 \"You have 4 seconds to look at this...\""
    } else
        print "pause 0 \"there is no rectrace data\""
}
