1 # Copyright (c) 2019 Cisco and/or its affiliates.
2 # Licensed under the Apache License, Version 2.0 (the "License");
3 # you may not use this file except in compliance with the License.
4 # You may obtain a copy of the License at:
6 # http://www.apache.org/licenses/LICENSE-2.0
8 # Unless required by applicable law or agreed to in writing, software
9 # distributed under the License is distributed on an "AS IS" BASIS,
10 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 # See the License for the specific language governing permissions and
12 # limitations under the License.
14 """Linux scheduler util library"""
16 from resources.libraries.python.ssh import SSH
18 __all__ = [u"SchedUtils"]
22 """General class for any linux scheduler related methods/functions."""
25 def set_vpp_scheduling_rr(node):
26 """Set real-time scheduling attributes of VPP worker threads to
27 SCHED_RR with priority 1.
29 :param node: DUT node with running VPP.
31 :raises RuntimeError: Failed to retrieve PID for VPP worker threads.
36 cmd = u"cat /proc/`pidof vpp`/task/*/stat | grep -i vpp_wk" \
37 u" | awk '{print $1}'"
40 ret, out, _ = ssh.exec_command_sudo(cmd)
46 print(u"Reading VPP worker thread PID failed.")
48 for pid in out.split(u"\n"):
49 if pid and pid[0] != u"#":
50 SchedUtils.set_proc_scheduling_rr(node, int(pid))
54 u"Failed to retrieve PID for VPP worker threads."
58 def set_proc_scheduling_rr(node, pid, priority=1):
59 """Set real-time scheduling of a process to SCHED_RR with priority for
62 :param node: Node where to apply scheduling changes.
63 :param pid: Process ID.
64 :param priority: Realtime priority in range 1-99. Default is 1.
68 :raises ValueError: Parameters out of allowed ranges.
69 :raises RuntimeError: Failed to set policy for PID.
75 raise ValueError(u"SCHED_RR: PID must be higher then 1.")
77 if 1 <= priority <= 99:
78 cmd = f"chrt -r -p {priority} {pid}"
79 ret, _, _ = ssh.exec_command_sudo(cmd)
82 f"SCHED_RR: Failed to set policy for PID {pid}."
85 raise ValueError(u"SCHED_RR: Priority must be in range 1-99.")
88 def set_proc_scheduling_other(node, pid):
89 """Set normal scheduling of a process to SCHED_OTHER for specified PID.
91 :param node: Node where to apply scheduling changes.
92 :param pid: Process ID.
95 :raises ValueError: Parameters out of allowed ranges.
96 :raises RuntimeError: Failed to set policy for PID.
102 raise ValueError(u"SCHED_OTHER: PID must be higher then 1.")
104 cmd = f"chrt -o -p 0 {pid}"
105 ret, _, _ = ssh.exec_command_sudo(cmd)
108 f"SCHED_OTHER: Failed to set policy for PID {pid}."