Skip to contentSkip to conversation

rteval: rteval-cmd: Use "with" with open

PATCH In review John Kacur 2 participants 8 messages Jun 3, 2022 – Jun 6, 2022

lore
Share & tools
lore

John Kacur

author
#0 lore raw

PATCH 1/7 rteval: rteval-cmd: Use "with" with open

Use "with" for resource allocating operation open

---
 rteval-cmd | 12 +++++-------
 1 file changed, 5 insertions(+), 7 deletions(-)
rteval-cmd +5 -7
index 2c775ee52f69..4598ba514ddc 100755
@@ -72,15 +72,13 @@ def summarize(repfile, xslt):
         isarchive = True
 
     # Load the XSLT template
-    xsltfp = open(xslt, "r")
-    xsltdoc = lxml.etree.parse(xsltfp)
-    xsltprs = lxml.etree.XSLT(xsltdoc)
-    xsltfp.close()
+    with open(xslt, "r") as xsltfp:
+        xsltdoc = lxml.etree.parse(xsltfp)
+        xsltprs = lxml.etree.XSLT(xsltdoc)
 
     # Load the summay.xml report - with some simple sanity checks
-    xmlfp = open(summaryfile, "r")
-    xmldoc = lxml.etree.parse(xmlfp)
-    xmlfp.close()
+    with open(summaryfile, "r") as xmlfp:
+        xmldoc = lxml.etree.parse(xmlfp)
 
     if xmldoc.docinfo.root_name != 'rteval':
         raise RuntimeError("The report doesn't seem like a rteval summary report")
Signature · 1 line
2.36.1
author
#1 lore raw

PATCH 2/7 rteval: misc.py: Use "with" for resource allocation

- Use "with" for resource allocation.
- Add docstrings to the module and to functions

---
 rteval/misc.py | 28 +++++++++++++++++-----------
 1 file changed, 17 insertions(+), 11 deletions(-)
rteval/misc.py +17 -11
index 6fdb24261bec..a7c515b0d293 100755
@@ -15,6 +15,7 @@
 # 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.
+''' Functions for operating on a cpulists '''
 
 
 import os
@@ -36,6 +37,7 @@ def expand_cpulist(cpulist):
     return [str(i) for i in list(set(result))]
 
 def online_cpus():
+    ''' Collapse a list of cpu numbers into a string range '''
     online_cpus = []
     # Check for the online file with cpu1 since cpu0 can't always be offlined
     if os.path.exists('/sys/devices/system/cpu/cpu1/online'):
@@ -56,27 +58,31 @@ def online_cpus():
     return online_cpus
 
 def invert_cpulist(cpulist):
+    ''' return a list of online cpus not in cpulist '''
     return [c for c in online_cpus() if c not in cpulist]
 
 def compress_cpulist(cpulist):
+    ''' return a string representation of cpulist '''
     if isinstance(cpulist[0], int):
         return ",".join(str(e) for e in cpulist)
     return ",".join(cpulist)
 
 def cpuinfo():
+    ''' return a dictionary of cpu keys with various cpu information '''
     core = -1
     info = {}
-    for l in open('/proc/cpuinfo'):
-        l = l.strip()
-        if not l:
-            continue
-        # Split a maximum of one time. In case a model name has ':' in it
-        key, val = [i.strip() for i in l.split(':', 1)]
-        if key == 'processor':
-            core = val
-            info[core] = {}
-            continue
-        info[core][key] = val
+    with open('/proc/cpuinfo') as fp:
+        for l in fp:
+            l = l.strip()
+            if not l:
+                continue
+            # Split a maximum of one time. In case a model name has ':' in it
+            key, val = [i.strip() for i in l.split(':', 1)]
+            if key == 'processor':
+                core = val
+                info[core] = {}
+                continue
+            info[core][key] = val
 
     for (core, pcdict) in info.items():
         if not 'model name' in pcdict:
Signature · 1 line
2.36.1
author
#2 lore raw

PATCH 3/7 rteval: stressng.py: Remove unnecessary list

In reply to John Kacur

Remove unnecessary word list around cpus.items()

---
 rteval/modules/loads/stressng.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
rteval/modules/loads/stressng.py +1 -1
index d084814142fb..d0772ce77662 100644
@@ -69,7 +69,7 @@ class Stressng(CommandLineLoad):
                 cpus[n] = [c for c in cpus[n] if str(c) in expand_cpulist(self.cpulist)]
 
         # remove nodes with no cpus available for running
-        for node, cpu in list(cpus.items()):
+        for node, cpu in cpus.items():
             if not cpu:
                 nodes.remove(node)
                 self._log(Log.DEBUG, "node %s has no available cpus, removing" % node)
Signature · 1 line
2.36.1
author
#3 lore raw

PATCH 4/7 rteval: cyclictest.py: Use "with" with resource allocation

In reply to John Kacur

- Use "with" with open
- Add a module docstring

---
 rteval/modules/measurement/cyclictest.py | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)
rteval/modules/measurement/cyclictest.py +8 -8
index 8fdd5341794a..4c8d510c4a34 100644
@@ -24,6 +24,7 @@
 #   including keys needed to generate an equivalently functional executable
 #   are deemed to be part of the source code.
 #
+""" cyclictest.py - object to manage a cyclictest executable instance """
 
 import os
 import subprocess
@@ -254,14 +255,13 @@ class Cyclictest(rtevalModulePrototype):
     @staticmethod
     def __get_debugfs_mount():
         ret = None
