XGrid getXGridOutput.py
Back to C-Program on the XGrid
Modified --Thiebaut 22:46, 20 October 2008 (UTC)
#! /usr/bin/python
# getXGridOutput.py
# D. Thiebaut
#
# A filter program receiving (through a pipe, typically) the output of the command
#
# xgrid -job submit xxxx
#
# and displaying the result of the computation, including execution time and
# the output of each process.
#
# Example:
#
# runQueensAll.sh 15 | getXGridOutput.py
# Job 1250 stopped: Execution time: 2.000000 seconds
# First Queen in column 0 of 15x15 board: 69516 solutions found
#
# Job 1251 stopped: Execution time: 2.000000 seconds
# First Queen in column 1 of 15x15 board: 98156 solutions found
# ...
# ...
# Job 1264 stopped: Execution time: 2.000000 seconds
# First Queen in column 14 of 15x15 board: 69516 solutions found
#
# Total execution time: 6.000000 seconds
#
import sys
import os
import time
jobStatus = {}
RUNNING = 1
STOPPED = 0
STARTTIMES= []
ENDTIMES = []
def printResults( jobId ):
"""Prints the output of job #jobId. Format of time: 2008-09-27 19:09:27"""
global STARTTIMES, ENDTIMES
[status, startTime, endTime] = jobStatus[ jobId ]
time1 = time.mktime( time.strptime( startTime, "%Y-%m-%d %H:%M:%S" ) )
time2 = time.mktime( time.strptime( endTime, "%Y-%m-%d %H:%M:%S" ) )
STARTTIMES.append( time1 )
ENDTIMES.append( time2 )
print "Job %d stopped: Execution time: %f seconds" % ( jobId, time2-time1 )
dothis = "xgrid -job results -id %d" % jobId
handle = os.popen( dothis )
while 1:
line = handle.readline()
if line=="": break
print line,
def deleteJob( jobId ):
""" deletes a job given its jobId """
#--- prepare command ---
dothis = "xgrid -job delete -id %d" % jobId
handle = os.popen( dothis )
#--- read output from xgrid ---
while 1:
line = handle.readline()
if line=="": break
if line=="}": break
return 1
def getJobsStatus( jobIds ):
"""Polls all the jobs and see if they are still running.
returns the number of stopped jobs"""
global RUNNING, STOPPED
noStoppedJobs = 0
for jobId in jobIds:
#--- don't poll the xgrid if the job is already stoppped---
status = jobStatus[ jobId ]
if status[ 0 ]==STOPPED:
noStoppedJobs += 1
continue
#--- poll xgrid ---
dothis = "xgrid -job attributes -id %d" % jobId
handle = os.popen( dothis )
#--- read the output from xgrid ---
while 1:
line = handle.readline()
if line=="": break
if line.find( "dateStarted" )!=-1:
status[1] = line[ line.find( "=" )+1 :-8 ]
status[1] = status[1].strip()
if line.find( "dateStopped" )!=-1:
status[2] = line[ line.find( "=" )+1 :-8 ]
status[2] = status[2].strip()
#---- if the job is now finished record it and display its output ---
if line.find( "jobStatus" )!=-1 and \
line.find( "Finished" ) != -1:
status[0] = STOPPED
noStoppedJobs += 1
printResults( jobId )
deleteJob( jobId )
#--- record status ---
jobStatus[ jobId ] = status
#--- returned # of stopped jobs ---
return noStoppedJobs
def main():
""" ---------------------------- M A I N ---------------------------
"""
count = 1
jobIds = []
for line in sys.stdin:
if line.find( "jobIdentifier" ) != -1:
jobIds.append( int( line[ line.find("=")+1 :-2 ] ) )
#--- create job status records ---
for jobId in jobIds:
jobStatus[jobId] = [ RUNNING, 0, 0 ] # running, start time, end time
#--- keep on going until all jobs have stopped ---
while True:
#--- get job status ---
noStoppedJobs = getJobsStatus( jobIds )
if noStoppedJobs == len( jobIds ):
break
time.sleep( 0.1 ) # wait 0.1 second before polling the xgrid again
#--- print total execution time ---
#print "ENDTIMES = ", ENDTIMES
#print "STARTTIMES = ", STARTTIMES
print "\nTotal execution time: %f seconds\n\n" % ( max( ENDTIMES )-min( STARTTIMES ) )
main()