From 9a5c9c514d217d2b7c33469c4f29cbea3d6c25c6 Mon Sep 17 00:00:00 2001 From: Clark Williams Date: Fri, 23 Feb 2018 09:55:25 -0600 Subject: [PATCH 1/3] hwlatdetect: initial python3 changes Signed-off-by: Clark Williams --- src/hwlatdetect/hwlatdetect.py | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/src/hwlatdetect/hwlatdetect.py b/src/hwlatdetect/hwlatdetect.py index 5ebd4f1..6d8aeeb 100755 --- a/src/hwlatdetect/hwlatdetect.py +++ b/src/hwlatdetect/hwlatdetect.py @@ -1,5 +1,6 @@ -#!/usr/bin/python +#!/usr/bin/python3 +# (C) 2018 Clark Williams # (C) 2015,2016 Clark Williams # (C) 2009 Clark Williams # @@ -16,7 +17,7 @@ import subprocess import errno import os.path -version = "0.7" +version = "0.8" debugging = False quiet = False watch = False @@ -145,9 +146,9 @@ class Kmod(object): def __init__(self, name): if name not in Kmod.names: - raise RuntimeError, "unsupported module name: %s" % name + raise RuntimeError("unsupported module name: %s" % name) if name == "smi_detector": - raise RuntimeError, "smi_detector module no longer supported!" + raise RuntimeError("smi_detector module no longer supported!") self.name = name self.preloaded = False self.builtin = False @@ -165,7 +166,7 @@ class Kmod(object): debug("using already loaded %s" % self.name) return if not self.__find_module(): - raise DetectorNotAvailable, name, "module %s does not exist!" % self.name + raise DetectorNotAvailable(name, "module %s does not exist!" % self.name) def load(self): if self.builtin: @@ -213,23 +214,23 @@ class Detector(object): return counts def cleanup(self): - raise RuntimeError, "must override base method 'cleanup'!" + raise RuntimeError("must override base method 'cleanup'!") def get(self, field): '''get the value of a debugfs field''' - raise RuntimeError, "must override base method 'get'!" + raise RuntimeError("must override base method 'get'!") def set(self, field, val): '''set a value in a debugfs field''' - raise RuntimeError, "must override base method 'set'!" + raise RuntimeError("must override base method 'set'!") def save(self, reportfile=None): '''save sample data to reportfile''' - raise RuntimeError, "must override base method 'save'!" + raise RuntimeError("must override base method 'save'!") def display(self): '''output the sample data as a string''' - raise RuntimeError, "must override base method 'display'!" + raise RuntimeError("must override base method 'display'!") def start(self): count = 0 @@ -261,7 +262,7 @@ class Detector(object): def detect(self): '''get detector output''' - raise RuntimeError, "must override base method 'detect'!" + raise RuntimeError("must override base method 'detect'!") # # class to handle running the hwlat tracer module of ftrace # @@ -306,7 +307,7 @@ class Tracer(Detector): super(Tracer, self).__init__() path = self.debugfs.getpath('tracing/hwlat_detector') if not os.path.exists(path): - raise DetectorNotAvailable, "hwlat", "hwlat tracer not available" + raise DetectorNotAvailable("hwlat", "hwlat tracer not available") self.type = "tracer" self.samples = [] self.set("enable", 0) @@ -544,7 +545,7 @@ if __name__ == '__main__': else: try: detect = Tracer() - except DetectorNotAvailable, err: + except DetectorNotAvailable as err: detect = HwLat() if o.threshold: @@ -555,8 +556,8 @@ if __name__ == '__main__': if o.hardlimit: hardlimit = microseconds(o.hardlimit) else: - hardlimit = detect.get("threshold") - debug("hardlimit set to %dus" % int(hardlimit)) + hardlimit = int(detect.get("threshold")) + debug("hardlimit set to %dus" % hardlimit) if o.window: w = microseconds(o.window) From 61d21a918b9ce9bac3f63dbfc9bd36f435de2d84 Mon Sep 17 00:00:00 2001 From: Clark Williams Date: Fri, 23 Feb 2018 10:05:55 -0600 Subject: [PATCH 2/3] hwlatdetect: fix str/binary mismatches Signed-off-by: Clark Williams --- src/hwlatdetect/hwlatdetect.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/hwlatdetect/hwlatdetect.py b/src/hwlatdetect/hwlatdetect.py index 6d8aeeb..d3b79a2 100755 --- a/src/hwlatdetect/hwlatdetect.py +++ b/src/hwlatdetect/hwlatdetect.py @@ -561,7 +561,7 @@ if __name__ == '__main__': if o.window: w = microseconds(o.window) - if w < detect.get("width"): + if w < int(detect.get("width")): debug("shrinking width to %d for new window of %d" % (w/2, w)) detect.set("width", w/2) debug("window parameter = %d" % w) @@ -570,7 +570,7 @@ if __name__ == '__main__': if o.width: w = microseconds(o.width) - if w > detect.get("window"): + if w > int(detect.get("window")): debug("widening window to %d for new width of %d" % (w*2, w)) detect.set("window", w*2) debug("width parameter = %d" % w) From 809eed92c1187c5d431a4ba806ab12625be05484 Mon Sep 17 00:00:00 2001 From: Clark Williams Date: Fri, 23 Feb 2018 14:13:57 -0600 Subject: [PATCH 3/3] hwlatdetect: convert from optparse to argparse for python3 Signed-off-by: Clark Williams --- src/hwlatdetect/hwlatdetect.py | 96 +++++++++++++++++----------------- 1 file changed, 48 insertions(+), 48 deletions(-) diff --git a/src/hwlatdetect/hwlatdetect.py b/src/hwlatdetect/hwlatdetect.py index d3b79a2..2c8f9f1 100755 --- a/src/hwlatdetect/hwlatdetect.py +++ b/src/hwlatdetect/hwlatdetect.py @@ -485,62 +485,62 @@ def microseconds(str): # if __name__ == '__main__': - from optparse import OptionParser + from argparse import ArgumentParser - parser = OptionParser() - parser.add_option("--duration", default=None, type="string", - dest="duration", - help="total time to test for hardware latency ({smdw})") + parser = ArgumentParser() + parser.add_argument("--duration", default=None, + dest="duration", + help="total time to test for hardware latency: {smdw}") - parser.add_option("--threshold", default=None, type="string", - dest="threshold", - help="value above which is considered an hardware latency") + parser.add_argument("--threshold", default=None, + dest="threshold", + help="value above which is considered an hardware latency") - parser.add_option("--hardlimit", default=None, type="string", - dest="hardlimit", - help="value above which the test is considered to fail") + parser.add_argument("--hardlimit", default=None, + dest="hardlimit", + help="value above which the test is considered to fail") - parser.add_option("--window", default=None, type="string", - dest="window", - help="time between samples") + parser.add_argument("--window", default=None, + dest="window", + help="time between samples") - parser.add_option("--width", default=None, type="string", - dest="width", - help="time to actually measure") + parser.add_argument("--width", default=None, + dest="width", + help="time to actually measure") - parser.add_option("--report", default=None, type="string", - dest="report", - help="filename for sample data") + parser.add_argument("--report", default=None, + dest="report", + help="filename for sample data") - parser.add_option("--debug", action="store_true", default=False, - dest="debug", - help="turn on debugging prints") + parser.add_argument("--debug", action="store_true", default=False, + dest="debug", + help="turn on debugging prints") - parser.add_option("--quiet", action="store_true", default=False, - dest="quiet", - help="turn off all screen output") + parser.add_argument("--quiet", action="store_true", default=False, + dest="quiet", + help="turn off all screen output") - parser.add_option("--watch", action="store_true", default=False, - dest="watch", - help="print sample data to stdout as it arrives") + parser.add_argument("--watch", action="store_true", default=False, + dest="watch", + help="print sample data to stdout as it arrives") - parser.add_option("--kmodule", action="store_true", default=False, - dest="kmodule", - help="force using the kernel module") + parser.add_argument("--kmodule", action="store_true", default=False, + dest="kmodule", + help="force using the kernel module") - (o, a) = parser.parse_args() + args = parser.parse_args() # need these before creating detector instance - if o.debug: + if args.debug: debugging = True quiet = False debug("debugging prints turned on") - if o.quiet: + if args.quiet: quiet = True debugging = False - if o.kmodule: + if args.kmodule: detect = Hwlat() else: try: @@ -548,19 +548,19 @@ if __name__ == '__main__': except DetectorNotAvailable as err: detect = HwLat() - if o.threshold: - t = microseconds(o.threshold) + if args.threshold: + t = microseconds(args.threshold) detect.set("threshold", t) debug("threshold set to %dus" % t) - if o.hardlimit: - hardlimit = microseconds(o.hardlimit) + if args.hardlimit: + hardlimit = microseconds(args.hardlimit) else: hardlimit = int(detect.get("threshold")) debug("hardlimit set to %dus" % hardlimit) - if o.window: - w = microseconds(o.window) + if args.window: + w = microseconds(args.window) if w < int(detect.get("width")): debug("shrinking width to %d for new window of %d" % (w/2, w)) detect.set("width", w/2) @@ -568,8 +568,8 @@ if __name__ == '__main__': detect.set("window", w) debug("window for sampling set to %dus" % w) - if o.width: - w = microseconds(o.width) + if args.width: + w = microseconds(args.width) if w > int(detect.get("window")): debug("widening window to %d for new width of %d" % (w*2, w)) detect.set("window", w*2) @@ -577,16 +577,16 @@ if __name__ == '__main__': detect.set("width", w) debug("sample width set to %dus" % w) - if o.duration: - detect.testduration = seconds(o.duration) + if args.duration: + detect.testduration = seconds(args.duration) else: detect.testduration = 120 # 2 minutes debug("test duration is %ds" % detect.testduration) - if o.watch: + if args.watch: watch = True - reportfile = o.report + reportfile = args.report info("hwlatdetect: test duration %d seconds" % detect.testduration) info(" detector: %s" % detect.type)