Read clock granularity from the system rather than hardcoded value
authorJean-Michel Nirgal Vourgère <jmv@nirgal.com>
Wed, 9 May 2012 12:41:23 +0000 (12:41 +0000)
committerJean-Michel Nirgal Vourgère <jmv@nirgal.com>
Wed, 9 May 2012 12:41:23 +0000 (12:41 +0000)
bin/proc.py

index 6d30bcf800401ba58960b8a67c7da194ca813b77..309a20d07b59600e14853fd4f6f0c13731ae482b 100644 (file)
@@ -59,14 +59,22 @@ __states__ = {
     'W': 'swapping',
 }
 
+__clk_tck__ = None
 def get_clk_tck():
-    process = subprocess.Popen(['getconf', 'CLK_TCK'], shell=True, stdout=subprocess.PIPE)
-    if process.wait() != 0:
-        raise "getconf CLK_TCK failed"
-    return process[0]
-
+    # clocks per second:
     #= 100 -> 1 tick = 1/100 seconds
-    # require libc-bin to be installed
+    global __clk_tck__
+    if __clk_tck__ is not None:
+        return __clk_tck__
+        
+    # require libc-bin to be installed. Priority is required anyways
+    process = subprocess.Popen(['getconf', 'CLK_TCK'], stdout=subprocess.PIPE)
+    output = process.communicate()[0]
+    retcode = process.returncode
+    if retcode != 0:
+        raise OsError("getconf CLK_TCK failed. subprocess returned " + str(retcode))
+    __clk_tck__ = int(output)
+    return __clk_tck__
 
 class Stat(dict):
     def __init__(self, processid):
@@ -84,13 +92,11 @@ class Stat(dict):
             return state
         return "%s (%s)" % (state, nice_state)
     
-    # TODO: 
-    # subprocess.call(['getconf', 'CLK_TCK']) = 100 -> 1 tick = 1/100 seconds
     def nice_utime(self):
-        return self['utime'] / 100
+        return self['utime'] / get_clk_tck()
 
     def nice_stime(self):
-        return self['stime'] / 100
+        return self['stime'] / get_clk_tck()
 
 if __name__ == '__main__':
-    print get_clk_tck()
+    print "clock granularity is %s" % get_clk_tck()