#!/bin/sh
# Script that reads in output generated by Manager.timeAndMemory()
# and prints the averages of the second and successive runs 

#
# @Authors: Christopher Hylands, Steve Neuendorffer
#
# @Version: $Id: memorystats,v 1.15 2005/02/28 20:51:01 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

# Alternate lines look like
# 719 ms. Memory: 3520K Free: 2212K (63%)
# GCStats: static: 160K staticRT: 3K totalStatic: 163K dynamicRT: 18532K
#
# The GCStats lines are produced by running java -Xloggc and then
# running $PTII/util/testsuite/gcstats


awk 'BEGIN {min=99999999}
    NR > 1  {   if ( $0 ~ /ms. Memory:/) {
		    count++;
		    sum+= $1;
		    if ($1 < min) {
			min = $1
		    } else if ($1 > max) {
			max = $1
		    }
                    print $0
                    getline
                    print $0
		    static=substr($3, 1, length($3)-1) + 0
		    #staticRT=substr($5, 1, length($5)-1) + 0
		    #totalStatic=substr($7, 1, length($7)-1) + 0
                    dynamicRT=substr($5, 1, length($5)-1) + 0

		    staticSum += static
		    #staticRTSum += staticRT
		    #totalStaticSum += totalStatic
		    dynamicRTSum += dynamicRT
		}
	    }
	END {   
		if (count==0) {
		    print description ": count == 0! Perhaps the output did not contain   \"ms. Memory\""
		} else {
		    timeAverage= int(sum/count+0.5)
		
		    print description ": Averages of second and succeeding " count-1 " runs:" 
		    #print description " ave: " timeAverage " ms. Stat: " staticSum/count "K StatRT: " staticRTSum/count "K totalStatic: " totalStaticSum/count "K DynRT: " dynamicRTSum/count "K"

                    print description " ave: " timeAverage " ms. Stat: " staticSum/count "K DynRT: " dynamicRTSum/count "K"
	       }
}' description="$1" -

