aboutsummaryrefslogtreecommitdiffstats
path: root/resources/libraries/python/SchedUtils.py
blob: bb14c29de8116d647ddecfbefe2cde957237f1b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# Copyright (c) 2019 Cisco and/or its affiliates.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at:
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Linux scheduler util library"""

from resources.libraries.python.ssh import SSH

__all__ = [u"SchedUtils"]


class SchedUtils:
    """General class for any linux scheduler related methods/functions."""

    @staticmethod
    def set_vpp_scheduling_rr(node):
        """Set real-time scheduling attributes of VPP worker threads to
        SCHED_RR with priority 1.

        :param node: DUT node with running VPP.
        :type node: dict
        :raises RuntimeError: Failed to retrieve PID for VPP worker threads.
        """
        ssh = SSH()
        ssh.connect(node)

        cmd = u"cat /proc/`pidof vpp`/task/*/stat | grep -i vpp_wk" \
            u" | awk '{print $1}'"

        for _ in range(3):
            ret, out, _ = ssh.exec_command_sudo(cmd)
            if ret == 0:
                try:
                    if not out:
                        raise ValueError
                except ValueError:
                    print(u"Reading VPP worker thread PID failed.")
                else:
                    for pid in out.split(u"\n"):
                        if pid and pid[0] != u"#":
                            SchedUtils.set_proc_scheduling_rr(node, int(pid))
                    break
        else:
            raise RuntimeError(
                u"Failed to retrieve PID for VPP worker threads."
            )

    @staticmethod
    def set_proc_scheduling_rr(node, pid, priority=1):
        """Set real-time scheduling of a process to SCHED_RR with priority for
        specified PID.

        :param node: Node where to apply scheduling changes.
        :param pid: Process ID.
        :param priority: Realtime priority in range 1-99. Default is 1.
        :type node: dict
        :type pid: int
        :type priority: int
        :raises ValueError: Parameters out of allowed ranges.
        :raises RuntimeError: Failed to set policy for PID.
        """
        ssh = SSH()
        ssh.connect(node)

        if pid < 1:
            raise ValueError(u"SCHED_RR: PID must be higher then 1.")

        if 1 <= priority <= 99:
            cmd = f"chrt -r -p {priority} {pid}"
            ret, _, _ = ssh.exec_command_sudo(cmd)
            if ret != 0:
                raise RuntimeError(
                    f"SCHED_RR: Failed to set policy for PID {pid}."
                )
        else:
            raise ValueError(u"SCHED_RR: Priority must be in range 1-99.")

    @staticmethod
    def set_proc_scheduling_other(node, pid):
        """Set normal scheduling of a process to SCHED_OTHER for specified PID.

        :param node: Node where to apply scheduling changes.
        :param pid: Process ID.
        :type node: dict
        :type pid: int
        :raises ValueError: Parameters out of allowed ranges.
        :raises RuntimeError: Failed to set policy for PID.
        """
        ssh = SSH()
        ssh.connect(node)

        if pid < 1:
            raise ValueError(u"SCHED_OTHER: PID must be higher then 1.")

        cmd = f"chrt -o -p 0 {pid}"
        ret, _, _ = ssh.exec_command_sudo(cmd)
        if ret != 0:
            raise RuntimeError(
                f"SCHED_OTHER: Failed to set policy for PID {pid}."
            )