When users change the priority of a thread, allow them to use various
formats. For example, SCHED_FIFO can be indicated as
SCHED_FIFO, sched_fifo, FIFO or fifo
Create class Policy for this purpose.
The utility functions sched_fromstr and sched_str are left in place for
backwards compatibility and for when a full class is not necessary.
There are certain advantages to using the class though.
- The ability to specify a policy using more formats.
- Functionality such as is_rt() which returns True if a policy is
realtime
- more readable code
Signed-off-by: John Kacur <jkacur(a)redhat.com>
---
tuna/tuna.py | 21 ++++++++++------
tuna/tuna_sched.py | 62 ++++++++++++++++++++++++++++++++++++++++++++--
2 files changed, 73 insertions(+), 10 deletions(-)
diff --git a/tuna/tuna.py b/tuna/tuna.py
index 6b6edcc8ff72..98a1725598dd 100755
--- a/tuna/tuna.py
+++ b/tuna/tuna.py
@@ -506,17 +506,22 @@ def get_policy_and_rtprio(parm):
parms = parm.split(":")
rtprio = 0
policy = None
- if parms[0].upper() in ["OTHER", "BATCH", "IDLE",
"FIFO", "RR"]:
- policy = tuna_sched.sched_fromstr("SCHED_%s" % parms[0].upper())
+
+ try:
+ cp = tuna_sched.Policy(parms[0])
+ except:
+ if parms[0].isdigit():
+ rtprio = int(parms[0])
+ else:
+ raise ValueError
+ else:
+ policy = cp.get_policy()
if len(parms) > 1:
rtprio = int(parms[1])
- elif parms[0].upper() in ["FIFO", "RR"]:
+ elif cp.is_rt():
rtprio = 1
- elif parms[0].isdigit():
- rtprio = int(parms[0])
- else:
- raise ValueError
- return (policy, rtprio)
+ finally:
+ return(policy, rtprio)
def thread_filtered(tid, cpus_filtered, show_kthreads, show_uthreads):
if cpus_filtered:
diff --git a/tuna/tuna_sched.py b/tuna/tuna_sched.py
index fee5f166b004..de9846bb5fae 100644
--- a/tuna/tuna_sched.py
+++ b/tuna/tuna_sched.py
@@ -1,3 +1,5 @@
+#!/usr/bin/python3
+# Copyright (C) 2022 John Kacur
"""
Functions to translate a scheduling policy into either a string name or an
equivalent integer
@@ -13,12 +15,13 @@ SCHED_POLICIES = {
"SCHED_DEADLINE": 6
}
+
def sched_fromstr(policy):
""" Given a policy as a string, return the equivalent integer
"""
try:
return SCHED_POLICIES[policy]
- except:
- raise OSError()
+ except KeyError as exc:
+ raise OSError('No such policy') from exc
def sched_str(policy):
@@ -27,3 +30,58 @@ def sched_str(policy):
if pnum == policy:
return pstr
return "UNKNOWN"
+
+class Policy:
+ """ class to encapsulate some scheduling policies operations
"""
+
+ SCHED_POLICIES = {
+ "SCHED_OTHER": os.SCHED_OTHER,
+ "SCHED_FIFO": os.SCHED_FIFO,
+ "SCHED_RR": os.SCHED_RR,
+ "SCHED_BATCH": os.SCHED_BATCH,
+ "SCHED_IDLE": os.SCHED_IDLE,
+ "SCHED_DEADLINE": 6
+ }
+
+ RT_POLICIES = ["SCHED_FIFO", "SCHED_RR"]
+
+ def __init__(self, policy):
+ """
+ init the class given a policy as a string
+ can use fifo, FIFO, sched_fifo, SCHED_FIFO, etc
+ """
+ self.policy = None
+ policy = policy.upper()
+ if policy[:6] != "SCHED_":
+ policy = "SCHED_" + policy
+ if policy not in list(Policy.SCHED_POLICIES):
+ raise OSError
+ self.policy = policy
+
+ @classmethod
+ def num_init(cls, policy):
+ """ init the class with an integer corresponding to the policy
"""
+ for pstr, pnum in cls.SCHED_POLICIES.items():
+ if policy == pnum:
+ return cls(pstr)
+ return cls("UNKNOWN")
+
+ def __str__(self):
+ """ return the policy in string format """
+ return self.policy
+
+ def get_sched(self):
+ """ return the policy in string format """
+ return self.policy
+
+ def get_short_str(self):
+ """ return the policy in string format, without the SCHED_ part
"""
+ return self.policy[6:]
+
+ def get_policy(self):
+ """ return the integer equivlent of the policy """
+ return SCHED_POLICIES[self.policy]
+
+ def is_rt(self):
+ """ Return True if policy is a realtime policy """
+ return self.policy in Policy.RT_POLICIES
--
2.31.1
Show replies by date