#! /bin/csh -f
#
#  Run iorate tests with variable (fluctuating) workloads - for Bill Zahavi
#

# List of device files to test against
set devs = devices 

# List the automated tiering test plan to use - should be ONE test
#  -- a target IOPS level needs to be included in this test
set tests = tests-var

# The lower limit on the IOPS range - upper is 100%
set iops_low = 60

# The maximum shift in the IOPS range
set iops_shift = 20

# How frequently the skew can shift by 1 - in test runs, 20 minutes by default
set skew_min_wait = 6

# Set the odds - X to 1 that we will shift when allowed
set skew_odds = 10

# Set the odds that the shift will move up when allowed
set skew_up_base = 100
set skew_up_pct = 60



# Archive directory to place the results in
set archive = data

# Set silent mode - only warnings and errors are visible
set silent = -s


# Be sure the local directory is in the path, so we find ior_rand
set path = ( $path `pwd` )

# Note which run we are doing
@ count = `cat run_var.count` + 1
echo $count > run_var.count

# Set the log count to limit us to 100 sets of log files, then wrap, to keep it reasonable
@ log_count = $count % 100

# Get the prior IOPS level, adjust, and save
@ iops = `cat run_var.iops`
@ iops_shift = `ior_rand` % $iops_shift
@ iops_up = `ior_rand` % 2
if ( $iops_up == 1 ) then
    @ iops += $iops_shift
    echo "    --> Moving  IOPS  up   to "$iops"%" | tee -a run_var.log
else
    @ iops -= $iops_shift
    echo "    --> Moving  IOPS  down to "$iops"%" | tee -a run_var.log
endif
if ( $iops > 100 ) set iops = 100
if ( $iops < $iops_low ) set iops = $iops_low
echo $iops > run_var.iops
echo "    --> Running IOPS  at      "$iops"%" | tee -a run_var.log

# Get the prior skew level, adjust, and save
@ shift = `cat run_var.skew`
@ skew_last = `cat run_var.last`
@ skew_next = $skew_last + $skew_min_wait
@ skew_move = `ior_rand` % $skew_odds
if ( $count > $skew_next ) then
    if ( $skew_move == 1 ) then
	@ skew_up = `ior_rand` % $skew_up_base
	if ( $skew_up < $skew_up_pct ) then
	    @ shift += 1
	    echo "    --> Moving  shift up   to "$shift"%" | tee -a run_var.log
	else
	    @ shift -= 1
	    echo "    --> Moving  shift down to "$shift"%" | tee -a run_var.log
	endif

	echo $count > run_var.last
    endif
endif
if ( $shift > 99 ) set shift = 0
if ( $shift < 0 ) set shift = 99
echo $shift > run_var.skew
echo "    --> Running shift at      "$shift"%" | tee -a run_var.log


# Make the archive space if needed
if ( ! -d $archive ) then
    mkdir $archive
endif

# Log the start
date +"Starting io test pass "$count" at %T on %A, %B %e, %Y" | tee -a run_var.log

# In case silent is not set, make sure it is defined
if ( ! $?silent ) set silent = ""

# Run each device file
foreach dev ( $devs )

# Run each test file
    foreach test ( $tests )

	set target = ( $silent -f $dev".ior" -t $test".ior" )

	./iorate $target -r$iops -c$shift -o $archive"/run_var."$log_count

# this is VMAX FAST VP specific, to gather tiering statistics after each run
	symfast list -tech ALL -demand -v -vp >& $archive"/run_var.fastvp."$log_count".log"

    end

end

# Note we are done
date +"Finished pass "$count" at %T on %A, %B %e, %Y" | tee -a run_var.log

