#/bin/sh
# This script is hereby placed in the public domain,
# as all shell scripts should be.
#
if [ -z $1 ];  then
  echo "Usage $0 vg-device fs testname (should be vgadaptec vgarchives";
  exit 1;
else
 VGDEV="$1"
fi

if [ -z $2 ];  then
  echo "Usage $0 vg-device fs testname (should be vgadaptec vgarchives";
  exit 1;
else
 FS="$2"
fi

if [ -z $3 ];  then
  echo "Usage $0 vg-device fs testname (should be vgadaptec vgarchives";
  exit 1;
else
 LABEL="$3"
fi

# the directory for dbench's tests (must be a
# mount point for automatic FS detection to work)
MNTPT=/media/tests
if [ ! -d $MNTPT ]; then
 mkdir -p $MNTPT
fi

# Build a new lvm for test 
lvcreate -A y --addtag adaptec-test -L 800G -n lvdata_test -v $VGDEV
# In case of bug 
udevadm trigger

# Format our lv for test
case "$FS" in
	"ext4")
		echo " Building ext4 lv "
		mkfs.ext4 -v -L "test_ext4" -m1 $VGDEV/lvdata_test
		mount -t ext4 $VGDEV/lvdata_test $MNTPT -o noatime,nodiratime,data=ordered,auto_da_alloc,commit=15,acl,user_xattr,barrier=0,usrjquota=aquota.user,grpjquota=aquota.group,jqfmt=vfsv0
	;;

	"xfs")
		echo " Building xfs lv "
		mkfs.xfs -f -L "test_xfs" -l lazy-count=1 $VGDEV/lvdata_test
		mount -t xfs $VGDEV/lvdata_test $MNTPT -o noatime,nodiratime,nobarrier,usrquota,grpquota
	;;
esac

echo " Building quotas" 

quotacheck -cugfm $MNTPT
sleep 2
quotaon -ugv $MNTPT
sleep 1
#
# 

# Available governance Not possible on WALT ?
# cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_available_governors
# conservative userspace powersave ondemand performance
# OLDGOV=`cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor`
# get them from
# cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_available_frequencies
# 2400000 1900000 1500000 1200000 800000
# CPU speeds in kHz; your system will probably differ
SLOW=1000000
FAST=3600000

# Number of processors
PROCESSORS="0 1 2 3 4 5 6"

# So we can place a subdirectory with a complicated acl to check with them
mkdir -p $MNTPT/dbench-acl
T=$MNTPT/dbench-acl
chown 512:512 $T
chmod 0770 $T
setfacl -m d:u:512:rwx,d:g:512:rwx,d:o:---,d:g:1001:rwx,d:g:1004:rwx,d:g:1006:r-x,d:g:1002:r-x,d:u:fsp:rwx,d:u:vhe:rwx,d:u:jdw:r-x,g:1005:rw-,g:1009:rw,u:dr:r-- $T

#
# We run 3minutes with 20 concurrent clients 
#
COMMAND="dbench -t 120 -D $T 15"

# what filesystem is $MOUNTPT using?
fsys=$(mount | grep "$MNTPT" | awk '{ print $5; }')
if [ -z "$fsys" ] ; then
	echo "ERROR: $MNTPT not mounted?"
	exit 1
fi

# first, we sync & clear all caches 
echo " Sync & clear kernel & disks buffer caches"
eval "sync && echo 1 > /proc/sys/vm/drop_caches && sync"

# first, warm the caches by dbench
echo " Warming disk buffer caches "
eval $COMMAND | grep "[05] sec"
sync
echo " "
echo "              -- Starting Tests --                     "

# check which are available with 
# cat /sys/block/sd[a-z]/queue/scheduler
# until 11.2 (kernel < 26.34 ) noop deadline cfq anticipatory
# otherwise > 2.6.34 noop deadline cfq

# now do the actual testing for each schedulers noop deadline cfq (anticipatory as)
for i in cfq ; do
  echo ""

  # Put the elevator
  for d in $(ls /dev/sd[a-z] | cut -d "/" -f 3) ; do
    echo $i > /sys/block/$d/queue/scheduler
  done

# Repeat this step when governor can have more state
#  GOV="powersave"
#  # Adjust governance for slow motion
#  for g in $PROCESSORS ; do
#    # run slow CPU speed test
#    echo $GOV > /sys/devices/system/cpu/cpu$g/cpufreq/scaling_governor
#    sleep 1
#    echo "CPU$g  |  Gov : `cat /sys/devices/system/cpu/cpu$g/cpufreq/scaling_governor`  | Freq : `cat /sys/devices/system/cpu/cpu$g/cpufreq/scaling_cur_freq`";    echo ""
#  done
  # echo $SLOW > /sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq
  # sleep 1
#  echo "Slow CPU / Powersave Governance, $i scheduler:"
#  eval $COMMAND | tee /tmp/${fsys}-$GOV-${i}.txt
#  echo "              ----                     "
  GOV="auto"
  # run fast CPU speed test
  echo "Auto CPU / $GOV Governance, $i scheduler:"
  eval $COMMAND | tee /tmp/${LABEL}_${fsys}-${i}.txt
  echo "              ----                     "
  echo " "
done

#Umount partition 
echo " Cleanup "
eval "sync && echo 1 > /proc/sys/vm/drop_caches && sync"

quotaoff -ugv $MNTPT
sync

umount $MNTPT

lvremove -fv $VGDEV/lvdata_test
sync

## Reset governance to ondemand
#  echo "     --- Reseting Governance to old-state : $OLDGOV ------     "
#  # Adjust governance for old governance
#  for g in $PROCESSORS ; do
#    # run slow CPU speed test
#    echo $OLDGOV > /sys/devices/system/cpu/cpu$g/cpufreq/scaling_governor
#    sleep 1
#    echo -n "Proc : $g \t Governor : "
#    echo "CPU$g  |  Gov : `cat /sys/devices/system/cpu/cpu$g/cpufreq/scaling_governor`  | Freq : `cat /sys/devices/system/cpu/cpu$g/cpufreq/scaling_cur_freq`";
#    echo ""
#  done
echo " --- End test at `date +"%F %X"` "
echo " "

