-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathSGE.py
executable file
·352 lines (272 loc) · 10.8 KB
/
SGE.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
#!/usr/bin/env python
"""
TODO Add support for SGE logs
$SGE_ROOT/$SGE_CELL/spool/qmaster/messages
$SGE_ROOT/$SGE_CELL/spool/qmaster/messages
execd spool logs
$SGE_ROOT/$SGE_CELL/spool/<node>/messages
panic location
/tmp in lieu of $SGE_ROOT
"""
import logging, pwd, os, re, subprocess, time
#TODO Use qsubOptions
from qsubopts import qsubOptions
class _JobData:
"""
Internal helper class to manage job data from qstat
"""
def __init__(self, qstat_job_line):
#The format of the line goes like
#job-ID prior name user state submit/start at queue slots ja-task-ID
tokens = qstat_job_line.split()
assert len(tokens) >= 8, 'Not a valid qstat line: '+qstat_job_line
#Job line must have at least 8 tokens
self.id = int(tokens[0])
self.priority = float(tokens[1])
self.name = tokens[2]
self.user = tokens[3]
self.state = tokens[4]
self.time = ' '.join(tokens[5:7])
if '@' in qstat_job_line:
#Has queue assigned, e.g. core2.q@q2.caspian.mit.edu
self.queue = tokens[7]
self.slots = tokens[8]
if len(tokens) == 9:
self.ja_task_ID = None
elif len(tokens) == 10:
self.ja_task_ID = tokens[-1]
else:
raise ValueError, 'Could not parse line: '+qstat_job_line
else:
#No queue assigned
self.slots = tokens[7]
if len(tokens) == 8:
self.ja_task_ID = None
elif len(tokens) == 9:
self.ja_task_ID = tokens[-1]
else:
raise ValueError, 'Could not parse line: '+qstat_job_line
#Convert array indices ja_task_ID into python list format
ja_task_ID = []
if self.ja_task_ID is not None:
for blob in self.ja_task_ID.split(','):
#Parse data of form '193-349:1' or '1934'
x = blob.split(':')
if len(x) == 1:
ja_task_ID += x
else:
subjobs, step = x
begin, end = subjobs.split('-')
ja_task_ID += range(int(begin), int(end)+1, int(step))
self._ja_tasklist = ja_task_ID
def __repr__(self):
repr = ['{']
for key, value in self.__dict__.items():
if key[0] != '_':
repr.append(key+'='+str(value))
repr.append('}')
return '\n'.join(repr)
class JobList:
"""
Internal helper class to manage job lists
"""
def __init__(self, qstat_output = None):
self._joblist = []
for line in qstat_output.split('\n')[2:-1]:
self._joblist.append(_JobData(line))
def __iter__(self):
for job in self._joblist:
yield job
def __repr__(self):
return '\n'.join([str(job) for job in self._joblist])
class SGE:
"""External system call handler for Sun Grid Engine environment."""
def __init__(self, q = None, path = '', ):
logger = logging.getLogger('SGE.__init__')
if q is None:
#No queue specified. By default, submit to all available queues.
self.cmd_qconf = os.path.join(path, 'qconf')
try:
qliststr = _exec(self.cmd_qconf+' -sql')
except IOError:
error_msg = 'Error querying queue configuration'
logger.error(error_msg)
raise IOError, error_msg
self.q = qliststr.replace('\n',',')[:-1]
logger.info("""Sun Grid Engine handler initialized
Queues detected: %s""", self.q)
else:
self.q = q
self.cmd_qsub = os.path.join(path, 'qsub')
self.cmd_qstat = os.path.join(path, 'qstat')
def wait(self, jobid, interval = 10, name = None, pbar = None,
pbar_mode = None):
"""Waits for job running on SGE Grid Engine environment to finish.
If you are just waiting for one job, this becomes a dumb substitute for
the -sync y option which can be specified to qsub.
Inputs:
jobid
interval - Polling interval of SGE queue, in seconds. (Default: 10)
"""
logger = logging.getLogger('SGE.wait')
dowait = True
while dowait:
p = subprocess.Popen(self.cmd_qstat, shell = True,
stdout = subprocess.PIPE)
pout, _ = p.communicate()
if pbar is not None:
logger.error('Progress bar handling not implemented')
#if pbar_mode == 'quantum':
# pbar.update(getstarfleet(getouts()))
#elif pbar_mode == 'mdrun':
# pbar.update(getstep())
dowait = False
for line in pout.split('\n'):
t = line.split()
if len(t) >= 5: #Find a line with useful info
if t[0] == str(jobid) and re.search(t[4], 'qwrt'):
#Job must be queued, running or being transferred
dowait = True
break
if t[0] == str(jobid) and re.search(t[4], 'acuE'):
#Job or host in error state
logger.warning('Job %d in error state', str(jobid))
if dowait:
time.sleep(interval)
waited = True
if name is None:
logger.info("Time %s: waiting for jobid %s to finish",
time.ctime(), str(jobid))
else:
logger.info("Time %s: waiting for job '%s' (jobid %s) to \
finish", time.ctime(), name, str(jobid))
def submit(self, job, array = False, useenvironment = True, usecwd = True,
name = None, stdin = None, stdout = None, stderr = None,
joinstdouterr = True, nproc = 1, wait = True, lammpi = True):
"""Submits a job to SGE
Returns jobid as a number"""
logger = logging.getLogger('SGE.submit')
logger.info("Submitting job: \x1b[1;91m%-50s\x1b[0m Stdout: %s \
Stderr: %s", job, stdout, stderr)
#Parameters to qsub specified as the header of the job specified on
#STDIN
lamstring = lammpi and " -pe lammpi %d" % nproc or ""
qsuboptslist = ['-cwd -V ', lamstring]
if name != None:
qsuboptslist.append('-N '+name)
if stdin != None:
qsuboptslist.append('-i '+stdin)
if stdout != None:
qsuboptslist.append('-o '+stdout)
if stderr != None:
qsuboptslist.append('-e '+stderr)
if joinstdouterr:
qsuboptslist.append('-j')
if wait:
qsuboptslist.append('-sync y')
if usecwd:
qsuboptslist.append('-cwd')
if useenvironment:
qsuboptslist.append('-V')
if array != False:
try:
n = int(array[0])
except IndexError:
n = int(array)
try:
m = int(array[1])
except IndexError:
m = None
try:
s = int(array[2])
except IndexError:
s = None
if m == s == None:
qsuboptslist.append('-t %d' % n)
elif s == None:
qsuboptslist.append('-t %d-%d' % (n, m))
else:
qsuboptslist.append('-t %d-%d:%d' % (n, m, s))
qsubopts = ' '.join(qsuboptslist)
pout = _exec(self.cmd_qsub, stdin = qsubopts + '\n' + job,
print_command = False)
try:
#Next to last line should be
#"Your job 1389 (name) has been submitted"
#parse for job id
jobid = int(pout.split('\n')[-2].split()[2])
return jobid
except (ValueError, IndexError, AttributeError), e:
error_msg = """Error submitting SGE job:
%s
%s
Output was:
%s""" % (qsubopts, job, pout)
logger.error(error_msg)
raise e
def getuserjobs(self, user = pwd.getpwuid(os.getuid())[0]):
"""Returns a list of SGE jobids run by a specific user
Inputs
user - SGE user to poll (Default = '', i.e. current user)
qstat - path to qstat binary (Default = 'qstat')
"""
p = subprocess.Popen(self.cmd_qstat + " -u " + user, shell = True,
stdout = subprocess.PIPE)
qstat_output, _ = p.communicate()
joblist = JobList(qstat_output)
return [job for job in joblist if job.user == user]
def run_job(self, command, name = 'default', logfnm = 'default.log',
wait = True):
"""Run job on SGE with piped logging."""
jobid = self.submit(command, name = name, stdout = logfnm,
stderr = logfnm, wait = wait)
def get_job_data(self, jobid):
output = _exec(' '.join([self.cmd_qstat, '-j', str(jobid)]))
if 'Following jobs do not exist' in output:
return None
data = dict()
for line in output.split('\n')[1:]:
t = line.split()
if len(t) > 1 and t[0][-1] == ':':
data[t[0][:-1]] = t[1]
return data
def get_queue_instance_status(self):
"""
Get loads for each queue instance
"""
output = _exec(' '.join([self.cmd_qstat, '-f']))
data = []
for line in output.split('\n')[1:]:
t = line.split()
if len(t) != 5:
continue
nodename = t[0].split('@')[1].split('.')[0]
maxslots = int(t[2].split('/')[2])
load = float(t[3])
data.append({'name':nodename, 'maxslots':maxslots, 'load':load})
return data
def _exec(command, print_to_screen = False, logfnm = None, stdin = '',
print_command = False):
"""
Runs command line using subprocess, optionally returning stdout
"""
logger = logging.getLogger('_exec')
print_to_file = (logfnm != None)
if print_to_file:
f = open(logfnm, 'a')
if print_command:
logger.info("Executing process: \x1b[1;92m%-50s\x1b[0m Logfile: %s",
command, logfnm)
if print_to_file:
print >> f, "Executing process: %s" % command
p = subprocess.Popen(command, shell=True, stdin = subprocess.PIPE,
stdout = subprocess.PIPE,
stderr = subprocess.STDOUT)
Output, _ = p.communicate(stdin)
logger.info('Output of command is:\n%s', Output)
if print_to_screen:
print Output
if logfnm != None:
f.write(Output)
f.close()
return Output