Skip to content

Instantly share code, notes, and snippets.

@ricsonc
Last active February 8, 2018 20:11
Show Gist options
  • Save ricsonc/e8713a987b8c8c88beddd3e527599e17 to your computer and use it in GitHub Desktop.
Save ricsonc/e8713a987b8c8c88beddd3e527599e17 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import subprocess
from collections import defaultdict
def split_jobs(stuff):
jobs = []
while stuff:
job = []
while stuff:
line = stuff.pop()
if not line:
if job:
jobs.append(job[::-1])
job = []
break
else:
job.append(line)
jobs.append(job)
return jobs
def get_between(s, start, end):
s = s[s.find(start)+len(start):]
if not end:
return s
return s[:s.find(end)]
def get_gpus(tres_string):
stuff = tres_string.split(',')
for resource in stuff:
if 'gpu' in resource:
return int(resource.split('=')[-1])
return 0
def linebykey(lines, key):
return [line for line in lines if key in line][0]
def parse_job(job):
def foo(start, end):
return get_between(linebykey(job, start), start, end)
name = foo('UserId=', '(')
status = foo('JobState=', ' ')
usage = foo('TRES=', None)
gpus = get_gpus(usage)
return name, status, gpus
def combine(parsed_jobs):
users = defaultdict(int)
for (username, status, numgpus) in parsed_jobs:
if status == 'RUNNING':
users[username] += numgpus
return users
def printdict(dct):
maxkeylen = max(map(len, dct.keys()))
for k, v in dct.items():
k_ = k+' '*(maxkeylen-len(k))
print '%s : %d' % (k_,v)
if __name__ == '__main__':
printdict(combine(map(parse_job, split_jobs(subprocess.check_output(['scontrol', 'show', 'jobs']).split('\n')))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment