#!/bin/sh
#
#	HP High Performance Systems Division
#	(c)Copyright 1999 Hewlett-Packard Co.
#
#	$Revision: 5211 $
#
#	Function:	- MPI jobs listing script
#
#	"@(#)HP MPI 02.02.05.00 (10/03/2006) Linux x86-64"
#

#
#	usage
#
#	Function:	- print usage information and exit
#
usage ()

{
	echo
	echo "Synopsis:        $prog [-help] [-a] [-u] [-j <jid> [<jid> ...] ]"
	echo
	echo "Description:     List MPI jobs."
	echo
	echo "Options:         -help         Print this help menu."
	echo "                 -a            List jobs for all users."
	echo "                 -u            Sort jobs by username."
	echo "                 -j <id>       Give process status for job <id>."
	exit 1
}

#
#	jobstatus
#
#	Function:	- get status of processes for given job
#
jobstatus ()

{
#
# Check if jobfile is readable.
#
	if [ ! -r $jobfile ]; then
		echo "${prog}: cannot get information on job $jid"
		return 1
	fi

        remsh=${MPI_REMSH:-/usr/bin/ssh}

#
# Sort processes by host.
#
	awk -F: -v remsh=$remsh -v mh=`hostname` -v mu=$user ' \
BEGIN	{ n = 0; nlive = 0; } \
	{ k = $1; if ((k == "") || (k == "localhost")) k = mh; \
	  if ($2 == "") u[k] = mu; else u[k] = $2; \
	  hr[n] = k; pr[n] = $3; ar[n] = " "; gr[n] = $4; n++; } \
END	{ for (k in u) { \
		if (k == mh) { \
			cmd = "/bin/ps -e"; \
		} else { \
			if (u[k] == mu) uu = ""; else uu = "-l " u[k]; \
			cmd = remsh " " k " " uu " /bin/ps -e"; \
		} \
		if (cmd | getline <= 0) { \
			printf "mpijob: cannot execute \"%s\"\n", cmd; \
			exit(-1); \
		} \
		while (cmd | getline > 0) { \
			split($0, args, " ");
			for (i = 0; i < n; i++) { \
				if ((hr[i] == k) && (pr[i] == args[1]) && \
						(args[4] != "<defunct>")) { \
					ar[i] = "x"; nlive++; \
				} \
			} \
		} \
	  } \
	  if (nlive > 0) { \
		for (i = 0; i < n; i++) { \
			printf "%-5d %-20s %-7d %-4s %s\n", \
				i, substr(hr[i], 1, 20), pr[i], ar[i], gr[i]; \
		} \
	  } \
	  exit(nlive); \
	} \
	' $jobfile
#
# If no processes are alive, delete the job file if it is mine.
#
	ret=$?
	if [ $ret -lt 0 ]; then return 1; fi

	if [ $ret -eq 0 ]; then
		echo $jobfile | awk -F_ ' \
			{ printf "job %s for user \"%s\" is done\n", $3, $2; }'
		if [ "$jobfile" = "$tmpdir/mpijob_${user}_${jid}" ]; then
			/bin/rm -f $jobfile
		fi
	fi

	return 0
}

#
# main code
#
{
	PATH=/bin:/usr/bin:/usr/ucb
	prog=mpijob

	user=`whoami`
	tmpdir=${MPI_TMPDIR:-/tmp}
	f_all=0
	f_sort=1
	f_jid=0
	jidlist=""

#
# Loop parsing command line arguments.
#
	while [ $# -ne 0 ]; do
		case "$1" in
# help menu
		-help)	usage;;
# all users
		-a)	f_all=1;;
# no job ID sorting
		-u)	f_sort=0;;
# specific job info
		-j)	if [ $# -eq 1 ]; then usage; fi
			if [ "$jidlist" != "" ]; then usage; fi
			shift
			f_jid=1
			jidlist=$1;;
# unknown option
		-*)	usage;;
# argument
		*)	jidlist="$jidlist $1";;
		esac

		if [ $# -ne 0 ]; then shift; fi
	done

#
# If a job ID was given, get process status.
#
	if [ $f_jid -eq 1 ]; then
		gerr=0

		echo "RANK  HOST                 PID     LIVE PROGNAME"
		njob=`echo $jidlist | wc -w`

		for jid in $jidlist; do
			if [ $njob -gt 1 ]; then echo "[job $jid]"; fi

			if [ `expr $jid : \[0-9\]\*$` -eq 0 ]; then
				echo "${prog}: invalid job ID \"$jid\""
				gerr=1
			else
				files=`ls $tmpdir/mpijob_*_${jid} 2> /dev/null`
				if [ $? -ne 0 ]; then
					echo "${prog}: job $jid does not exist"
					gerr=1
				else
					num=`echo $files | wc -w`

					for jobfile in $files; do
						if [ $num -gt 1 ]; then
							echo $jobfile \
							| awk -F_ \
							'{printf "<%s>\n", $2}'
						fi

						jobstatus

						if [ $? -ne 0 ]; then
							gerr=1;
						fi
					done
				fi
			fi
		done
		exit $gerr
	fi

#
# List the jobs found.
# Sort the output by job ID if needed.
#

	if [ $f_sort -eq 0 ]; then sortcmd="cat"; else sortcmd="sort -n"; fi

#
# Self-cleaning: delete user's empty jobfiles that are more than a day old.
#
	find $tmpdir \( ! -name `basename $tmpdir` \) -prune \
		-type f -size 0c -name 'mpijob_*' -user $user \
		-mtime +1 -exec /bin/rm -f {} \; 2> /dev/null

#
# Select matching jobfiles: mpijob_<username>_<jobid>
# Loop over the selected files.
#
	echo "JOB      USER     NPROCS   PROGNAME"

	(for file in `ls -1 $tmpdir | awk -F_ -v fa=$f_all -v user=$user ' \
		(NF == 3) && ($1 == "mpijob") && ($3 ~ /[0-9]+/) \
		{ if ((fa == 1) || ($2 == user)) print }'` ; \
	do

#
# If file is readable, determine the # processes and the binary names.
#
		if [ -r $tmpdir/$file ]; then
			nproc=`awk -F: '{ c++; n[$4] = 1 } \
				END { print c; for (a in n) print a }' \
					$tmpdir/$file`
		else
			nproc="??"
		fi
#
# Print and format the job information.
#
		echo $file $nproc | awk '{ split($1, a, "_"); \
			for (i = 3; i <= NF; ++i) n = n $i " "; \
			printf "%-8d %-8s %-8d %s\n", a[3], a[2], $2, n }'

	done) | $sortcmd

	exit 0
}
