CMS 3D CMS Logo

Mpslibclass.py
Go to the documentation of this file.
1 # This Jobdatabas-Class interacts with the mps.db file.
2 # It's member-variables are often called in the mps_... scripts.
3 #
4 # Meaning of the database variables: (still need to work on these)
5 #
6 # (1) Header
7 # header - version information
8 # batchScript - base script for serial job
9 # cfgTemplate - template for cfg file
10 # infiList - list of input files to be serialized
11 # classInf - batch class information (might contain two ':'-separated)
12 # addFiles - job name for submission
13 # driver - specifies whether merge job is foreseen
14 # nJobs - number of serial jobs (not including merge job)
15 # mergeScript - base script for merge job
16 # mssDir - directory for mass storage (e.g. Castor)
17 # updateTime - time of last update (seconds since 1970)
18 # updateTimeHuman - time of last update (human readable)
19 # elapsedTime - seconds since last update
20 # mssDirPool - pool for $mssDir (e.g. cmscaf/cmscafuser)
21 # pedeMem - Memory allocated for pede
22 # spare1
23 # spare2
24 # spare3
25 
26 # (2) Job-level variables/lists
27 # JOBNUMBER - ADDED, selfexplanatory
28 # JOBDIR - name of job directory (not full path)
29 # JOBSTATUS - status of job
30 # JOBRUNTIME - present CPU time of job
31 # JOBNEVT - number of events processed by job
32 # JOBHOST - presently used to store remark
33 # JOBINCR - CPU increment since last check
34 # JOBREMARK - comment
35 # JOBSP1 - spare
36 # JOBSP2 - possible weight for pede
37 # JOBSP3 - possible name as given to mps_setup.pl -N <name> ...
38 # JOBID - ID of the LSF/HTCondor job
39 
40 import datetime
41 import time
42 import os
43 import sys
44 
45 #-------------------------------------------------------------------------------
47 
48  JOBNUMBER, JOBDIR, JOBID, JOBSTATUS, JOBNTRY, JOBRUNTIME, JOBNEVT, JOBHOST, JOBINCR, \
49  JOBREMARK, JOBSP1, JOBSP2, JOBSP3 = ([] for i in range(13))
50 
51  header, batchScript, cfgTemplate, infiList, classInf, addFiles, driver, mergeScript, \
52  mssDir, updateTimeHuman, mssDirPool, spare1, spare2, spare3 = ('' for i in range(14))
53 
54  updateTime, elapsedTime, pedeMem , nJobs = -1, -1, -1, -1
55 
56  #-------------------------------------------------------------------------------
57  # parses the mps.db file into the member variables and arrays
58  __default_db_file_name = "mps.db"
59  def read_db(self, db_file_name = __default_db_file_name):
60  try:
61  DBFILE = open(db_file_name,'r')
62  except IOError as e:
63  if e.args != (2, 'No such file or directory'):
64  raise
65  else:
66  if db_file_name == jobdatabase.__default_db_file_name:
67  msg = ("No 'mps.db' found. Make sure you are in a campaign "
68  "directory and that the campaign is set up.")
69  else:
70  msg = "Database file '"+db_file_name+"' not found. Exiting."
71  print msg
72  sys.exit(1)
73 
74  #read infolines at the top, used rstrip to delete the '\n'
75  self.header = DBFILE.readline().strip()
76  self.batchScript = DBFILE.readline().rstrip('\n')
77  self.cfgTemplate = DBFILE.readline().rstrip('\n')
78  self.infiList = DBFILE.readline().rstrip('\n')
79  self.classInf = DBFILE.readline().rstrip('\n') #formerly named 'class' ->conflict
80  self.addFiles = DBFILE.readline().rstrip('\n')
81  self.driver = DBFILE.readline().rstrip('\n')
82  self.mergeScript = DBFILE.readline().rstrip('\n')
83  self.mssDir = DBFILE.readline().rstrip('\n')
84  self.updateTime = int(DBFILE.readline())
85  self.updateTimeHuman = DBFILE.readline().rstrip('\n')
86  self.elapsedTime = int(DBFILE.readline())
87  self.mssDirPool = DBFILE.readline().rstrip('\n')
88  self.pedeMem = int(DBFILE.readline())
89  self.spare1 = DBFILE.readline().rstrip('\n')
90  self.spare2 = DBFILE.readline().rstrip('\n')
91  self.spare3 = DBFILE.readline().rstrip('\n')
92 
93  #read actual jobinfo into arrays
94  self.nJobs = 0
95  milleJobs = 0
96 
97 
98  for line in DBFILE:
99  line = line.rstrip('\n') #removes the pesky \n from line
100  parts = line.split(":") #read each line and split into parts list
101  self.JOBNUMBER.append(int(parts[0]))
102  self.JOBDIR.append(parts[1].strip())
103  self.JOBID.append(parts[2])
104  self.JOBSTATUS.append(parts[3].strip())
105  self.JOBNTRY.append(int(parts[4]))
106  self.JOBRUNTIME.append(int(parts[5])) #int float?
107  self.JOBNEVT.append(int(parts[6]))
108  self.JOBHOST.append(parts[7].strip())
109  self.JOBINCR.append(int(parts[8]))
110  self.JOBREMARK.append(parts[9].strip())
111  self.JOBSP1.append(parts[10].strip())
112  self.JOBSP2.append(parts[11].strip())
113  self.JOBSP3.append(parts[12].strip())
114 
115  #count number of jobs
116  if not self.JOBDIR[self.nJobs].startswith("jobm"):
117  milleJobs += 1
118  self.nJobs += 1
119  self.nJobs = milleJobs
120 
121  DBFILE.close()
122 
123 
124 
125  #-------------------------------------------------------------------------------
126  # prints the member varaiables and arrays to the terminal
127  def print_memdb(self):
128  #print metainfo
129  print "\n=== mps database printout ===\n"
130  print self.header
131  print 'Script:\t\t', self.batchScript
132  print 'cfg:\t\t', self.cfgTemplate
133  print 'files:\t\t', self.infiList
134  print 'class:\t\t', self.classInf
135  print 'name:\t\t', self.addFiles
136  print 'driver:\t\t', self.driver
137  print 'mergeScript:\t', self.mergeScript
138  print 'mssDir:\t\t', self.mssDir
139  print 'updateTime:\t', self.updateTimeHuman
140  print 'elapsed:\t', self.elapsedTime
141  print 'mssDirPool:\t', self.mssDirPool
142  print 'pedeMem:\t', self.pedeMem, '\n'
143 
144  #print interesting Job-level lists ---- to add: t/evt, fix remarks
145  print '### dir jobid stat try rtime nevt remark weight name'
146  print "------------------------------------------------------------------------------"
147  for i in xrange(self.nJobs):
148  print '%03d %6s %9s %6s %3d %5d %8d %8s %5s %s' % (
149  self.JOBNUMBER[i],
150  self.JOBDIR[i],
151  self.JOBID[i],
152  self.JOBSTATUS[i],
153  self.JOBNTRY[i],
154  self.JOBRUNTIME[i],
155  self.JOBNEVT[i],
156  self.JOBHOST[i],
157  self.JOBSP2[i],
158  self.JOBSP3[i])
159 
160  #print merge Jobs if merge mode
161  if self.driver == 'merge':
162  for i in xrange(self.nJobs,len(self.JOBDIR)):
163  print '%s %6s %9s %6s %3d %5d %8d %8s %5s %s' % (
164  'MMM',
165  self.JOBDIR[i],
166  self.JOBID[i],
167  self.JOBSTATUS[i],
168  self.JOBNTRY[i],
169  self.JOBRUNTIME[i],
170  self.JOBNEVT[i],
171  self.JOBHOST[i],
172  self.JOBSP2[i],
173  self.JOBSP3[i])
174 
175  #print summed info
176  totalEvents = sum(self.JOBNEVT[:self.nJobs])
177  totalCpu = sum(self.JOBRUNTIME[:self.nJobs])
178  meanCpuPerEvent = 0.
179  if totalEvents > 0:
180  meanCpuPerEvent = float(totalCpu)/totalEvents
181  print "------------------------------------------------------------------------------"
182  print "\t\t\t\t\tEvent total:\t", totalEvents
183  print "\t\t\t\t\tCPU total:\t", totalCpu, 's'
184  print "\t\t\t\t\tMean CPU/event:\t",meanCpuPerEvent,'s'
185 
186 
187 
188 
189 
190  #-------------------------------------------------------------------------------
191  # writes a new mps.db file from the members. Replaces the old mps.db
192  def write_db(self):
193  self.header = "mps database schema 3.2"
194  self.currentTime = int(time.time())
195  self.elapsedTime = 0;
196  if self.updateTime != 0:
197  self.elapsedTime = self.currentTime - self.updateTime
198  self.updateTime = self.currentTime
199  self.updateTimeHuman = str(datetime.datetime.today()) #no timezone :(
200  self.spare1 = "-- unused --"
201  self.spare2 = "-- unused --"
202  self.spare3 = "-- unused --"
203 
204  #if mps.db already exists, backup as mps.db~ (in case of interupt during write)
205  os.system('[[ -a mps.db ]] && cp -p mps.db mps.db~')
206 
207  #write mps.db header
208  DBFILE = open ("mps.db", "w")
209  headData = [ self.header, self.batchScript, self.cfgTemplate, self.infiList,
210  self.classInf, self.addFiles, self.driver, self.mergeScript,
211  self.mssDir, self.updateTime, self.updateTimeHuman,
212  self.elapsedTime, self.mssDirPool, self.pedeMem,
213  self.spare1, self.spare2, self.spare3 ]
214  for item in headData:
215  DBFILE.write("%s\n" % item)
216 
217  #write mps.db jobinfo
218  for i in xrange(len(self.JOBID)):
219  DBFILE.write('%03d:%s:%s:%s:%s:%s:%s:%s:%s:%s:%s:%s:%s\n' %
220  (i+1,
221  self.JOBDIR[i],
222  self.JOBID[i],
223  self.JOBSTATUS[i],
224  self.JOBNTRY[i],
225  self.JOBRUNTIME[i],
226  self.JOBNEVT[i],
227  self.JOBHOST[i],
228  self.JOBINCR[i],
229  self.JOBREMARK[i],
230  self.JOBSP1[i],
231  self.JOBSP2[i],
232  self.JOBSP3[i]))
233  DBFILE.close()
234 
235  #-------------------------------------------------------------------------------
236  # returns job class as stored in db
237  # one and only argument may be "mille" or "pede" for mille or pede jobs
238  def get_class(self, argument=''):
239  CLASSES = self.classInf.split(':')
240  if len(CLASSES)<1 or len(CLASSES)>2:
241  print '\nget_class():\n class must be of the form \'class\' or \'classMille:classPede\', but is \'%s\'!\n\n', classInf
242  sys.exit(1)
243  elif argument == 'mille':
244  return CLASSES[0]
245  elif argument == 'pede':
246  if len(CLASSES) == 1:
247  return CLASSES[0]
248  elif len(CLASSES) == 2:
249  return CLASSES[1]
250  else:
251  print '\nget_class():\n Know class only for \'mille\' or \'pede\', not %s!\n\n' %argument
252  sys.exit(1)
def read_db(self, db_file_name=__default_db_file_name)
Definition: Mpslibclass.py:59
def get_class(self, argument='')
Definition: Mpslibclass.py:238