Line length: Fix recent merges
[csit.git] / resources / libraries / python / TelemetryUtil.py
1 # Copyright (c) 2021 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.OptionString import OptionString
18 from resources.libraries.python.ssh import exec_cmd, exec_cmd_no_error
19 from resources.libraries.python.topology import NodeType
20
21 __all__ = [u"TelemetryUtil"]
22
23
24 class TelemetryUtil:
25     """Class contains methods for telemetry utility."""
26
27     @staticmethod
28     def perf_stat(node, cpu_list=None, duration=1):
29         """Get perf stat read for duration.
30
31         :param node: Node in the topology.
32         :param cpu_list: CPU List as a string separated by comma.
33         :param duration: Measure time in seconds.
34         :type node: dict
35         :type cpu_list: str
36         :type duration: int
37         """
38         if cpu_list:
39             cpu_list = list(dict.fromkeys(cpu_list.split(u",")))
40             cpu_list = ",".join(str(cpu) for cpu in cpu_list)
41
42         cmd_opts = OptionString(prefix=u"--")
43         cmd_opts.add(u"no-aggr")
44         cmd_opts.add_with_value_if(
45             u"cpu", cpu_list, cpu_list
46         )
47         cmd_opts.add_if(
48             u"all-cpus", not(cpu_list)
49         )
50         cmd_opts.add_with_value_if(
51             u"event", f"'{{{Constants.PERF_STAT_EVENTS}}}'",
52             Constants.PERF_STAT_EVENTS
53         )
54         cmd_opts.add_with_value(
55             u"interval-print", 1000
56         )
57         cmd_opts.add_with_value(
58             u"field-separator", u"';'"
59         )
60
61         cmd_base = OptionString()
62         cmd_base.add(f"perf stat")
63         cmd_base.extend(cmd_opts)
64         cmd_base.add(u"--")
65         cmd_base.add_with_value(u"sleep", int(duration))
66
67         exec_cmd(node, cmd_base, sudo=True)
68
69     @staticmethod
70     def perf_stat_on_all_duts(nodes, cpu_list=None, duration=1):
71         """Get perf stat read for duration on all DUTs.
72
73         :param nodes: Nodes in the topology.
74         :param cpu_list: CPU List.
75         :param duration: Measure time in seconds.
76         :type nodes: dict
77         :type cpu_list: str
78         :type duration: int
79         """
80         for node in nodes.values():
81             if node[u"type"] == NodeType.DUT:
82                 TelemetryUtil.perf_stat(
83                     node, cpu_list=cpu_list, duration=duration
84                 )
85
86     @staticmethod
87     def run_telemetry(node, profile, hook=None):
88         """Get telemetry stat read for duration.
89
90         :param node: Node in the topology.
91         :param profile: Telemetry configuration profile.
92         :param hook: Process ID or socket path (optional).
93         :type node: dict
94         :type profile: str
95         :type hook: str
96         """
97         config = u""
98         config += f"{Constants.REMOTE_FW_DIR}/"
99         config += f"{Constants.RESOURCES_TPL_TELEMETRY}/"
100         config += f"{profile}"
101
102         cd_cmd = u""
103         cd_cmd += f"sh -c \"cd {Constants.REMOTE_FW_DIR}/"
104         cd_cmd += f"{Constants.RESOURCES_TOOLS}"
105
106         bin_cmd = f"python3 -m telemetry --config {config} --hook {hook}\""
107
108         exec_cmd_no_error(node, f"{cd_cmd} && {bin_cmd}", sudo=True)
109         exec_cmd_no_error(node, f"cat /tmp/metric.prom", sudo=True)
110
111     @staticmethod
112     def run_telemetry_on_all_duts(nodes, profile):
113         """Get telemetry stat read on all DUTs.
114
115         :param nodes: Nodes in the topology.
116         :param profile: Telemetry configuration profile.
117         :param hooks: Dict of Process IDs or socket paths (optional).
118         :type nodes: dict
119         :type profile: str
120         :type hooks: dict
121         """
122         for node in nodes.values():
123             if node[u"type"] == NodeType.DUT:
124                 try:
125                     for socket in node[u"sockets"][u"PAPI"].values():
126                         TelemetryUtil.run_telemetry(
127                             node, profile=profile, hook=socket
128                         )
129                 except IndexError:
130                     pass