From: Jean-Michel Nirgal Vourgère Date: Wed, 9 May 2012 12:41:23 +0000 (+0000) Subject: Read clock granularity from the system rather than hardcoded value X-Git-Url: https://git.nirgal.com/?p=ais.git;a=commitdiff_plain;h=9812c3d16a9a65c6df49a1ce56537e15fa65cfd0;hp=88cea8e71750209510b3811f25ad456fe86bc487 Read clock granularity from the system rather than hardcoded value --- diff --git a/bin/proc.py b/bin/proc.py index 6d30bcf..309a20d 100644 --- a/bin/proc.py +++ b/bin/proc.py @@ -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()