aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorClark Williams <williams@redhat.com>2015-06-09 13:00:15 -0500
committerClark Williams <williams@redhat.com>2015-06-10 11:49:48 -0500
commita1a0ce0582abd9a4391452704ed9dbfe9bb533c6 (patch)
tree4d47e593c3186650cc7ae59ce0a83b51d381dfc5
parentd604cda55824db23cf7a9889077227fcb334e1da (diff)
downloadrteval-a1a0ce0582abd9a4391452704ed9dbfe9bb533c6.tar.gz
rteval: add miscellaneous global helper functions
Add helper function in misc.py: expand_cpulist() - expands a string cpu range into a list of cpu numbers (make sure this is a list of string values) online_cpus() - return a list of online cpu numbers cpuinfo() - return a dictionary of dictionaries where the first index is the core number (as a string) and the second is the property defined under that core in the /proc/cpuinfo file. Signed-off-by: Clark Williams <williams@redhat.com>
-rw-r--r--rteval/misc.py63
1 files changed, 63 insertions, 0 deletions
diff --git a/rteval/misc.py b/rteval/misc.py
new file mode 100644
index 0000000..062355a
--- /dev/null
+++ b/rteval/misc.py
@@ -0,0 +1,63 @@
+#!/usr/bin/python -tt
+#
+# Copyright (C) 2015 Clark Williams <clark.williams@gmail.com>
+# Copyright (C) 2015 Red Hat, Inc.
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; version 2 of the License.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+
+
+import os
+import glob
+
+# expand a string range into a list
+# don't error check against online cpus
+def expand_cpulist(cpulist):
+ '''expand a range string into an array of cpu numbers'''
+ result = []
+ for part in cpulist.split(','):
+ if '-' in part:
+ a, b = part.split('-')
+ a, b = int(a), int(b)
+ result.extend(range(a, b + 1))
+ else:
+ a = int(part)
+ result.append(a)
+ return [ str(i) for i in list(set(result)) ]
+
+def online_cpus():
+ return [ str(c.replace('/sys/devices/system/cpu/cpu', '')) for c in glob.glob('/sys/devices/system/cpu/cpu[0-9]*') ]
+
+def cpuinfo():
+ core = -1
+ info = {}
+ for l in open('/proc/cpuinfo'):
+ l = l.strip()
+ if not l: continue
+ key,val = [ i.strip() for i in l.split(':')]
+ if key == 'processor':
+ core = val
+ info[core] = {}
+ continue
+ info[core][key] = val
+ return info
+
+if __name__ == "__main__":
+
+ info = cpuinfo()
+ idx = info.keys()
+ idx.sort()
+ for i in idx:
+ print "%s: %s" % (i, info[i])
+
+ print "0: %s" % (info['0']['model name'])