CMS 3D CMS Logo

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