1 # Copyright (c) 2016 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"""
15 from resources.libraries.python.ssh import SSH
18 __all__ = ["SchedUtils"]
20 class SchedUtils(object):
21 """General class for any linux scheduler related methods/functions."""
24 def set_vpp_scheduling_rr(node):
25 """Set real-time scheduling attributes of VPP worker threads to
26 SCHED_RR with priority 1.
28 :param node: DUT node with running VPP.
30 :raises RuntimeError: Failed to retrieve PID for VPP worker threads.
35 cmd = "cat /proc/`pidof vpp`/task/*/stat | grep -i vpp_wk"\
39 (ret, out, _) = ssh.exec_command_sudo(cmd)
45 print 'Reading VPP worker thread PID failed.'
47 for pid in out.split("\n"):
48 if len(pid) > 0 and pid[0] != "#":
49 SchedUtils.set_proc_scheduling_rr(node, int(pid))
52 raise RuntimeError('Failed to retrieve PID for VPP worker threads.')
55 def set_proc_scheduling_rr(node, pid, priority=1):
56 """Set real-time scheduling of a process to SCHED_RR with priority for
59 :param node: Node where to apply scheduling changes.
60 :param pid: Process ID.
61 :param priority: Realtime priority in range 1-99. Default is 1.
65 :raises ValueError: Parameters out of allowed ranges.
66 :raises RuntimeError: Failed to set policy for PID.
72 raise ValueError("SCHED_RR: PID must be higher then 1.")
74 if 1 <= priority <= 99:
75 cmd = "chrt -r -p {0} {1}".format(priority, pid)
76 (ret, _, _) = ssh.exec_command_sudo(cmd)
78 raise RuntimeError("SCHED_RR: Failed to set policy "\
79 "for PID {0}.".format(pid))
81 raise ValueError("SCHED_RR: Priority must be in range 1-99.")
84 def set_proc_scheduling_other(node, pid):
85 """Set normal scheduling of a process to SCHED_OTHER for specified PID.
87 :param node: Node where to apply scheduling changes.
88 :param pid: Process ID.
91 :raises ValueError: Parameters out of allowed ranges.
92 :raises RuntimeError: Failed to set policy for PID.
98 raise ValueError("SCHED_OTHER: PID must be higher then 1.")
100 cmd = "chrt -o -p 0 {1}".format(pid)
101 (ret, _, _) = ssh.exec_command_sudo(cmd)
103 raise RuntimeError("SCHED_OTHER: Failed to set policy "\
104 "for PID {0}.".format(pid))