Dpdk in VM: Increase num_mbufs
[csit.git] / resources / libraries / python / VppCounters.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 """VPP counters utilities library."""
15
16 from pprint import pformat
17
18 from robot.api import logger
19
20 from resources.libraries.python.PapiExecutor import PapiExecutor, \
21     PapiSocketExecutor
22 from resources.libraries.python.topology import Topology, SocketType, NodeType
23
24
25 class VppCounters:
26     """VPP counters utilities."""
27
28     def __init__(self):
29         self._stats_table = None
30
31     @staticmethod
32     def vpp_show_errors(node):
33         """Run "show errors" debug CLI command.
34
35         :param node: Node to run command on.
36         :type node: dict
37         """
38         PapiSocketExecutor.run_cli_cmd_on_all_sockets(node, u"show errors")
39
40     @staticmethod
41     def vpp_show_errors_on_all_duts(nodes):
42         """Show errors on all DUTs.
43
44         :param nodes: VPP nodes.
45         :type nodes: dict
46         """
47         for node in nodes.values():
48             if node[u"type"] == NodeType.DUT:
49                 VppCounters.vpp_show_errors(node)
50
51     @staticmethod
52     def vpp_show_runtime(node, log_zeros=False):
53         """Run "show runtime" CLI command.
54
55         :param node: Node to run command on.
56         :param log_zeros: Log also items with zero values.
57         :type node: dict
58         :type log_zeros: bool
59         """
60         args = dict(path=u"^/sys/node")
61         sockets = Topology.get_node_sockets(node, socket_type=SocketType.STATS)
62         if sockets:
63             for socket in sockets.values():
64                 with PapiExecutor(node) as papi_exec:
65                     stats = papi_exec.add(u"vpp-stats", **args).\
66                         get_stats(socket=socket)[0]
67
68                 names = stats[u"/sys/node/names"]
69
70                 if not names:
71                     return
72
73                 runtime = list()
74                 runtime_nz = list()
75
76                 for name in names:
77                     runtime.append({u"name": name})
78
79                 for idx, runtime_item in enumerate(runtime):
80
81                     calls_th = []
82                     for thread in stats[u"/sys/node/calls"]:
83                         calls_th.append(thread[idx])
84                     runtime_item[u"calls"] = calls_th
85
86                     vectors_th = []
87                     for thread in stats[u"/sys/node/vectors"]:
88                         vectors_th.append(thread[idx])
89                     runtime_item[u"vectors"] = vectors_th
90
91                     suspends_th = []
92                     for thread in stats[u"/sys/node/suspends"]:
93                         suspends_th.append(thread[idx])
94                     runtime_item[u"suspends"] = suspends_th
95
96                     clocks_th = []
97                     for thread in stats[u"/sys/node/clocks"]:
98                         clocks_th.append(thread[idx])
99                     runtime_item[u"clocks"] = clocks_th
100
101                     if (sum(calls_th) or sum(vectors_th) or
102                             sum(suspends_th) or sum(clocks_th)):
103                         runtime_nz.append(runtime_item)
104
105                 if log_zeros:
106                     logger.info(
107                         f"stats runtime ({node[u'host']} - {socket}):\n"
108                         f"{pformat(runtime)}"
109                     )
110                 else:
111                     logger.info(
112                         f"stats runtime ({node[u'host']} - {socket}):\n"
113                         f"{pformat(runtime_nz)}"
114                     )
115         # Run also the CLI command, the above sometimes misses some info.
116         PapiSocketExecutor.run_cli_cmd_on_all_sockets(node, u"show runtime")
117
118     @staticmethod
119     def vpp_show_runtime_on_all_duts(nodes):
120         """Clear VPP runtime counters on all DUTs.
121
122         :param nodes: VPP nodes.
123         :type nodes: dict
124         """
125         for node in nodes.values():
126             if node[u"type"] == NodeType.DUT:
127                 VppCounters.vpp_show_runtime(node)
128
129     @staticmethod
130     def vpp_show_hardware(node):
131         """Run "show hardware" debug CLI command.
132
133         :param node: Node to run command on.
134         :type node: dict
135         """
136         PapiSocketExecutor.run_cli_cmd_on_all_sockets(
137             node, u"show hardware verbose"
138         )
139
140     @staticmethod
141     def vpp_show_memory(node):
142         """Run "show memory" debug CLI command.
143
144         Currently, every flag is hardcoded, giving the longest output.
145
146         :param node: Node to run command on.
147         :type node: dict
148         """
149         PapiSocketExecutor.run_cli_cmd_on_all_sockets(
150             node, u"show memory verbose api-segment stats-segment main-heap"
151         )
152
153     @staticmethod
154     def vpp_show_memory_on_all_duts(nodes):
155         """Run "show memory" on all DUTs.
156
157         :param nodes: VPP nodes.
158         :type nodes: dict
159         """
160         for node in nodes.values():
161             if node[u"type"] == NodeType.DUT:
162                 VppCounters.vpp_show_memory(node)
163
164     @staticmethod
165     def vpp_clear_runtime(node):
166         """Run "clear runtime" CLI command.
167
168         :param node: Node to run command on.
169         :type node: dict
170         """
171         PapiSocketExecutor.run_cli_cmd_on_all_sockets(
172             node, u"clear runtime", log=False
173         )
174
175     @staticmethod
176     def vpp_clear_runtime_on_all_duts(nodes):
177         """Run "clear runtime" CLI command on all DUTs.
178
179         :param nodes: VPP nodes.
180         :type nodes: dict
181         """
182         for node in nodes.values():
183             if node[u"type"] == NodeType.DUT:
184                 VppCounters.vpp_clear_runtime(node)
185
186     @staticmethod
187     def vpp_clear_hardware(node):
188         """Run "clear hardware" CLI command.
189
190         :param node: Node to run command on.
191         :type node: dict
192         :returns: Verified data from PAPI response.
193         :rtype: dict
194         """
195         PapiSocketExecutor.run_cli_cmd_on_all_sockets(
196             node, u"clear hardware", log=False
197         )
198
199     @staticmethod
200     def vpp_clear_hardware_on_all_duts(nodes):
201         """Clear hardware on all DUTs.
202
203         :param nodes: VPP nodes.
204         :type nodes: dict
205         """
206         for node in nodes.values():
207             if node[u"type"] == NodeType.DUT:
208                 VppCounters.vpp_clear_hardware(node)
209
210     @staticmethod
211     def vpp_clear_errors(node):
212         """Run "clear errors" CLI command.
213
214         :param node: Node to run command on.
215         :type node: dict
216         """
217         PapiSocketExecutor.run_cli_cmd_on_all_sockets(
218             node, u"clear errors", log=False
219         )
220
221     @staticmethod
222     def vpp_clear_errors_on_all_duts(nodes):
223         """Clear VPP errors counters on all DUTs.
224
225         :param nodes: VPP nodes.
226         :type nodes: dict
227         """
228         for node in nodes.values():
229             if node[u"type"] == NodeType.DUT:
230                 VppCounters.vpp_clear_errors(node)
231
232     @staticmethod
233     def show_vpp_statistics(node):
234         """Show [errors, hardware] stats.
235
236         :param node: VPP node.
237         :type node: dict
238         """
239         VppCounters.vpp_show_errors(node)
240         VppCounters.vpp_show_hardware(node)
241
242     @staticmethod
243     def show_statistics_on_all_duts(nodes):
244         """Show statistics on all DUTs.
245
246         :param nodes: DUT nodes.
247         :type nodes: dict
248         """
249         for node in nodes.values():
250             if node[u"type"] == NodeType.DUT:
251                 VppCounters.show_vpp_statistics(node)
252
253     @staticmethod
254     def clear_vpp_statistics(node):
255         """Clear [errors, hardware] stats.
256
257         :param node: VPP node.
258         :type node: dict
259         """
260         VppCounters.vpp_clear_errors(node)
261         VppCounters.vpp_clear_hardware(node)
262
263     @staticmethod
264     def clear_statistics_on_all_duts(nodes):
265         """Clear statistics on all DUTs.
266
267         :param nodes: DUT nodes.
268         :type nodes: dict
269         """
270         for node in nodes.values():
271             if node[u"type"] == NodeType.DUT:
272                 VppCounters.clear_vpp_statistics(node)