Python3: resources and libraries
[csit.git] / resources / libraries / python / SchedUtils.py
index b4c5721..bb14c29 100644 (file)
@@ -1,4 +1,4 @@
-# Copyright (c) 2016 Cisco and/or its affiliates.
+# Copyright (c) 2019 Cisco and/or its affiliates.
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.
 # You may obtain a copy of the License at:
 # limitations under the License.
 
 """Linux scheduler util library"""
+
 from resources.libraries.python.ssh import SSH
 
+__all__ = [u"SchedUtils"]
 
-__all__ = ["SchedUtils"]
 
-class SchedUtils(object):
+class SchedUtils:
     """General class for any linux scheduler related methods/functions."""
 
     @staticmethod
@@ -32,24 +33,26 @@ class SchedUtils(object):
         ssh = SSH()
         ssh.connect(node)
 
-        cmd = "cat /proc/`pidof vpp`/task/*/stat | grep -i vpp_wk"\
-            " | awk '{print $1}'"
+        cmd = u"cat /proc/`pidof vpp`/task/*/stat | grep -i vpp_wk" \
+            u" | awk '{print $1}'"
 
         for _ in range(3):
-            (ret, out, _) = ssh.exec_command_sudo(cmd)
+            ret, out, _ = ssh.exec_command_sudo(cmd)
             if ret == 0:
                 try:
                     if not out:
                         raise ValueError
                 except ValueError:
-                    print 'Reading VPP worker thread PID failed.'
+                    print(u"Reading VPP worker thread PID failed.")
                 else:
-                    for pid in out.split("\n"):
-                        if pid and pid[0] != '#':
+                    for pid in out.split(u"\n"):
+                        if pid and pid[0] != u"#":
                             SchedUtils.set_proc_scheduling_rr(node, int(pid))
                     break
         else:
-            raise RuntimeError('Failed to retrieve PID for VPP worker threads.')
+            raise RuntimeError(
+                u"Failed to retrieve PID for VPP worker threads."
+            )
 
     @staticmethod
     def set_proc_scheduling_rr(node, pid, priority=1):
@@ -69,16 +72,17 @@ class SchedUtils(object):
         ssh.connect(node)
 
         if pid < 1:
-            raise ValueError("SCHED_RR: PID must be higher then 1.")
+            raise ValueError(u"SCHED_RR: PID must be higher then 1.")
 
         if 1 <= priority <= 99:
-            cmd = "chrt -r -p {0} {1}".format(priority, pid)
-            (ret, _, _) = ssh.exec_command_sudo(cmd)
+            cmd = f"chrt -r -p {priority} {pid}"
+            ret, _, _ = ssh.exec_command_sudo(cmd)
             if ret != 0:
-                raise RuntimeError("SCHED_RR: Failed to set policy "\
-                    "for PID {0}.".format(pid))
+                raise RuntimeError(
+                    f"SCHED_RR: Failed to set policy for PID {pid}."
+                )
         else:
-            raise ValueError("SCHED_RR: Priority must be in range 1-99.")
+            raise ValueError(u"SCHED_RR: Priority must be in range 1-99.")
 
     @staticmethod
     def set_proc_scheduling_other(node, pid):
@@ -95,10 +99,11 @@ class SchedUtils(object):
         ssh.connect(node)
 
         if pid < 1:
-            raise ValueError("SCHED_OTHER: PID must be higher then 1.")
+            raise ValueError(u"SCHED_OTHER: PID must be higher then 1.")
 
-        cmd = "chrt -o -p 0 {1}".format(pid)
-        (ret, _, _) = ssh.exec_command_sudo(cmd)
+        cmd = f"chrt -o -p 0 {pid}"
+        ret, _, _ = ssh.exec_command_sudo(cmd)
         if ret != 0:
-            raise RuntimeError("SCHED_OTHER: Failed to set policy "\
-                    "for PID {0}.".format(pid))
+            raise RuntimeError(
+                f"SCHED_OTHER: Failed to set policy for PID {pid}."
+            )