CMS 3D CMS Logo

mps_update.py
Go to the documentation of this file.
1 #!/usr/bin/env python3
2 from __future__ import print_function
3 from builtins import range
4 import os
5 import re
6 import subprocess
7 import Alignment.MillePedeAlignmentAlgorithm.mpslib.Mpslibclass as mpslib
8 
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 
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 
45 lib = mpslib.jobdatabase()
46 lib.read_db()
47 
48 submitted_jobs = {}
49 for i in range(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 
62 if len(submitted_jobs) > 0:
63  job_status = {}
64  condor_q = subprocess.check_output(["condor_q", "-af:j",
65  "JobStatus", "RemoteSysCpu"],
66  stderr = subprocess.STDOUT).decode()
67  for line in condor_q.splitlines():
68  job_id, status, cpu_time = line.split()
69  job_status[job_id] = {"status": htcondor_jobstatus[status],
70  "cpu": float(cpu_time)}
71 
72  for job_id, job_info in job_status.items():
73  mps_index = submitted_jobs.get(job_id, -1)
74  # check for disabled Jobs
75  disabled = "DISABLED" if "DISABLED" in lib.JOBSTATUS[mps_index] else ""
76 
77  # continue with next batch job if not found or not interesting
78  if mps_index == -1:
79  print("mps_update.py - the job", job_id, end=' ')
80  print("was not found in the JOBID array")
81  continue
82  else: # pop entry from submitted jobs
83  submitted_jobs.pop(job_id)
84 
85 
86  # if found update Joblists for mps.db
87  lib.JOBSTATUS[mps_index] = disabled+job_info["status"]
88  fill_time_info(mps_index, job_info["status"], job_info["cpu"])
89 
90 
91 
93 submitted_jobs_copy = { k:v for k,v in submitted_jobs.items() }
94 for job_id, mps_index in submitted_jobs_copy.items(): # IMPORTANT to copy here (no iterator!)
95  # check if current job is disabled. Print stuff.
96  disabled = "DISABLED" if "DISABLED" in lib.JOBSTATUS[mps_index] else ""
97  print(" DB job ", job_id, mps_index)
98 
99  # check if it is a HTCondor job already moved to "history"
100  userlog = os.path.join("jobData", lib.JOBDIR[mps_index], "HTCJOB")
101  condor_h = subprocess.check_output(["condor_history", job_id, "-limit", "1",
102  "-userlog", userlog,
103  "-af:j", "JobStatus", "RemoteSysCpu"],
104  stderr = subprocess.STDOUT).decode()
105  if len(condor_h.strip()) > 0:
106  job_id, status, cpu_time = condor_h.split()
107  status = htcondor_jobstatus[status]
108  lib.JOBSTATUS[mps_index] = disabled + status
109  fill_time_info(mps_index, status, float(cpu_time))
110  submitted_jobs.pop(job_id)
111  continue
112 
113  if "RUN" in lib.JOBSTATUS[mps_index]:
114  print("WARNING: Job ", mps_index, end=' ')
115  print("in state RUN, neither found by htcondor, nor bjobs, nor find", end=' ')
116  print("LSFJOB directory!")
117 
118 
119 
121 for job_id, mps_index in submitted_jobs.items():
122  for status in ("SETUP", "DONE", "FETCH", "TIMEL", "SUBTD"):
123  if status in lib.JOBSTATUS[mps_index]:
124  print("Funny entry index", mps_index, " job", lib.JOBID[mps_index], end=' ')
125  print(" status", lib.JOBSTATUS[mps_index])
126 
127 
128 lib.write_db()
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
Definition: Utilities.cc:47
def fill_time_info(mps_index, status, cpu_time)
Definition: mps_update.py:10
bool decode(bool &, std::string_view)
Definition: types.cc:72
#define str(s)