FIX: Remove old restart sequence - Honeycomb
[csit.git] / resources / libraries / python / SchedUtils.py
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:
5 #
6 #     http://www.apache.org/licenses/LICENSE-2.0
7 #
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.
13
14 """Linux scheduler util library"""
15 from resources.libraries.python.ssh import SSH
16
17
18 __all__ = ["SchedUtils"]
19
20 class SchedUtils(object):
21     """General class for any linux scheduler related methods/functions."""
22
23     @staticmethod
24     def set_vpp_scheduling_rr(node):
25         """Set real-time scheduling attributes of VPP worker threads to
26         SCHED_RR with priority 1.
27
28         :param node: DUT node with running VPP.
29         :type node: dict
30         :raises RuntimeError: Failed to retrieve PID for VPP worker threads.
31         """
32         ssh = SSH()
33         ssh.connect(node)
34
35         cmd = "cat /proc/`pidof vpp`/task/*/stat | grep -i vpp_wk"\
36             " | awk '{print $1}'"
37
38         for _ in range(3):
39             (ret, out, _) = ssh.exec_command_sudo(cmd)
40             if ret == 0:
41                 try:
42                     if not out:
43                         raise ValueError
44                 except ValueError:
45                     print 'Reading VPP worker thread PID failed.'
46                 else:
47                     for pid in out.split("\n"):
48                         if pid and pid[0] != '#':
49                             SchedUtils.set_proc_scheduling_rr(node, int(pid))
50                     break
51         else:
52             raise RuntimeError('Failed to retrieve PID for VPP worker threads.')
53
54     @staticmethod
55     def set_proc_scheduling_rr(node, pid, priority=1):
56         """Set real-time scheduling of a process to SCHED_RR with priority for
57         specified PID.
58
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.
62         :type node: dict
63         :type pid: int
64         :type priority: int
65         :raises ValueError: Parameters out of allowed ranges.
66         :raises RuntimeError: Failed to set policy for PID.
67         """
68         ssh = SSH()
69         ssh.connect(node)
70
71         if pid < 1:
72             raise ValueError("SCHED_RR: PID must be higher then 1.")
73
74         if 1 <= priority <= 99:
75             cmd = "chrt -r -p {0} {1}".format(priority, pid)
76             (ret, _, _) = ssh.exec_command_sudo(cmd)
77             if ret != 0:
78                 raise RuntimeError("SCHED_RR: Failed to set policy "\
79                     "for PID {0}.".format(pid))
80         else:
81             raise ValueError("SCHED_RR: Priority must be in range 1-99.")
82
83     @staticmethod
84     def set_proc_scheduling_other(node, pid):
85         """Set normal scheduling of a process to SCHED_OTHER for specified PID.
86
87         :param node: Node where to apply scheduling changes.
88         :param pid: Process ID.
89         :type node: dict
90         :type pid: int
91         :raises ValueError: Parameters out of allowed ranges.
92         :raises RuntimeError: Failed to set policy for PID.
93         """
94         ssh = SSH()
95         ssh.connect(node)
96
97         if pid < 1:
98             raise ValueError("SCHED_OTHER: PID must be higher then 1.")
99
100         cmd = "chrt -o -p 0 {1}".format(pid)
101         (ret, _, _) = ssh.exec_command_sudo(cmd)
102         if ret != 0:
103             raise RuntimeError("SCHED_OTHER: Failed to set policy "\
104                     "for PID {0}.".format(pid))