BEGIN {
	# same as eventdir, except also prints out syncsnc for wt case
	indx = 0
	str[indx++] = "dirninst"
	str[indx++] = "dirnread"
	str[indx++] = "dirnrhit"
	str[indx++] = "dirnrmwr"
	str[indx++] = "dirnrmnw"
	str[indx++] = "dirnrmbn"
	str[indx++] = "dirnwrit"
	str[indx++] = "dirnwhit"
	str[indx++] = "dirnwmwr"
	str[indx++] = "dirnwmnw"
	str[indx++] = "dirnwhnw"
	str[indx++] = "dirnwmbn"

	str[indx++] = "dirninsm"
	str[indx++] = "dirnrmi1"
	str[indx++] = "dirnwi**"
	str[indx++] = "drtyrplc"
	str[indx++] = "dclnrplc"
	str[indx++] = "syncsnc"

	maxind = indx -1
	indx = 0
	for (i=0;i<=maxind;i++) freq[str[i]] = 0.0
	now = 0
		#now signals when to start looking for dirn stuff
	rdwrtflag = 0 #flag indicating whether first "writes:" has been read
	cpumax = 0
	zset = 0
}
{
	if ($1 == "scheme:") scheme = $2
	else if (($1 == "total") && ($2 == "references:")) totrefs = $3
	else if (($1 == "instruction") && ($2 == "misses:"))  
	{
		freq["dirninsm"] = $4
		wtinsm = $4
	}
	else if (($4 == "dirty") && ($5 == "shared")) drty = $7
	else if (($2 == "replaces") && ($4 == "dir:")) trplc = $5
	else if (($1 == "total") && ($2 == "shared")) wtshrd = $4 * 100/totrefs
	else if (($1 == "private") && ($2 == "read")) wtprdm = $4 * 100/totrefs
	else if (($1 == "private") && ($2 == "write")) wtpwrm= $4 * 100/totrefs
	else if (($2 == "synch.")) freq["syncsnc"] = $6 * 100/totrefs
	else if ($8 == "ptrs:") freq["dirnrmi1"] = $9 * 100/totrefs
	else if (($1 == "writes:") && (rdwrtflag == 0))
	{
		rdwrtflag = 1
		totalwrites = $2
	}
	else if (($1 == "write") && ($2 == "hit") && ($4 == "read"))
	 wrthitreadmode = $6
	else if (($1 == "write") && ($2 == "hit") && ($4 == "write")) 
	 wrthitwrtmode = $6
	else if (($1 =="total") && ($3 == "misses:")) totwrtmisses = $4
	else if ($2 == "miss" && $5 == "mode:") wrtbcks = $6 * 100/totrefs
	else if ($2 == "caches(s):" && $1 == 0 && zset == 0)
	{
		zset = 1
		zeroinvals = $3 * 100 / totrefs
	}
	else if ($2 == "caches(s):")
	{
		nfreq[$1] = $3 * 100 / totrefs
		if (cpumax<$1) cpumax=$1
	}
	else if ((now == 1) || ($1 == "dirninst")) 
	{
		now = 1
		use = 0
		for (i=0;i<=maxind;i++) if ($1 == str[i]) use = 1
		if (use) freq[$1] = $2
	}
}
END {
	# need to do some post-processing on numbers read in
	# include traffic due to private references
	freq["dirnrmbn"] += wtprdm
	freq["dirnwmbn"] += wtpwrm
	freq["dirnwhit"] += (totalwrites - (wrthitreadmode + wrthitwrtmode + totwrtmisses)) * 100 / totrefs
	# assign drtryrplc finally, because totrefs has its value
	freq["drtyrplc"] = drty * 100/totrefs
	freq["dclnrplc"] = (trplc - drty) * 100/totrefs

	# All invalidates in the totals list are now added up instead of
	# counting some of them in the whnw and wmnw categories and
	# subtracting those out of the invalidate totals before summing.
	# However, writebacks, which are also included in the invalidates
	# list, must be counted under writebacks for bookkeeping purposes,
	# and must not be summed into these invalidations. Thus the
	# writebacks are subtracted out below.

	nfreq[0] -= drty * 100/totrefs	# nfreq[0] should not contain dirty replaces
	nfreq[0] += zeroinvals		# nfreq[0] contains only 0 invals due to reads; add those for writes
	nfreq[1] -= wrtbcks

	invs = 0.0
#	printf("%d ",cpumax)
	for (i=0;i<=cpumax;i++) 
	{
#		printf("%8.4f ",nfreq[i])
		invs += (i+1.0) * nfreq[i] 
# 		1.0 is added to account for final ack from each inv
	}
	printf("\n")

#	printf("%8s ", "scheme")
#	for (i=0;i<=maxind;i++) 
#		if ((str[i]!="dirninst")&&(str[i]!="dirnread")&&(str[i]!="dirnrhit")&&(str[i]!="dirnrmbn")&&(str[i]!="dirnwrit")&&(str[i]!="dirnwhit")&&(str[i]!="dirnwmbn")&&(str[i]!="drtyrplc")&&(str[i]!="dclnrplc")&&(str[i]!="syncsnc")&&(str[i]!="dirninsm"))
#		{
#			if (str[i]=="dirnwi**") 
#				printf("%8s ", "invals")
#			else if (str[i]!="dirnrmi1") 
#				printf("%8s ", str[i]);
#		}
#	printf("\n")

#	allfrac = 0.33
#	above represents the fraction of data refs
#	printf("%8s ", scheme)
#	for (i=0;i<=maxind;i++) 
#		if ((str[i]!="dirninst")&&(str[i]!="dirnread")&&(str[i]!="dirnrhit")&&(str[i]!="dirnrmbn")&&(str[i]!="dirnwrit")&&(str[i]!="dirnwhit")&&(str[i]!="dirnwmbn")&&(str[i]!="drtyrplc")&&(str[i]!="dclnrplc")&&(str[i]!="syncsnc")&&(str[i]!="dirninsm"))
#		{
#			if ((str[i]=="dirnrmwr")||(str[i]=="dirnwmwr")||(str[i]=="dirnwmnw")||(str[i]=="dirnwhnw"))
#				invs += freq[str[i]]
#
#			if (str[i]=="dirnrmi1") {}
#			else if (str[i]=="dirnwi**") 
#				{
#					printf("%8.4f ", invs * allfrac)
#				}
#			else 	
#				printf("%8.4f ", freq[str[i]] * allfrac)
#		}
# The above is disgusting.  Chaiken rewrote it:
# Fudge for RISC architecture:
	allfrac = 0.33
	printf ("Scheme: Single link chain\n\n")
	printf ("          Event               %% Prob Out Msg Out Size In Msg In Size\n")
	printf ("----------------------------  ------ ------- -------- ------ -------\n")
	printf ("Read Miss in Write Mode:      %1.4f   %3s      %d       %3s    %3s\n", freq["dirnrmwr"] * allfrac, "yes", 4, "yes", "***")
	invs += freq["dirnrmwr"]
	printf ("Read Miss not in Write Mode:  %1.4f   %3s      %d       %3s    %3s\n", freq["dirnrmnw"] * allfrac, "yes", 4, "yes", "***")
	printf ("Write Miss in Write Mode:     %1.4f   %3s      %d       %3s    %3s\n", freq["dirnwmwr"] * allfrac, "yes", 4, "yes", "***")
	invs += freq["dirnwmwr"]
	printf ("Write Miss not in Write Mode: %1.4f   %3s      %d       %3s    %3s\n", freq["dirnwmnw"] * allfrac, "yes", 4, "yes", "***")
	invs += freq["dirnwmnw"]
	printf ("Write Hit not in Write Mode:  %1.4f   %3s      %d       %3s    %3s\n", freq["dirnwhnw"] * allfrac, "yes", 4, "yes", "***")
	invs += freq["dirnwhnw"]
	printf ("Other Messages:               %1.4f   %3s      %d       %3s    %3s\n", invs * allfrac, "yes", 4, "no", "N/A")

printf("\n")
printf("*** - Inbound Message Size = (4 + (cache block size / flit size)) flits\n")
printf("\n")
printf("Input parameters to calculate:\n")
printf("\n")
printf("message rate = Sum(over events){ (msgs per event) * (event prob) }\n")
printf("\n")
printf("               Sum(over events){ Sum(over msgs){ msg size } * (event prob) }\n")
printf("avg msg size = -------------------------------------------------------------\n")
printf("                                       message rate\n")

}
