[svn] r5536 - trunk/tools/dstat/plugins
packagers at lists.rpmforge.net
packagers at lists.rpmforge.net
Sun Jun 24 21:42:19 CEST 2007
Author: dag
Date: 2007-06-24 21:42:18 +0200 (Sun, 24 Jun 2007)
New Revision: 5536
Added:
trunk/tools/dstat/plugins/dstat_topmem.py
Log:
Updates
Added: trunk/tools/dstat/plugins/dstat_topmem.py
===================================================================
--- trunk/tools/dstat/plugins/dstat_topmem.py (rev 0)
+++ trunk/tools/dstat/plugins/dstat_topmem.py 2007-06-24 19:42:18 UTC (rev 5536)
@@ -0,0 +1,67 @@
+### Dstat most expensive process plugin
+### Displays the name of the most expensive process
+###
+### Authority: dag at wieers.com
+
+global string
+import string
+
+class dstat_topmem(dstat):
+ def __init__(self):
+ self.name = 'most expensive'
+ self.format = ('s', 18, 0)
+ self.nick = ('memory process',)
+ self.vars = self.nick
+ self.pid = str(os.getpid())
+ self.cn1 = {}; self.cn2 = {}; self.val = {}
+
+ def extract(self):
+ self.val['usage'] = 0.0
+ for pid in os.listdir('/proc/'):
+ try: int(pid)
+ except: continue
+ if os.path.exists('/proc/%s/stat' % pid):
+ if pid == self.pid: continue
+
+ ### Using dopen() will cause too many open files
+# l = string.split(dopen('/proc/%s/stat' % pid).read())
+ l = string.split(open('/proc/%s/stat' % pid).read())
+ if len(l) < 23: continue
+ usage = int(l[23]) * pagesize
+
+ ### Get the process that spends the most jiffies
+ if usage > self.val['usage']:
+ self.val['usage'] = usage
+ self.val['name'] = l[1][1:-1]
+ self.val['pid'] = pid
+
+ if self.val['usage'] == 0.0:
+ self.val['process'] = ''
+ else:
+ ### If the name is a known interpreter, take the second argument from the cmdline
+ if self.val['name'] in ('bash', 'csh', 'ksh', 'perl', 'python', 'sh'):
+ ### Using dopen() will cause too many open files
+# l = string.split(dopen('/proc/%s/cmdline' % self.val['pid']).read(), '\0')
+ l = string.split(open('/proc/%s/cmdline' % self.val['pid']).read(), '\0')
+ if len(l) > 2:
+ self.val['process'] = os.path.basename(l[1])
+ else:
+ self.val['process'] = self.val['name']
+
+# l = l.reverse()
+# for x in l:
+# print x
+# if x[0] != '-':
+# self.val['name'] = os.path.basename(x)
+# break
+
+ ### Debug (show PID)
+# self.val['process'] = '%*s %-*s' % (5, self.val['pid'], self.format[1]-6, self.val['name'])
+
+ def show(self):
+ return '%s%-*s%s%s' % (ansi['default'], self.format[1]-4, self.val['process'], ansi['yellow'], cprint(self.val['usage']))
+
+ def showcsv(self):
+ return '%s / %d%%' % (self.val['name'], self.val['usage'])
+
+# vim:ts=4:sw=4:et
More information about the svn-commits
mailing list