#!/bin/bash

dir_tool_2="/home/ercoppa/Desktop/code/C/BSA/malloclab-tools/alloc-analyser/"
trace_dir="/home/ercoppa/Desktop/code/C/BSA/malloc-benchmarks"
trace_dir_2="../malloc-benchmarks/"
initial_dir=`pwd`

program[0]="cfrac"
program[1]="espresso"
program[2]="perl"
program[3]="ghostscript"
program[4]="ls"
program[5]="inkscape"
program[6]="gimp"
program[7]="deluge"
program[8]="vlc"
program[9]="audacity"

allocator[0]="BSA"
allocator[1]="BSAllnpa"
allocator[2]="TLSF"
allocator[3]="BSApp"
allocator[4]="BSAppnpa"
allocator[5]="BSAppas"
allocator[6]="BSAppavg"
allocator[7]="BSAppnpaavg"
allocator[8]="BSAppasavg"
allocator[9]="BSAppvar"
allocator[10]="BSAppnpavar"
allocator[11]="BSAppasvar"

allocator_conf[0]="M_1=PAGE_ALLOCATION"
allocator_conf[1]="M_1=DISABLE_SPLIT_COALESCE M_2=DISABLE_ALTERNATE_SPLIT_SIDE"
allocator_conf[2]=""
allocator_conf[3]="M_1=PLUS_PLUS M_2=DISABLE_ALTERNATE_SPLIT_SIDE M_3=PAGE_ALLOCATION"
allocator_conf[4]="M_1=PLUS_PLUS M_2=DISABLE_ALTERNATE_SPLIT_SIDE"
allocator_conf[5]="M_1=PLUS_PLUS M_3=PAGE_ALLOCATION"
allocator_conf[6]="M_1=PLUS_PLUS M_2=DISABLE_ALTERNATE_SPLIT_SIDE M_3=PAGE_ALLOCATION"
allocator_conf[7]="M_1=PLUS_PLUS M_2=DISABLE_ALTERNATE_SPLIT_SIDE"
allocator_conf[8]="M_1=PLUS_PLUS M_3=PAGE_ALLOCATION"
allocator_conf[9]="M_1=PLUS_PLUS M_2=DISABLE_ALTERNATE_SPLIT_SIDE M_3=PAGE_ALLOCATION"
allocator_conf[10]="M_1=PLUS_PLUS M_2=DISABLE_ALTERNATE_SPLIT_SIDE"
allocator_conf[11]="M_1=PLUS_PLUS M_3=PAGE_ALLOCATION"

allocator_mode[3]="POP"
allocator_mode[4]="POP"
allocator_mode[5]="POP"
allocator_mode[6]="AVG"
allocator_mode[7]="AVG"
allocator_mode[8]="AVG"
allocator_mode[9]="VAR"
allocator_mode[10]="VAR"
allocator_mode[11]="VAR"

REPEAT_BENCH=3
CA=0

