#!/bin/sh
# Print out timing and size stats for deep codegen
# @author Christopher Hylands
# @version $Id: cgstats,v 1.8 2005/02/28 20:51:05 cxh Exp $

# @Copyright (c) 1997-2005 The Regents of the University of California.
# All rights reserved.
#
# Permission is hereby granted, without written agreement and without
# license or royalty fees, to use, copy, modify, and distribute this
# software and its documentation for any purpose, provided that the
# above copyright notice and the following two paragraphs appear in all
# copies of this software.
#
# IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
# FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
# ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
# THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#
# THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
# INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
# PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
# CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
# ENHANCEMENTS, OR MODIFICATIONS.
#
# 						PT_COPYRIGHT_VERSION_2
# 						COPYRIGHTENDKEY

# Usage: cgstats [ptIInightly.txt file]
if [ $# -eq 0 ]; then
    statsfile=/vol/cooley/cooley1/cxh/ptII/ptolemy/copernicus/java/runDemoTest2.out
else
    statsfile=$1
fi

grep StatRT $statsfile | grep ave | sort |
	awk 'BEGIN { runs["codegen"] = 1
		     runs["interpreted"] = 1
		     runs["jimple1"] = 1
		     runs["jimple2"] = 1
		     runs["jimple3"] = 1
		     runs["jimple4"] = 1
		     runs["jimple5"] = 1
		     runs["jimple6"] = 1
		     runs["codegen"] = 1
		     print "# Time and Memory"
		     printf ("# Model Name   cg/interp% ")
		     for (run in runs) {		     
			    printf("%s: time mem free static statRT dynRT ",
				    run);
		     }
		     printf("\n");
		}
		{   if ($1 != current && current == "") {
			current = $1
		    } else if ( $1 != current) {
		        printf("%-16s ", current)
			if (time["interpreted"] == 0) {
				codegenOverInterp = 0
			} else {
				codegenOverInterp = int ( ((time["codegen"]+0.0)/(time["interpreted"]+0.0) * 100.0) +0.5)
			}
			printf("%2d%% ", codegenOverInterp);
			#if (time["interpreted"] == 0) {
			#    printf("code/interp 0 0 0 0 0 0 ");
			#} else {    
			#    printf("code/interp %3d %3d %3d %3d %3d %3d ", int( (time["codegen"]+0.0)/(time["interpreted"]+0.0) * 100 + 0.5),
  #int( (mem["codegen"]+0.0)/(mem["interpreted"]+0.0) * 100 + 0.5),
  #int( (free["codegen"]+0.0)/(free["interpreted"]+0.0) * 100 + 0.5),
  #int( (static["codegen"]+0.0)/(static["interpreted"]+0.0) * 100 + 0.5),
  #int( (staticRT["codegen"]+0.0)/(staticRT["interpreted"]+0.0) * 100 + 0.5),
  #int( (dynamicRT["codegen"]+0.0)/(dynamicRT["interpreted"]+0.0) * 100 + 0.5))
			#}
			for (run in runs) {
			    printf("%s: %2d %5s %5s %5s %5s %5s ", run, time[run], mem[run], free[run], static[run], staticRT[run], dynamicRT[run]);
			    time[run] = 0
			    mem[run] = 0
			    free[run] = 0
			    static[run] = 0
			    staticRT[run] = 0
			    dynamicRT[run] = 0
			}    
			printf("\n");
			current = $1
		 }
		time[$2] = $4
		mem[$2] = substr($7,1,length($7) -1)
		free[$2] = substr($9,1,length($9) -1)
		static[$2] = substr($12,1,length($12) -1)
		staticRT[$2] = substr($14,1,length($14) -1)
		dynamicRT[$2] = substr($16,1,length($15) -1)
        }
        END {
		        printf("%-16s ", $1)
			if (time["interpreted"] == 0) {
				codegenOverInterp = 0
			} else {
				codegenOverInterp = int ( ((time["codegen"]+0.0)/(time["interpreted"]+0.0) * 100.0) +0.5)
			}
			printf("%2d%% ", codegenOverInterp);
			for (run in runs) {
			    printf("%s: %3d %5s %5s %5s %5s %5s ", run, time[run], mem[run], free[run], static[run], staticRT[run], dynamicRT[run]);
			    time[run] = 0
			    mem[run] = 0
			    free[run] = 0
			    static[run] = 0
			    staticRT[run] = 0
			    dynamicRT[run] = 0
			}    
			printf("\n");

	}' 


# Generate jar size info
grep /treeshake $statsfile | egrep '^-rw' | sort | uniq |
	grep -v treeshake_jardiff |
	sed 's/treeshake.jar/treeshake_interp.jar/' |
	sed 's/treeshake_//' | sed 's/.jar//' |
	awk '{n=split($8,f,"/"); print f[n-1], f[n], $4}' | sort |
	awk '
		BEGIN { print "#"
			print "# Jar File sizes"
			print "#Model        cg/interp% interp codegen jimple1 jimple2"  
		}
		{   if ($1 != current && current == "") {
			current = $1
		    } else if ( $1 != current) {
			if (row["interp"] == 0) {
				codegenOverInterp = 0
			} else {
				codegenOverInterp = int ( ((row["codegen"]+0.0)/(row["interp"]+0.0) * 100.0) +0.5)
			}
			printf("%-16s %2d%% %6d %6d %6d %6d %6d %6d %6d %6d \n", current, codegenOverInterp, row["interp"], row["codegen"], row["jimple1"], row["jimple2"], row["jimple3"], row["jimple4"], row["jimple5"], row ["jimple6"])
			row["interp"] = 0
			row["codegen"]= 0 
			row["jimple1"]= 0
			row["jimple2"]= 0
			row["jimple3"]= 0
			row["jimple4"]= 0
			row["jimple5"]= 0
			row["jimple6"]= 0
			current = $1
		 }
		 row[$2] = $3
	     }	 
	     END {
			if (row["interp"] == 0) {
				codegenOverInterp = 0
			} else {
				codegenOverInterp = int ( ((row["codegen"]+0.0)/(row["interp"]+0.0) * 100.0) +0.5)
			}
			printf("%-16s %2d%% %6d %6d %6d %6d %6d %6d %6d %6d \n", $1, codegenOverInterp, row["interp"], row["codegen"], row["jimple1"], row["jimple2"], row["jimple3"], row["jimple4"], row["jimple5"], row ["jimple6"])
			row["interp"] = 0
	    }'  
