Skip to content
Snippets Groups Projects
foamPrintJobs 6.82 KiB
#!/bin/sh
#------------------------------------------------------------------------------
# =========                 |
# \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
#  \\    /   O peration     |
#   \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
#    \\/     M anipulation  |
#-------------------------------------------------------------------------------
# License
#     This file is part of OpenFOAM.
#
#     OpenFOAM is free software; you can redistribute it and/or modify it
#     under the terms of the GNU General Public License as published by the
#     Free Software Foundation; either version 2 of the License, or (at your
#     option) any later version.
#
#     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
#     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
#     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
#     for more details.
#
#     You should have received a copy of the GNU General Public License
#     along with OpenFOAM; if not, write to the Free Software Foundation,
#     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
# Script
#     foamPrintJobs
#
# Description
#     Uses finishedJobs/ and runningJobs/ and stateFile to print job info
#
#------------------------------------------------------------------------------

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


#-------------------------------------------------------------------------------
#
# Functions
#
#-------------------------------------------------------------------------------

printUsage() {
cat << LABEL
Usage: $PROGNAME [stateFile]

This program prints a table of all the running and finished jobs.

It is normally used in conjunction with foamCheckJobs which outputs
a "stateFile" containing the actual process status of all jobs.

If stateFile is not supplied the default $DEFSTATEFILE
is used.
LABEL
}


# printJob stat user case machine pid ncpus start end code
printJob() {
    printf "$JOBSTRING" "$1" "$2" "$3" "$4" "$5" "$6" "$7" "$8" "$9"
}


# getRawEntry dictionary entry
# Prints value of dictionary entry
getRawEntry() {
    grep -v '^//' $1 | grep "^[ \t]*$2 " | sed -e "s/^[ \t]*$2 [ ]*//"
}

# getEntry dictionary entry
# Like getRawEntry but strips " and ending ';'
getEntry() {
    getRawEntry $1 $2 | sed -e 's/^"//'  -e 's/;$//' -e 's/"$//'
}


# notEmpty directory
# Returns 0 if directory contains files/directories
notEmpty() {
    if [ "`ls $1`" ]; then
        return 0
    else
        return 1
    fi
}

# rightStr nChars string
# Prints rightmost nChars of string
rightStr() {
    echo "$2" | sed -e "s/.*\(.\{$1\}\)\$/\1/"
}

leftStr() {
    echo "$2" | sed -e "s/\(.\{$1\}\).*/\1/"
}

#-------------------------------------------------------------------------------
#
# Main
#
#-------------------------------------------------------------------------------

if [ ! "$FOAM_JOB_DIR" ]; then
    echo "$PROGNAME : FOAM_JOB_DIR environment variable not set."
    exit 1
fi

if [ ! -d "$FOAM_JOB_DIR" ]; then
    echo "$PROGNAME : directory does not exist."
    echo "            FOAM_JOB_DIR: $FOAM_JOB_DIR"
fi
if [ ! -d "$FOAM_JOB_DIR/runningJobs" -o ! -d "$FOAM_JOB_DIR/finishedJobs" ]; then
    echo "$PROGNAME : invalid directory."
    echo "            FOAM_JOB_DIR: $FOAM_JOB_DIR"
fi


if [ $# -eq 1 ]; then
    if [ "$1" = '-h' ]; then
        printUsage
        exit 1
    else
        STATEFILE=$1
    fi
elif [ $# -eq 0 ]; then
    STATEFILE=${STATEFILE:-$DEFSTATEFILE}
else
    printUsage
    exit 1
fi


if [ -f "$STATEFILE" ]; then
    echo ""
    echo "Using process information from"
    echo "  $STATEFILE"
    echo "on jobs in"
    echo "  $FOAM_JOB_DIR"
    echo ""
else
    echo ""
    echo "Cannot read $STATEFILE."
    echo ""
    STATEFILE=''
fi


#-- print header
printJob 'stat' 'user' 'case' 'machine' 'pid' 'ncpu' 'start' 'end' 'code'
printJob '----' '----' '----' '-------' '---' '----' '-----' '---' '----'



#-- print submitted


#-- print running
echo "Running:"
if notEmpty $FOAM_JOB_DIR/runningJobs; then
    for f in `ls -t $FOAM_JOB_DIR/runningJobs/*`
    do
        machinePid=`basename $f`

        machine=`echo $machinePid | sed -e 's/\..*$//'`
        machine=`rightStr 10 "$machine"`

        pid=`echo $machinePid | sed -e 's/.*\.\([0-9][0-9]*\)$/\1/'`

        if [ "$STATEFILE" ]; then
            stat=`getEntry $STATEFILE $machinePid`
        fi
        stat=${stat:-'UNKN'}

        case=`getEntry $f 'case'`
        case=${case:-'---'}
        case=`echo $case | sed -e 's!/.*!!'`       #strip of processorXXX ending
        case=`rightStr 20 "$case"`

        start=`getEntry $f 'startDate'`
        start=${start:-'---'}
        start=`leftStr 12 "$start"`

        end='---'

        code=`getEntry $f 'code'`
        if [ "$code" ]; then
            code=`basename $code`
        else
            code='---'
        fi
        code=`rightStr 20 "$code"`

        nProcs=`getEntry $f 'nProcs'`
        nProcs=${nProcs:-'1'}
        if [ $nProcs -eq 1 ]; then
            nProcs='---'
        fi
        nProcs=`rightStr 3 "$nProcs"`

        user=`getEntry $f 'username'`
        user=${user:-'---'}
        user=`leftStr 8 "$user"`

        printJob "$stat" "$user" "$case" "$machine" "$pid" "$nProcs" "$start" "$end" "$code"
    done
fi


#-- print finished
echo ""
echo "Finished:"
if notEmpty $FOAM_JOB_DIR/finishedJobs; then
    for f in `ls -t $FOAM_JOB_DIR/finishedJobs/*`
    do
        machinePid=`basename $f`

        machine=`echo $machinePid | sed -e 's/\..*$//'`
        machine=`rightStr 10 "$machine"`

        pid=`echo $machinePid | sed -e 's/.*\.\([0-9][0-9]*\)$/\1/'`

        end=`getEntry $f endDate`
        end=${end:-'---'}
        end=`leftStr 12 "$end"`

        if [ "$STATEFILE" ]; then
            stat=`getEntry $STATEFILE $machinePid`
        fi
        stat=${stat:-'UNKN'}

        case=`getEntry $f case`
        case=`echo $case | sed -e 's!/.*!!'`       #strip of processorXXX ending
        case=${case:-'---'}
        case=`rightStr 20 "$case"`

        start=`getEntry $f startDate`
        start=${start:-'---'}
        start=`leftStr 12 "$start"`

        code=`getEntry $f code`
        if [ "$code" ]; then
            code=`basename $code`
        else
            code='---'
        fi
        code=`rightStr 20 "$code"`

        nProcs=`getEntry $f 'nProcs'`
        nProcs=${nProcs:-'1'}
        if [ $nProcs -eq 1 ]; then
            nProcs='---'
        fi
        nProcs=`rightStr 3 "$nProcs"`

        user=`getEntry $f 'username'`
        user=${user:-'---'}
        user=`leftStr 8 "$user"`

        printJob "$stat" "$user" "$case" "$machine" "$pid" "$nProcs" "$start" "$end" "$code"
    done
fi

#------------------------------------------------------------------------------