while [ $CA -lt ${#allocator[@]} ]; do

	echo "Eseguendo benchmarks con ${allocator[$CA]}..."

	MAKE_ARG=${allocator_conf[$CA]}
	COUNTER=0;
	MODE=${allocator_mode[$CA]}

	if [ "${allocator[$CA]}" = "TLSF" ]; then
		dir_tool_1="/home/ercoppa/Desktop/code/C/BSA/malloclab-TLSF/"
	else
		dir_tool_1="/home/ercoppa/Desktop/code/C/BSA/malloclab-BSA/"
	fi
	
	cd $initial_dir
	if [ -f ${allocator[$CA]}.php ]; then
		rm ${allocator[$CA]}.php
	fi
	echo "<?php" >> ${allocator[$CA]}.php
	while [ $COUNTER -lt ${#program[@]} ]; do
		if [[ "${allocator[$CA]}" = "BSA" || "${allocator[$CA]}" = "TLSF" || "${allocator[$CA]}" = "BSAllnpa" ]]; then
			COUNTER_2=0
			time[0]=""
			space[0]=""
			cd  $dir_tool_1
			make clean >/dev/null
			make $MAKE_ARG >/dev/null
			for i in $( ls $trace_dir/${program[$COUNTER]}/trace/glibc/); do
				Q=0
				time[$COUNTER_2]=0
				space[$COUNTER_2]=0
				while [ $Q -lt $REPEAT_BENCH ]; do
					# echo -n $i": "
					RESULT=`./mdriver -v -f $trace_dir_2/${program[$COUNTER]}/log/glibc/$i | grep Total`
					# echo $RESULT | awk '{print "Space:", $2, "- Time:", $5}'
					let time[$COUNTER_2]=time[$COUNTER_2]+`echo $RESULT | awk '{print $5}'`
					let space[$COUNTER_2]=space[$COUNTER_2]+`echo $RESULT | awk '{print $2}'`
					let Q=Q+1
				done
				let time[$COUNTER_2]=time[$COUNTER_2]/$REPEAT_BENCH
				let space[$COUNTER_2]=space[$COUNTER_2]/$REPEAT_BENCH
				let COUNTER_2=COUNTER_2+1
			done
			# echo ${program[$COUNTER]}:
			K=0
			cd $initial_dir
			echo -n "\$time['"${program[$COUNTER]}"']['"${allocator[$CA]}"'][0] = array( " >> ${allocator[$CA]}.php
			while [ $K -lt $COUNTER_2 ]; do
				echo -n ${time[$K]} >> ${allocator[$CA]}.php
				J=0
				let J=K+1
				if [ "$J" -ne "$COUNTER_2" ]; then
					echo -n ", " >> ${allocator[$CA]}.php
				else
					echo -n " " >> ${allocator[$CA]}.php
				fi
				let K=K+1
			done
			echo ");" >> ${allocator[$CA]}.php
			K=0
			echo -n "\$space['"${program[$COUNTER]}"']['"${allocator[$CA]}"'][0] = array( " >> ${allocator[$CA]}.php
			while [ $K -lt $COUNTER_2 ]; do
				echo -n ${space[$K]} >> ${allocator[$CA]}.php
				J=0
				let J=K+1
				if [ "$J" -ne "$COUNTER_2" ]; then
					echo -n ", " >> ${allocator[$CA]}.php
				else
					echo -n " " >> ${allocator[$CA]}.php
				fi
				let K=K+1
			done
			echo ");" >> ${allocator[$CA]}.php
			let COUNTER=COUNTER+1
			
		else
			if [[ "$MODE" = "POP" || "$MODE" = "AVG" ]]; then
				MIN=10
				MAX=90
				STEP=20
			elif [ "$MODE" = "VAR" ]; then
				MIN=115
				MAX=920
				STEP=115
			else
				echo ${allocator[$CA]}
				echo "You must indicate a mode between POP, AVG and VAR."
				exit 1
			fi

			THRESHOLD=$MIN
			while [ $THRESHOLD -le $MAX ]; do

				COUNTER_2=0
				time[0]=""
				space[0]="" 
				for i in $( ls $trace_dir/${program[$COUNTER]}/trace/glibc/); do
					cd $dir_tool_2
					
					if [ "$MODE" = "POP" ]; then
						make M_1=POPULAR_THRESHOLD=$THRESHOLD >/dev/null
					elif [ "$MODE" = "AVG" ]; then
						make M_2=AVERAGE_POPULAR_THRESHOLD=$THRESHOLD >/dev/null
					else
						make M_2=VARIANCE_POPULAR_THRESHOLD=$THRESHOLD >/dev/null
					fi
					./alloc-analyser -c $trace_dir/${program[$COUNTER]}/trace/glibc/$i test.htm >/dev/null

					cd  $dir_tool_1
					make clean >/dev/null
					make $MAKE_ARG >/dev/null
					Q=0
					time[$COUNTER_2]=0
					space[$COUNTER_2]=0
					while [ $Q -lt $REPEAT_BENCH ]; do
						# echo -n $i": "
						RESULT=`./mdriver -v -f $trace_dir_2/${program[$COUNTER]}/log/glibc/$i | grep Total`
						# echo $RESULT
						# echo $RESULT | awk '{print "Space:", $2, "- Time:", $5}'
						let time[$COUNTER_2]=time[$COUNTER_2]+`echo $RESULT | awk '{print $5}'`
						let space[$COUNTER_2]=space[$COUNTER_2]+`echo $RESULT | awk '{print $2}'`
						let Q=Q+1
					done
					let time[$COUNTER_2]=time[$COUNTER_2]/$REPEAT_BENCH
					let space[$COUNTER_2]=space[$COUNTER_2]/$REPEAT_BENCH
					let COUNTER_2=COUNTER_2+1
				done
				# echo ${program[$COUNTER]}:
				K=0
				cd $initial_dir
				echo -n "\$time['"${program[$COUNTER]}"']['"${allocator[$CA]}"']["$THRESHOLD"] = array( " >> ${allocator[$CA]}.php
				while [ $K -lt $COUNTER_2 ]; do
					echo -n ${time[$K]} >> ${allocator[$CA]}.php
					J=0
					let J=K+1
					if [ "$J" -ne "$COUNTER_2" ]; then
						echo -n ", " >> ${allocator[$CA]}.php
					else
						echo -n " " >> ${allocator[$CA]}.php
					fi
					let K=K+1
				done
				echo ");" >> ${allocator[$CA]}.php
				K=0
				echo -n "\$space['"${program[$COUNTER]}"']['"${allocator[$CA]}"']["$THRESHOLD"] = array( " >> ${allocator[$CA]}.php
				while [ $K -lt $COUNTER_2 ]; do
					echo -n ${space[$K]} >> ${allocator[$CA]}.php
					J=0
					let J=K+1
					if [ "$J" -ne "$COUNTER_2" ]; then
						echo -n ", " >> ${allocator[$CA]}.php
					else
						echo -n " " >> ${allocator[$CA]}.php
					fi
					let K=K+1
				done
				echo ");" >> ${allocator[$CA]}.php
				let THRESHOLD=THRESHOLD+STEP
				
			done
			let COUNTER=COUNTER+1

		fi

	done
	echo "?>" >> ${allocator[$CA]}.php

	let CA=CA+1

done

exit 0
