#!/bin/csh -f
#
# config-one:
#   make a dmcache configuration for a set of parameters.
# This takes a few steps
#
# -  combine the parameters into a directory name
#
# -  if the directory does not exist,
#   	make the directory
#   	link GraphFile, ConfigFile, ParamHelp to the new directory
#   	copy the *.param files to the new directory
#
#######
#
set usage='usage: config-one recsize file-maxblocks iopfs sorting layout Ndisk [ last ]\
where "last" is either max-oustanding or  "filter" filt-percent' 
#
#
# Part of
#              The STARFISH Parallel file-system simulator
#        (Simulation Tool for Advanced Research in File Systems)
# 
#                               David Kotz
#                           Dartmouth College
#                              Version 3.0
#                              October 1996
#                          dfk@cs.dartmouth.edu

if ($#argv < 6 && $#argv > 8) then
    echo "$usage"
    exit 1
endif

onintr cleanup

set recsize=$1
set fileblocks=$2
set iopfs=$3
set sort=$4
set layout=$5
set Ndisk=$6

# This is really sleazy.  Usually I use the last field in the config
# directory name for the maxout parameter, but occasionally I use it
# for the filter parameter.

set maxout=4
set filter=0
set last=$maxout
if ($#argv >= 7) then
    if ("$7" == filter) then
	set filter=$8
	set last=$filter
    else
	if ($#argv == 8) then
	    echo $usage
	    exit 1
	endif
        set maxout=$7
	set last=$maxout
    endif
endif

set dirname=configs/$recsize.$fileblocks.$iopfs.$sort.$layout.$Ndisk.$last

echo $dirname
if (-d $dirname) exit 2 

mkdir $dirname
ln -s ../../base/{Configfile,ParamHelp} $dirname/
cp base/*.param $dirname/
chmod +w $dirname/*.param

cd $dirname
echo $dirname

# NOT ALL PARAMS can be set this way...
# and here I assume that base is set to have all options #undef
# among the sort, iopfs, cpfs, and layout choices

switch ($iopfs)
case cache:
    setparam IOP_CACHE  true iopfs.param  || goto error
    setparam CP_NONE    true cpfs.param  || goto error
    breaksw

case general:
    setparam IOP_GENERAL true iopfs.param  || goto error
    setparam CP_DIRECT   true cpfs.param  || goto error
    setparam TWO_PHASE	false cpfs.param  || goto error
    breaksw

default:
    echo iopfs $iopfs not supported
    goto error
    breaksw
endsw

switch ($sort)
case sort:
    setparam DISK_SORT true diskmodel.param  || goto error
    breaksw
case nosort:
#    setparam DISK_SORT false diskmodel.param  || goto error
    breaksw
default:
    echo sort $sort not supported
    goto error
    breaksw
endsw

switch ($layout)
case contig:
    setparam LAYOUT_CONTIGUOUS true diskmodel.param  || goto error
    breaksw
case rsectors:
    setparam LAYOUT_RANDOM_SECTORS true diskmodel.param  || goto error
    breaksw
case rtracks:
    setparam LAYOUT_RANDOM_TRACKS true diskmodel.param  || goto error
    breaksw
default:
    echo layout $layout not supported
    goto error
    breaksw
endsw

setparam RECORD_SIZE $recsize file.param  || goto error

if ($recsize < 8192) then
    setparam MEMPUT_QUEUED true iopfs.param  || goto error
endif

setparam FILE_BLOCKS_MAX $fileblocks file.param  || goto error
setparam NO_OF_DISKS $Ndisk disk.param  || goto error

setparam MAX_OUTSTANDING $maxout iopfs.param  || goto error

setparam FILTER_PERCENT $filter iopfs.param  || goto error

set Nproc=`grep NO_OF_PROCESSORS conf.param | colex 3`
echo NO_OF_PROCESSORS is $Nproc
set maxprocthreads=`expr 2 + \( $Nproc - 2 \) \* $Ndisk \* \( $maxout + 1 \)`

setparam MAX_PROC_THREADS $maxprocthreads conf.param  || goto error

set maxthreads=`expr \( $Nproc - 2 \) + \( \( $Nproc - 2 \) \* \( $maxout + 1 \) + 1 \) \* $Ndisk`

setparam MAX_THREADS $maxthreads conf.param  || goto error

set maxstkblks=`expr 3 \* $maxthreads`

setparam MAX_STKBLKS $maxstkblks sim.param  || goto error

# setparam IOP_AHEAD_BEHIND false iopfs.param  || goto error

cd ../..
# domake-one $dirname

exit 0

error:

echo error in setparam, cannot configure $dirname
/bin/rm -rf $dirname
exit 3
