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++] = "dirnwi01"
	str[indx++] = "drtyrplc"
	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
}
{
	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 (($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 == "caches(s):") && ($1 == 0) && ($3 != 0))
	{
		# glitch fix, see next one
		zeroinvals = $3
	}
	else if (($2 == "caches(s):") && ($1 == "1"))
	{
		# theory behind the next two if statements:
		# Reads can cause at most one invalidation or
		# cleanup message. These are included in dirnrm(nw,bn).
		# Writes in the form of dirn(wmwr,wmnw,whnw) cause one or
		# more invalidations. Of these writes dirnwmwr causes only
		# one and is included with dirnwmwr number. Of the
		# dirn(wmnw,whnw) one is included with the dirn(wmnw,whnw)
		# number, and for the remainder if there are any we
		# include them as the number of invalidations minus 1
		# times the frequency of number of invalidations.
		# These latter are summed to give the total number of
		# single invalidations.
		# We count only total write invalidaions. The first
		# set in the stat file are overwritten by these.
		# glitch: since whnw always counts one inval, need to keep
		# track of how many 0 invals there were and subtract them out

		freq["dirnwi01"] = 0.0
		freq["dirnwi01"] -= zeroinvals * 100/ totrefs
	}
	else if (($2 == "caches(s):") && ($1 > 1))
	{
		freq["dirnwi01"] += ($3 * ($1 - 1.0)) * 100/ totrefs
	}
	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

#	invs = 0
#	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]!="syncsnc")&&(str[i]!="dirninsm"))
#		{
#			if (str[i]=="dirnwi01") 
#				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]!="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")
#				invs += freq[str[i]]
#			else if (str[i]=="dirnwi01") 
#				{
#					invs += freq[str[i]]
#					printf("%8.4f ", invs * allfrac)
#				}
#			else 	
#				printf("%8.4f ", freq[str[i]] * allfrac)
#		}
# The above is disgusting.  Chaiken rewrote it:
# Fudge for RISC architecture:
	invs = 0.0
	allfrac = 0.33
	printf ("Scheme: %s\n\n",   scheme)
	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"]
	invs += freq["dirnrmi1"]
	invs += freq["dirnwi01"]
	printf ("Other Messages                %1.4f   %3s      %d       %3s    %3s\n", invs * allfrac, "yes", 4, "yes", " 4 ")

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")

}
