-
Notifications
You must be signed in to change notification settings - Fork 6
/
slurm2sql.py
1204 lines (1043 loc) · 45.7 KB
/
slurm2sql.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
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
# pylint: disable=too-few-public-methods, missing-docstring
"""Import Slurm accounting database from sacct to sqlite3 database
"""
from __future__ import division, print_function
import argparse
import datetime
import json
import logging
import os
import re
import sqlite3
import subprocess
import sys
import time
__version__ = '0.9.2'
LOG = logging.getLogger('slurm2sql')
LOG.setLevel(logging.DEBUG)
if sys.version_info[0] >= 3:
logging.lastResort.setLevel(logging.INFO)
else:
ch = logging.lastResort = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
LOG.addHandler(ch)
#
# First, many converter functions/classes which convert strings to
# useful values.
#
# Single converter functions: transform one column to sqlite value
# stored as that same column.
def settype(type):
"""Decorator: Set type of function, for sql column definitions."""
def _(x):
x.type = type
return x
return _
@settype('int')
def int_(x):
"""int"""
return (int(x))
@settype('int')
def nullint(x):
"""int or None"""
return int(x) if x else None
@settype('text')
def nullstr(x):
"""str or None"""
return str(x) if x else None
@settype('text')
def nullstr_strip(x):
"""str or None"""
return str(x).strip() if x else None
@settype('int')
def unixtime(x):
"""Timestamp in local time, converted to unixtime"""
if not x: return None
if x == 'Unknown': return None
if x == 'None': return None
return time.mktime(time.strptime(x, '%Y-%m-%dT%H:%M:%S'))
@settype('int')
def datetime_timestamp(dt):
"""Convert a datetime object to unixtime
- Only needed because we support python 2"""
if hasattr(dt, 'timestamp'): # python3
return dt.timestamp()
return time.mktime(dt.timetuple())
@settype('real')
def slurmtime(x):
"""Parse slurm time of format [dd-[hh:]]mm:ss"""
if not x: return None
# Handle 'UNLIMITED' ,'Partition_Limit' in 'timelimit' field
if x in {'Partition_Limit', 'UNLIMITED'}:
return None
seconds = 0
# The anchor is different if there is '-' or not. With '-' it is [dd]-hh[:mm[:ss]]. Without it is mm[:ss] first, then hh:mm:ss
if '-' in x:
days, time_ = x.split('-', 1)
seconds += int(days) * 24 * 3600
hms = time_.split(':')
if len(hms) >= 1: seconds += 3600 * int(hms[0]) # hour
if len(hms) >= 2: seconds += 60 * float(hms[1]) # min
if len(hms) >= 3: seconds += int(hms[2]) # sec
else:
time_ = x
hms = time_.split(':')
# If only a single number is given, it is interperted as minutes
if len(hms) >= 3: seconds += 3600 * int(hms[-3]) # hour
if len(hms) >= 2: seconds += float(hms[-1]) # sec
if len(hms) >= 1: seconds += 60 * int(hms[-2] if len(hms)>=2 else hms[-1]) # min
return seconds
@settype('text')
def slurm_timestamp(x):
"""Convert a datetime to the Slurm format of timestamp
"""
if not isinstance(x, datetime.datetime):
x = datetime.datetime.fromtimestamp(x - 5)
return x.strftime('%Y-%m-%dT%H:%M:%S')
@settype('text')
def str_unknown(x):
if x == 'Unknown': return None
return x
@settype('real')
def slurmmem(x):
"""Memory, removing 'n' or 'c' at end, in KB"""
if not x: return None
x = x.strip('nc')
return float_bytes(x)
# Converting kibi/mibi, etc units to numbers
def unit_value_binary(unit):
"""Convert a unit to its value, e.g. 'K'-->1024, 'M'-->1048576"""
if unit is None: unit = '_'
return 2**(10*'_kmgtpezy'.index(unit.lower()))
def unit_value_metric(unit):
"""Convert a unit to its value, e.g. 'K'-->1000, 'M'-->1000000"""
if unit is None: unit = '_'
return 1000**('_kmgtpezy'.index(unit.lower()))
@settype('real')
def float_bytes(x, convert=float):
"""Convert a float with unit (K,M, etc) to value"""
if not x: return None
unit = x[-1].lower()
if unit in 'kmgtpezy':
return convert(x[:-1]) * unit_value_binary(unit)
return convert(x)
@settype('int')
def int_bytes(x):
return float_bytes(x, convert=lambda x: int(float(x)))
@settype('real')
def float_metric(x, convert=float):
"""Convert a float with unit (K,M, etc) to value"""
if not x: return None
unit = x[-1].lower()
if unit in 'kmgtpezy':
return convert(x[:-1]) * unit_value_metric(unit)
return convert(x)
@settype('int')
def int_metric(x):
return float_metric(x, convert=lambda x: int(float(x)))
# Row converter fuctions which need *all* values to convert. Classes
# with one method, 'calc', which takes the whole row (a dict) and
# converts it to a sqlite value. The only point of the class is to be
# able to easily distinguish the function from the column functions
# above.
class linefunc(object):
"""Base class for all converter functions"""
linefunc = True
type = ''
# Generic extractor-generator function.
def ExtractField(name, columnname, fieldname, type_, wrap=None):
"""Extract a field out of a column, in the TRES/GRSE column formats.
Example: 'gres/gpuutil'
"""
_re = re.compile(rf'\b{fieldname}=([^,]*)\b')
@staticmethod
def calc(row):
if columnname not in row: return None
# Slurm 20.11 uses gres= within ReqTRES (instead of ReqGRES)
#print(row[columnname])
m = _re.search(row[columnname])
if m:
val = type_(m.group(1))
if wrap:
val = wrap(val)
return val
return type(name, (linefunc,), {'calc': calc, 'type': type_.type})
# The main converter functions
# Submit, start, and end times as unixtimes
class slurmDefaultTime(linefunc):
@staticmethod
def calc(row):
"""Latest active time.
All jobs in sacct are already started, so this is either current
time or end time.
"""
if row['End'] != 'Unknown':
return row['End']
if row['Start'] != 'Unknown':
# Currently running, return current time since it's constantly updated.
return time.strftime("%Y-%m-%dT%H:%M:%S")
# Return submit time, since there is nothing else.
return row['Submit']
class slurmDefaultTimeTS(linefunc):
type = 'int'
@staticmethod
def calc(row):
"""Lastest active time (see above), unixtime."""
return unixtime(slurmDefaultTime.calc(row))
class slurmSubmitTS(linefunc):
type = 'int'
@staticmethod
def calc(row):
return unixtime(row['Submit'])
class slurmStartTS(linefunc):
type = 'int'
@staticmethod
def calc(row):
return unixtime(row['Start'])
class slurmEndTS(linefunc):
type = 'int'
@staticmethod
def calc(row):
return unixtime(row['End'])
class slurmQueueTime(linefunc):
type = 'int'
@staticmethod
def calc(row):
submit = unixtime(row['Submit'])
start = unixtime(row['Start'])
if submit is not None and start is not None:
return start - submit
billing_re = re.compile(r'billing=(\d+)')
class slurmBilling(linefunc):
type = 'int'
@staticmethod
def calc(row):
tres = row['AllocTRES']
if not tres: return None
m = billing_re.search(tres)
if m:
return int(m.group(1))
# Memory stuff
class slurmMemNode(linefunc):
"""Memory per node. ReqMem is total across all nodes"""
type = 'real'
@staticmethod
def calc(row):
reqmem = row['ReqMem']
if not reqmem: return None
ncpus = int(row['NCPUS'])
if ncpus == 0: return 0
nnodes = int(row['NNodes'])
if nnodes == 0: return None
return slurmmem(reqmem) / nnodes
class slurmMemCPU(linefunc):
"""Memory per cpu, computed if necessary"""
type = 'real'
@staticmethod
def calc(row):
reqmem = row['ReqMem']
if not reqmem: return None
nnodes = int(row['NNodes'])
if nnodes == 0: return None
ncpus = int(row['NCPUS'])
if ncpus == 0: return None
return slurmmem(reqmem) / ncpus
class slurmMemType(linefunc):
"""Memory type: 'n' per node, 'c' per core"""
type = 'real'
@staticmethod
def calc(row):
reqmem = row['ReqMem']
if not reqmem: return None
if reqmem.endswith('n'): return 'n'
if reqmem.endswith('c'): return 'c'
return None # latest slurm seems to not have this, ~2021-2022
class slurmMemRaw(linefunc):
"""Raw value of ReqMem column, with 'c' or 'n' suffix"""
@staticmethod
def calc(row):
return row['ReqMem']
# GPU stuff
gpu_re = re.compile(r'gpu[:=](\d+)')
class slurmReqGPU(linefunc):
type = 'int'
@staticmethod
def calc(row):
if 'ReqGRES' in row:
gres = row['ReqGRES']
else:
gres = row['ReqTRES']
if not gres: return None
# Slurm 20.11 uses gres= within ReqTRES (instead of ReqGRES)
m = gpu_re.search(gres)
if m:
return int(m.group(1))
class slurmGPUMem(linefunc):
type = 'real'
@staticmethod
def calc(row):
comment = row['Comment']
if not comment.strip(): return
if 'No GPU stats' in comment: return
if comment == 'abort': return
try:
comment = json.loads(comment)
except:
return None
if not isinstance(comment, dict) or 'gpu_mem_max' not in comment:
return
return comment.get('gpu_mem_max') * (2**20)
class slurmGPUEff(linefunc):
type = 'real'
@staticmethod
def calc(row):
comment = row['Comment']
if not comment.strip(): return
if 'No GPU stats' in comment: return
if comment == 'abort': return
try:
comment = json.loads(comment)
except:
return None
if not isinstance(comment, dict) or 'gpu_util' not in comment:
return
return comment['gpu_util']/100.
class slurmGPUCountComment(linefunc):
type = 'int'
@staticmethod
def calc(row):
comment = row['Comment']
if not comment.strip(): return
if 'No GPU stats' in comment: return
if comment == 'abort': return
try:
comment = json.loads(comment)
except:
return None
if not isinstance(comment, dict) or 'ngpu' not in comment:
return
return int(comment.get('ngpu'))
gpu_re2 = re.compile(r'gpu=(\d+)')
class slurmGPUCount(linefunc):
type = 'int'
@staticmethod
def calc(row):
tres = row['AllocTRES'] or row['ReqTRES']
if not tres: return None
m = gpu_re2.search(tres)
if m:
return int(m.group(1))
# Job ID related stuff
jobidonly_re = re.compile(r'[0-9]+')
jobidnostep_re = re.compile(r'[0-9]+(_[0-9]+)?')
class slurmJobID(linefunc):
"""The JobID field as slurm gives it, including _ and ."""
type = 'text'
@staticmethod
def calc(row):
if 'JobID' not in row: return
return row['JobID']
class slurmJobIDonly(linefunc):
"""The JobID without any . or _. This is the same for all array tasks/het offsets"""
type = 'int'
@staticmethod
def calc(row):
if 'JobID' not in row: return
return int(jobidonly_re.match(row['JobID']).group(0))
class slurmJobIDnostep(linefunc):
"""The JobID without any `.` suffixes. This is the same for all het offsets"""
type = 'text'
@staticmethod
def calc(row):
if 'JobID' not in row: return
return jobidnostep_re.match(row['JobID']).group(0)
class slurmJobIDrawonly(linefunc):
"""The (raw) JobID without any . or _. This is different for every job in an array."""
type = 'int'
@staticmethod
def calc(row):
if 'JobIDRaw' not in row: return
return int(jobidonly_re.match(row['JobIDRaw']).group(0))
arraytaskid_re = re.compile(r'_([0-9]+)')
class slurmArrayTaskID(linefunc):
"""Array task ID, the part after _."""
type = 'int'
@staticmethod
def calc(row):
if 'JobID' not in row: return
if '_' not in row['JobID']: return
if '[' in row['JobID']: return
return int(arraytaskid_re.search(row['JobID']).group(1))
class slurmJobStep(linefunc):
type = 'text'
@staticmethod
def calc(row):
if 'JobID' not in row: return
if '.' not in row['JobID']: return
return row['JobID'].split('.')[-1] # not necessarily an integer
# Efficiency stuff
class slurmMemEff(linefunc):
"""Slurm memory efficiency.
In modern Slurm, this does *not* work because the allocation rows
have ReqMem, and the task rows have MaxRSS, but slurm2sql handled
things only one row at a time. The `eff` view computes this per
job.
"""
# https://github.com/SchedMD/slurm/blob/master/contribs/seff/seff
type = 'real'
@staticmethod
def calc(row):
reqmem_type = slurmMemType.calc(row)
mem_max = slurmmem(row['MaxRSS'])
reqmem = slurmmem(row['ReqMem'])
nnodes = slurmmem(row['NNodes'])
if not reqmem or mem_max is None: return
if reqmem_type == 'c':
nodemem = reqmem * int(row['NCPUS'])
elif reqmem_type == 'n':
nodemem = reqmem
elif reqmem_type is None:
nodemem = reqmem / nnodes
else:
raise ValueError('unknown memory type: %s'%reqmem_type)
return mem_max / nodemem
class slurmCPUEff(linefunc):
# This matches the seff tool currently:
# https://github.com/SchedMD/slurm/blob/master/contribs/seff/seff
type = 'real'
@staticmethod
def calc(row):
if not ('Elapsed' in row and 'TotalCPU' in row and 'NCPUS' in row):
return
walltime = slurmtime(row['Elapsed'])
if not walltime: return None
try:
cpueff = slurmtime(row['TotalCPU']) / (walltime * int(row['NCPUS']))
except ZeroDivisionError:
return float('nan')
return cpueff
class slurmConsumedEnergy(linefunc):
type = 'int'
@staticmethod
def calc(row):
if not row['ConsumedEnergyRaw']: return None
return int(row['ConsumedEnergyRaw'])
class slurmExitCodeRaw(linefunc):
type = 'text'
@staticmethod
def calc(row):
if not row['ExitCode']: return None
return row['ExitCode']
class slurmExitCode(linefunc):
type = 'int'
@staticmethod
def calc(row):
if not row['ExitCode']: return None
return int(row['ExitCode'].split(':')[0])
class slurmExitSignal(linefunc):
type = 'int'
@staticmethod
def calc(row):
if not row['ExitCode']: return None
return int(row['ExitCode'].split(':')[1])
# All defined columns and their respective converter functions. If a
# key begins in a underscore, this is not a Slurm DB field (don't
# query it from sacct), it is computed from other fields in this
# program. It gets added to our sqlite database without the
# underscore.
COLUMNS = {
# Basic job metadata
# Job IDs are of the forms (from sacct man page):
# - JobID.JobStep
# - ArrayJobID_ArrayTaskID.JobStep
# And the below is consistent with this.
'_JobID': slurmJobID, # JobID directly as Slurm presents it
# (with '_' and '.')
'_JobIDnostep': slurmJobIDnostep, # Integer JobID without '.' suffixes
'_JobIDonly': slurmJobIDonly, # Integer JobID without '_' or '.' suffixes
'_JobStep': slurmJobStep, # Part after '.'
'_ArrayTaskID': slurmArrayTaskID, # Part between '_' and '.'
'_JobIDRawonly': slurmJobIDrawonly,
# if array jobs, unique ID for each array task,
# otherwise JobID
#'JobIDRawSlurm': str, #
'JobName': nullstr, # Free-form text name of the job
'User': nullstr, # Username
'Group': nullstr, # Group
'Account': nullstr, # Account
'SubmitLine': nullstr, # SubmitLine (execution command line)
'_Billing': slurmBilling, # Billing (from tres)
# Times and runtime info
'State': nullstr, # Job state
'Timelimit': slurmtime, # Timelimit specified by user
'Elapsed': slurmtime, # Walltime of the job
#'_Time': slurmDefaultTime, # Genalized time, max(Submit, End, (current if started))
#'Submit': str_unknown, # Submit time in yyyy-mm-ddThh:mm:ss straight from slurm
#'Start': str_unknown, # Same, job start time
#'End': str_unknown, # Same, job end time
'_Time': slurmDefaultTimeTS, # unixtime: Genalized time, max(Submit, End, (current if started))
'Submit': slurmSubmitTS, # unixtime: Submit
'Start': slurmStartTS, # unixtime: Start
'End': slurmEndTS, # unixtime: End
'_QueueTime': slurmQueueTime, # seconds, difference between submission and start
'Partition': nullstr, # Partition
'_ExitCodeRaw': slurmExitCodeRaw, # ExitStatus:Signal
'ExitCode': slurmExitCode, # ExitStatus from above, int
'_ExitSignal': slurmExitSignal, # Signal from above, int
'NodeList': nullstr, # Node list of jobs
'Priority': nullint, # Slurm priority (higher = will run sooner)
'_ConsumedEnergy': slurmConsumedEnergy,
# Stuff about number of nodes
'ReqNodes': int_bytes, # Requested number of nodes
'NNodes': nullint, # Number of nodes (allocated if ran, requested if not yet)
'AllocNodes': nullint, # Number of nodes (allocated, zero if not running yet)
# Miscelaneous requested resources
'ReqTRES': nullstr,
'NTasks': nullint,
#'AllocGRES'
'AllocTRES': nullstr,
'TRESUsageInTot': nullstr,
'TRESUsageOutTot': nullstr,
# CPU related
'NCPUS': nullint, # === AllocCPUS
'ReqCPUS': nullint, # Requested CPUs
'AllocCPUS': nullint, # === NCPUS
'CPUTime': slurmtime, # = Elapsed * NCPUS (= CPUTimeRaw) (not how much used)
'TotalCPU': slurmtime, # = Elapsed * NCPUS * efficiency
'UserCPU': slurmtime, #
'SystemCPU': slurmtime, #
'_CPUEff': slurmCPUEff, # CPU efficiency, should be same as seff
'MinCPU': slurmtime, # Minimum CPU used by any task in the job
'MinCPUNode': nullstr,
'MinCPUTask': nullstr,
# Memory related
'ReqMem': float_bytes, # Requested mem, value from slurm. Sum across all nodes
'_ReqMemNode': slurmMemNode, # Mem per node, computed
'_ReqMemCPU': slurmMemCPU, # Mem per cpu, computed
'AveRSS': slurmmem,
'MaxRSS': slurmmem,
'MaxRSSNode': nullstr,
'MaxRSSTask': nullstr,
'MaxPages': int_metric,
'MaxVMSize': slurmmem,
#'_MemEff': slurmMemEff, # Slurm memory efficiency - see above for why this doesn't work
# Disk related
'AveDiskRead': int_bytes,
'AveDiskWrite': int_bytes,
'MaxDiskRead': int_bytes,
'MaxDiskWrite': int_bytes,
'_TotDiskRead': ExtractField('TotDiskRead', 'TRESUsageInTot', 'fs/disk', float_bytes),
'_TotDiskWrite': ExtractField('TotDiskWrite', 'TRESUsageOutTot', 'fs/disk', float_bytes),
# GPU related
#'_ReqGPUS': slurmReqGPU, # Number of GPUS requested
'_ReqGPUS': ExtractField('ReqGpus', 'ReqTRES', 'gres/gpu', float_metric),
'Comment': nullstr_strip, # Slurm Comment field (at Aalto used for GPU stats)
#'_GPUMem': slurmGPUMem, # GPU mem extracted from comment field
#'_GPUEff': slurmGPUEff, # GPU utilization (0.0 to 1.0) extracted from comment field
#'_NGPU': slurmGPUCount, # Number of GPUs, extracted from comment field
'_NGpus': ExtractField('NGpus', 'AllocTRES', 'gres/gpu', float_metric),
'_GpuUtil': ExtractField('GpuUtil', 'TRESUsageInAve', 'gres/gpuutil', float_metric, wrap=lambda x: x/100.),
'_GpuMem': ExtractField('GpuMem2', 'TRESUsageInAve', 'gres/gpumem', float_metric),
'_GpuUtilTot': ExtractField('GpuUtilTot', 'TRESUsageInTot', 'gres/gpuutil', float_metric),
'_GpuMemTot': ExtractField('GpuMemTot', 'TRESUsageInTot', 'gres/gpumem', float_metric),
}
# Everything above that does not begin with '_' is queried from sacct.
# These extra columns are added (don't duplicate with the above!)
COLUMNS_EXTRA = ['JobID',
'JobIDRaw',
'ConsumedEnergyRaw',
'TRESUsageInAve',
'TRESUsageOutTot',
]
def main(argv=sys.argv[1:], db=None, raw_sacct=None, csv_input=None):
"""Parse arguments and use the other API"""
parser = argparse.ArgumentParser(usage='slurm2sql DBNAME [other args] [SACCT_FILTER]')
parser.add_argument('db', help="Database filename to create or update")
parser.add_argument('--update', '-u', action='store_true',
help="If given, don't delete existing database and "
"instead insert or update rows")
parser.add_argument('--history',
help="Scrape dd-hh:mm:ss or [hh:]mm:ss from the past to now (Slurm time format)")
parser.add_argument('--history-resume', action='store_true',
help="Day-by-day collect history, starting from last collection.")
parser.add_argument('--history-days', type=int,
help="Day-by-day collect history, starting this many days ago.")
parser.add_argument('--history-start',
help="Day-by-day collect history, starting on this day.")
parser.add_argument('--history-end',
help="Day-by-day collect history ends on this day. Must include one "
"of the other history options to have any effect.")
parser.add_argument('--jobs-only', action='store_true',
help="Don't include job steps but only the man jobs")
parser.add_argument('--csv-input',
help="Don't parse sacct but import this CSV file. It's read with "
"Python's default csv reader (excel format). Beware badly "
"formatted inputs, for example line breaks in job names.")
parser.add_argument('--completed', '-c', action='store_true',
help=f"Select for completed job states ({COMPLETED_STATES}) You need to specify --starttime (-S) at some point in the past, due to how saccont default works (for example '-S now-1week'). This option automatically sets '-E now'")
parser.add_argument('--quiet', '-q', action='store_true',
help="Don't output anything unless errors")
parser.add_argument('--verbose', '-v', action='store_true',
help="Output more logging info")
args, sacct_filter = parser.parse_known_args(argv)
if args.verbose:
logging.lastResort.setLevel(logging.DEBUG)
if args.quiet:
logging.lastResort.setLevel(logging.WARN)
LOG.debug(args)
sacct_filter = process_sacct_filter(args, sacct_filter)
# db is only given as an argument in tests (normally)
if db is None:
# Delete existing database unless --update/-u is given
if not (args.update or args.history_resume) and os.path.exists(args.db):
os.unlink(args.db)
db = sqlite3.connect(args.db)
# If --history-days, get just this many days history
if (args.history is not None
or args.history_resume
or args.history_days is not None
or args.history_start is not None):
errors = get_history(db, sacct_filter=sacct_filter,
history=args.history,
history_resume=args.history_resume,
history_days=args.history_days,
history_start=args.history_start,
history_end=args.history_end,
jobs_only=args.jobs_only,
raw_sacct=raw_sacct,
# --history real usage doesn't make sense with csv
# (below is just for running tests)
csv_input=csv_input)
create_indexes(db)
# Normal operation
else:
errors = slurm2sql(db, sacct_filter=sacct_filter,
update=args.update,
jobs_only=args.jobs_only,
raw_sacct=raw_sacct,
verbose=args.verbose,
csv_input=args.csv_input or csv_input)
create_indexes(db)
if errors:
LOG.warning("Completed with %s errors", errors)
return(1)
return(0)
def get_history(db, sacct_filter=['-a'],
history=None, history_resume=None, history_days=None,
history_start=None, history_end=None,
jobs_only=False, raw_sacct=None, csv_input=None):
"""Get history for a certain period of days.
Queries each day and updates the database, so as to avoid
overloading sacct and causing a failure.
Returns: the number of errors.
"""
errors = 0
now = datetime.datetime.now().replace(microsecond=0)
today = datetime.date.today()
if history_resume:
try:
start = get_last_timestamp(db)
except sqlite3.OperationalError:
import traceback
traceback.print_exc()
print()
print("Error fetching last start time (see above)", file=sys.stderr)
exit(5)
start = datetime.datetime.fromtimestamp(start - 5)
elif history is not None:
start = now - datetime.timedelta(seconds=slurmtime(history))
elif history_days is not None:
start = datetime.datetime.combine(today - datetime.timedelta(days=history_days), datetime.time())
elif history_start is not None:
start = datetime.datetime.strptime(history_start, '%Y-%m-%d')
if history_end is not None:
stop = datetime.datetime.strptime(history_end, '%Y-%m-%d')
else:
stop = now + datetime.timedelta(seconds=6*3600)
days_ago = (now - start).days
day_interval = 1
while start <= stop:
end = start+datetime.timedelta(days=day_interval)
end = end.replace(hour=0, minute=0, second=0, microsecond=0)
new_filter = sacct_filter + [
'-S', slurm_timestamp(start),
'-E', slurm_timestamp(end),
]
LOG.debug(new_filter)
LOG.info("%s %s", days_ago, start.date() if history_days is not None else start)
errors += slurm2sql(db, sacct_filter=new_filter, update=True, jobs_only=jobs_only,
raw_sacct=raw_sacct, csv_input=csv_input)
db.commit()
update_last_timestamp(db, update_time=end)
start = end
days_ago -= day_interval
return errors
def sacct(slurm_cols, sacct_filter):
cmd = ['sacct', '-o', ','.join(slurm_cols), '-P',# '--units=K',
'--delimiter=;|;',
#'--allocations', # no job steps, only total jobs, but doesn't show used resources.
] + list(sacct_filter)
#LOG.debug(' '.join(cmd))
error_handling = {'errors':'replace'} if sys.version_info[0]>=3 else {}
p = subprocess.Popen(cmd,
stdout=subprocess.PIPE, universal_newlines=True,
**error_handling)
return p.stdout
def create_indexes(db):
db.execute('CREATE INDEX IF NOT EXISTS idx_slurm_jobidnostep ON slurm (JobIDnostep)')
db.execute('CREATE INDEX IF NOT EXISTS idx_slurm_start ON slurm (Start)')
db.execute('CREATE INDEX IF NOT EXISTS idx_slurm_user_start ON slurm (User, Start)')
db.execute('CREATE INDEX IF NOT EXISTS idx_slurm_time ON slurm (Time)')
db.execute('CREATE INDEX IF NOT EXISTS idx_slurm_user_time ON slurm (User, Time)')
db.execute('ANALYZE;')
db.commit()
def sacct_iter(slurm_cols, sacct_filter, errors=[0], raw_sacct=None):
"""Iterate through sacct, returning rows as dicts"""
# Read data from sacct, or interpert sacct_filter directly as
# testdata if it has the attribute 'testdata'
if raw_sacct:
# Support tests - raw lines can be put in
lines = raw_sacct
else:
# This is a real filter, read data
lines = sacct(slurm_cols, sacct_filter)
# We don't use the csv module because the csv can be malformed.
# In particular, job name can include newlines(!). TODO: handle job
# names with newlines.
line_continuation = None
for i, rawline in enumerate(lines):
if i == 0:
# header
header = rawline.strip().split(';|;')
continue
# Handle fields that have embedded newline (JobName). If we
# have too few fields, save the line and continue.
if line_continuation:
rawline = line_continuation + rawline
line_continuation = None
line = rawline.strip().split(';|;')
if len(line) < len(slurm_cols):
line_continuation = rawline
continue
# (end)
if len(line) > len(slurm_cols):
LOG.error("Line with wrong number of columns: (want columns=%s, line has=%s)", len(slurm_cols), len(line))
LOG.error("columns = %s", header)
LOG.error("rawline = %s", rawline)
errors[0] += 1
continue
line = dict(zip(header, line))
yield line
def slurm2sql(db, sacct_filter=['-a'], update=False, jobs_only=False,
raw_sacct=None, verbose=False,
csv_input=None):
"""Import one call of sacct to a sqlite database.
db:
open sqlite3 database file object.
sacct_filter:
filter for sacct, list of arguments. This should only be row
filters, such as ['-a'], ['-S' '2019-08-01'], and so on. The
argument should be a list. You can't currently filter what columns
are selected.
raw_sacct: If given, do not run sacct but use this as the input
(file-like object)
Returns: the number of errors
"""
columns = COLUMNS.copy()
def infer_type(cd):
if hasattr(cd, 'type'): return cd.type
elif cd == str: return 'text'
return ''
create_columns = ', '.join('"%s" %s'%(c.strip('_'), infer_type(cd))
for c, cd in columns.items())
create_columns = create_columns.replace('JobID" text', 'JobID" text UNIQUE')
db.execute('CREATE TABLE IF NOT EXISTS slurm (%s)'%create_columns)
db.execute('CREATE TABLE IF NOT EXISTS meta_slurm_lastupdate (id INTEGER PRIMARY KEY, update_time REAL)')
db.execute('CREATE VIEW IF NOT EXISTS allocations AS select * from slurm where JobStep is null;')
db.execute('CREATE VIEW IF NOT EXISTS eff AS select '
'JobIDnostep AS JobID, '
'max(User) AS User, '
'max(Partition), '
'Account, '
'State, '
'Time, '
'TimeLimit, '
'min(Start) AS Start, '
'max(End) AS End, '
'max(NNodes) AS NNodes, '
'ReqTRES, '
'max(Elapsed) AS Elapsed, '
'max(NCPUS) AS NCPUS, '
'max(totalcpu)/max(cputime) AS CPUeff, ' # highest TotalCPU is for the whole allocation
'max(cputime) AS cpu_s_reserved, '
'max(totalcpu) AS cpu_s_used, '
'max(ReqMemNode) AS MemReq, '
'max(ReqMemNode*Elapsed) AS mem_s_reserved, ' # highest of any job
'max(MaxRSS) AS MaxRSS, '
'max(MaxRSS) / max(ReqMemNode) AS MemEff, '
'max(NGpus) AS NGpus, '
'max(NGpus)*max(Elapsed) AS gpu_s_reserved, '
'max(NGpus)*max(Elapsed)*max(GPUutil) AS gpu_s_used, '
'max(GPUutil) AS GPUeff, ' # Individual job with highest use (check this)
'max(GPUMem) AS GPUMem, '
'MaxDiskRead, '
'MaxDiskWrite, '
'sum(TotDiskRead) as TotDiskRead, '
'sum(TotDiskWrite) as TotDiskWrite '
'FROM slurm GROUP BY JobIDnostep')
db.execute('PRAGMA journal_mode = WAL;')
db.commit()
c = db.cursor()
slurm_cols = tuple(c for c in list(columns.keys()) + COLUMNS_EXTRA if not c.startswith('_'))
errors = [ 0 ]
if csv_input:
import collections
import csv
if not isinstance(csv_input, csv.DictReader):
csv_input = csv.DictReader(open(csv_input, 'r'))
def rows():
for row in csv_input:
row = collections.defaultdict(str, ((k,v.strip()) for k,v in row.items()))
yield row
rows = rows() # activate the generator
else:
rows = sacct_iter(slurm_cols, sacct_filter, raw_sacct=raw_sacct, errors=errors)
for i, row in enumerate(rows):
# If --jobs-only, then skip all job steps (sacct updates the
# mem/cpu usage on the allocation itself already)
step_id = slurmJobStep.calc(row)
if jobs_only and step_id is not None:
continue
#LOG.debug(row)
processed_row = {k.strip('_'): (columns[k](row[k])
#if not isinstance(columns[k], type) or not issubclass(columns[k], linefunc)
if not hasattr(columns[k], 'linefunc')
else columns[k].calc(row))
for k in columns.keys()}
c.execute('INSERT %s INTO slurm (%s) VALUES (%s)'%(
'OR REPLACE' if update else '',
','.join('"'+x+'"' for x in processed_row.keys()),
','.join(['?']*len(processed_row))),
tuple(processed_row.values()))
# Committing every so often allows other queries to succeed
if i%10000 == 0:
#print('committing')
db.commit()
if verbose:
print('... processing row %d'%i)
db.commit()
return errors[0]
def process_sacct_filter(args, sacct_filter):
"""Generate sacct filter args in a standard way
For example adding a --completed argument that translates into
different sacct arguments.
"""
# A single argument that looks like a jobID is used.
if len(sacct_filter) == 1 and re.match(r'[0-9+_]+(.[0-9a-z]+)?', sacct_filter[0]):
sacct_filter = [f'--jobs={sacct_filter[0]}']
# Set for completed jobs.
if getattr(args, 'completed', None):
sacct_filter[:0] = ['--endtime=now', f'--state={COMPLETED_STATES}']
return sacct_filter
def update_last_timestamp(db, update_time=None):
"""Update the last update time in the database, for resuming.
Updates the one row of the meta_slurm_lastupdate with the latest
unix timestamp, as passed as an argument (or now)
"""
if update_time is None:
update_time = time.time()
if isinstance(update_time, datetime.datetime):
update_time = datetime_timestamp(update_time)
update_time = min(update_time, time.time())
db.execute("INSERT OR REPLACE INTO meta_slurm_lastupdate (id, update_time) VALUES (0, ?)", (update_time, ))
db.commit()
def get_last_timestamp(db):
"""Return the last update timestamp from the database"""
return db.execute('SELECT update_time FROM meta_slurm_lastupdate').fetchone()[0]
def slurm_version(cmd=['sacct', '--version']):
"""Return the version number of Slurm, as a tuple"""
# example output: b'slurm 18.08.8\n' or slurm 19.05.7-Bull.1.0
try:
slurm_version = subprocess.check_output(cmd).decode()
except FileNotFoundError: # no sacct
return (20, 11) # lastest with a schema change
slurm_version = re.match(r"slurm\s([0-9]+)\.([0-9]+)\.([0-9]+)", slurm_version)
slurm_version = tuple(int(x) for x in slurm_version.groups())
return slurm_version
def compact_table():
"""Compact display format. Function to not depend on tabulate in main body."""
import tabulate
from tabulate import Line, DataRow, TableFormat