Better handling of unknown memory thingies and proper handling of Darwin and changed memory to RSS and stuff like that.

This commit is contained in:
Jeremy Fincher 2004-01-01 19:12:14 +00:00
parent 526f8d7314
commit 6393a0c36e
2 changed files with 26 additions and 28 deletions

View File

@ -163,33 +163,35 @@ class Status(callbacks.Privmsg):
(user, system, childUser, childSystem, elapsed) = os.times() (user, system, childUser, childSystem, elapsed) = os.times()
now = time.time() now = time.time()
timeRunning = now - world.startedAt timeRunning = now - world.startedAt
if user+system > timeRunning: if user+system < timeRunning+1: # Fudge for FPU inaccuracies.
irc.error(msg, 'I seem to be running on a platform without an ' children = 'My children have taken %.2f seconds of user time ' \
'accurate way of determining how long I\'ve been ' 'and %.2f seconds of system time ' \
'running.') 'for a total of %.2f seconds of CPU time. ' % \
return (childUser, childSystem, childUser+childSystem)
else:
children = ''
activeThreads = threading.activeCount() activeThreads = threading.activeCount()
response = ('I have taken %.2f seconds of user time and %.2f seconds ' response = ('I have taken %.2f seconds of user time and %.2f seconds '
'of system time, for a total of %.2f seconds of CPU ' 'of system time, for a total of %.2f seconds of CPU '
'time. My children have taken %.2f seconds of user time ' 'time. %sOut of %s I have %s active.' %
'and %.2f seconds of system time for a total of %.2f ' (user, system, user + system, children,
'seconds of CPU time. Out of %s I have %s active.' %
(user, system, user + system,
childUser, childSystem, childUser + childSystem,
utils.nItems('thread', world.threadsSpawned, 'spawned'), utils.nItems('thread', world.threadsSpawned, 'spawned'),
activeThreads)) activeThreads))
mem = None mem = 'an unknown amount'
pid = os.getpid() pid = os.getpid()
plat = sys.platform plat = sys.platform
try: try:
if plat.startswith('linux') or plat.startswith('sunos') or \ if plat.startswith('linux') or plat.startswith('sunos') or \
plat.startswith('freebsd') or plat.startswith('openbsd'): plat.startswith('freebsd') or plat.startswith('openbsd') or \
r = os.popen('ps -o vsz -p %s' % pid) plat.startswith('darwin'):
r.readline() # VSZ Header. try:
mem = int(r.readline().strip()) r = os.popen('ps -o rss -p %s' % pid)
r.close() r.readline() # VSZ Header.
mem = r.readline().strip() + ' kB'
finally:
r.close()
elif sys.platform.startswith('netbsd'): elif sys.platform.startswith('netbsd'):
mem = os.stat('/proc/%s/mem')[7] mem = '%s kB' % os.stat('/proc/%s/mem')[7]
response += ' I\'m taking up %s kB of memory.' % mem response += ' I\'m taking up %s kB of memory.' % mem
except Exception: except Exception:
self.log.exception('Uncaught exception in cpu:') self.log.exception('Uncaught exception in cpu:')

View File

@ -47,17 +47,13 @@ class StatusTestCase(PluginTestCase, PluginDocumentation):
self.assertNotError('net') self.assertNotError('net')
def testCpustats(self): def testCpustats(self):
try: m = self.assertNotError('status cpu')
original = world.startedAt self.failIf('None' in m.args[1], 'None in cpu output: %r.' % m)
world.startedAt = time.time() for s in ['linux', 'freebsd', 'openbsd', 'netbsd', 'darwin']:
self.assertError('cpu') if sys.platform.startswith(s):
world.startedAt = 0 self.failUnless('kB' in m.args[1],
self.assertNotError('cpu') 'No memory string on supported platform.')
for s in ['linux', 'freebsd', 'openbsd', 'netbsd']:
if sys.platform.startswith(s):
self.assertRegexp('cpu', 'kB')
finally:
world.startedAt = original
def testUptime(self): def testUptime(self):
self.assertNotError('uptime') self.assertNotError('uptime')