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:
6 # http://www.apache.org/licenses/LICENSE-2.0
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.
14 """VPP counters utilities library."""
16 from pprint import pformat
18 from robot.api import logger
20 from resources.libraries.python.PapiExecutor import PapiExecutor, \
22 from resources.libraries.python.topology import Topology, SocketType, NodeType
26 """VPP counters utilities."""
29 self._stats_table = None
32 def vpp_show_errors(node):
33 """Run "show errors" debug CLI command.
35 :param node: Node to run command on.
38 PapiSocketExecutor.run_cli_cmd_on_all_sockets(node, u"show errors")
41 def vpp_show_errors_on_all_duts(nodes):
42 """Show errors on all DUTs.
44 :param nodes: VPP nodes.
47 for node in nodes.values():
48 if node[u"type"] == NodeType.DUT:
49 VppCounters.vpp_show_errors(node)
52 def vpp_show_runtime(node, log_zeros=False):
53 """Run "show runtime" CLI command.
55 :param node: Node to run command on.
56 :param log_zeros: Log also items with zero values.
60 args = dict(path=u"^/sys/node")
61 sockets = Topology.get_node_sockets(node, socket_type=SocketType.STATS)
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]
68 names = stats[u"/sys/node/names"]
77 runtime.append({u"name": name})
79 for idx, runtime_item in enumerate(runtime):
82 for thread in stats[u"/sys/node/calls"]:
83 calls_th.append(thread[idx])
84 runtime_item[u"calls"] = calls_th
87 for thread in stats[u"/sys/node/vectors"]:
88 vectors_th.append(thread[idx])
89 runtime_item[u"vectors"] = vectors_th
92 for thread in stats[u"/sys/node/suspends"]:
93 suspends_th.append(thread[idx])
94 runtime_item[u"suspends"] = suspends_th
97 for thread in stats[u"/sys/node/clocks"]:
98 clocks_th.append(thread[idx])
99 runtime_item[u"clocks"] = clocks_th
101 if (sum(calls_th) or sum(vectors_th) or
102 sum(suspends_th) or sum(clocks_th)):
103 runtime_nz.append(runtime_item)
107 f"stats runtime ({node[u'host']} - {socket}):\n"
108 f"{pformat(runtime)}"
112 f"stats runtime ({node[u'host']} - {socket}):\n"
113 f"{pformat(runtime_nz)}"
117 def vpp_show_runtime_counters_on_all_duts(nodes):
118 """Clear VPP runtime counters on all DUTs.
120 :param nodes: VPP nodes.
123 for node in nodes.values():
124 if node[u"type"] == NodeType.DUT:
125 VppCounters.vpp_show_runtime(node)
128 def vpp_show_hardware_verbose(node):
129 """Run "show hardware-interfaces verbose" debug CLI command.
131 :param node: Node to run command on.
134 PapiSocketExecutor.run_cli_cmd_on_all_sockets(
135 node, u"show hardware verbose"
139 def vpp_show_memory(node):
140 """Run "show memory" debug CLI command.
142 Currently, every flag is hardcoded, giving the longest output.
144 :param node: Node to run command on.
147 PapiSocketExecutor.run_cli_cmd(
148 node, u"show memory verbose api-segment stats-segment main-heap"
152 def vpp_clear_runtime(node):
153 """Run "clear runtime" CLI command.
155 :param node: Node to run command on.
158 PapiSocketExecutor.run_cli_cmd_on_all_sockets(
159 node, u"clear runtime", log=False
163 def vpp_clear_runtime_counters_on_all_duts(nodes):
164 """Run "clear runtime" CLI command on all DUTs.
166 :param nodes: VPP nodes.
169 for node in nodes.values():
170 if node[u"type"] == NodeType.DUT:
171 VppCounters.vpp_clear_runtime(node)
174 def vpp_clear_hardware_counters(node):
175 """Run "clear hardware" CLI command.
177 :param node: Node to run command on.
179 :returns: Verified data from PAPI response.
182 PapiSocketExecutor.run_cli_cmd_on_all_sockets(
183 node, u"clear hardware", log=False
187 def vpp_clear_hardware_counters_on_all_duts(nodes):
188 """Clear hardware counters on all DUTs.
190 :param nodes: VPP nodes.
193 for node in nodes.values():
194 if node[u"type"] == NodeType.DUT:
195 VppCounters.vpp_clear_hardware_counters(node)
198 def vpp_clear_errors_counters(node):
199 """Run "clear errors" CLI command.
201 :param node: Node to run command on.
204 PapiSocketExecutor.run_cli_cmd_on_all_sockets(
205 node, u"clear errors", log=False
209 def vpp_clear_error_counters_on_all_duts(nodes):
210 """Clear VPP errors counters on all DUTs.
212 :param nodes: VPP nodes.
215 for node in nodes.values():
216 if node[u"type"] == NodeType.DUT:
217 VppCounters.vpp_clear_errors_counters(node)
220 def show_vpp_statistics(node):
221 """Show [error, hardware, interface] stats.
223 :param node: VPP node.
226 VppCounters.vpp_show_errors(node)
227 VppCounters.vpp_show_hardware_verbose(node)
228 VppCounters.vpp_show_runtime(node)
229 VppCounters.vpp_show_memory(node)
232 def show_statistics_on_all_duts(nodes):
233 """Show statistics on all DUTs.
235 :param nodes: DUT nodes.
238 for node in nodes.values():
239 if node[u"type"] == NodeType.DUT:
240 VppCounters.show_vpp_statistics(node)
243 def clear_vpp_statistics(node):
244 """Clear [error, hardware, interface] stats.
246 :param node: VPP node.
249 VppCounters.vpp_clear_errors_counters(node)
250 VppCounters.vpp_clear_hardware_counters(node)
251 VppCounters.vpp_clear_runtime(node)
254 def clear_statistics_on_all_duts(nodes):
255 """Clear statistics on all DUTs.
257 :param nodes: DUT nodes.
260 for node in nodes.values():
261 if node[u"type"] == NodeType.DUT:
262 VppCounters.clear_vpp_statistics(node)