Created
March 6, 2012 18:49
-
-
Save lukemarsden/1988153 to your computer and use it in GitHub Desktop.
analyse-top.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# Run sudo top -o res -a -S infinity > ~/topres.txt | |
import re | |
def parseNum(val): | |
nre = re.compile('([0-9\.-]+)([TGMK]?)') | |
num, unit = nre.match(val).groups() | |
# Supports mbuffer and zfs list style suffixes | |
if unit == 'TB' or unit == 'T': | |
return int(float(num)*1024*1024*1024*1024) | |
elif unit == 'GB' or unit == 'G': | |
return int(float(num)*1024*1024*1024) | |
elif unit == 'MB' or unit == 'M': | |
return int(float(num)*1024*1024) | |
elif unit == 'kB' or unit == 'K': | |
return int(float(num)*1024) | |
else: | |
return int(float(num)) | |
lines = open("topres.txt").readlines() | |
print len(lines), "lines..." | |
print lines[3] | |
wsre = re.compile('\s+') | |
work = lines[8:] | |
count = 0; aggr = 0; aggrSize = 0 | |
from collections import defaultdict | |
userTotals = defaultdict(float) | |
userTotalsSize = defaultdict(float) | |
for line in work: | |
stripped = line.strip() | |
if stripped: | |
count += 1 | |
pid, username, thr, pri, nice, size, res, state, c, time, wcpu, command = ( | |
wsre.split(stripped, 11)) | |
# print res, parseNum(res) | |
thisProc = parseNum(res) | |
thisProcSize = parseNum(size) | |
aggr += thisProc | |
aggrSize += thisProcSize | |
userTotals[username] += thisProc / (1024. * 1024.) | |
userTotalsSize[username] += thisProcSize / (1024. * 1024.) | |
print "There were", count, "processes totalling", aggr / (1024. * 1024.), "MB resident memory" | |
print "and", aggrSize / (1024. * 1024.), "MB 'size' memory" | |
import pprint | |
print " == RES ==" | |
pprint.pprint(dict(userTotals)) | |
print " == SIZE ==" | |
pprint.pprint(dict(userTotalsSize)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment