[yt-svn] commit/yt: brittonsmith: Fixed the get_memory_usage for an alternate return value

Bitbucket commits-noreply at bitbucket.org
Tue Feb 28 14:40:32 PST 2012


1 new commit in yt:


https://bitbucket.org/yt_analysis/yt/changeset/ab0e7664f75d/
changeset:   ab0e7664f75d
branch:      yt
user:        brittonsmith
date:        2012-02-28 23:24:45
summary:     Fixed the get_memory_usage for an alternate return value
from ps -o rss= -p PID.
affected #:  1 file

diff -r 052fac82670180c2de69fb2173dd9503e2b4cd1c -r ab0e7664f75da1ed9eb7212219cb4af8254a3929 yt/funcs.py
--- a/yt/funcs.py
+++ b/yt/funcs.py
@@ -93,6 +93,22 @@
 except ImportError:
     pass
 
+def __memory_fallback(pid):
+    """
+    Get process memory from a system call.
+    """
+    value = os.popen('ps -o rss= -p %d' % pid).read().strip().split('\n')
+    if len(value) == 1: return float(value[0])
+    value.pop(0)
+    for line in value:
+        online = line.split()
+        if online[0] != pid: continue
+        try:
+            return float(online[2])
+        except:
+            return 0.0
+    return 0.0
+
 def get_memory_usage():
     """
     Returning resident size in megabytes
@@ -101,10 +117,10 @@
     try:
         pagesize = resource.getpagesize()
     except NameError:
-        return float(os.popen('ps -o rss= -p %d' % pid).read()) / 1024
+        return __memory_fallback(pid) / 1024
     status_file = "/proc/%s/statm" % (pid)
     if not os.path.isfile(status_file):
-        return float(os.popen('ps -o rss= -p %d' % pid).read()) / 1024
+        return __memory_fallback(pid) / 1024
     line = open(status_file).read()
     size, resident, share, text, library, data, dt = [int(i) for i in line.split()]
     return resident * pagesize / (1024 * 1024) # return in megs

Repository URL: https://bitbucket.org/yt_analysis/yt/

--

This is a commit notification from bitbucket.org. You are receiving
this because you have the service enabled, addressing the recipient of
this email.



More information about the yt-svn mailing list