-
Notifications
You must be signed in to change notification settings - Fork 2
/
qtotal.py
executable file
·74 lines (59 loc) · 1.71 KB
/
qtotal.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Get the total resource usage from SLURM
similar to:
squeue -u $USER -o '%i|%T|%P|%m'
squeue -u $USER -O 'jobid,state,minmemory,mincpus'
Usage:
$ ./qtotal.py
Total usage: 82 CPUs, 1064.0GB memory
"""
import util.slurm as slurm
import os
import re
username = os.environ.get('USER')
args = ['squeue', '-u', username, '-O', 'jobid,state,minmemory,mincpus']
queue = slurm.Squeue(command = args)
running = {}
running['entries'] = []
running['cpus'] = 0
running['mem'] = 0.0
pending = {}
pending['entries'] = []
pending['cpus'] = 0
pending['mem'] = 0.0
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
non_decimal = re.compile(r'[^\d.]+')
for entry in queue.entries:
cpus = entry['MIN_CPUS']
mem = entry['MIN_MEMORY']
# convert to a float
mem_num = float(non_decimal.sub('', mem))
# parse the memory; 16T, "16G", "16M", "16K"
if 'K' in mem:
mem_val = mem_num * mem_key['K']
elif 'M' in mem:
mem_val = mem_num * mem_key['M']
elif 'G' in mem:
mem_val = mem_num * mem_key['G']
elif 'T' in mem:
mem_val = mem_num * mem_key['T']
else:
# silently drop non-matching values.. ?
mem_val = 0
if entry['STATE'] == 'PENDING':
pending['entries'].append(entry)
pending['cpus'] += int(cpus)
pending['mem'] += mem_val
elif entry['STATE'] == 'RUNNING':
running['entries'].append(entry)
running['cpus'] += int(cpus)
running['mem'] += mem_val
print("Total usage: {cpus} CPUs, {mem}GB memory".format(cpus = running['cpus'], mem = running['mem'] / mem_key['G']))