changeset 10:479e73ddf945

making box.status use 'ps' instead of python's resource module.
author Atul Varma <avarma@mozilla.com>
date Mon, 31 May 2010 14:58:18 -0700
parents fb7ca2688d3d
children 7759069e8655
files sjsbox/box.py tests/test_box.py
diffstat 2 files changed, 24 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/sjsbox/box.py	Mon May 31 12:34:33 2010 -0700
+++ b/sjsbox/box.py	Mon May 31 14:58:18 2010 -0700
@@ -1,7 +1,29 @@
 import logging
 import resource
+import subprocess
 import multiprocessing as mproc
 
+def get_pid_statuses(pids):
+    keywords = ["pid", "%cpu", "%mem", "rss", "utime", "cputime"]
+    cmdline = ["ps"]
+    cmdline.extend(["-p%d" % pid for pid in pids])
+    cmdline.extend(["-o%s" % keyword for keyword in keywords])
+    popen = subprocess.Popen(cmdline, stdout=subprocess.PIPE,
+                             stderr=subprocess.PIPE)
+    stdout, _ = popen.communicate()
+    lines = stdout.splitlines()[1:]
+    statuses = {}
+    for line in lines:
+        status = {}
+        parts = line.split()
+        pid = int(parts[0])
+        i = 1
+        for keyword in keywords[1:]:
+            status[keyword] = parts[i]
+            i += 1
+        statuses[pid] = status
+    return statuses
+
 class BoxChild(object):
     LIMITS = {
         #resource.RLIMIT_DATA: (1024 * 1024, 1024 * 2048),
@@ -26,14 +48,6 @@
             if cmd == 'shutdown':
                 self.__impl.shutdown()
                 return
-            elif cmd == 'status':
-                rusage = resource.getrusage(resource.RUSAGE_SELF)
-                props = [name for name in dir(rusage)
-                         if name.startswith('ru_')]
-                retval = {}
-                for name in props:
-                    retval[name] = getattr(rusage, name)
-                self.pipe.send(retval)
             elif cmd == 'handle':
                 self.pipe.send(self.__impl.handle(*args))
 
@@ -77,7 +91,7 @@
 
     @property
     def status(self):
-        return self.__child_call('status')
+        return get_pid_statuses([self.child.pid])[self.child.pid]
 
     def shutdown(self):
         self.child_pipe.send(('shutdown', None))
--- a/tests/test_box.py	Mon May 31 12:34:33 2010 -0700
+++ b/tests/test_box.py	Mon May 31 14:58:18 2010 -0700
@@ -26,7 +26,7 @@
                          404)
 
     def test_status_works(self):
-        self.assertTrue('ru_maxrss' in self.boxes['foo'].status)
+        self.assertTrue('rss' in self.boxes['foo'].status)
 
     def test_update_works(self):
         self.boxes.update()