T-Rex: 2.82, core pin, 8 workers
[csit.git] / resources / libraries / python / PerfUtil.py
1 # Copyright (c) 2020 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 perf utility."""
15
16 from resources.libraries.python.Constants import Constants
17 from resources.libraries.python.ssh import exec_cmd
18 from resources.libraries.python.topology import NodeType
19
20 __all__ = [u"PerfUtil"]
21
22
23 class PerfUtil:
24     """Class contains methods for perf utility."""
25
26     @staticmethod
27     def perf_stat(node, cpu_list=None, duration=1):
28         """Get perf stat read for duration.
29
30         :param node: Node in the topology.
31         :param cpu_list: CPU List.
32         :param duration: Measure time in seconds.
33         :type node: dict
34         :type cpu_list: str
35         :type duration: int
36         """
37         cpu = cpu_list if cpu_list else u"0-$(($(nproc) - 1))"
38         if Constants.PERF_STAT_EVENTS:
39             command = (
40                 u"perf stat"
41                 f" --cpu {cpu} --no-aggr"
42                 f" --event '{{{Constants.PERF_STAT_EVENTS}}}'"
43                 f" --interval-print 1000 "
44                 f" -- sleep {int(duration)}"
45             )
46         else:
47             command = (
48                 u"perf stat"
49                 f" --cpu {cpu} --no-aggr"
50                 f" --interval-print 1000 "
51                 f" -- sleep {int(duration)}"
52             )
53         exec_cmd(node, command, sudo=True)
54
55     @staticmethod
56     def perf_stat_on_all_duts(nodes, cpu_list=None, duration=1):
57         """Get perf stat read for duration on all DUTs.
58
59         :param nodes: Nodes in the topology.
60         :param cpu_list: CPU List.
61         :param duration: Measure time in seconds.
62         :type nodes: dict
63         :type cpu_list: str
64         :type duration: int
65         """
66         for node in nodes.values():
67             if node[u"type"] == NodeType.DUT:
68                 PerfUtil.perf_stat(node, cpu_list=cpu_list, duration=duration)