CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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 - what is this?
39 
40 import datetime
41 import time
42 import os
43 
44 #-------------------------------------------------------------------------------
46 
47  JOBNUMBER, JOBDIR, JOBID, JOBSTATUS, JOBNTRY, JOBRUNTIME, JOBNEVT, JOBHOST, JOBINCR, \
48  JOBREMARK, JOBSP1, JOBSP2, JOBSP3 = ([] for i in range(13))
49 
50  header, batchScript, cfgTemplate, infiList, classInf, addFiles, driver, mergeScript, \
51  mssDir, updateTimeHuman, mssDirPool, spare1, spare2, spare3 = ('' for i in range(14))
52 
53  updateTime, elapsedTime, pedeMem , nJobs = -1, -1, -1, -1
54 
55  #-------------------------------------------------------------------------------
56  # parses the mps.db file into the member variables and arrays
57  def read_db(self):
58  DBFILE = open('mps.db','r')
59 
60  #read infolines at the top, used rstrip to delete the '\n'
61  self.header = DBFILE.readline().strip()
62  self.batchScript = DBFILE.readline().rstrip('\n')
63  self.cfgTemplate = DBFILE.readline().rstrip('\n')
64  self.infiList = DBFILE.readline().rstrip('\n')
65  self.classInf = DBFILE.readline().rstrip('\n') #formerly named 'class' ->conflict
66  self.addFiles = DBFILE.readline().rstrip('\n')
67  self.driver = DBFILE.readline().rstrip('\n')
68  self.mergeScript = DBFILE.readline().rstrip('\n')
69  self.mssDir = DBFILE.readline().rstrip('\n')
70  self.updateTime = int(DBFILE.readline())
71  self.updateTimeHuman = DBFILE.readline().rstrip('\n')
72  self.elapsedTime = int(DBFILE.readline())
73  self.mssDirPool = DBFILE.readline().rstrip('\n')
74  self.pedeMem = int(DBFILE.readline())
75  self.spare1 = DBFILE.readline().rstrip('\n')
76  self.spare2 = DBFILE.readline().rstrip('\n')
77  self.spare3 = DBFILE.readline().rstrip('\n')
78 
79  #read actual jobinfo into arrays
80  self.nJobs = 0
81  milleJobs = 0
82 
83 
84  for line in DBFILE:
85  line = line.rstrip('\n') #removes the pesky \n from line
86  parts = line.split(":") #read each line and split into parts list
87  self.JOBNUMBER.append(int(parts[0]))
88  self.JOBDIR.append(parts[1])
89  self.JOBID.append(int(parts[2]))
90  self.JOBSTATUS.append(parts[3])
91  self.JOBNTRY.append(int(parts[4]))
92  self.JOBRUNTIME.append(int(parts[5])) #int float?
93  self.JOBNEVT.append(int(parts[6]))
94  self.JOBHOST.append(parts[7])
95  self.JOBINCR.append(int(parts[8]))
96  self.JOBREMARK.append(parts[9])
97  self.JOBSP1.append(parts[10])
98  self.JOBSP2.append(parts[11])
99  self.JOBSP3.append(parts[12])
100 
101  #count number of jobs
102  if 'jobm' not in self.JOBDIR[self.nJobs]:
103  milleJobs += 1
104  self.nJobs += 1
105  self.nJobs = milleJobs
106 
107  DBFILE.close()
108 
109 
110 
111  #-------------------------------------------------------------------------------
112  # prints the member varaiables and arrays to the terminal
113  def print_memdb(self):
114  #print metainfo
115  print "\n=== mps database printout ===\n"
116  print self.header
117  print 'Script:\t\t', self.batchScript
118  print 'cfg:\t\t', self.cfgTemplate
119  print 'files:\t\t', self.infiList
120  print 'class:\t\t', self.classInf
121  print 'name:\t\t', self.addFiles
122  print 'driver:\t\t', self.driver
123  print 'mergeScript:\t', self.mergeScript
124  print 'mssDir:\t\t', self.mssDir
125  print 'updateTime:\t', self.updateTimeHuman
126  print 'elapsed:\t', self.elapsedTime
127  print 'mssDirPool:\t', self.mssDirPool
128  print 'pedeMem:\t', self.pedeMem, '\n'
129 
130  #print interesting Job-level lists ---- to add: t/evt, fix remarks
131  print '### dir jobid stat try rtime nevt remark weight name'
132  print "------------------------------------------------------------------------------"
133  for i in xrange(self.nJobs):
134  print '%03d %6s %9d %6s %3d %5d %8d %8s %5s %s' % (
135  self.JOBNUMBER[i],
136  self.JOBDIR[i],
137  self.JOBID[i],
138  self.JOBSTATUS[i],
139  self.JOBNTRY[i],
140  self.JOBRUNTIME[i],
141  self.JOBNEVT[i],
142  self.JOBHOST[i],
143  self.JOBSP2[i],
144  self.JOBSP3[i])
145 
146  #print merge Jobs if merge mode
147  if self.driver == 'merge':
148  for i in xrange(self.nJobs,len(self.JOBDIR)):
149  print '%s %6s %9d %6s %3d %5d %8d %8s %5s %s' % (
150  'MMM',
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 summed info
162  totalEvents = sum(self.JOBNEVT[:self.nJobs])
163  totalCpu = sum(self.JOBRUNTIME[:self.nJobs])
164  meanCpuPerEvent = 0.
165  if totalEvents > 0:
166  meanCpuPerEvent = float(totalCpu)/totalEvents
167  print "------------------------------------------------------------------------------"
168  print "\t\t\t\t\tEvent total:\t", totalEvents
169  print "\t\t\t\t\tCPU total:\t", totalCpu, 's'
170  print "\t\t\t\t\tMean CPU/event:\t",meanCpuPerEvent,'s'
171 
172 
173 
174 
175 
176  #-------------------------------------------------------------------------------
177  # writes a new mps.db file from the members. Replaces the old mps.db
178  def write_db(self):
179  self.header = "mps database schema 3.2"
180  self.currentTime = int(time.time())
181  self.elapsedTime = 0;
182  if self.updateTime != 0:
183  self.elapsedTime = self.currentTime - self.updateTime
184  self.updateTime = self.currentTime
185  self.updateTimeHuman = str(datetime.datetime.today()) #no timezone :(
186  self.spare1 = "-- unused --"
187  self.spare2 = "-- unused --"
188  self.spare3 = "-- unused --"
189 
190  #if mps.db already exists, backup as mps.db~ (in case of interupt during write)
191  os.system('[[ -a mps.db ]] && cp -p mps.db mps.db~')
192 
193  #write mps.db header
194  DBFILE = open ("mps.db", "w")
195  headData = [ self.header, self.batchScript, self.cfgTemplate, self.infiList,
196  self.classInf, self.addFiles, self.driver, self.mergeScript,
197  self.mssDir, self.updateTime, self.updateTimeHuman,
198  self.elapsedTime, self.mssDirPool, self.pedeMem,
199  self.spare1, self.spare2, self.spare3 ]
200  for item in headData:
201  DBFILE.write("%s\n" % item)
202 
203  #write mps.db jobinfo
204  for i in xrange(len(self.JOBID)):
205  DBFILE.write('%03d:%s:%05d:%s:%s:%s:%s:%s:%s:%s:%s:%s:%s\n' %
206  (i+1,
207  self.JOBDIR[i],
208  self.JOBID[i],
209  self.JOBSTATUS[i],
210  self.JOBNTRY[i],
211  self.JOBRUNTIME[i],
212  self.JOBNEVT[i],
213  self.JOBHOST[i],
214  self.JOBINCR[i],
215  self.JOBREMARK[i],
216  self.JOBSP1[i],
217  self.JOBSP2[i],
218  self.JOBSP3[i]))
219  DBFILE.close()
220 
221  #-------------------------------------------------------------------------------
222  # returns job class as stored in db
223  # one and only argument may be "mille" or "pede" for mille or pede jobs
224  def get_class(self, argument=''):
225  CLASSES = self.classInf.split(':')
226  if len(CLASSES)<1 or len(CLASSES)>2:
227  print '\nget_class():\n class must be of the form \'class\' or \'classMille:classPede\', but is \'%s\'!\n\n', classInf
228  exit()
229  return ''
230  elif argument == 'mille':
231  return CLASSES[0]
232  elif argument == 'pede':
233  if len(CLASSES) == 1:
234  return CLASSES[0]
235  elif len(CLASSES) == 2:
236  return CLASSES[1]
237  else:
238  print '\nget_class():\n Know class only for \'mille\' or \'pede\', not %s!\n\n' %argument
239  exit() #???????????????
240  return ''
241 
242 
243 
244 
245 
246 
247 
248 
249