Get rid of regexp bits

This commit is contained in:
Matt Mackall 2009-01-29 18:21:50 -06:00
commit 3e4d9a1305

65
smem
View file

@ -1,43 +1,36 @@
import re, os, sys, pwd
#!/usr/bin/python
import re, os, sys, pwd, getopt
def pids():
'''get a list of processes'''
for e in os.listdir("/proc"):
if e.isdigit():
yield int(e)
_rangepat = re.compile(r'([a-f\d]+)-([a-f\d]+)\s+(....)\s+([a-f\d]+)\s+(\S+)\s+(\d+)\s+(\S*)')
_fieldpat = re.compile(r'(\w+):\s+(\d+) kB')
return [int(e) for e in os.listdir("/proc") if e.isdigit()]
def pidmaps(pid):
maps = {}
start = None
for l in file('/proc/%s/smaps' % pid):
m = _fieldpat.match(l)
if m:
field, val = m.groups()
maps[start][field.lower()] = int(val)
continue
m = _rangepat.match(l)
if m:
start, end, mode, offset, device, inode, name = m.groups()
d = dict(end=int(end, 16), mode=mode, offset=int(offset, 16),
device=device, inode=inode, name=name)
f = l.split()
if f[-1] == 'kB':
maps[start][f[0][:-1].lower()] = int(f[1])
else:
start, end = f[0].split('-')
start = int(start, 16)
maps[start] = d
continue
name = ""
if len(f) > 5:
name = f[5]
maps[start] = dict(end=int(end, 16), mode=f[1],
offset=int(f[2], 16),
device=f[3], inode=f[4], name=name)
return maps
def pidtotals(pid):
maps = pidmaps(pid)
t = dict(size=0, rss=0, pss=0, shared_clean=0, shared_dirty=0,
private_clean=0, private_dirty=0, referenced=0, swap=0)
for m in maps:
# t = dict(size=0, rss=0, pss=0, shared_clean=0, shared_dirty=0,
# private_clean=0, private_dirty=0, referenced=0, swap=0)
t = dict(pss=0)
for m in maps.iterkeys():
for k in t:
if pid == 31694: # and k == 'pss':
print m, k, maps[m].get(k, 0), t[k]
t[k] += maps[m].get(k, 0)
t['maps'] = len(maps)
return t
@ -45,7 +38,10 @@ def pidtotals(pid):
def processtotals(pids):
totals = {}
for pid in pids:
totals[pid] = pidtotals(pid)
try:
totals[pid] = pidtotals(pid)
except:
continue
return totals
def maptotals():
@ -67,10 +63,10 @@ def pidcmd(pid):
return file('/proc/%d/cmdline').read()
def piduser(pid):
return os.stat('/proc/%d/cmdline').st_uid
return os.stat('/proc/%d/cmdline' % pid).st_uid
def piduser(pid):
return os.stat('/proc/%d/cmdline').st_gid
def pidgroup(pid):
return os.stat('/proc/%d/cmdline' % pid).st_gid
def memory():
t = {}
@ -81,16 +77,9 @@ def memory():
return t
def showpids():
p = list(pids())
p.sort()
p = pids()
pt = processtotals(p)
for n in p:
print n, pidname(n), pt[n]['pss']
#pt = processtotals()
#t = sum([pt[n]['pss'] for n in pt])
#print sortmaps(pt, 'pss')[-10:]
#print os.getpid(), pidtotals(os.getpid())['pss'], len(list(pids()))
#print memory()['memtotal'], t
print "% 6d %-8s %-30s % 8d" % (n, piduser(n), pidname(n), pt[n]['pss'])
showpids()