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  if line.strip() == "": continue # ignore empty lines
100  line = line.rstrip('\n') # removes the pesky \n from line
101  parts = line.split(":") # read each line and split into parts list
102  self.JOBNUMBER.append(int(parts[0]))
103  self.JOBDIR.append(parts[1].strip())
104  self.JOBID.append(parts[2])
105  self.JOBSTATUS.append(parts[3].strip())
106  self.JOBNTRY.append(int(parts[4]))
107  self.JOBRUNTIME.append(int(parts[5])) #int float?
108  self.JOBNEVT.append(int(parts[6]))
109  self.JOBHOST.append(parts[7].strip())
110  self.JOBINCR.append(int(parts[8]))
111  self.JOBREMARK.append(parts[9].strip())
112  self.JOBSP1.append(parts[10].strip())
113  self.JOBSP2.append(parts[11].strip())
114  self.JOBSP3.append(parts[12].strip())
115 
116  #count number of jobs
117  if not self.JOBDIR[self.nJobs].startswith("jobm"):
118  milleJobs += 1
119  self.nJobs += 1
120  self.nJobs = milleJobs
121 
122  DBFILE.close()
123 
124 
125 
126  #-------------------------------------------------------------------------------
127  # prints the member varaiables and arrays to the terminal
128  def print_memdb(self):
129  #print metainfo
130  print "\n=== mps database printout ===\n"
131  print self.header
132  print 'Script:\t\t', self.batchScript
133  print 'cfg:\t\t', self.cfgTemplate
134  print 'files:\t\t', self.infiList
135  print 'class:\t\t', self.classInf
136  print 'name:\t\t', self.addFiles
137  print 'driver:\t\t', self.driver
138  print 'mergeScript:\t', self.mergeScript
139  print 'mssDir:\t\t', self.mssDir
140  print 'updateTime:\t', self.updateTimeHuman
141  print 'elapsed:\t', self.elapsedTime
142  print 'mssDirPool:\t', self.mssDirPool
143  print 'pedeMem:\t', self.pedeMem, '\n'
144 
145  #print interesting Job-level lists ---- to add: t/evt, fix remarks
146  print '### dir jobid stat try rtime nevt remark weight name'
147  print "------------------------------------------------------------------------------"
148  for i in xrange(self.nJobs):
149  print '%03d %6s %9s %6s %3d %5d %8d %8s %5s %s' % (
150  self.JOBNUMBER[i],
151  self.JOBDIR[i],
152  self.JOBID[i],
153  self.JOBSTATUS[i],
154  self.JOBNTRY[i],
155  self.JOBRUNTIME[i],
156  self.JOBNEVT[i],
157  self.JOBHOST[i],
158  self.JOBSP2[i],
159  self.JOBSP3[i])
160 
161  #print merge Jobs if merge mode
162  if self.driver == 'merge':
163  for i in xrange(self.nJobs,len(self.JOBDIR)):
164  print '%s %6s %9s %6s %3d %5d %8d %8s %5s %s' % (
165  'MMM',
166  self.JOBDIR[i],
167  self.JOBID[i],
168  self.JOBSTATUS[i],
169  self.JOBNTRY[i],
170  self.JOBRUNTIME[i],
171  self.JOBNEVT[i],
172  self.JOBHOST[i],
173  self.JOBSP2[i],
174  self.JOBSP3[i])
175 
176  #print summed info
177  totalEvents = sum(self.JOBNEVT[:self.nJobs])
178  totalCpu = sum(self.JOBRUNTIME[:self.nJobs])
179  meanCpuPerEvent = 0.
180  if totalEvents > 0:
181  meanCpuPerEvent = float(totalCpu)/totalEvents
182  print "------------------------------------------------------------------------------"
183  print "\t\t\t\t\tEvent total:\t", totalEvents
184  print "\t\t\t\t\tCPU total:\t", totalCpu, 's'
185  print "\t\t\t\t\tMean CPU/event:\t",meanCpuPerEvent,'s'
186 
187 
188 
189 
190 
191  #-------------------------------------------------------------------------------
192  # writes a new mps.db file from the members. Replaces the old mps.db
193  def write_db(self):
194  self.header = "mps database schema 3.2"
195  self.currentTime = int(time.time())
196  self.elapsedTime = 0;
197  if self.updateTime != 0:
198  self.elapsedTime = self.currentTime - self.updateTime
199  self.updateTime = self.currentTime
200  self.updateTimeHuman = str(datetime.datetime.today()) #no timezone :(
201  self.spare1 = "-- unused --"
202  self.spare2 = "-- unused --"
203  self.spare3 = "-- unused --"
204 
205  #if mps.db already exists, backup as mps.db~ (in case of interupt during write)
206  os.system('[[ -a mps.db ]] && cp -p mps.db mps.db~')
207 
208  #write mps.db header
209  DBFILE = open ("mps.db", "w")
210  headData = [ self.header, self.batchScript, self.cfgTemplate, self.infiList,
211  self.classInf, self.addFiles, self.driver, self.mergeScript,
212  self.mssDir, self.updateTime, self.updateTimeHuman,
213  self.elapsedTime, self.mssDirPool, self.pedeMem,
214  self.spare1, self.spare2, self.spare3 ]
215  for item in headData:
216  DBFILE.write("%s\n" % item)
217 
218  #write mps.db jobinfo
219  for i in xrange(len(self.JOBID)):
220  DBFILE.write('%03d:%s:%s:%s:%s:%s:%s:%s:%s:%s:%s:%s:%s\n' %
221  (i+1,
222  self.JOBDIR[i],
223  self.JOBID[i],
224  self.JOBSTATUS[i],
225  self.JOBNTRY[i],
226  self.JOBRUNTIME[i],
227  self.JOBNEVT[i],
228  self.JOBHOST[i],
229  self.JOBINCR[i],
230  self.JOBREMARK[i],
231  self.JOBSP1[i],
232  self.JOBSP2[i],
233  self.JOBSP3[i]))
234  DBFILE.close()
235 
236  #-------------------------------------------------------------------------------
237  # returns job class as stored in db
238  # one and only argument may be "mille" or "pede" for mille or pede jobs
239  def get_class(self, argument=''):
240  CLASSES = self.classInf.split(':')
241  if len(CLASSES)<1 or len(CLASSES)>2:
242  print '\nget_class():\n class must be of the form \'class\' or \'classMille:classPede\', but is \'%s\'!\n\n', classInf
243  sys.exit(1)
244  elif argument == 'mille':
245  return CLASSES[0]
246  elif argument == 'pede':
247  if len(CLASSES) == 1:
248  return CLASSES[0]
249  elif len(CLASSES) == 2:
250  return CLASSES[1]
251  else:
252  print '\nget_class():\n Know class only for \'mille\' or \'pede\', not %s!\n\n' %argument
253  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:239
#define str(s)