#!/bin/bash -

false=0; true=1

# Usage: arch [ hostname ] returns hostname, cores, startcore
#
#   Define machine architecture based on starting socket, CPUs (cores) per socket, number of
#   sockets, has hyperthreading.

start=0

arch() {
	hostname=${1:-`hostname`}			# return value
	hashyper=${true}					# assume machine has hyperthreads
	if [ "${hostname}" = "plg2" ] ; then
		startsocket=${start}
		cps=16							# coresPerSocket
		sockets=2
		hashyper=${false}				# has no hyperthreads
	elif [ "${hostname}" = "nasus" ] ; then
		startsocket=${start}
		cps=64							# coresPerSocket
		sockets=2
	elif [ "${hostname}" = "pyke" ] ; then
		startsocket=${start}
		cps=24							# coresPerSocket
		sockets=2
	elif [ "${hostname}" = "jax" ] ; then
		startsocket=${start}
		cps=24							# coresPerSocket
		sockets=4
	else
		echo "unsupported host" ${hostname}
		exit 1
	fi
	cores=$(( ${cps} * ${sockets} ))
	startcore=$(( ${startsocket} * ${cps} ))
}

# Usage: affinity (global cps, sockets, startsocket, hashyper, cores, startcore, wrap)
#   returns taskset argument
#
#   This routine assumes hyperthreading has only 2 hyperthreads per core.
#
#   If hyperthread scanning is used: processor units are assigned across the low-number hyperthreads
#   of the socket's cores. When the low-number hyperthreads are filled, the high-number hyperhtreads
#   are assigned across the socket's cores. Then the next socket is assigned.
#
#   If hyperthread wrapping is used: processor units are assigned in low/high-number pairs of
#   hyperthreads across the socket's cores. Then the next socket is assigned.

wrap=${false}							# set to control hyperthread assignment across socket cores

affinity() {
	if [ ${wrap} -eq ${true} -a ${hashyper} -eq ${false} ] ; then
		echo "architecture does not support hyperthreading for wrapping"
		exit 1
	fi
	taskset=""							# return value
	set -- $(( ${1} - 1 ))				# decrement $1
	if [ ${1} -eq 0 ] ; then taskset="${startcore}-${startcore}"; return; fi
	if [ ${1} -ge $(( ${cps} * ( ${sockets} - ${startsocket} ) * ( ${hashyper} + 1 ) )) ] ; then # error
		echo "not enough cores $(( ${cores} * ${sockets} )) for $(( ${1} + 1 )) starting at ${startcore}"
		exit 1
	fi
	if [ ${hashyper} -eq ${false} ] ; then taskset="${startcore}-$(( ${1} + ${startcore} ))"; return; fi # no hyperthreads
	start2=$(( ${startcore} + ${cores} ))
	if [ ${wrap} -eq ${true} ] ; then 	# hyperthread wrapping
		end1=$(( ${1} / 2 + ${startcore} ))
		end2=$(( ${end1} + ${cores} ))
		if [ $(( ${1} % 2 )) -eq 0 ] ; then
			end2=$(( ${end2} - 1 ))
		fi
		taskset="${startcore}-${end1},${start2}-${end2}"
	else								# hyperthread scanning
		if [ ${1} -lt ${cps} ] ; then taskset="${startcore}-$(( ${1} + ${startcore} ))"; return; fi
		filled=$(( ${1} / ( ${cps} * 2 ) * ${cps} ))
		modulus=$(( ${1} % ( ${cps} * 2 ) ))	# leftover cores added to saturated sockets
		if [ ${modulus} -gt ${cps} ] ; then
			taskset="${startcore}-$(( ${startcore} + ${filled} + ${cps} - 1 )),${start2}-$(( ${start2} + ${filled} + ${modulus} % ${cps} ))"
		else
			taskset="${startcore}-$(( ${startcore} + ${filled} + ${modulus} )),${start2}-$(( ${start2} + ${filled} - 1 ))"
		fi
	fi
}

numtimes=5

num_threads='2 4 8 16 24 32'
# num_threads='2'

# toggle benchmarks
zero=${false}
contend=${true}
barrier=${false}
churn=${false}
daisy_chain=${false}
hot_potato=${false}
pub_sub=${false}

runCFA=${true}
runGO=${true}
# runCFA=${false}
# runGO=${false}

cfa=~/cfa-cc/driver/cfa

# Helpers to minimize code duplication

# repeats a command ${numtimes}
preprint=''
repeat_command() {
    t=1
    while [ ${t} -le ${numtimes} ] ; do
        echo -n -e ${preprint}
        "${@}"
        t=`expr ${t} + 1`
    done
}

# prints the leading info for a given run of a variant
print_header() {
    echo ${1}':'
    echo -e "cores\tthroughput (entries)"
}

# runs the current benchmark with provided args
# only works for standard-run benchmarks (not Akka)
# must split into pre and post args to be able to supply val of p
pre_args=''
post_args=''
single_run() {
    affinity ${1}
    preprint="${1}\t"
    repeat_command taskset -c ${taskset} ./a.${hostname} ${pre_args} ${1} ${post_args}
}

# runs the current bench for all processor vals
# works for standard benchs that dont need to set a config file (not Akka or CAF)
run_bench() {
    for p in ${num_threads} ; do
        single_run ${p}
    done
}

arch # get hostname

# set up leading info for python script
echo $numtimes
echo $num_threads

if [ ${runCFA} -eq ${true} ]; then
    echo -n 'CFA '
fi
if [ ${runGO} -eq ${true} ]; then
    echo -n 'Go '
fi
echo ""

# done printing header info for output

# cfa flags
cfa_flags='-quiet -O3 -nodebug -DNDEBUG'

# run the benchmarks

run_contend() {
    post_args=${1}

    if [ ${runCFA} -eq ${true} ] ; then
        cd cfa # CFA RUN
        print_header 'CFA'
        ${cfa} ${cfa_flags} ${2}.cfa -o a.${hostname} > /dev/null 2>&1
        run_bench
        rm a.${hostname}
        cd - > /dev/null
    fi # done CFA

    if [ ${runGO} -eq ${true} ] ; then
        cd go/${2} # Go RUN
        print_header 'Go'
        go build -o a.${hostname} > /dev/null 2>&1
        run_bench
        rm a.${hostname}
        cd - > /dev/null
    fi # done Go
}

# /usr/bin/time -f "%Uu %Ss %Er %Mkb"
if [ ${contend} -eq ${true} ] ; then
    echo "contend: "
    run_contend '128' 'contend'
fi

if [ ${zero} -eq ${true} ] ; then
    echo "zero: "
    run_contend '0' 'contend'
fi

if [ ${barrier} -eq ${true} ] ; then
    echo "barrier: "
    run_contend '' 'barrier'
fi

if [ ${churn} -eq ${true} ] ; then
    echo "churn: "
    run_contend '' 'churn'
fi

if [ ${daisy_chain} -eq ${true} ] ; then
    echo "daisy_chain: "
    run_contend '' 'daisy_chain'
fi

if [ ${hot_potato} -eq ${true} ] ; then
    echo "hot_potato: "
    run_contend '' 'hot_potato'
fi

if [ ${pub_sub} -eq ${true} ] ; then
    echo "pub_sub: "
    run_contend '' 'pub_sub'
fi
