#!/bin/bash
#------------------------------------------------------------------------------
#------------------------------------------------------------------------------
function usage()
{
echo -e "\nUsage:\n\t$(basename $0) [-dl dir_list_file] [-T] [-f]\n\n"
exit 0
}
#------------------------------------------------------------------------------
function usageError()
{
echo -e "\nUsage Error: $1\n\n"
exit 1
}
#------------------------------------------------------------------------------
function load_verify_path_list()
{
declare -i dc=0
declare tag
echo "Loading data for $(wc -l $PathListFile|cut -d ' ' -f 1) dirs from [$PathListFile]."
while read dir; do
tag=d.$(echo $dir|sed "s://::"|sed "s:\.::g"|tr '/' '_'|tr ' ' '_'|tr '&' '_'|tr '!' '_')
tag=$(echo $tag|sed "s:_$::")
[[ -r $LogDir/$tag ]] && continue
PathList[$dc]=$dir
DirTag[$dc]="$tag"
dc+=1
done < $PathListFile
echo "Loaded $dc dirs for processing."
DirCount=$dc
}
#------------------------------------------------------------------------------
#
#------------------------------------------------------------------------------
function dry_run()
{
echo -e "\n\nDRY RUN: Potential $DirCount dirs to verify:\n"
declare -i i
echo -e "${H}\nProcessing.\n"
i=0; while [[ $i -lt $DirCount ]]; do
echo " $i [${PathList[$i]}] - [${DirTag[$i]}]";
i+=1;
done
}
#------------------------------------------------------------------------------
function count_verifies
{
declare -i c=0
if [[ $TestMode -eq 0 ]]; then
c=$($P4BIN monitor show -s R | grep verify | wc -l)
else
c=$(/bin/ps | grep "/bin/sleep" | grep -v grep | wc -l)
fi
echo $c
return $c
}
#------------------------------------------------------------------------------
#
#------------------------------------------------------------------------------
function live_run()
{
echo -e "\n\nLIVE RUN: Potential $DirCount dirs to verify:\n"
declare -i i
declare -i max_procs
declare -i curr_procs
declare -i sleepDelay=30
[[ $TestMode -eq 1 ]] && sleepDelay=2
echo "Processing."
i=0; while [[ $i -lt $DirCount ]]; do
max_procs=$(grep MAX_VERIFY_PROCS= $CfgFile|cut -d '=' -f 2)
curr_procs=$(count_verifies)
echo "Throttle status: $curr_procs of max $max_procs verify procs running."
while [[ $curr_procs -ge $max_procs ]]; do
echo "Too may verifies already running. Waiting $sleepDelay secs before checking again."
sleep $sleepDelay;
max_procs=$(grep MAX_VERIFY_PROCS= $CfgFile|cut -d '=' -f 2)
curr_procs=$(count_verifies)
echo "Throttle check: $curr_procs of max $max_procs verify procs running."
done
echo verify_dir "${PathList[$i]}" "${DirTag[$i]}"
verify_dir "${PathList[$i]}" "${DirTag[$i]}"
Pids="$Pids $NewPid"
i+=1;
done
if [[ -n "$Pids" ]]; then
echo Waiting on pids: $Pids.
wait $Pids
Pids=
[[ -n "$Garbage" ]] && /bin/rm -f $Garbage
Garbage=
fi
}
#------------------------------------------------------------------------------
# Generate, execute, and kick off a temporary verify script. Delete the script
# and leave the log.
#------------------------------------------------------------------------------
function verify_dir()
{
declare subdir=$1
declare tag=$2
declare tmpScript=$LogDir/verify.$tag.sh
declare log=$LogDir/verify.$tag.log
declare targetDir=$(dirname "$subdir")
# Process directories handled earlier only if '-f' is used.
if [[ $Force -eq 0 ]]; then
if [[ -r $log ]]; then
echo "Subdir [$subdir] already processed. Skipping."
NewPid=""
SCount+=1;
return 0
else
PCount+=1;
fi
else
PCount+=1;
fi
echo "Verifying [$subdir]."
echo \#\!/bin/bash > $tmpScript
echo "startTime=\$SECONDS endTime= elapsedTime=" >> $tmpScript
echo "$P4BIN -s verify -q -b 50000 $subdir" >> $tmpScript
echo "ExitCode=\$?" >> $tmpScript
echo "endTime=\$SECONDS" >> $tmpScript
echo "runTime=\$((\$endTime-\$startTime))" >> $tmpScript
echo "echo VERIFY_TIME: Subdir [$subdir] verified in \$((\$runTime/3600)) hours \$((\$runTime%3600/60)) minutes \$((\$runTime%60)) seconds." >> $tmpScript
echo "echo VERIFY_EXIT_CODE=\$ExitCode" >> $tmpScript
chmod +x $tmpScript
[[ $VERBOSITY -gt 3 ]] && echo -e "Generated script $tmpScript:\n$(cat $tmpScript)\n"
if [[ $TestMode -eq 0 ]]; then
nohup $tmpScript > $log 2>&1 &
NewPid=$!
else
NewPid=""
fi
Garbage+="$tmpScript "
}
#==============================================================================
# Initialization.
declare PathListFile
PathList[0]="SkipMe"
DirTag[0]="SkipMe"
declare -i DirCount=0
declare LogDir=${P4HOME}/logs/verify
[[ -d "${LogDir}" ]] || /bin/mkdir -p "${LogDir}"
declare CfgFile=/p4/common/bin/mpverify.cfg
declare Pids=
declare NewPid=
declare Garbage=""
declare H="=============================================================================="
declare -i PCount=0
declare -i SCount=0
#==============================================================================
# Command Line Processing
export VERBOSITY=3
declare -i TestMode=0
declare -i shiftArgs=0
declare -i Force=0
set +u
while [[ $# -gt 0 ]]; do
case $1 in
(-h) usage -h;;
(-man) usage -man;;
(-T|-n) TestMode=1;;
(-dl) PathListFile=$2; shiftArgs=1;;
(-f) Force=1;;
(-v1) export VERBOSITY=1;;
(-v2) export VERBOSITY=2;;
(-v3) export VERBOSITY=3;;
(-v4) export VERBOSITY=4;;
(-v5) export VERBOSITY=5;;
(-D) set -x;; # Debug; use 'set -x' mode.
(*) usageError "Unknown arg ($1).";;
esac
# Shift (modify $#) the appropriate number of times.
shift; while [[ $shiftArgs -gt 0 ]]; do
[[ $# -eq 0 ]] && usageError "Bad usage."
shiftArgs=$shiftArgs-1
shift
done
done
set -u
#------------------------------------------------------------------------------
# MAIN Program
#------------------------------------------------------------------------------
echo -e "${H}\nStarted with command line:\n\t$0 $*"
if [[ -a "$P4INSTANCE" ]]; then
echo P4INSTANCE not set. Aborting.
exit 1
fi
load_verify_path_list
dry_run
live_run
echo "All scheduled verifies have been kicked off."
if [[ -n "$Pids" ]]; then
echo Waiting on pids: $Pids.
wait $Pids
Pids=
fi
if [[ -n "$Garbage" ]]; then
/bin/rm -f $Garbage
Garbage=
fi
echo "Processed $PCount dirs, skipped $SCount."
echo "All Done. That took $(($SECONDS/3600)) hours $(($SECONDS%3600/60)) minutes $(($SECONDS%60)) seconds."
exit 0
# |
Change |
User |
Description |
Committed |
|
#1
|
11680 |
C. Thomas Tyler |
Added sample massively parallel processing script. |
|
|