-
Notifications
You must be signed in to change notification settings - Fork 0
/
molsim_job_scheduler.py
656 lines (516 loc) · 17.2 KB
/
molsim_job_scheduler.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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
import os
import re
import copy
import time
import atexit
import pathlib
import threading
import subprocess
import collections
from xml.etree import ElementTree
import zmq
QSTAT_PATH = "/usr/local/pbs/bin/qstat"
STAT_DATA_PATH = str(pathlib.Path(__file__).parent / "stat.dat")
JOBS_DATA_PATH = str(pathlib.Path(__file__).parent / "jobs.dat")
Stat = collections.namedtuple("Stat",
[
"user",
"nodes",
"start_time",
"end_time",
"last_update_time",
"duration",
]
)
def print_xml(elem, level=0):
for c in elem:
print(" "*level, c.tag, c.text)
print_xml(c, level+1)
def extract_nodes_from_qsub(qsub):
with open(qsub, "r") as f:
data = f.read()
nodes = re.search(r"#PBS -l .+?\n", data)
if not nodes:
return None
else:
nodes = nodes.group().split()[-1][6:]
names = re.search(r"#PBS -N .+?\n", data)
if names:
names = names.group()
else:
names = str(qsub)
#if 'debug' in names.lower():
# nodes = nodes+'_debug'
return nodes
def parse_nodes(nodes):
"""
Example of nodes: "1:ppn=8:ac"
"""
tokens = nodes.split(":")
assert len(tokens) == 3
try:
num_nodes = int(tokens[0])
except ValueError:
num_nodes = len(tokens[0].split("+"))
num_cores = int(tokens[1].split("=")[1])
total_cores = num_nodes * num_cores
node_name = tokens[2]
return node_name, total_cores
def read_stat():
# Get previous stat.
stat_data = {}
stat_path = pathlib.Path(STAT_DATA_PATH)
if stat_path.exists():
with stat_path.open("r") as f:
for line in f:
# [-1] for duration.
tokens = line[:-1].split("|")
if len(tokens) != 6: # To prevent write_token_error
continue
stat = Stat._make(tokens[1:] + [-1])
stat_data[tokens[0]] = stat
# Alias.
stat_days = StatParser.STAT_DAYS
stat_seconds = stat_days * 24 * 3600
current_time = time.time()
stat_start_time = current_time - stat_seconds
# Calculate duration.
for k in stat_data.keys():
start_time = stat_data[k].start_time
if start_time == "-1":
continue
end_time = stat_data[k].end_time
start_time = max(int(start_time), stat_start_time)
if end_time == "-1":
end_time = current_time
else:
end_time = float(end_time)
duration = end_time - start_time
stat_data[k] = stat_data[k]._replace(duration=duration)
return stat_data
def write_stat(stat_data):
# Update stat file.
stat_path = pathlib.Path(STAT_DATA_PATH)
with stat_path.open("w") as f:
for k, stat in stat_data.items():
# Duration is not saved ([:-1]).
v = list(stat._asdict().values())[:-1]
f.write("{}|{}|{}|{}|{}|{}\n".format(k, *v))
class StatParser:
"""
StatParser.parse() returns stat_data.
stat_data
key: job id
value: Stat
time = second unit
"""
# Day unit.
DATA_STORAGE_PERIOD = 30
STAT_DAYS = 30
# Hour unit
DEBUG_PERIOD = 1
def parse(self):
# Alias.
stat_days = StatParser.STAT_DAYS
stat_seconds = stat_days * 24 * 3600
# Stat storage period.
storage_seconds = StatParser.DATA_STORAGE_PERIOD * 24 * 3600
current_time = time.time()
stat_start_time = current_time - stat_seconds
# Debug time
debug_seconds = StatParser.DEBUG_PERIOD * 3600
# Get qstat information.
result = subprocess.run(
[QSTAT_PATH, "-xf"],
stdout=subprocess.PIPE
)
root = ElementTree.fromstring(result.stdout.decode("utf-8"))
stat_data = read_stat()
# Get current stat.
data = ""
for e in root.findall("Job"):
c = e.find("job_state")
#if c.text != "R":
# continue
c = e.find("Job_Owner")
user = c.text.split("@")[0]
c = e.find("Job_Id")
job_id = c.text
c = e.find("start_time")
if c is None:
start_time = "-1"
else:
start_time = int(c.text)
c = e.find("Resource_List").find("nodes")
nodes = c.text
c = e.find("Job_Name")
#debug = 'debug' in c.text.lower()
#if debug:
# nodes = nodes+'_debug'
#if debug and (current_time-start_time > debug_seconds): # DEBUG over limit
# command = f'qdel {job_id}'
# result = subprocess.run(
# ["su", "-", user, "-c", command]
# )
# continue
end_time = "-1"
data += "{}|{}|{}|{}|{}|{}\n".format(
job_id, user, nodes, start_time, end_time, current_time
)
temp_data = {}
for line in data.split("\n")[:-1]:
tokens = line.split("|")
# [-1] for duration.
stat = Stat._make(tokens[1:] + [-1])
temp_data[tokens[0]] = stat
# Update Q -> R jobs.
for k in stat_data.keys():
if k not in temp_data:
continue
if stat_data[k].start_time != "-1":
continue
if temp_data[k].start_time == "-1":
continue
stat_data[k] = temp_data[k]
# Set end time of ended jobs.
ended_jobs = []
for k in stat_data.keys():
if k in temp_data:
continue
if stat_data[k].end_time != "-1":
continue
ended_jobs.append(k)
for job_id in ended_jobs:
stat_data[job_id] = stat_data[job_id]._replace(
end_time=stat_data[job_id].last_update_time
)
# Update stat data.
for k, v in temp_data.items():
if k in stat_data:
continue
stat_data[k] = v
# Remode old jobs.
old_jobs = []
old_time = current_time - storage_seconds
for k, v in stat_data.items():
if v.end_time == "-1":
continue
end_time = float(v.end_time)
if end_time < old_time:
old_jobs.append(k)
for k in old_jobs:
stat_data.pop(k, None)
# Calculate duration.
for k in stat_data.keys():
start_time = stat_data[k].start_time
if start_time == "-1":
continue
end_time = stat_data[k].end_time
start_time = max(int(start_time), stat_start_time)
if end_time == "-1":
end_time = current_time
else:
end_time = float(end_time)
duration = end_time - start_time
stat_data[k] = stat_data[k]._replace(duration=duration)
# Update last_update_time.
for k in stat_data.keys():
stat_data[k] = stat_data[k]._replace(last_update_time=current_time)
#LOCK.acquire()
write_stat(stat_data)
#LOCK.release()
return stat_data
def calculate_usage(stat_data):
defaultdict = collections.defaultdict
usage = defaultdict(lambda: defaultdict(int))
for k, v in stat_data.items():
user = v.user
# N cores.
nodes = v.nodes
try:
node_name, n_cores = parse_nodes(nodes)
except:
node_name, n_cores = "error", 1
duration = v.duration
if str(duration) == "-1":
continue
usage[user][node_name] += n_cores * duration
return usage
def calculate_running_cores(stat_data):
defaultdict = collections.defaultdict
running_cores = defaultdict(lambda: defaultdict(int))
for k, v in stat_data.items():
if v.end_time != "-1":
continue
user = v.user
# N cores.
nodes = v.nodes
try:
node_name, n_cores = parse_nodes(nodes)
except:
node_name, n_cores = "error", 1
running_cores[user][node_name] += n_cores
return running_cores
def print_usage(usage):
for k, v in usage.items():
print("="*20)
print("User:", k, "\n")
print("{:10s}{:10s}".format("Node", "Usage (sec)"))
for kk, vv in v.items():
print("{:10s}{:<10.0f}".format(kk, vv))
class Job:
def __init__(self, _id, _dir, _file, _time, nodes, user):
self.id = _id
self.dir = str(pathlib.Path(_dir).resolve())
self.file = _file
# Submission time.
self.time = _time
self.nodes = nodes
self.user = user
self.submitted = False
def submit(self):
su_command = 'cd {}; qsub {}'.format(self.dir, self.file)
result = subprocess.run(
["su", "-", self.user, "-c", su_command]
)
print(self.id, self.user, self.nodes, "submitted")
self.submitted = True
@staticmethod
def from_string(string):
if string[-1] == "\n":
string = string[:-1]
tokens = string.split("|")
try:
job = Job(*tokens)
except TypeError:
print (tokens)
raise TypeError()
# Type cast.
job.id = int(job.id)
job.time = float(job.time)
return job
def to_string(self):
return "{}|{}|{}|{}|{}|{}".format(
self.id, self.dir, self.file, self.time, self.nodes, self.user
)
def __repr__(self):
return self.to_string()
# Global variables.
MAX_ID = 0
JOBS = []
STAT_DATA = {}
LOCK = threading.Lock()
def save_jobs(jobs):
"""
Usage:
LOCK.acquire()
save_jobs(jobs)
LOCK.release()
"""
with open(JOBS_DATA_PATH, "w") as f:
for job in jobs:
f.write(job.to_string()+"\n")
class Scheduler(threading.Thread):
UPDATE_INTERVAL = 1
def __init__(self, parser):
super().__init__()
self.config = self._parse_config()
self.parser = parser
def _parse_config(self):
config = {}
with open("config.txt", "r") as f:
for line in f.readlines():
if line.startswith("#"):
continue
node_name, cores, limits = line.split()
config[node_name] = int(cores), int(limits)
return config
def _iter_jobs(self, sorted_key_jobs, running_cores):
check_node = collections.defaultdict(lambda : True)
for key, job in sorted_key_jobs:
try:
node_name, n_cores = parse_nodes(job.nodes)
except Exception:
node_name, n_cores = "error", 1
# Neglect invalid node names.
if node_name not in self.config:
continue
if not check_node[node_name]:
continue
limits = self.config[node_name][1]
if running_cores[job.user][node_name] + n_cores > limits:
continue
all_cores = sum([v[node_name] for v in running_cores.values()])
max_cores = self.config[node_name][0]
if all_cores + n_cores > max_cores:
check_node[node_name] = False
continue
job.submit()
running_cores[job.user][node_name] += n_cores
return sorted_key_jobs
def run(self):
global JOBS
while True:
start_time = time.time()
# -------------------------------------------
LOCK.acquire()
STAT_DATA = self.parser.parse()
usage_dict = calculate_usage(STAT_DATA)
running_cores = calculate_running_cores(STAT_DATA)
# Sort by (node_name, usage, submission_time).
key_jobs = []
for job in JOBS:
node_name = job.nodes.split(":")[2]
usage = usage_dict[job.user][node_name]
key = (node_name, usage, job.time)
key_jobs.append((key, job))
sorted_key_jobs = sorted(key_jobs, key=lambda x: x[0])
sorted_key_jobs = self._iter_jobs(sorted_key_jobs, running_cores)
JOBS = [job for _, job in sorted_key_jobs if not job.submitted]
save_jobs(JOBS)
LOCK.release()
# ------------------------------------------
duration = time.time() - start_time
time.sleep(max(Scheduler.UPDATE_INTERVAL - duration, 1))
def read_jobs():
jobs_path = pathlib.Path(JOBS_DATA_PATH)
jobs = []
if jobs_path.exists():
with jobs_path.open("r") as f:
for line in f:
job = Job.from_string(line)
jobs.append(job)
return jobs
def check_pbc_name(_dir, _file):
path = pathlib.Path("{}/{}".format(_dir, _file))
try:
with path.open("r") as f:
for line in f:
line = line.strip()
if not line:
continue
tokens = line.split()
if tokens[0] == "#PBS" and tokens[1] == "-N":
return " ".join(tokens[2:])
return None
except:
return None
class JobManipulator(threading.Thread):
PORT = 55554
def __init__(self):
super().__init__()
self.max_id = 0
def run(self):
global JOBS
LOCK.acquire()
# Load backup jobs.
JOBS = read_jobs()
LOCK.release()
# Set max id.
self.max_id = 0
if JOBS:
self.max_id = max([job.id for job in JOBS])
# Prepair communications.
context = zmq.Context()
socket = context.socket(zmq.REP)
socket.bind("tcp://*:{}".format(JobManipulator.PORT))
while True:
# Wait for next request from client
message = socket.recv().decode("utf-8")
tokens = message.split("|")
function = tokens[0]
if function == "qas":
self.do_qas(tokens[1:])
socket.send(b"Done")
elif function == "qrm":
self.do_qrm(tokens[1:])
socket.send(b"Done")
elif function == "qinfo":
message = self.do_qinfo(tokens[1:])
socket.send(message.encode())
def do_qas(self, args):
global JOBS
LOCK.acquire()
for qsub in args:
JOBS.append(self.job_from_qsub(qsub))
time.sleep(0.5)
LOCK.release()
time.sleep(0.5)
def do_qinfo(self, args):
global JOBS
LOCK.acquire()
jobs = copy.deepcopy(JOBS)
LOCK.release()
message = []
if args and args[0] == '-u':
user = args[1]
else:
user = 'all'
message.append("="*(15+20+25+15))
message.append("{:15s} {:20s} {:25s} {:15s}".format("Id", "Nodes", "File", "User"))
message.append("="*(15+20+25+15))
for job in jobs:
name = check_pbc_name(job.dir, job.file)
if name is None:
name = job.file
if user=='all' or user==job.user:
message.append("{:<15d} {:20s} {:<25s} {:<15s}"
.format(job.id, job.nodes, name[:25], job.user)
)
return "\n".join(message)
def do_qrm(self, args):
def get_ids(args):
ls = []
for num in args:
if "-" in num:
st, ed = num.split('-')
ls.extend(range(int(st), int(ed)+1))
else:
ls.append(int(num))
return set(ls)
global JOBS
user = args[0]
if args[1] == 'all':
ids = 'all'
else:
ids = get_ids(args[1:])
LOCK.acquire()
if ids=='all':
JOBS = [job for job in JOBS if not job.user == user]
else:
JOBS = [job for job in JOBS
if not (job.user == user and (job.id in ids))
]
time.sleep(0.5)
LOCK.release()
time.sleep(0.5)
def job_from_qsub(self, qsub):
path = pathlib.Path(qsub)
self.max_id += 1
_id = self.max_id
_dir = path.parent
_file = path.name
_time = time.time()
nodes = extract_nodes_from_qsub(path)
user = path.parts[3]
print (_id, _dir, _file, _time, nodes, user)
return Job(
_id=_id, _dir=_dir, _file=_file,
_time=_time, nodes=nodes, user=user
)
def main():
@atexit.register
def print_jobs():
print("BACKUP JOBS.")
save_jobs(JOBS)
print("BACKUP DONE.")
parser = StatParser()
usage = calculate_usage(parser.parse())
job_manipulator = JobManipulator()
scheduler = Scheduler(parser=parser)
job_manipulator.start()
scheduler.start()
job_manipulator.join()
scheduler.join()
if __name__ == "__main__":
main()