-        mounts = open('/proc/mounts')
-        for l in mounts:
-            field = l.split()
-            if field[2] == "debugfs":
-                ret = field[1]
-                break
-        mounts.close()
-        return ret
+        with open('/proc/mounts') as mounts:
+            for l in mounts:
+                field = l.split()
+                if field[2] == "debugfs":
+                    ret = field[1]
+                    break
+            return ret
 
 
     def _WorkloadSetup(self):
Signature · 1 line
2.36.1
author
#4 lore raw

PATCH 5/7 rteval: systopology.py: Use "with" for resource allocation

In reply to John Kacur

- Use "with" with open

---
 rteval/systopology.py | 30 ++++++++++++++++--------------
 1 file changed, 16 insertions(+), 14 deletions(-)
rteval/systopology.py +16 -14
index b2da7bba7de0..e852f86e450f 100644
@@ -180,13 +180,14 @@ class NumaNode:
     def getmeminfo(self):
         """ read info about memory attached to this node """
         self.meminfo = {}
-        for l in open(os.path.join(self.path, "meminfo"), "r"):
-            elements = l.split()
-            key = elements[2][0:-1]
-            val = int(elements[3])
-            if len(elements) == 5 and elements[4] == "kB":
-                val *= 1024
-            self.meminfo[key] = val
+        with open(os.path.join(self.path, "meminfo"), "r") as fp:
+            for l in fp:
+                elements = l.split()
+                key = elements[2][0:-1]
+                val = int(elements[3])
+                if len(elements) == 5 and elements[4] == "kB":
+                    val *= 1024
+                self.meminfo[key] = val
 
     def getcpustr(self):
         """ return list of cpus for this node as a string """
@@ -212,13 +213,14 @@ class SimNumaNode(NumaNode):
 
     def getmeminfo(self):
         self.meminfo = {}
-        for l in open(SimNumaNode.mempath, "r"):
-            elements = l.split()
-            key = elements[0][0:-1]
-            val = int(elements[1])
-            if len(elements) == 3 and elements[2] == "kB":
-                val *= 1024
-            self.meminfo[key] = val
+        with open(SimNumaNode.mempath, "r") as fp:
+            for l in fp:
+                elements = l.split()
+                key = elements[0][0:-1]
+                val = int(elements[1])
+                if len(elements) == 3 and elements[2] == "kB":
+                    val *= 1024
+                self.meminfo[key] = val
 
 #
 # Class to abstract the system topology of numa nodes and cpus
Signature · 1 line
2.36.1
author
#5 lore raw

PATCH 6/7 rteval: kcompile: Fix regular expression to match kernel prefix

In reply to John Kacur

If the user specifies a kernel to compile as a load other than the default kernel, the kernel prefix is obtained with a regular expression.

Currently the regular expression does not accomodate two digit numbers in the kernel version.
Fix that regular expression to accomodate different kernel versions, with room to grow for the future.

---
 rteval/modules/loads/kcompile.py | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
rteval/modules/loads/kcompile.py +2 -1
index 2701a0dcba91..3d9b882d8810 100644
@@ -169,7 +169,7 @@ class Kcompile(CommandLineLoad):
             if not os.path.exists(tarfile):
                 raise rtevalRuntimeError(self, " tarfile %s does not exist!" % tarfile)
             self.source = tarfile
-            kernel_prefix = re.search(r"linux-\d\.\d", self.source).group(0)
+            kernel_prefix = re.search(r"linux-\d{1,2}\.\d{1,3}", self.source).group(0)
         else:
             tarfiles = glob.glob(os.path.join(self.srcdir, "%s*" % DEFAULT_KERNEL_PREFIX))
             if tarfiles:
@@ -177,6 +177,7 @@ class Kcompile(CommandLineLoad):
             else:
                 raise rtevalRuntimeError(self, " no kernel tarballs found in %s" % self.srcdir)
             kernel_prefix = DEFAULT_KERNEL_PREFIX
+        self._log(Log.DEBUG, f"kernel_prefix = {kernel_prefix}")
 
         # check for existing directory
         kdir = None
Signature · 1 line
2.36.1
#6 lore raw

Re: [PATCH 6/7] rteval: kcompile: Fix regular expression to match kernel prefix

rteval/modules/loads/kcompile.py class Kcompile(CommandLineLoad): · line 172

Quoted 6 lines · John Kacur
rteval/modules/loads/kcompile.py +1 -1
@@ -169,7 +169,7 @@ class Kcompile(CommandLineLoad):
             if not os.path.exists(tarfile):
                 raise rtevalRuntimeError(self, " tarfile %s does not exist!" % tarfile)
             self.source = tarfile
-            kernel_prefix = re.search(r"linux-\d\.\d", self.source).group(0)
+            kernel_prefix = re.search(r"linux-\d{1,2}\.\d{1,3}", self.source).group(0)

Sorry about that one!
That looks OK to me, though why not go for "linux-\d+\.\d+" ?

author
#7 lore raw

PATCH 7/7 rteval: stressng: The stressng option should not be a string

In reply to John Kacur

When rteval passes stressng a timeout option, it should not be stringified.

---
 rteval/modules/loads/stressng.py | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
rteval/modules/loads/stressng.py +2 -1
index d0772ce77662..fe97189d3816 100644
@@ -54,7 +54,8 @@ class Stressng(CommandLineLoad):
         if self.cfg.arg is not None:
             self.args.append(self.cfg.arg)
         if self.cfg.timeout is not None:
-            self.args.append('--timeout %s' % str(self.cfg.timeout))
+            self.args.append('--timeout')
+            self.args.append(self.cfg.timeout)
 
         systop = SysTopology()
         # get the number of nodes
Signature · 1 line
2.36.1