Skip to content

Commit

Permalink
mxvmem: add options to query memory usage
Browse files Browse the repository at this point in the history
e.g.

  #> mxvmem -q DS -t 7

will show users that have sleeping/dead jobs for over a week.
  • Loading branch information
thomas committed Jul 12, 2023
1 parent bb559ae commit 709d2fd
Showing 1 changed file with 37 additions and 4 deletions.
41 changes: 37 additions & 4 deletions forensics/mxvmem
Original file line number Diff line number Diff line change
Expand Up @@ -220,14 +220,14 @@ class ProcStreamParser():
# --------------------------------------------------------------- workhorse

class ProcFsHandler():
def __init__(self, classifier = Classifier()):
def __init__(self, classifier = Classifier(), age_thresh = 2):
self.Cfy = classifier
self.store = {}
self.usermap = {}
self.report = []
self.uptime = -1
self.memtotal = -1
self.age_threshold = 2 * 60*60*24 # Two days
self.age_threshold = age_thresh * 60*60*24 # days

def set_uptime(self, t):
self.uptime = t
Expand Down Expand Up @@ -340,6 +340,22 @@ class ProcMemClassifier(Classifier):

return ret

def collectusage(self, keys= '', memlimit=0):
ret = list()
for k in self:
memused = 0
for key in keys:
if key == 'D': key = 'DZT' # ugly
if key == 'd': key = 'dzt'
if key in k[1].fld:
if k[1].fld[key].val > memlimit:
memused += k[1].fld[key].val

if memused:
ret.append((k[0], memused))

return ret

# ------------------------------------------------------------------- tools

def register_logs(logdir):
Expand Down Expand Up @@ -401,10 +417,18 @@ def handle_args():
help='print all entries, makes you scroll, helps when piping', action='store_true')
ap.add_argument("-d", dest='logdir', metavar='dir', default=None,
help='location of forensics logs (/var/log)')
ap.add_argument('-m', dest='memthresh', metavar='percent' ,
help='threshold for memory usage report (10%%)', default=10.0, type=float)
ap.add_argument('-q', dest='query', metavar='query' ,
help='report memory usage for given categories (eg. \'SD\')', default='')
ap.add_argument('-p', dest='readproc',
help='read current data from proc', action='store_true')
ap.add_argument('-s', dest='summary',
help='print short summary', action='store_true')
ap.add_argument('-t', dest='durationthresh', metavar='days' ,
help='time in days when a job is considered as old', default=2)
ap.add_argument('-v', dest='verbose',
help='be a bit more verbose', action='store_true')
ap.add_argument('forensicsfile', metavar='file', nargs='?',
help='forensics file (defaults to most recent log found)')
return ap.parse_args()
Expand Down Expand Up @@ -450,10 +474,11 @@ if __name__ == '__main__':
quit()
proc = ProcInfoSaved(logs[0][0])

print(' Reading:', "'%s'" % proc.source, '...')
if args.verbose:
print(' Reading:', "'%s'" % proc.source, '...')

pmc = ProcMemClassifier()
whs = ProcFsHandler(pmc)
whs = ProcFsHandler(pmc, float(args.durationthresh))
psp = ProcStreamParser(whs)

for line in proc:
Expand All @@ -462,6 +487,14 @@ if __name__ == '__main__':

whs.analyze()

if args.query:
memthresh = args.memthresh
keys = args.query
result = pmc.collectusage(keys, whs.memtotal*(memthresh/100))
for r in result:
print('%s %.1f' % (r[0], r[1]/1e6))
quit()

print(' Memory: %.1f Gb available, %.1f Gb in use (%.1f %%)\n' %
( whs.memtotal/1024**2,
pmc.getval()/1024**2,
Expand Down

0 comments on commit 709d2fd

Please sign in to comment.