CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_0/src/Configuration/PyReleaseValidation/python/MatrixRunner.py

Go to the documentation of this file.
00001 
00002 import os, sys, time
00003 import random
00004 
00005 from Configuration.PyReleaseValidation.WorkFlow import WorkFlow
00006 from Configuration.PyReleaseValidation.WorkFlowRunner import WorkFlowRunner
00007 
00008 # ================================================================================
00009 
00010 class MatrixRunner(object):
00011 
00012     def __init__(self, wfIn=None, nThrMax=4):
00013 
00014         self.workFlows = wfIn
00015 
00016         self.threadList = []
00017         self.maxThreads = nThrMax
00018 
00019         #the directories in which it happened
00020         self.runDirs={}
00021 
00022     def activeThreads(self):
00023 
00024         nActive = 0
00025         for t in self.threadList:
00026             if t.isAlive() : nActive += 1
00027 
00028         return nActive
00029 
00030         
00031     def runTests(self, opt):
00032 
00033         testList=opt.testList
00034         dryRun=opt.dryRun
00035         cafVeto=opt.cafVeto
00036         
00037         startDir = os.getcwd()
00038 
00039         report=''
00040         noRun=(self.maxThreads==0)
00041         if noRun:
00042             print 'Not running the wf, only creating cfgs and logs'
00043             print 'resetting to default number of threads'
00044             self.maxThreads=4
00045 
00046         print 'Running in %s thread(s)' % self.maxThreads
00047 
00048             
00049         for wf in self.workFlows:
00050 
00051             if testList and float(wf.numId) not in [float(x) for x in testList]: continue
00052 
00053             item = wf.nameId
00054             if os.path.islink(item) : continue # ignore symlinks
00055             
00056             # make sure we don't run more than the allowed number of threads:
00057             while self.activeThreads() >= self.maxThreads:
00058                 time.sleep(10)
00059                 continue
00060             
00061             print '\nPreparing to run %s %s' % (wf.numId, item)
00062           
00063             current = WorkFlowRunner(wf,noRun,dryRun,cafVeto)
00064             self.threadList.append(current)
00065             current.start()
00066             if not dryRun:
00067                 time.sleep(random.randint(1,5)) # try to avoid race cond by sleeping random amount of time [1,5] sec
00068 
00069         # wait until all threads are finished
00070         while self.activeThreads() > 0:
00071             time.sleep(0.5)
00072 
00073 
00074         #wrap up !
00075         totpassed=[]
00076         totfailed=[]
00077         def count(collect,result):
00078             #pad with zeros
00079             for i in range(len(collect),len(result)):
00080                 collect.append(0)
00081             for i,c in enumerate(result):
00082                 collect[i]+=c
00083                 
00084         for pingle in self.threadList:
00085             pingle.join()
00086             try:
00087                 count(totpassed,pingle.npass)
00088                 count(totfailed,pingle.nfail)
00089                 report+=pingle.report
00090                 self.runDirs[pingle.wf.numId]=pingle.wfDir
00091             except Exception, e:
00092                 msg = "ERROR retrieving info from thread: " + str(e)
00093                 report += msg
00094                 
00095         report+=' '.join(map(str,totpassed))+' tests passed, '+' '.join(map(str,totfailed))+' failed\n'
00096         print report
00097 
00098         runall_report_name='runall-report-step123-.log'
00099         runall_report=open(runall_report_name,'w')
00100         runall_report.write(report)
00101         runall_report.close()
00102         os.chdir(startDir)
00103 
00104         anyFail=sum(totfailed)
00105                                         
00106         return anyFail
00107