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