#!/var/opt/STORtools/bin/perl

require  "subroutines.pm";
&st_globals();

$PROGNAME   = "ran_dex";

$| = 1; # flush immediately
#####################################################################
# Purpose: To start up dex in the background and log the pid number #
# to a file. Stormenu will use this pid file when exiting stortools #
# for killing pids or cleaning.                                     #
#####################################################################

&check_root_access( $PROGNAME );
if (($ENV{'DEX_ARGS'} eq '') and ($#ARGV < 3 )) {
    print "\n  Usage: ran_dex <controller number> <rw|ro> <runtime in mins> <storage device> [dev [,dev]]\n";
    exit;
}

if (! -d $LOGDIR ) {
    `mkdir $LOGDIR`;
}

($BASECON, $JOB, $TIME) = @ARGV;
shift; shift; shift; 

$TIME .= "m" if ($TIME =~ /^\d+$/);
#####################################################
# Cut time in half for running 2 simultaneous tests #
#####################################################

if ($ENV{'DEX_ARGS'}) {
    ########################################################
    # desired test has been chosen by upper level menu and #
    # dex args placed in environment                       #
    ########################################################
    $dexargs = $ENV{'DEX_ARGS'};
#} elsif ($JOB eq "rw") {
#    ######################
#    # read/write testing #
#    ######################
#    $dexargs = "-v -R4 200m 0 $TIME -x 1k -wr -e 1000";
} else {
    ############################
    # assume read only testing #
    ############################
    $JOB = "ro";
    $dexargs = "-v -R4 200m 0 $TIME -x 1k -r -e 1000";
}

chomp($RELEASE  = `/bin/uname -r`);
if ($RELEASE < 5.8) {
  #####################################################
  # not allowed to run tests with transfer size of 1b #
  # because it could hang driver. change to 1k        #
  # and if -x 1b appears in second test, don't run it #
  #####################################################
  $dexargs =~ s/-x 1b/-x 1k/ if ($dexargs =~ /-x 1b/);
}
#------------------------------------------------------------------------#
# If specific drives are on the command line, collect them into $drives  #
#------------------------------------------------------------------------#
$drives = '';
foreach $i (@ARGV) {
    $drives = `/usr/bin/echo $drives /dev/rdsk/${i}s2`;
    chomp $drives;
}

if ($drives eq '') {
    #####################################
    # get them all from base controller #
    #####################################
    $drives = `/usr/bin/echo /dev/rdsk/${BASECON}t*s2`;
    chomp $drives;
}

#############################
# check for unlabeled LUN's #
#############################

foreach $i (split /\s+/, $drives) {
    $cmd = "/usr/bin/ksh -c \'/usr/sbin/devinfo -i $i 2>&1\'";
    $results = `$cmd`;
    chomp $results;
    if ($results =~ /Invalid argument|I\/O error|No such/) {
        print "WARNING: Possible missing or unlabeled disk/LUN, not testing $i\n";
    } else {
        $tested_drives .= " $i";
    }
}
$drives = $tested_drives;

$cmd = "/usr/bin/ksh -c '$BINDIR/dex32 $dexargs $drives 2>&1 & echo \$! > /tmp/pid_$$'" if $dexargs;

print "    Monitor physmem with vmstat to prevent memory starvation\n";
print "    Monitor /var/adm/messages for disk problems and warnings\n\n";

&logger("info", "2010", "Start Disk Exerciser $PROGNAME $JOB");
if ($JOB eq "rw") {
    print "******** WRITE TEST *******\n\n";
} else {
    print "********* READ TEST *******\n\n";
}
###############################################
# Launch command, get pid from temporary file #
# delete that file, write proper pid file for #
# stormenu to be able to kill later.          #
###############################################
&st_sync();
if ($dexargs) {
  ##################
  # run first test #
  ##################
  system($cmd);

  $pid = `/usr/bin/cat /tmp/pid_$$`;
  chomp $pid;
  `usr/bin/rm /tmp/pid_$$`;
  &make_pid_file($LOGDIR, $BASECON, $pid);
  while (wait2comp($pid)) {
      sleep 5;
  }
  `/usr/bin/rm $LOGDIR/pids/$BASECON.$pid`;
}
&st_sync();
print "********* END TEST *********\n";
&logger("info", "2011", "End Disk Exerciser $PROGNAME $JOB");
