CMS 3D CMS Logo

mps_update.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 import os
3 import re
4 import subprocess
5 import Alignment.MillePedeAlignmentAlgorithm.mpslib.Mpslibclass as mpslib
6 
7 
8 def fill_time_info(mps_index, status, cpu_time):
9  """Fill timing info in the database for `mps_index`.
10 
11  Arguments:
12  - `mps_index`: index in the MPS database
13  - `status`: job status
14  - `cpu_time`: extracted CPU timing information
15  """
16 
17  cpu_time = int(round(cpu_time)) # care only about seconds for now
18  if status in ("RUN", "DONE"):
19  if cpu_time > 0:
20  diff = cpu_time - lib.JOBRUNTIME[mps_index]
21  lib.JOBRUNTIME[mps_index] = cpu_time
22  lib.JOBHOST[mps_index] = "+"+str(diff)
23  lib.JOBINCR[mps_index] = diff
24  else:
25  lib.JOBRUNTIME[mps_index] = 0
26  lib.JOBINCR[mps_index] = 0
27 
28 
29 
30 ################################################################################
31 # mapping of HTCondor status codes to MPS status
32 htcondor_jobstatus = {"1": "PEND", # Idle
33  "2": "RUN", # Running
34  "3": "EXIT", # Removed
35  "4": "DONE", # Completed
36  "5": "PEND", # Held
37  "6": "RUN", # Transferring output
38  "7": "PEND"} # Suspended
39 
40 
41 ################################################################################
42 # collect submitted jobs (use 'in' to handle composites, e.g. DISABLEDFETCH)
43 lib = mpslib.jobdatabase()
44 lib.read_db()
45 
46 submitted_jobs = {}
47 for i in xrange(len(lib.JOBID)):
48  submitted = True
49  for status in ("SETUP", "OK", "DONE", "FETCH", "ABEND", "WARN", "FAIL"):
50  if status in lib.JOBSTATUS[i]:
51  submitted = False
52  break
53  if submitted:
54  submitted_jobs[lib.JOBID[i]] = i
55 print "submitted jobs:", len(submitted_jobs)
56 
57 
58 ################################################################################
59 # deal with submitted jobs by looking into output of shell (bjobs/condor_q)
60 if len(submitted_jobs) > 0:
61  job_status = {}
62  if "htcondor" in lib.get_class("pede"):
63  condor_q = subprocess.check_output(["condor_q", "-af:j",
64  "JobStatus", "RemoteSysCpu"],
65  stderr = subprocess.STDOUT)
66  for line in condor_q.splitlines():
67  job_id, status, cpu_time = line.split()
68  job_status[job_id] = {"status": htcondor_jobstatus[status],
69  "cpu": float(cpu_time)}
70 
71  bjobs = subprocess.check_output(["bjobs", "-l", "-a"],
72  stderr = subprocess.STDOUT)
73  bjobs = bjobs.replace("\n","")
74 
75  job_regex = re.compile(r"Job<(\d+?)>,")
76  status_regex = re.compile(r"Status<([A-Z]+?)>")
77  cputime_regex = re.compile(r"TheCPUtimeusedis(\d+(\.\d+)?)seconds")
78  if bjobs != "No job found":
79  results = bjobs.replace(" ","").split("-----------------------")
80  for line in results:
81  if len(line.strip()) == 0: continue
82  # extract jobID
83  job_id = job_regex.search(line).group(1)
84  # extract job status
85  status = status_regex.search(line).group(1)
86  # extract CPU time (only present for finished job)
87  match = cputime_regex.search(line)
88  cpu_time = float(match.group(1)) if match else 0
89  print "out ", job_id, " ", status, " ", cpu_time
90  job_status[job_id] = {"status": status,
91  "cpu": cpu_time}
92 
93  for job_id, job_info in job_status.iteritems():
94  mps_index = submitted_jobs.get(job_id, -1)
95  # check for disabled Jobs
96  disabled = "DISABLED" if "DISABLED" in lib.JOBSTATUS[mps_index] else ""
97 
98  # continue with next batch job if not found or not interesting
99  if mps_index == -1:
100  print "mps_update.py - the job", job_id,
101  print "was not found in the JOBID array"
102  continue
103  else: # pop entry from submitted jobs
104  submitted_jobs.pop(job_id)
105 
106 
107  # if found update Joblists for mps.db
108  lib.JOBSTATUS[mps_index] = disabled+job_info["status"]
109  fill_time_info(mps_index, job_info["status"], job_info["cpu"])
110 
111 
112 ################################################################################
113 # loop over remaining jobs to see whether they are done
114 for job_id, mps_index in submitted_jobs.items(): # IMPORTANT to copy here (no iterator!)
115  # check if current job is disabled. Print stuff.
116  disabled = "DISABLED" if "DISABLED" in lib.JOBSTATUS[mps_index] else ""
117  print " DB job ", job_id, mps_index
118 
119  # check if job may be done by looking if a folder exists in the project directory.
120  # if True -> jobstatus is set to DONE
121  theBatchDirectory = "LSFJOB_"+job_id
122  if os.path.isdir(theBatchDirectory):
123  print "Directory ", theBatchDirectory, "exists"
124  lib.JOBSTATUS[mps_index] = disabled + "DONE"
125  submitted_jobs.pop(job_id)
126  continue
127 
128  # check if it is a HTCondor job already moved to "history"
129  elif "htcondor" in lib.get_class("pede"):
130  userlog = os.path.join("jobData", lib.JOBDIR[mps_index], "HTCJOB")
131  condor_h = subprocess.check_output(["condor_history", job_id, "-limit", "1",
132  "-userlog", userlog,
133  "-af:j", "JobStatus", "RemoteSysCpu"],
134  stderr = subprocess.STDOUT)
135  if len(condor_h.strip()) > 0:
136  job_id, status, cpu_time = condor_h.split()
137  status = htcondor_jobstatus[status]
138  lib.JOBSTATUS[mps_index] = disabled + status
139  fill_time_info(mps_index, status, float(cpu_time))
140  submitted_jobs.pop(job_id)
141  continue
142 
143  if "RUN" in lib.JOBSTATUS[mps_index]:
144  print "WARNING: Job ", mps_index,
145  print "in state RUN, neither found by htcondor, nor bjobs, nor find",
146  print "LSFJOB directory!"
147 
148 
149 ################################################################################
150 # check for orphaned jobs
151 for job_id, mps_index in submitted_jobs.iteritems():
152  for status in ("SETUP", "DONE", "FETCH", "TIMEL", "SUBTD"):
153  if status in lib.JOBSTATUS[mps_index]:
154  print "Funny entry index", mps_index, " job", lib.JOBID[mps_index],
155  print " status", lib.JOBSTATUS[mps_index]
156 
157 
158 lib.write_db()
def fill_time_info(mps_index, status, cpu_time)
Definition: mps_update.py:8
double split
Definition: MVATrainer.cc:139