Telemetry: Add more operational data
[csit.git] / resources / libraries / python / VppCounters.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 """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
116     @staticmethod
117     def vpp_show_runtime_on_all_duts(nodes):
118         """Clear VPP runtime counters on all DUTs.
119
120         :param nodes: VPP nodes.
121         :type nodes: dict
122         """
123         for node in nodes.values():
124             if node[u"type"] == NodeType.DUT:
125                 VppCounters.vpp_show_runtime(node)
126
127     @staticmethod
128     def vpp_show_interface(node):
129         """Run "show interface" debug CLI command.
130
131         :param node: Node to run command on.
132         :type node: dict
133         """
134         PapiSocketExecutor.run_cli_cmd_on_all_sockets(
135             node, u"show hardware verbose"
136         )
137
138     @staticmethod
139     def vpp_show_memory(node):
140         """Run "show memory" debug CLI command.
141
142         Currently, every flag is hardcoded, giving the longest output.
143
144         :param node: Node to run command on.
145         :type node: dict
146         """
147         PapiSocketExecutor.run_cli_cmd_on_all_sockets(
148             node, u"show memory verbose api-segment stats-segment main-heap"
149         )
150
151     @staticmethod
152     def vpp_show_memory_on_all_duts(nodes):
153         """Run "show memory" on all DUTs.
154
155         :param nodes: VPP nodes.
156         :type nodes: dict
157         """
158         for node in nodes.values():
159             if node[u"type"] == NodeType.DUT:
160                 VppCounters.vpp_show_memory(node)
161
162     @staticmethod
163     def vpp_clear_runtime(node):
164         """Run "clear runtime" CLI command.
165
166         :param node: Node to run command on.
167         :type node: dict
168         """
169         PapiSocketExecutor.run_cli_cmd_on_all_sockets(
170             node, u"clear runtime", log=False
171         )
172
173     @staticmethod
174     def vpp_clear_runtime_on_all_duts(nodes):
175         """Run "clear runtime" CLI command on all DUTs.
176
177         :param nodes: VPP nodes.
178         :type nodes: dict
179         """
180         for node in nodes.values():
181             if node[u"type"] == NodeType.DUT:
182                 VppCounters.vpp_clear_runtime(node)
183
184     @staticmethod
185     def vpp_clear_interfaces(node):
186         """Run "clear interfaces" CLI command.
187
188         :param node: Node to run command on.
189         :type node: dict
190         :returns: Verified data from PAPI response.
191         :rtype: dict
192         """
193         PapiSocketExecutor.run_cli_cmd_on_all_sockets(
194             node, u"clear interfaces", log=False
195         )
196
197     @staticmethod
198     def vpp_clear_interfaces_on_all_duts(nodes):
199         """Clear interfaces on all DUTs.
200
201         :param nodes: VPP nodes.
202         :type nodes: dict
203         """
204         for node in nodes.values():
205             if node[u"type"] == NodeType.DUT:
206                 VppCounters.vpp_clear_interfaces(node)
207
208     @staticmethod
209     def vpp_clear_errors(node):
210         """Run "clear errors" CLI command.
211
212         :param node: Node to run command on.
213         :type node: dict
214         """
215         PapiSocketExecutor.run_cli_cmd_on_all_sockets(
216             node, u"clear errors", log=False
217         )
218
219     @staticmethod
220     def vpp_clear_errors_on_all_duts(nodes):
221         """Clear VPP errors counters on all DUTs.
222
223         :param nodes: VPP nodes.
224         :type nodes: dict
225         """
226         for node in nodes.values():
227             if node[u"type"] == NodeType.DUT:
228                 VppCounters.vpp_clear_errors(node)
229
230     @staticmethod
231     def show_vpp_statistics(node):
232         """Show [error, hardware, interface] stats.
233
234         :param node: VPP node.
235         :type node: dict
236         """
237         VppCounters.vpp_show_errors(node)
238         VppCounters.vpp_show_interface(node)
239
240     @staticmethod
241     def show_statistics_on_all_duts(nodes):
242         """Show statistics on all DUTs.
243
244         :param nodes: DUT nodes.
245         :type nodes: dict
246         """
247         for node in nodes.values():
248             if node[u"type"] == NodeType.DUT:
249                 VppCounters.show_vpp_statistics(node)
250
251     @staticmethod
252     def clear_vpp_statistics(node):
253         """Clear [error, hardware, interface] stats.
254
255         :param node: VPP node.
256         :type node: dict
257         """
258         VppCounters.vpp_clear_errors(node)
259         VppCounters.vpp_clear_interfaces(node)
260
261     @staticmethod
262     def clear_statistics_on_all_duts(nodes):
263         """Clear statistics on all DUTs.
264
265         :param nodes: DUT nodes.
266         :type nodes: dict
267         """
268         for node in nodes.values():
269             if node[u"type"] == NodeType.DUT:
270                 VppCounters.clear_vpp_statistics(node)