#!/var/opt/STORtools/bin/perl
# @(#)stormenu.pl 1.91 98/11/16

$PROGNAME   = "seq_dex";

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

$| = 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: seq_dex <controller number> <rw|ro> <runtime in mins> <storage device>\n";
    exit;
}

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

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

$TIME .= "m" if ($TIME =~ /^\d+$/);

if ($ENV{'DEX_ARGS'}) {
    ########################################################
    # desired test has been chosen by upper level menu and #
    # dex args placed in environment                       #
    ########################################################
    $dexargs = $ENV{'DEX_ARGS'};
} else {
    ############################
    # assume read only testing #
    ############################
    $JOB = "ro";
    $dexargs = "-v -S1 8g 0 $TIME -x 128k -r -e 1000";
}

#------------------------------------------------------------------------#
# 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_$$'";

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();
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");
