Change in vdsm[master]: Returning namedtuple from util.pidStat for simpler usage

ybronhei at redhat.com ybronhei at redhat.com
Thu May 23 13:22:51 UTC 2013


Yaniv Bronhaim has uploaded a new change for review.

Change subject: Returning namedtuple from util.pidStat for simpler usage
......................................................................

Returning namedtuple from util.pidStat for simpler usage

Change-Id: I7cbac1c9175afbdecf42d40290026d518098e193
Signed-off-by: Yaniv Bronhaim <ybronhei at redhat.com>
---
M lib/vdsm/utils.py
M tests/miscTests.py
M tests/utilsTests.py
M vdsm/storage/blockSD.py
M vdsm/storage/misc.py
5 files changed, 19 insertions(+), 7 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/vdsm refs/changes/24/15024/1

diff --git a/lib/vdsm/utils.py b/lib/vdsm/utils.py
index 8b2dd4f..30a0a9f 100644
--- a/lib/vdsm/utils.py
+++ b/lib/vdsm/utils.py
@@ -26,6 +26,7 @@
 
     Contains a reverse dictionary pointing from error string to its error code.
 """
+from collections import namedtuple
 from SimpleXMLRPCServer import SimpleXMLRPCServer
 from StringIO import StringIO
 from weakref import proxy
@@ -168,6 +169,14 @@
 
 def pidStat(pid):
     res = []
+    stat = namedtuple('stat', 'pid comm state ppid pgrp session \
+                      tty_nr tpgid flags minflt cminflt majflt cmajflt \
+                      utime stime cutime cstime priority nice num_threads \
+                      itrealvalue starttime vsize rss rsslim \
+                      startcode endcode startstack kstkesp \
+                      kstkeip signal blocked sigignore sigcatch \
+                      wchan nswap cnswap exit_signal processor rt_priority \
+                      policy delayacct_blkio_ticks guest_time cguest_time')
     with open("/proc/%d/stat" % pid, "r") as f:
         statline = f.readline()
         procNameStart = statline.find("(")
@@ -177,7 +186,10 @@
         args = statline[procNameEnd + 2:].split()
         res.append(args[0])
         res.extend([int(item) for item in args[1:]])
-        return tuple(res)
+        # Only 44 feilds are documented in man page while /proc/pid/stat has 52
+        # The rest of the fields contain the process memory layout and
+        # exit_code, which are not relevant for our use.
+        return stat._make(res[:44])
 
 
 def convertToStr(val):
diff --git a/tests/miscTests.py b/tests/miscTests.py
index 96549c8..f39aac8 100644
--- a/tests/miscTests.py
+++ b/tests/miscTests.py
@@ -1028,7 +1028,7 @@
         proc = misc.execCmd(cmd, sudo=False, nice=10, sync=False)
 
         def test():
-            nice = utils.pidStat(proc.pid)[18]
+            nice = utils.pidStat(proc.pid).nice
             self.assertEquals(nice, 10)
 
         utils.retry(AssertionError, test, tries=10, sleep=0.1)
diff --git a/tests/utilsTests.py b/tests/utilsTests.py
index 758bc8a..a6a222d 100644
--- a/tests/utilsTests.py
+++ b/tests/utilsTests.py
@@ -53,9 +53,9 @@
         args = ["sleep", "3"]
         sproc = misc.execCmd(args, sync=False, sudo=False)
         stats = utils.pidStat(sproc.pid)
-        pid = int(stats[0])
+        pid = int(stats.pid)
         # procName comes in the format of (procname)
-        name = stats[1]
+        name = stats.comm
         self.assertEquals(pid, sproc.pid)
         self.assertEquals(name, args[0])
         sproc.kill()
diff --git a/vdsm/storage/blockSD.py b/vdsm/storage/blockSD.py
index e8ce11c..2a831cb 100644
--- a/vdsm/storage/blockSD.py
+++ b/vdsm/storage/blockSD.py
@@ -1119,7 +1119,7 @@
 
         for umountPid in umountPids:
             try:
-                state = utils.pidStat(umountPid)[2]
+                state = utils.pidStat(umountPid).state
                 mountPoint = misc.getCmdArgs(umountPid)[-1]
             except:
                 # Process probably exited
diff --git a/vdsm/storage/misc.py b/vdsm/storage/misc.py
index 37dba7c..a550c44 100644
--- a/vdsm/storage/misc.py
+++ b/vdsm/storage/misc.py
@@ -801,7 +801,7 @@
             continue
 
         try:
-            procName = utils.pidStat(pid)[1]
+            procName = utils.pidStat(pid).comm
             if procName == name:
                 res.append(pid)
         except (OSError, IOError):
@@ -820,7 +820,7 @@
     # Retrying seems to solve it.
     while len(res) == 0:
         # cmdline is empty for zombie processes
-        if utils.pidStat(pid)[2] in ("Z", "z"):
+        if utils.pidStat(pid).state in ("Z", "z"):
             return tuple()
 
         res = _parseCmdLine(pid)


--
To view, visit http://gerrit.ovirt.org/15024
To unsubscribe, visit http://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I7cbac1c9175afbdecf42d40290026d518098e193
Gerrit-PatchSet: 1
Gerrit-Project: vdsm
Gerrit-Branch: master
Gerrit-Owner: Yaniv Bronhaim <ybronhei at redhat.com>


More information about the vdsm-patches mailing list