From aa088ef6d902dd5a11b3437fa56154aaa393c1fa Mon Sep 17 00:00:00 2001 From: Jeremy Fincher Date: Fri, 24 Oct 2003 13:38:55 +0000 Subject: [PATCH] Added memory reporting stuff to cpustats. --- plugins/Status.py | 38 +++++++++++++++++++++++++++++++------- test/test_Status.py | 5 +++++ 2 files changed, 36 insertions(+), 7 deletions(-) diff --git a/plugins/Status.py b/plugins/Status.py index bd669e664..54a32e28a 100644 --- a/plugins/Status.py +++ b/plugins/Status.py @@ -174,17 +174,41 @@ class Status(callbacks.Privmsg): 'running.') return activeThreads = threading.activeCount() - response = 'I have taken %s seconds of user time and %s seconds of '\ - 'system time, for a total of %s seconds of CPU time. My '\ - 'children have taken %s seconds of user time and %s seconds'\ - ' of system time for a total of %s seconds of CPU time. ' \ - 'I\'ve taken a total of %s%% of this computer\'s time. ' \ - 'Out of %s I have %s active. ' % \ + response = ('I have taken %.2f seconds of user time and %.2f seconds ' + 'of system time, for a total of %.2f seconds of CPU ' + 'time. My children have taken %.2f seconds of user time ' + 'and %.2f seconds of system time for a total of %.2f ' + 'seconds of CPU time. I\'ve taken a total of %.2f%% of ' + 'this computer\'s time. Out of %s I have %s active.' % (user, system, user + system, childUser, childSystem, childUser + childSystem, (user+system+childUser+childSystem)/timeRunning, utils.nItems(world.threadsSpawned, 'thread', 'spawned'), - activeThreads) + activeThreads)) + mem = None + pid = os.getpid() + try: + if sys.platform.startswith('linux'): + mem = 0 + fd = file('/proc/%s/status' % pid) + for line in fd: + if line.startswith('VmSize'): + line = line.rstrip() + mem = int(line.split()[1]) + break + assert mem, 'Format changed in /proc//status' + elif sys.platform.startswith('netbsd'): + mem = os.stat('/proc/%s/mem')[7] + elif sys.platform.startswith('freebsd') or \ + sys.platform.startswith('openbsd'): + r = os.popen('/bin/ps -l -p %s' % pid) + line = r.readline() + line = line.strip() + r.close() + mem = int(line.split()[20]) + response += ' I\'m taking up %s kB of memory.' % mem + except Exception, e: + debug.msg(debug.exnToString(e)) irc.reply(msg, response) def cmdstats(self, irc, msg, args): diff --git a/test/test_Status.py b/test/test_Status.py index 0e9b5bcf2..35eed4393 100644 --- a/test/test_Status.py +++ b/test/test_Status.py @@ -31,6 +31,8 @@ from test import * +import sys + import world class StatusTestCase(PluginTestCase, PluginDocumentation): @@ -51,6 +53,9 @@ class StatusTestCase(PluginTestCase, PluginDocumentation): self.assertError('cpustats') world.startedAt = 0 self.assertNotError('cpustats') + for s in ['linux', 'freebsd', 'openbsd', 'netbsd']: + if sys.platform.startswith(s): + self.assertRegexp('cpustats', 'kB') finally: world.startedAt = original def testUptime(self):