From b5616b4ea495e2edfb0b51b1efac8a7684633308 Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Sun, 2 Aug 2009 11:08:33 +0200 Subject: [PATCH] consistency fix for scripts: foamCheckJobs, foamPrintJobs, foamLog - drop reference to license directory - check -help before checking directories - search for foamLog.db is more consistent with foamEtcFile $HOME/.OpenFOAM/$WM_PROJECT_VERSION $HOME/.OpenFOAM $WM_PROJECT_INST_DIR/site/$WM_PROJECT_VERSION $WM_PROJECT_INST_DIR/site $WM_PROJECT_DIR/etc $PROGDIR/tools --- bin/foamCheckJobs | 213 +++++++++++++++++++++---------------- bin/foamLog | 153 ++++++++++++++++---------- bin/foamPrintJobs | 106 ++++++++++-------- bin/{ => tools}/foamLog.db | 0 4 files changed, 280 insertions(+), 192 deletions(-) rename bin/{ => tools}/foamLog.db (100%) diff --git a/bin/foamCheckJobs b/bin/foamCheckJobs index a2696e1f80..8812c96466 100755 --- a/bin/foamCheckJobs +++ b/bin/foamCheckJobs @@ -38,7 +38,7 @@ # #------------------------------------------------------------------------------ -PROGNAME=${0##*/} +Script=${0##*/} #------------------------------------------------------------------------------- #- User settings @@ -48,9 +48,9 @@ NDAYSLIMIT=7 #------------------------------------------------------------------------------- #- work file -TMPFILE=/tmp/${PROGNAME}$$.tmp +TMPFILE=/tmp/${Script}$$.tmp #- work dir. Needs to be accessible for all machines -MACHDIR=$HOME/.OpenFOAM/${PROGNAME} +MACHDIR=$HOME/.OpenFOAM/${Script} DEFSTATEFILE=$HOME/.OpenFOAM/foamCheckJobs.out @@ -62,6 +62,29 @@ else fi +usage() { + cat< 13 dayDiff() { date -d "$1" > /dev/null 2>&1 - if [ $? -ne 0 ]; then + if [ $? -ne 0 ] + then #- option '-d' on date not supported. Give up. echo "0" else @@ -119,12 +144,14 @@ dayDiff() { # Also handles 'slaves' entries in jobInfo: # slaves 1 ( penfold.23766 ); getAllJobs() { - if notEmpty $1; then + if notEmpty $1 + then jobs=$1/* for f in $jobs do line=`grep '^[ ]*slaves' $f 2>/dev/null` - if [ $? -eq 0 ]; then + if [ $? -eq 0 ] + then slaveJobs=`echo "$line" | sed -e 's/.*(\(.*\)).*/\1/'` jobs="$jobs $slaveJobs" fi @@ -138,34 +165,12 @@ getAllJobs() { # releaseLock jobId lockFile # Releases lock on jobId releaseLock () { - if [ -f $2 ]; then + if [ -f $2 ] + then #- move lock to finishedJobs mv $2 $FOAM_JOB_DIR/finishedJobs/ fi - $ECHO "Lock on job $1 released." -} - - -printUsage() { -cat << LABEL -Usage: $PROGNAME [stateFile] - -This program checks all the locks in the license directory to see if -their processes are still running. Processes will not release their -lock if they exit abnormally. This program will try to obtain process -information on the machine the process ran on and release the lock -if the program is no longer running. - -Requirements: the environment variable FOAM_JOB_DIR needs to point to the -license directory and all machines have to be reachable using ssh. - -The output from checking all running jobs is collected in an optional -file. - -FILES: - \$FOAM_JOB_DIR/runningJobs locks for running processes - /finishedJobs ,, finished processes -LABEL + echo "Lock on job $1 released." } @@ -175,39 +180,50 @@ LABEL # #------------------------------------------------------------------------------- -#- Check a few things - -if [ ! "$FOAM_JOB_DIR" ]; then - $ECHO "$PROGNAME : FOAM_JOB_DIR environment variable not set." - $ECHO "This should point to your central license directory." - exit 1 -fi - -if [ ! -d "$FOAM_JOB_DIR" ]; then - $ECHO "$PROGNAME : The license directory accoring to FOAM_JOB_DIR is not valid." - $ECHO "FOAM_JOB_DIR: $FOAM_JOB_DIR" - exit 1 -fi -if [ ! -d "$FOAM_JOB_DIR/runningJobs" -o ! -d "$FOAM_JOB_DIR/finishedJobs" ]; then - $ECHO "$PROGNAME : The license directory according to FOAM_JOB_DIR is not valid." - $ECHO "FOAM_JOB_DIR: $FOAM_JOB_DIR" - exit 1 -fi - - -if [ $# -eq 1 ]; then - STATEFILE=$1 -elif [ $# -eq 0 ]; then +if [ $# -eq 1 ] +then + if [ "$1" = "-h" -o "$1" = "-help" ] + then + usage + fi + STATEFILE="$1" +elif [ $# -eq 0 ] +then STATEFILE=${STATEFILE:-$DEFSTATEFILE} else - printUsage + usage +fi + + +#- Check a few things + +if [ ! "$FOAM_JOB_DIR" ] +then + echo "$Script : FOAM_JOB_DIR environment variable not set." + echo exit 1 fi +if [ ! -d "$FOAM_JOB_DIR" ] +then + echo "$Script : directory does not exist." + echo " FOAM_JOB_DIR=$FOAM_JOB_DIR" + echo + exit 1 +fi +if [ ! -d "$FOAM_JOB_DIR/runningJobs" -o ! -d "$FOAM_JOB_DIR/finishedJobs" ] +then + echo "$Script : invalid directory." + echo " FOAM_JOB_DIR=$FOAM_JOB_DIR" + echo + exit 1 +fi + + + #- obtain rsh method RSH='ssh' echo "Using remote shell type : $RSH" - echo "" echo "Collecting information on jobs in" echo " $FOAM_JOB_DIR" @@ -226,13 +242,14 @@ do pid=`echo $machinePid | sed -e 's/.*\.\([0-9][0-9]*\)$/\1/'` fgrep "$machine" $TMPFILE >/dev/null 2>&1 - if [ $? -ne 0 ]; then - $ECHO "$machine" >> $TMPFILE + if [ $? -ne 0 ] + then + echo "$machine" >> $TMPFILE fi done -$ECHO "Found machines:" +echo "Found machines:" cat $TMPFILE -$ECHO "" +echo "" @@ -243,31 +260,34 @@ cnt=1 while true do machine=`sed -n -e "${cnt}p" $TMPFILE` - if [ ! "$machine" ]; then + if [ ! "$machine" ] + then break fi machFile=$MACHDIR/$machine rm -f $machFile - $ECHO "Contacting $machine to collect process information:" - if [ $machine = `hostname` ]; then - $ECHO " foamProcessInfo $machFile" + echo "Contacting $machine to collect process information:" + if [ $machine = `hostname` ] + then + echo " foamProcessInfo $machFile" foamProcessInfo $machFile >/dev/null 2>&1 else - $ECHO " $RSH $machine foamProcessInfo $machFile" + echo " $RSH $machine foamProcessInfo $machFile" $RSH $machine foamProcessInfo $machFile >/dev/null 2>&1 fi - if [ $? -ne 0 -o ! -s $machFile ]; then - $ECHO "** Failed collecting process information on $machine." - $ECHO "Check $machFile and run foamProcessInfo by hand" + if [ $? -ne 0 -o ! -s $machFile ] + then + echo "** Failed collecting process information on $machine." + echo "Check $machFile and run foamProcessInfo by hand" rm -f $machFile else - $ECHO "Succesfully collected information in $machFile ..." + echo "Succesfully collected information in $machFile ..." fi cnt=`expr $cnt + 1` done -$ECHO "" +echo "" #- Construct state for runningJobs; move non runnning jobs to finishedJobs @@ -281,27 +301,32 @@ do pid=`echo $machinePid | sed -e 's/.*\.\([0-9][0-9]*\)$/\1/'` machFile=$MACHDIR/$machine - if [ -r $machFile ]; then + if [ -r $machFile ] + then entry=`grep "^$pid " $machFile 2>/dev/null` - if [ $? -ne 0 -o ! "$entry" ]; then - if [ "$releaseAll" ]; then + if [ $? -ne 0 -o ! "$entry" ] + then + if [ "$releaseAll" ] + then releaseLock $machinePid $f else - $ECHO "Job $machinePid seems to be no longer running. Release lock? (y/a)\c" + echo "Job $machinePid seems to be no longer running. Release lock? (y/a)\c" read answ - if [ "${answ:-y}" = 'y' ]; then + if [ "${answ:-y}" = 'y' ] + then releaseLock $machinePid $f - elif [ "${answ:-y}" = 'a' ]; then + elif [ "${answ:-y}" = 'a' ] + then releaseAll='yes' releaseLock $machinePid $f else state='OTHR' - $ECHO "$machinePid $state" >> $STATEFILE + echo "$machinePid $state" >> $STATEFILE fi fi else state=`echo "$entry" | awk '{print $2}'` - $ECHO "$machinePid $state" >> $STATEFILE + echo "$machinePid $state" >> $STATEFILE fi fi done @@ -314,29 +339,33 @@ OLDFILES=`find $FOAM_JOB_DIR/finishedJobs -mtime +$NDAYSLIMIT -print` #- Construct state for finishedJobs and check on date of files. -if notEmpty $FOAM_JOB_DIR/finishedJobs; then +if notEmpty $FOAM_JOB_DIR/finishedJobs +then dateNow=`date '+%b %d %Y'` for f in $FOAM_JOB_DIR/finishedJobs/* do sz=`ls -s $f | awk '{print $1}'` - if [ "$sz" -gt 0 ]; then + if [ "$sz" -gt 0 ] + then machinePid=`basename $f` machine=`echo $machinePid | sed -e 's/\.[0-9][0-9]*$//'` pid=`echo $machinePid | sed -e 's/.*\.\([0-9][0-9]*\)$/\1/'` end=`getEntry $f endDate` - if [ ! "$end" ]; then + if [ ! "$end" ] + then state='ABRT' else nDaysOld=`dayDiff "$dateNow" "$end"` - if [ "$nDaysOld" -gt $NDAYSLIMIT ]; then + if [ "$nDaysOld" -gt $NDAYSLIMIT ] + then OLDFILES="$OLDFILES $f" fi state='FINI' fi - $ECHO "$machinePid $state" >> $STATEFILE + echo "$machinePid $state" >> $STATEFILE fi done fi @@ -345,11 +374,13 @@ fi #- Remove old locks nOldFiles=`echo "$OLDFILES" | wc -w` -if [ "$nOldFiles" -gt 0 ]; then - $ECHO "You seem to have $nOldFiles locks older than $NDAYSLIMIT days in finishedJobs/" +if [ "$nOldFiles" -gt 0 ] +then + echo "You seem to have $nOldFiles locks older than $NDAYSLIMIT days in finishedJobs/" $ECHO "Do you want to remove these? (y)\c" read answ - if [ "${answ:-y}" = 'y' ]; then + if [ "${answ:-y}" = 'y' ] + then rm -f $OLDFILES fi fi @@ -358,9 +389,9 @@ fi rm -f $TMPFILE rm -r $MACHDIR -$ECHO "" -$ECHO "Updated stateFile:" -$ECHO " $STATEFILE" -$ECHO "" +echo "" +echo "Updated stateFile:" +echo " $STATEFILE" +echo "" #------------------------------------------------------------------------------ diff --git a/bin/foamLog b/bin/foamLog index a82410da4d..7490978c6e 100755 --- a/bin/foamLog +++ b/bin/foamLog @@ -35,17 +35,11 @@ PROGDIR=`dirname $0` PROGNAME=`basename $0` - -if [ -r $HOME/.${PROGNAME}.db ]; then - DBFILE=$HOME/.${PROGNAME}.db -else - DBFILE=$PROGDIR/$PROGNAME.db -fi - +DBFILE=${PROGNAME}.db printUsage() { -cat < extracts xy files from log @@ -54,39 +48,46 @@ Usage: $PROGNAME [-n][-s] $PROGNAME -h for a help message -LABUSAGE +USAGE } printHelp() { printUsage cat <_, for every specified, for every -occurrence inside a time step. +The program will generate and run an awk script which writes a set of files, +logs/_, for every specified, for every occurrence inside +a time step. -For variables that are 'Solved for' the initial residual name will -be , the final residual will get name FinalRes, +For variables that are 'Solved for', the initial residual name will be +, the final residual receive the name FinalRes, The files are a simple xy format with the first column Time (default) and the second the extracted values. Option -n creates single column files with the extracted data only. -The query database is a simple text format with three entries per line, -separated with '/'. Column 1 is the name of the variable (cannot contain -spaces), column 2 is the extended regular expression (egrep) to select -the line and column 3 is the string (fgrep) to select the column inside the -line. The value taken will be the first (non-space)word after this -column. The database will either be \$HOME/.${PROGNAME}.db or if not -found $PROGDIR/${PROGNAME}.db. +The query database is a simple text format with three entries per line, +separated with '/' : + Column 1 is the name of the variable (cannot contain spaces). + Column 2 is the extended regular expression (egrep) to select the line. + Column 3 is the string (fgrep) to select the column inside the line. +The value taken will be the first (non-space)word after this column. +The database ($PROGNAME.db) will taken from these locations: + + $HOME/.OpenFOAM/$WM_PROJECT_VERSION + $HOME/.OpenFOAM + $WM_PROJECT_INST_DIR/site/$WM_PROJECT_VERSION + $WM_PROJECT_INST_DIR/site + $WM_PROJECT_DIR/etc + $PROGDIR/tools Option -s suppresses the default information and only prints the extracted variables. @@ -95,9 +96,28 @@ LABHELP } +# The various places to be searched: +for i in \ + $HOME/.OpenFOAM/$WM_PROJECT_VERSION \ + $HOME/.OpenFOAM \ + $WM_PROJECT_INST_DIR/site/$WM_PROJECT_VERSION \ + $WM_PROJECT_INST_DIR/site \ + $WM_PROJECT_DIR/etc \ + $PROGDIR/tools \ + ; +do + if [ -r $i/$DBFILE ] + then + DBFILE="$i/$DBFILE" + break + fi +done + + myEcho() { - if [ "$VERBOSE" ]; then + if [ "$VERBOSE" ] + then echo "$*" fi } @@ -112,7 +132,8 @@ getSolvedVars() { # getQueries dbFile queryName # Gets regular expressions for a certain queryName from the database getQueries() { - if [ ! -f "$1" ]; then + if [ ! -f "$1" ] + then echo "Cannot find dbFile $1" exit 1 fi @@ -123,7 +144,8 @@ getQueries() { NUMQ=`grep -v '^#' $1 | awk -F '/' "/$queryName/ {if (\"$queryName\" "'!= $1) next; print $3}'` #echo "For $queryName found line selection /$LINEQ/ , column selection /$NUMQ/" 1>&2 - #if [ ! "$LINEQ" -o ! "$NUMQ" ]; then + #if [ ! "$LINEQ" -o ! "$NUMQ" ] + #then # echo "Did not find query for $2 in database $1" 1>&2 #fi } @@ -164,9 +186,11 @@ getAllQueries() { do getQueries $1 "$var" line=`egrep "$LINEQ" $2` - if [ "$line" ]; then + if [ "$line" ] + then column=`echo "$line" | fgrep "$NUMQ"` - if [ "$column" ]; then + if [ "$column" ] + then queries="$queries $var" fi fi @@ -190,18 +214,23 @@ LISTONLY='' while getopts nslh flags do case $flags in - n) TIMENAME="" - ;; - h) printHelp - exit 0 - ;; - s) VERBOSE="" - ;; - l) LISTONLY='yes' - ;; - \?) printUsage - exit 1 - ;; + n) + TIMENAME="" + ;; + h) + printHelp + exit 0 + ;; + s) + VERBOSE="" + ;; + l) + LISTONLY='yes' + ;; + \?) + printUsage + exit 1 + ;; esac done @@ -209,18 +238,22 @@ done # Shift options shift `expr $OPTIND - 1` -if [ ! -f $DBFILE ]; then +if [ ! -f $DBFILE ] +then echo "$PROGNAME: Cannot read database $DBFILE" exit 1 fi -if [ "$LISTONLY" ]; then - if [ $# -ne 1 ]; then +if [ "$LISTONLY" ] +then + if [ $# -ne 1 ] + then printUsage exit 1 fi LOG=$1; - if [ ! -r $LOG ]; then + if [ ! -r $LOG ] + then echo "$PROGNAME: Cannot read log $LOG" exit 1 fi @@ -228,14 +261,16 @@ if [ "$LISTONLY" ]; then exit 0 fi -if [ $# -ne 1 ]; then +if [ $# -ne 1 ] +then printUsage exit 1 fi CASEDIR=. LOG=$1 -if [ ! -r $LOG ]; then +if [ ! -r $LOG ] +then echo "$PROGNAME: Cannot read log $LOG" exit 1 fi @@ -243,17 +278,20 @@ fi QUERYNAMES=`getAllQueries $DBFILE $LOG` -if [ ! "$CASEDIR" ]; then +if [ ! "$CASEDIR" ] +then printUsage exit 1 fi -if [ ! -d "$CASEDIR" ]; then +if [ ! -d "$CASEDIR" ] +then echo "$PROGNAME: Cannot read $CASEDIR" exit 1 fi -if [ ! -f "$LOG" ]; then +if [ ! -f "$LOG" ] +then echo "$PROGNAME: Cannot read log file $LOG" exit 1 fi @@ -361,19 +399,19 @@ cat <> $AWKFILE varName=varNameVal[1] file=varName "_" subIter[varName]++ - file="$CASEDIR/logs/" file + file="$CASEDIR/logs/" file extract(\$0, "Initial residual = ", val) print $TIMENAME "\t" val[1] > file varName=varNameVal[1] "FinalRes" file=varName "_" subIter[varName]++ - file="$CASEDIR/logs/" file + file="$CASEDIR/logs/" file extract(\$0, "Final residual = ", val) print $TIMENAME "\t" val[1] > file varName=varNameVal[1] "Iters" file=varName "_" subIter[varName]++ - file="$CASEDIR/logs/" file + file="$CASEDIR/logs/" file extract(\$0, "No Iterations ", val) print $TIMENAME "\t" val[1] > file } @@ -384,7 +422,8 @@ LABSOLVE for queryName in $QUERYNAMES do getQueries $DBFILE $queryName - if [ "$LINEQ" -a "$NUMQ" ]; then + if [ "$LINEQ" -a "$NUMQ" ] + then counter=${queryName}Cnt echo "#-- Extraction of $queryName" >> $AWKFILE diff --git a/bin/foamPrintJobs b/bin/foamPrintJobs index 2b2ca15938..a2851f2902 100755 --- a/bin/foamPrintJobs +++ b/bin/foamPrintJobs @@ -30,10 +30,8 @@ # Uses finishedJobs/ and runningJobs/ and stateFile to print job info # #------------------------------------------------------------------------------ +Script=${0##*/} -PROGNAME=`basename $0` -TMPFILE=/tmp/${PROGNAME}$$.tmp -TMPFILE2=/tmp/${PROGNAME}$$.tmp2 JOBSTRING='%4s %8s %20s %10s %8s %4s %12s %12s %20s\n' DEFSTATEFILE=$HOME/.OpenFOAM/foamCheckJobs.out @@ -44,18 +42,19 @@ DEFSTATEFILE=$HOME/.OpenFOAM/foamCheckJobs.out # #------------------------------------------------------------------------------- -printUsage() { -cat << LABEL -Usage: $PROGNAME [stateFile] +usage() { + cat<