CMS 3D CMS Logo

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