Guest User

Untitled

a guest
Dec 15th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.61 KB | None | 0 0
  1. #! /usr/bin/env python
  2. """
  3. jobber.py
  4. Summarize status of running jobs under SLURM scheduler.
  5. """
  6.  
  7. from __future__ import print_function
  8.  
  9. import os
  10. import sys
  11. import subprocess as sp
  12. from collections import defaultdict
  13. from datetime import timedelta
  14.  
  15. ## parse runtime as reported by `squeue`, return as hours (floating-point)
  16. def parse_runtime(x):
  17. pieces = x.split(":")
  18. if len(pieces) == 2:
  19. d = 0
  20. h = 0
  21. m,s = [ int(_) for _ in pieces ]
  22. elif len(pieces) == 3:
  23. h,m,s = pieces
  24. if "-" in h:
  25. d,h = [ int(_) for _ in h.split("-") ]
  26. m,s = [ int(_) for _ in [m,s] ]
  27. else:
  28. d = 0
  29. h,m,s = [ int(_) for _ in [h,m,s] ]
  30. hh = 24*d + h + m/60 + s/(60*60)
  31. return hh
  32.  
  33. def reformat_time(x):
  34. return str( timedelta(hours = x) )
  35.  
  36. ## query scheduler for running jobs
  37. cmd = os.path.expandvars("squeue -u $USER")
  38. piper = sp.Popen(cmd, stdout = sp.PIPE, stderr = sp.PIPE, shell = True)
  39. STAT_CODE = [ "PD","R","CG","S","ST" ]
  40. STAT_DESC = [ "pending","running","compl","susp","stop" ]
  41.  
  42. ## slurp off header line
  43. jobs = iter(piper.stdout.readline, "")
  44. _ = next(jobs)
  45.  
  46. ## loop on jobs
  47. counts = defaultdict(int)
  48. runtimes = defaultdict(list)
  49. for line in jobs:
  50. pieces = line.decode().strip().split()
  51. if not len(pieces):
  52. break
  53. counts[ pieces[4] ] += 1
  54. runtimes[ pieces[4] ].append( parse_runtime(pieces[5]) )
  55.  
  56. ## print summary
  57. print("STATUS","NJOBS","MINTIME","MAXTIME", sep = "\t")
  58. for status,desc in zip(STAT_CODE, STAT_DESC):
  59. max_time, min_time = "--","--"
  60. if status in runtimes:
  61. max_time = reformat_time( max(runtimes[status]) )
  62. min_time = reformat_time( min(runtimes[status]) )
  63. print(desc, counts[status], min_time, max_time, sep = "\t")
Add Comment
Please, Sign In to add comment