VAT-to-PAPI: VPPCounters
[csit.git] / resources / libraries / python / LimitUtil.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 limit library."""
15
16 from resources.libraries.python.ssh import exec_cmd_no_error
17
18 __all__ = ["LimitUtil"]
19
20
21 class LimitUtil(object):
22     """Class contains methods for getting or setting process resource limits."""
23
24     @staticmethod
25     def get_pid_limit(node, pid):
26         """Get process resource limits.
27
28         :param node: Node in the topology.
29         :param pid: Process ID.
30         :type node: dict
31         :type pid: int
32         """
33         command = 'prlimit --noheadings --pid={pid}'.format(pid=pid)
34
35         message = 'Node {host} failed to run: {command}'.\
36             format(host=node['host'], command=command)
37
38         exec_cmd_no_error(node, command, sudo=True, message=message)
39
40     @staticmethod
41     def set_pid_limit(node, pid, resource, limit):
42         """Set process resource limits.
43
44         :param node: Node in the topology.
45         :param pid: Process ID.
46         :param resource: Resource to set limits.
47         :param limit: Limit value.
48         :type node: dict
49         :type pid: int
50         :type resource: str
51         :type limit: str
52         """
53         command = 'prlimit --{resource}={limit} --pid={pid}'.format(
54             resource=resource, limit=limit, pid=pid)
55
56         message = 'Node {host} failed to run: {command}'.\
57             format(host=node['host'], command=command)
58
59         exec_cmd_no_error(node, command, sudo=True, message=message)
60