Abstract proc interface
This commit is contained in:
parent
d3d4c72f1f
commit
40b49768d6
1 changed files with 50 additions and 31 deletions
81
smem
81
smem
|
|
@ -1,6 +1,41 @@
|
||||||
#!/usr/bin/python
|
#!/usr/bin/python
|
||||||
import re, os, sys, pwd, grp, optparse, errno
|
import re, os, sys, pwd, grp, optparse, errno
|
||||||
|
|
||||||
|
_ucache = {}
|
||||||
|
_gcache = {}
|
||||||
|
|
||||||
|
class procdata(object):
|
||||||
|
def pids(self):
|
||||||
|
'''get a list of processes'''
|
||||||
|
return [int(e) for e in os.listdir("/proc")
|
||||||
|
if e.isdigit() and not iskernel(e)]
|
||||||
|
def _read(self, f):
|
||||||
|
return file('/proc/' + f).read()
|
||||||
|
def _readlines(self, f):
|
||||||
|
return self._read(f).splitlines(True)
|
||||||
|
def mapdata(self, pid):
|
||||||
|
return self._readlines('%s/smaps' % pid)
|
||||||
|
def memdata(self):
|
||||||
|
return self._readlines('meminfo')
|
||||||
|
def pidname(self, pid):
|
||||||
|
l = self._read('%d/stat' % pid)
|
||||||
|
return l[l.find('(') + 1: l.find(')')]
|
||||||
|
def pidcmd(self, pid):
|
||||||
|
c = self._read('%s/cmdline' % pid)[:-1]
|
||||||
|
return c.replace('\0', ' ')
|
||||||
|
def piduser(self, pid):
|
||||||
|
return os.stat('/proc/%d/cmdline' % pid).st_uid
|
||||||
|
def pidgroup(self, pid):
|
||||||
|
return os.stat('/proc/%d/cmdline' % pid).st_gid
|
||||||
|
def username(self, uid):
|
||||||
|
if uid not in _ucache:
|
||||||
|
_ucache[uid] = pwd.getpwuid(uid)[0]
|
||||||
|
return _ucache[uid]
|
||||||
|
def groupname(self, gid):
|
||||||
|
if gid not in _gcache:
|
||||||
|
_gcache[gid] = pwd.getgrgid(gid)[0]
|
||||||
|
return _gcache[gid]
|
||||||
|
|
||||||
_totalmem = 0
|
_totalmem = 0
|
||||||
def totalmem():
|
def totalmem():
|
||||||
global _totalmem
|
global _totalmem
|
||||||
|
|
@ -11,14 +46,10 @@ def totalmem():
|
||||||
_totalmem = memory()['memtotal']
|
_totalmem = memory()['memtotal']
|
||||||
return _totalmem
|
return _totalmem
|
||||||
|
|
||||||
def pids():
|
|
||||||
'''get a list of processes'''
|
|
||||||
return [int(e) for e in os.listdir("/proc") if e.isdigit() and not iskernel(e)]
|
|
||||||
|
|
||||||
def pidmaps(pid):
|
def pidmaps(pid):
|
||||||
maps = {}
|
maps = {}
|
||||||
start = None
|
start = None
|
||||||
for l in file('/proc/%s/smaps' % pid):
|
for l in src.mapdata(pid):
|
||||||
f = l.split()
|
f = l.split()
|
||||||
if f[-1] == 'kB':
|
if f[-1] == 'kB':
|
||||||
maps[start][f[0][:-1].lower()] = int(f[1])
|
maps[start][f[0][:-1].lower()] = int(f[1])
|
||||||
|
|
@ -49,26 +80,12 @@ def sortmaps(totals, key):
|
||||||
return [pid for pid,key in l]
|
return [pid for pid,key in l]
|
||||||
|
|
||||||
def iskernel(pid):
|
def iskernel(pid):
|
||||||
return pidcmd(pid) == ""
|
return src.pidcmd(pid) == ""
|
||||||
|
|
||||||
def pidname(pid):
|
|
||||||
l = file('/proc/%d/stat' % pid).read()
|
|
||||||
return l[l.find('(') + 1: l.find(')')]
|
|
||||||
|
|
||||||
def pidcmd(pid):
|
|
||||||
c = file('/proc/%s/cmdline' % pid).read()[:-1]
|
|
||||||
return c.replace('\0', ' ')
|
|
||||||
|
|
||||||
def piduser(pid):
|
|
||||||
return os.stat('/proc/%d/cmdline' % pid).st_uid
|
|
||||||
|
|
||||||
def pidgroup(pid):
|
|
||||||
return os.stat('/proc/%d/cmdline' % pid).st_gid
|
|
||||||
|
|
||||||
def memory():
|
def memory():
|
||||||
t = {}
|
t = {}
|
||||||
f = re.compile('(\\S+):\\s+(\\d+) kB')
|
f = re.compile('(\\S+):\\s+(\\d+) kB')
|
||||||
for l in file('/proc/meminfo'):
|
for l in src.memdata():
|
||||||
m = f.match(l)
|
m = f.match(l)
|
||||||
if m:
|
if m:
|
||||||
t[m.group(1).lower()] = int(m.group(2)) * 1024
|
t[m.group(1).lower()] = int(m.group(2)) * 1024
|
||||||
|
|
@ -99,7 +116,7 @@ def username(uid):
|
||||||
return _ucache[uid]
|
return _ucache[uid]
|
||||||
|
|
||||||
def pidusername(pid):
|
def pidusername(pid):
|
||||||
return username(piduser(pid))
|
return username(src.piduser(pid))
|
||||||
|
|
||||||
_gcache = {}
|
_gcache = {}
|
||||||
def groupname(gid):
|
def groupname(gid):
|
||||||
|
|
@ -146,7 +163,7 @@ def pidtotals(pid):
|
||||||
def processtotals(pids):
|
def processtotals(pids):
|
||||||
totals = {}
|
totals = {}
|
||||||
for pid in pids:
|
for pid in pids:
|
||||||
if (filter(options.processfilter, pid, pidname, pidcmd) or
|
if (filter(options.processfilter, pid, src.pidname, src.pidcmd) or
|
||||||
filter(options.userfilter, pid, pidusername)):
|
filter(options.userfilter, pid, pidusername)):
|
||||||
continue
|
continue
|
||||||
try:
|
try:
|
||||||
|
|
@ -158,7 +175,7 @@ def processtotals(pids):
|
||||||
return totals
|
return totals
|
||||||
|
|
||||||
def showpids():
|
def showpids():
|
||||||
p = pids()
|
p = src.pids()
|
||||||
pt = processtotals(p)
|
pt = processtotals(p)
|
||||||
|
|
||||||
def showuser(p):
|
def showuser(p):
|
||||||
|
|
@ -169,8 +186,8 @@ def showpids():
|
||||||
fields = dict(
|
fields = dict(
|
||||||
pid=('PID', lambda n: n, '% 5s', lambda x: len(p)),
|
pid=('PID', lambda n: n, '% 5s', lambda x: len(p)),
|
||||||
user=('User', showuser, '%-8s', lambda x: len(dict.fromkeys(x))),
|
user=('User', showuser, '%-8s', lambda x: len(dict.fromkeys(x))),
|
||||||
name=('Name', pidname, '%-24.24s', None),
|
name=('Name', src.pidname, '%-24.24s', None),
|
||||||
command=('Command', pidcmd, '%-27.27s', None),
|
command=('Command', src.pidcmd, '%-27.27s', None),
|
||||||
maps=('Maps',lambda n: pt[n]['maps'], '% 5s', sum),
|
maps=('Maps',lambda n: pt[n]['maps'], '% 5s', sum),
|
||||||
swap=('Swap',lambda n: pt[n]['swap'], '% 8a', sum),
|
swap=('Swap',lambda n: pt[n]['swap'], '% 8a', sum),
|
||||||
uss=('USS', lambda n: pt[n]['uss'], '% 8a', sum),
|
uss=('USS', lambda n: pt[n]['uss'], '% 8a', sum),
|
||||||
|
|
@ -189,7 +206,7 @@ def showpids():
|
||||||
def maptotals(pids):
|
def maptotals(pids):
|
||||||
totals = {}
|
totals = {}
|
||||||
for pid in pids:
|
for pid in pids:
|
||||||
if (filter(options.processfilter, pid, pidname, pidcmd) or
|
if (filter(options.processfilter, pid, src.pidname, src.pidcmd) or
|
||||||
filter(options.userfilter, pid, pidusername)):
|
filter(options.userfilter, pid, pidusername)):
|
||||||
continue
|
continue
|
||||||
try:
|
try:
|
||||||
|
|
@ -212,7 +229,7 @@ def maptotals(pids):
|
||||||
return totals
|
return totals
|
||||||
|
|
||||||
def showmaps():
|
def showmaps():
|
||||||
p = pids()
|
p = src.pids()
|
||||||
pt = maptotals(p)
|
pt = maptotals(p)
|
||||||
|
|
||||||
fields = dict(
|
fields = dict(
|
||||||
|
|
@ -238,7 +255,7 @@ def showmaps():
|
||||||
def usertotals(pids):
|
def usertotals(pids):
|
||||||
totals = {}
|
totals = {}
|
||||||
for pid in pids:
|
for pid in pids:
|
||||||
if (filter(options.processfilter, pid, pidname, pidcmd) or
|
if (filter(options.processfilter, pid, src.pidname, src.pidcmd) or
|
||||||
filter(options.userfilter, pid, pidusername)):
|
filter(options.userfilter, pid, pidusername)):
|
||||||
continue
|
continue
|
||||||
try:
|
try:
|
||||||
|
|
@ -247,7 +264,7 @@ def usertotals(pids):
|
||||||
continue
|
continue
|
||||||
except:
|
except:
|
||||||
raise
|
raise
|
||||||
user = piduser(pid)
|
user = src.piduser(pid)
|
||||||
if user not in totals:
|
if user not in totals:
|
||||||
t = dict(size=0, rss=0, pss=0, shared_clean=0,
|
t = dict(size=0, rss=0, pss=0, shared_clean=0,
|
||||||
shared_dirty=0, private_clean=0, count=0,
|
shared_dirty=0, private_clean=0, count=0,
|
||||||
|
|
@ -264,7 +281,7 @@ def usertotals(pids):
|
||||||
return totals
|
return totals
|
||||||
|
|
||||||
def showusers():
|
def showusers():
|
||||||
p = pids()
|
p = src.pids()
|
||||||
pt = usertotals(p)
|
pt = usertotals(p)
|
||||||
|
|
||||||
def showuser(u):
|
def showuser(u):
|
||||||
|
|
@ -364,6 +381,8 @@ defaults = {}
|
||||||
parser.set_defaults(**defaults)
|
parser.set_defaults(**defaults)
|
||||||
(options, args) = parser.parse_args()
|
(options, args) = parser.parse_args()
|
||||||
|
|
||||||
|
src = procdata()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
if options.mappings:
|
if options.mappings:
|
||||||
showmaps()
|
showmaps()
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue