#!/bin/bash

check_leak() {
	HUGEPAGES_TOTAL=`grep HugePages_Total: /proc/meminfo | awk '{print $2}'`
	HUGEPAGES_FREE=`grep HugePages_Free: /proc/meminfo | awk '{print $2}'`
	if [ $HUGEPAGES_TOTAL != $HUGEPAGES_FREE ]; then
		echo Huge pages leaked
		exit -1
	fi
}

# set to -1 to run forever
ITERATIONS=60

echo $((3192*1048576)) > /proc/sys/kernel/shmmax
echo 2000 > /proc/sys/vm/nr_hugepages

echo Building test program
gcc -Wall tcbm.c -o tcbm || exit -1

ITER=0
while [ $ITER -ne $ITERATIONS ]; do
	echo Iteration $ITER
	./tcbm || exit
	check_leak
	ITER=$((ITER+1))
done
