Skip to content
Snippets Groups Projects
foamPrintJobs 6.75 KiB
Newer Older
Henry's avatar
Henry committed
#!/bin/sh
#------------------------------------------------------------------------------
# =========                 |
# \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
#  \\    /   O peration     |
#   \\  /    A nd           | Copyright (C) 2011 OpenFOAM Foundation
#    \\/     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 3 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, see <http://www.gnu.org/licenses/>.
#
# Script
#     foamPrintJobs
#
# Description
#     Uses finishedJobs/ and runningJobs/ and stateFile to print job info
#
#------------------------------------------------------------------------------
Script=${0##*/}

JOBSTRING='%4s %8s %20s %10s %8s %4s %12s %12s %20s\n'
DEFSTATEFILE=$HOME/.OpenFOAM/foamCheckJobs.out


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

usage() {
    cat<<USAGE
Usage: $Script [stateFile]

This program prints a table of all 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 is used:
    $DEFSTATEFILE
USAGE
    exit 1
}


# 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 [ $# -eq 1 ]
then
    if [ "$1" = "-h" -o "$1" = "-help" ]
    then
        usage
    else
        STATEFILE="$1"
    fi
elif [ $# -eq 0 ]
then
    STATEFILE=${STATEFILE:-$DEFSTATEFILE}
else
    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


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 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 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

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