Get rid of regexp bits

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

63
smem
View file

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