-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsuser
executable file
·515 lines (460 loc) · 16.2 KB
/
suser
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Get the SLURM cluster usage per user
author: Stephen Kelly, NYU Langone Medical Center
https://github.com/stevekm
"""
import subprocess as sp
from collections import defaultdict, namedtuple
import re
import time
import datetime
import csv
import sys
import argparse
SLURM_command = ('squeue', '-O', 'username,jobid,partition,state,minmemory,mincpus,timeused,timelimit')
# USER JOBID PARTITION STATE MIN_MEMORY MIN_CPUS TIME TIME_LIMIT
class Squeue(object):
"""
View information about jobs located in the Slurm scheduling queue.
https://slurm.schedmd.com/squeue.html
Examples
---------
sq = Squeue()
sq.get()
"""
def __init__(self, command = SLURM_command, debug = False):
self.command = command
if not debug:
self.update()
def update(self):
"""
Updates the attributes of the object
"""
returncode, entries = self.get_squeue()
self.returncode = returncode
self.entries = entries
def get_squeue(self):
"""
Get the 'squeue' HPC cluster usage information
Returns
-------
(int, list)
integer error code from the 'squeue' command
a list of dicts representing the 'squeue' values; the case of an error, returns an empty list
"""
# system command to run
process = sp.Popen(self.command,
stdout = sp.PIPE,
stderr = sp.PIPE,
shell = False,
universal_newlines = True)
# run the command, capture stdout and stderr
proc_stdout, proc_stderr = process.communicate()
# check the exit status
if process.returncode == 0:
# parse the stdout table
entries = [ entry for entry in self.parse_SLURM_table(stdout = proc_stdout) ]
else:
entries = []
return(process.returncode, entries)
def parse_SLURM_table(self, stdout):
"""
Convert the table formated output of SLURM 'sinfo -o '%all', 'squeue -o '%all', etc., commands into a list of dicts
Parameters
----------
stdout: str
the stdout of a SLURM sinfo or squeue command
Returns
-------
dict
yields a dict of entries from each valid line in the stdout
"""
# split all the stdout lines
lines = stdout.split('\n')
# get the headers from the first line
header_line = lines.pop(0)
# split the headers apart
header_cols = header_line.split()
header_cols = [ x.strip() for x in header_cols ]
# iterate over remaining lines
for line in lines:
# split each line
parts = line.split()
parts = [ x.strip() for x in parts ]
# start building dict for the values
d = {}
# make sure that the stdout line has the same number of fields as the headers
if len(parts) == len(header_cols):
# fill in the dict values and yield the results
for i, header_col in enumerate(header_cols):
d[header_col] = parts[i]
yield(d)
else:
pass # do something here
def parse_time(time_str):
"""
Parse a SLURM timestamp, return total seconds
Returns
-------
float
the number of seconds represented by the timestamp
Examples
--------
>>> parse_time('2-14:01:37')
223297.0
>>> parse_time('18-02:02:46')
1562566.0
>>> parse_time('0:00:00')
0.0
>>> parse_time("0:02")
2.0
"""
# num seconds in a day
seconds_per_day = 86400.0
num_days = 0
total_seconds = 0.0
if '-' in time_str:
parts = time_str.split('-')
num_days += int(parts[0])
time_str_split = parts[1]
x = time.strptime(time_str_split, '%H:%M:%S')
total_seconds += datetime.timedelta(hours=x.tm_hour,minutes=x.tm_min,seconds=x.tm_sec).total_seconds()
total_seconds += num_days * seconds_per_day
elif time_str.count(':') == 1:
x = time.strptime(time_str, '%M:%S')
total_seconds += datetime.timedelta(hours=x.tm_hour,minutes=x.tm_min,seconds=x.tm_sec).total_seconds()
else:
x = time.strptime(time_str, '%H:%M:%S')
total_seconds += datetime.timedelta(hours=x.tm_hour,minutes=x.tm_min,seconds=x.tm_sec).total_seconds()
return(total_seconds)
# number of bytes per size
mem_key = {
'T': 1024 * 1024 * 1024 * 1024,
'G': 1024 * 1024 * 1024,
'M': 1024 * 1024,
'K': 1024
}
# need regex to strip the letters from the mem value; "16G"
non_decimal = re.compile(r'[^\d.]+')
def parse_mem(mem_str, mem_key = mem_key, non_decimal = non_decimal):
"""
Parse SLURM memory values, return total in bytes
SLURM reports memory in values such as "16T", "16G", "16M", "16K"
Returns
-------
float
a float value representing the amount of memory in bytes
"""
# convert to a float
mem_num = float(non_decimal.sub('', mem_str))
if 'K' in mem_str:
mem_val = mem_num * mem_key['K']
elif 'M' in mem_str:
mem_val = mem_num * mem_key['M']
elif 'G' in mem_str:
mem_val = mem_num * mem_key['G']
elif 'T' in mem_str:
mem_val = mem_num * mem_key['T']
else:
# silently drop non-matching values.. ?
mem_val = 0
return(mem_val)
def create_user_dict(entries):
"""
Convert the per-job squeue output table dict into a per-user dict with job metrics aggregated
Returns
-------
dict
A dict with per-user, per-partition aggregated metrics. Memory in bytes, time in seconds.
Example:
{
"username1": {
"running": {
"mem": 309237645312.0,
"jobs": 5,
"cpus": 20,
"time": 476210.0
},
"partitions": {
"cpu_medium": {
"mem": 34359738368.0,
"jobs": 1,
"cpus": 1,
"time": 79753.0
},
"cpu_long": {
"mem": 137438953472.0,
"jobs": 1,
"cpus": 16,
"time": 60958.0
}
}
},
"username2": {
"pending": {
"mem": 8589934592.0,
"jobs": 2,
"cpus": 2,
"time": 0.0
}
}
}
"""
# dict to hold per-user entries
users = {}
# parse the squeue entries
for entry in entries:
username = entry['USER']
partition = entry['PARTITION']
state = entry['STATE']
mem = entry['MIN_MEMORY']
cpus = entry['MIN_CPUS']
time_used = entry['TIME']
time_limit = entry['TIME_LIMIT']
# parse values
time_used_val = parse_time(time_used)
mem_val = parse_mem(mem)
if state == "RUNNING":
# initialize user dict
if username not in users:
users[username] = {}
# dict for user's total running stats per partition
# initialize defaults if they are not already present
if partition not in users[username]:
users[username][partition] = {}
if 'jobs' not in users[username][partition]:
users[username][partition]['jobs'] = 0
if 'cpus' not in users[username][partition]:
users[username][partition]['cpus'] = 0
if 'mem' not in users[username][partition]:
users[username][partition]['mem'] = 0.0
if 'time' not in users[username][partition]:
users[username][partition]['time'] = 0.0
# add the new values
users[username][partition]['cpus'] += int(cpus)
users[username][partition]['mem'] += mem_val
users[username][partition]['time'] += time_used_val
users[username][partition]['jobs'] += 1
# dict for total running usage
if 'running' not in users[username]:
users[username]['running'] = {}
if 'cpus' not in users[username]['running']:
users[username]['running']['cpus'] = 0
if 'mem' not in users[username]['running']:
users[username]['running']['mem'] = 0.0
if 'time' not in users[username]['running']:
users[username]['running']['time'] = 0.0
if 'jobs' not in users[username]['running']:
users[username]['running']['jobs'] = 0
users[username]['running']['cpus'] += int(cpus)
users[username]['running']['mem'] += mem_val
users[username]['running']['time'] += time_used_val
users[username]['running']['jobs'] += 1
elif state == "PENDING":
# initialize user dict
if username not in users:
users[username] = {}
# initialize dict for pending jobs
if 'pending' not in users[username]:
users[username]['pending'] = {}
if 'jobs' not in users[username]['pending']:
users[username]['pending']['jobs'] = 0
if 'cpus' not in users[username]['pending']:
users[username]['pending']['cpus'] = 0
if 'mem' not in users[username]['pending']:
users[username]['pending']['mem'] = 0.0
if 'time' not in users[username]['pending']:
users[username]['pending']['time'] = 0.0
users[username]['pending']['cpus'] += int(cpus)
users[username]['pending']['mem'] += mem_val
users[username]['pending']['time'] += time_used_val
users[username]['pending']['jobs'] += 1
return(users)
def format_metric(value, metric, mem_key = mem_key):
"""
Format a value based on predefined methods for each metric
Parameters
----------
value: str/int/float
value to be formatted
metric: str
one of "cpus", "mem", "time", "jobs"
Returns
-------
str
a formatted string
"""
if metric == "cpus":
return("{0}".format(value))
elif metric == "mem":
return("{0}G".format(value / mem_key['G']))
elif metric == "jobs":
return("{0}".format(value))
elif metric == "time":
return("{0:.1f}hr".format(value / 3600))
else:
return('')
#
# def reformat_fieldname(fieldname):
# return("{0:>7.6s}".format(fieldname))
#
# def reformat_username(username):
# return("{0:>9.8}".format(username))
#
# def print_table(users, metric = "cpus"):
# """
# Prints the final table of user metrics
# """
# # get the fieldnames and partitions to print
# partitions, fieldnames = get_partition_fieldnames(users)
#
# # make a reformated list of the dicts for printing
# dicts_to_print = []
# for user, values in users.items():
# d = {}
# d[reformat_fieldname('user')] = reformat_username(user)
#
# for partition in partitions:
# if 'partitions' in values:
# if partition in values['partitions']:
# d[reformat_fieldname(partition)] = format_metric(
# value = values['partitions'][partition][metric],
# metric = metric)
# else:
# d[reformat_fieldname(partition)] = ''
#
# if 'pending' in values:
# d[reformat_fieldname('pending')] = format_metric(
# value = values['pending'][metric],
# metric = metric)
# else:
# d[reformat_fieldname('pending')] = ''
#
# if 'total' in values:
# d[reformat_fieldname('total')] = format_metric(
# value = values['total'][metric],
# metric = metric)
# else:
# d[reformat_fieldname('total')] = ''
# dicts_to_print.append(d)
#
# dicts_to_print = sorted(dicts_to_print, key=lambda k: k[reformat_fieldname('total')], reverse=True)
#
# # print to console
# writer = csv.DictWriter(sys.stdout,
# delimiter = '\t',
# fieldnames = [reformat_fieldname(fieldname) for fieldname in fieldnames])
# writer.writeheader()
# for d in dicts_to_print:
# writer.writerow(d)
def create_totals_tups(users, metric):
"""
flatten dict into list of tuples so we can sort
"""
totals_tups = []
for username, values in users.items():
for partition, totals in values.items():
t = (partition, username, totals[metric])
totals_tups.append(t)
totals_tups = sorted(totals_tups, key = lambda tup: tup[2], reverse = True)
return(totals_tups)
def print_table(users, totals, metric):
"""
https://docs.python.org/3.6/library/string.html#format-specification-mini-language
"""
# get sorted headers
headers = list(set([ t[0] for t in totals ]))
if 'running' in totals:
headers.remove('running')
if 'pending' in totals:
headers.remove('pending')
headers = sorted(headers)
if 'running' in totals:
headers.append('running')
if 'pending' in totals:
headers.append('pending')
num_headers = len(headers)
# get list of users to print; preserve order
ordered_users = []
for t in totals:
if t[1] not in ordered_users:
ordered_users.append(t[1])
print(ordered_users)
# :(align right)<size of space to pad>.<lenght of original str>
header_line = " ".join([ "{0:>9.7}".format(h)for h in headers ])
# pad 12 spaces for later usernames
header_line = "{0:>12}".format(" ") + header_line
print(header_line)
div_line = "-" * len(header_line)
print(div_line)
for user in ordered_users:
if user in users:
# start building line to print for each user
userline = "{0:>12} ".format(user)
# start making a list of the values to be printed
userline_vals = []
for header in headers:
if header in users[user]:
header_index = headers.index(header)
header_val = users[user][header]
userline_vals.append((header_index, header_val))
# TODO: finish this...
def print_totals(users, totals, metric):
"""
Prints the list of user totals per-partition to the console
"""
divider = '---------'
# get sorted headers
headers = list(set([ t[0] for t in totals ]))
if 'running' in totals:
headers.remove('running')
if 'pending' in totals:
headers.remove('pending')
headers = sorted(headers)
if 'running' in totals:
headers.append('running')
if 'pending' in totals:
headers.append('pending')
# rearrange the values per-partition
d = defaultdict(list)
for partition, username, total_val in totals:
d[partition].append((username, total_val))
# iterate over the headers list to ensure they are printed in the right order
for header in headers:
print("{0} ({1})".format(header, metric))
print(divider)
for username, total_val in d[header]:
# {0:10.10} : <pad to length 10>.<cut off str to length 10>
print("{0:10.10}: {1}".format(username, format_metric(value = total_val, metric = metric)))
print('\n')
def main(**kwargs):
"""
Main control function for the script
"""
metric = kwargs.pop('metric', "cpus")
# get the SLURM squeue
queue = Squeue()
# get dict of parsed values per user
users = create_user_dict(entries = queue.entries)
# get totals per user
# totals = create_totals_dict(users, metric)
totals = create_totals_tups(users, metric)
# print the parsed table
# print_table(users, totals, metric) # TODO: finish this eventually
print_totals(users, totals, metric)
def parse():
"""
Parses script args
"""
parser = argparse.ArgumentParser(description='Calculates per-user SLURM usage metrics')
parser.add_argument('metric',
nargs="?",
default = "jobs",
choices=["cpus", "mem", "time", "jobs"],
help='metric to report')
args = parser.parse_args()
main(**vars(args))
if __name__ == '__main__':
parse()