diff options
Diffstat (limited to 'rteval/sysinfo')
-rw-r--r-- | rteval/sysinfo/__init__.py | 40 | ||||
-rw-r--r-- | rteval/sysinfo/memory.py | 82 |
2 files changed, 88 insertions, 34 deletions
diff --git a/rteval/sysinfo/__init__.py b/rteval/sysinfo/__init__.py index 6e35743..01bc66b 100644 --- a/rteval/sysinfo/__init__.py +++ b/rteval/sysinfo/__init__.py @@ -25,15 +25,18 @@ # import ethtool, os, shutil, subprocess +import sys +sys.path.append(".") from Log import Log from glob import glob from kernel import KernelInfo from services import SystemServices from cputopology import CPUtopology +from memory import MemoryInfo import dmi -class SystemInfo(KernelInfo, SystemServices, dmi.DMIinfo, CPUtopology): +class SystemInfo(KernelInfo, SystemServices, dmi.DMIinfo, CPUtopology, MemoryInfo): def __init__(self, config, logger=None): self.__logger = logger KernelInfo.__init__(self, logger=logger) @@ -53,37 +56,6 @@ class SystemInfo(KernelInfo, SystemServices, dmi.DMIinfo, CPUtopology): self.__logger.log(logtype, msg) - def get_num_nodes(self): - nodes = len(glob('/sys/devices/system/node/node*')) - return nodes - - - def get_memory_size(self): - '''find out how much memory is installed''' - f = open('/proc/meminfo') - rawsize = 0 - for l in f: - if l.startswith('MemTotal:'): - parts = l.split() - if parts[2].lower() != 'kb': - raise RuntimeError, "Units changed from kB! (%s)" % parts[2] - rawsize = int(parts[1]) - f.close() - break - if rawsize == 0: - raise RuntimeError, "can't find memtotal in /proc/meminfo!" - - # Get a more readable result - # Note that this depends on /proc/meminfo starting in Kb - units = ('KB', 'MB','GB','TB') - size = rawsize - for unit in units: - if size < 1024: - break - size = float(size) / 1024 - return (size, unit) - - def get_base_os(self): '''record what userspace we're running on''' distro = "unknown" @@ -145,8 +117,8 @@ if __name__ == "__main__": si = SystemInfo(cfg, logger=l) print "\tRunning on %s" % si.get_base_os() - print "\tNUMA nodes: %d" % si.get_num_nodes() - print "\tMemory available: %03.2f %s\n" % si.get_memory_size() + print "\tNUMA nodes: %d" % si.mem_get_numa_nodes() + print "\tMemory available: %03.2f %s\n" % si.mem_get_size() print "\tServices: " for (s, r) in si.services_get().items(): diff --git a/rteval/sysinfo/memory.py b/rteval/sysinfo/memory.py new file mode 100644 index 0000000..60d0d6a --- /dev/null +++ b/rteval/sysinfo/memory.py @@ -0,0 +1,82 @@ +# -*- coding: utf-8 -*- +#!/usr/bin/python -tt +# +# Copyright 2009-2012 Clark Williams <williams@redhat.com> +# Copyright 2012 David Sommerseth <davids@redhat.com> +# +# 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; either version 2 of the License, or +# (at your option) any later version. +# +# 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +# +# For the avoidance of doubt the "preferred form" of this code is one which +# is in an open unpatent encumbered format. Where cryptographic key signing +# forms part of the process of creating an executable the information +# including keys needed to generate an equivalently functional executable +# are deemed to be part of the source code. +# + +from glob import glob + +class MemoryInfo(object): + numa_nodes = None + + def __init__(self): + pass + + + def mem_get_numa_nodes(self): + if self.numa_nodes is None: + self.numa_nodes = len(glob('/sys/devices/system/node/node*')) + return self.numa_nodes + + + def mem_get_size(self): + '''find out how much memory is installed''' + f = open('/proc/meminfo') + rawsize = 0 + for l in f: + if l.startswith('MemTotal:'): + parts = l.split() + if parts[2].lower() != 'kb': + raise RuntimeError, "Units changed from kB! (%s)" % parts[2] + rawsize = int(parts[1]) + f.close() + break + if rawsize == 0: + raise RuntimeError, "can't find memtotal in /proc/meminfo!" + + # Get a more readable result + # Note that this depends on /proc/meminfo starting in Kb + units = ('KB', 'MB','GB','TB') + size = rawsize + for unit in units: + if size < (1024*1024): + break + size = float(size) / 1024 + return (size, unit) + + +def unit_test(rootdir): + import sys + try: + mi = MemoryInfo() + print "Numa nodes: %i" % mi.mem_get_numa_nodes() + print "Memory: %i %s" % mi.mem_get_size() + except Exception, e: + import traceback + traceback.print_exc(file=sys.stdout) + print "** EXCEPTION %s", str(e) + return 1 + +if __name__ == '__main__': + unit_test(None) |