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 """CPU utilities library."""
16 from robot.libraries.BuiltIn import BuiltIn
18 from resources.libraries.python.Constants import Constants
19 from resources.libraries.python.ssh import exec_cmd_no_error
20 from resources.libraries.python.topology import Topology
22 __all__ = ["CpuUtils"]
25 class CpuUtils(object):
28 # Number of threads per core.
32 def __str2int(string):
33 """Conversion from string to integer, 0 in case of empty string.
35 :param string: Input string.
37 :returns: Integer converted from string, 0 in case of ValueError.
46 def is_smt_enabled(cpu_info):
47 """Uses CPU mapping to find out if SMT is enabled or not. If SMT is
48 enabled, the L1d,L1i,L2,L3 setting is the same for two processors. These
49 two processors are two threads of one core.
51 :param cpu_info: CPU info, the output of "lscpu -p".
53 :returns: True if SMT is enabled, False if SMT is disabled.
56 cpu_mems = [item[-4:] for item in cpu_info]
57 cpu_mems_len = len(cpu_mems) / CpuUtils.NR_OF_THREADS
59 for cpu_mem in cpu_mems[:cpu_mems_len]:
60 if cpu_mem in cpu_mems[cpu_mems_len:]:
62 return bool(count == cpu_mems_len)
65 def get_cpu_layout_from_all_nodes(nodes):
66 """Retrieve cpu layout from all nodes, assuming all nodes
69 :param nodes: DICT__nodes from Topology.DICT__nodes.
71 :raises RuntimeError: If the ssh command "lscpu -p" fails.
73 for node in nodes.values():
74 stdout, _ = exec_cmd_no_error(node, 'lscpu -p')
75 node['cpuinfo'] = list()
76 for line in stdout.split("\n"):
77 if line and line[0] != "#":
78 node['cpuinfo'].append([CpuUtils.__str2int(x) for x in
82 def cpu_node_count(node):
83 """Return count of numa nodes.
85 :param node: Targeted node.
87 :returns: Count of numa nodes.
89 :raises RuntimeError: If node cpuinfo is not available.
91 cpu_info = node.get("cpuinfo")
92 if cpu_info is not None:
93 return node["cpuinfo"][-1][3] + 1
95 raise RuntimeError("Node cpuinfo not available.")
98 def cpu_list_per_node(node, cpu_node, smt_used=False):
99 """Return node related list of CPU numbers.
101 :param node: Node dictionary with cpuinfo.
102 :param cpu_node: Numa node number.
103 :param smt_used: True - we want to use SMT, otherwise false.
107 :returns: List of cpu numbers related to numa from argument.
109 :raises RuntimeError: If node cpuinfo is not available
110 or if SMT is not enabled.
112 cpu_node = int(cpu_node)
113 cpu_info = node.get("cpuinfo")
115 raise RuntimeError("Node cpuinfo not available.")
117 smt_enabled = CpuUtils.is_smt_enabled(cpu_info)
118 if not smt_enabled and smt_used:
119 raise RuntimeError("SMT is not enabled.")
123 if cpu[3] == cpu_node:
124 cpu_list.append(cpu[0])
126 if not smt_enabled or smt_enabled and smt_used:
129 if smt_enabled and not smt_used:
130 cpu_list_len = len(cpu_list)
131 cpu_list = cpu_list[:cpu_list_len / CpuUtils.NR_OF_THREADS]
136 def cpu_slice_of_list_per_node(node, cpu_node, skip_cnt=0, cpu_cnt=0,
138 """Return string of node related list of CPU numbers.
140 :param node: Node dictionary with cpuinfo.
141 :param cpu_node: Numa node number.
142 :param skip_cnt: Skip first "skip_cnt" CPUs.
143 :param cpu_cnt: Count of cpus to return, if 0 then return all.
144 :param smt_used: True - we want to use SMT, otherwise false.
150 :returns: Cpu numbers related to numa from argument.
152 :raises RuntimeError: If we require more cpus than available.
154 cpu_list = CpuUtils.cpu_list_per_node(node, cpu_node, smt_used)
156 cpu_list_len = len(cpu_list)
157 if cpu_cnt + skip_cnt > cpu_list_len:
158 raise RuntimeError("cpu_cnt + skip_cnt > length(cpu list).")
161 cpu_cnt = cpu_list_len - skip_cnt
164 cpu_list_0 = cpu_list[:cpu_list_len / CpuUtils.NR_OF_THREADS]
165 cpu_list_1 = cpu_list[cpu_list_len / CpuUtils.NR_OF_THREADS:]
166 cpu_list = [cpu for cpu in cpu_list_0[skip_cnt:skip_cnt + cpu_cnt]]
167 cpu_list_ex = [cpu for cpu in
168 cpu_list_1[skip_cnt:skip_cnt + cpu_cnt]]
169 cpu_list.extend(cpu_list_ex)
171 cpu_list = [cpu for cpu in cpu_list[skip_cnt:skip_cnt + cpu_cnt]]
176 def cpu_list_per_node_str(node, cpu_node, skip_cnt=0, cpu_cnt=0, sep=",",
178 """Return string of node related list of CPU numbers.
180 :param node: Node dictionary with cpuinfo.
181 :param cpu_node: Numa node number.
182 :param skip_cnt: Skip first "skip_cnt" CPUs.
183 :param cpu_cnt: Count of cpus to return, if 0 then return all.
184 :param sep: Separator, default: 1,2,3,4,....
185 :param smt_used: True - we want to use SMT, otherwise false.
192 :returns: Cpu numbers related to numa from argument.
195 cpu_list = CpuUtils.cpu_slice_of_list_per_node(node, cpu_node,
199 return sep.join(str(cpu) for cpu in cpu_list)
202 def cpu_range_per_node_str(node, cpu_node, skip_cnt=0, cpu_cnt=0, sep="-",
204 """Return string of node related range of CPU numbers, e.g. 0-4.
206 :param node: Node dictionary with cpuinfo.
207 :param cpu_node: Numa node number.
208 :param skip_cnt: Skip first "skip_cnt" CPUs.
209 :param cpu_cnt: Count of cpus to return, if 0 then return all.
210 :param sep: Separator, default: "-".
211 :param smt_used: True - we want to use SMT, otherwise false.
218 :returns: String of node related range of CPU numbers.
221 cpu_list = CpuUtils.cpu_slice_of_list_per_node(node, cpu_node,
226 cpu_list_len = len(cpu_list)
227 cpu_list_0 = cpu_list[:cpu_list_len / CpuUtils.NR_OF_THREADS]
228 cpu_list_1 = cpu_list[cpu_list_len / CpuUtils.NR_OF_THREADS:]
229 cpu_range = "{}{}{},{}{}{}".format(cpu_list_0[0], sep,
234 cpu_range = "{}{}{}".format(cpu_list[0], sep, cpu_list[-1])
239 def cpu_slice_of_list_for_nf(node, cpu_node, nf_chains=1, nf_nodes=1,
240 nf_chain=1, nf_node=1, nf_dtc=1, nf_mtcr=2,
241 nf_dtcr=1, skip_cnt=0):
242 """Return list of DUT node related list of CPU numbers. The main
243 computing unit is physical core count.
245 :param node: DUT node.
246 :param cpu_node: Numa node number.
247 :param nf_chains: Number of NF chains.
248 :param nf_nodes: Number of NF nodes in chain.
249 :param nf_chain: Chain number indexed from 1.
250 :param nf_node: Node number indexed from 1.
251 :param vs_dtc: Amount of physical cores for vswitch dataplane.
252 :param nf_dtc: Amount of physical cores for NF dataplane.
253 :param nf_mtcr: NF main thread per core ratio.
254 :param nf_dtcr: NF dataplane thread per core ratio.
255 :param skip_cnt: Skip first "skip_cnt" CPUs.
257 :param cpu_node: int.
263 :type nf_dtc: int or float
267 :returns: List of CPUs allocated to NF.
269 :raises RuntimeError: If we require more cpus than available or if
270 placement is not possible due to wrong parameters.
272 if nf_chain - 1 >= nf_chains:
273 raise RuntimeError("ChainID is higher than total number of chains!")
274 if nf_node - 1 >= nf_nodes:
275 raise RuntimeError("NodeID is higher than chain nodes!")
277 smt_used = CpuUtils.is_smt_enabled(node['cpuinfo'])
278 cpu_list = CpuUtils.cpu_list_per_node(node, cpu_node, smt_used)
279 # CPU thread sibling offset.
280 sib = len(cpu_list) / CpuUtils.NR_OF_THREADS
282 if not smt_used and not isinstance(nf_dtc, int):
283 raise RuntimeError("Cannot allocate if SMT is not enabled!")
284 # TODO: Workaround as we are using physical core as main unit, we must
285 # adjust number of physical dataplane cores in case of float for further
286 # array referencing. As rounding method in Py2.7 and Py3.x differs, we
287 # are using static mapping. This can be rewritten using flat arrays and
288 # different logic (from Physical core unit to Logical core unit).
289 dtc = 1 if not isinstance(nf_dtc, int) else nf_dtc
291 mt_req = ((nf_chains * nf_nodes) + nf_mtcr - 1) / nf_mtcr
292 dt_req = ((nf_chains * nf_nodes) + nf_dtcr - 1) / nf_dtcr
293 cpu_req = skip_cnt + mt_req + dt_req
295 if smt_used and cpu_req > len(cpu_list) / CpuUtils.NR_OF_THREADS:
296 raise RuntimeError("Not enough CPU cores available for placement!")
297 elif not smt_used and cpu_req > len(cpu_list):
298 raise RuntimeError("Not enough CPU cores available for placement!")
300 offset = (nf_node - 1) + (nf_chain - 1) * nf_nodes
302 mt_odd = (offset / mt_req) & 1
303 mt_skip = skip_cnt + (offset % mt_req)
304 dt_odd = (offset / dt_req) & 1
305 dt_skip = skip_cnt + mt_req + (offset % dt_req) * dtc
306 except ZeroDivisionError:
307 raise RuntimeError("Invalid placement combination!")
309 mt_list = [cpu for cpu in cpu_list[mt_skip+sib:mt_skip+sib + 1]] \
310 if mt_odd else [cpu for cpu in cpu_list[mt_skip:mt_skip + 1]]
311 dt_list = [cpu for cpu in cpu_list[dt_skip+sib:dt_skip+sib + dtc]] \
312 if dt_odd else [cpu for cpu in cpu_list[dt_skip:dt_skip + dtc]]
313 if isinstance(nf_dtc, int):
315 [cpu for cpu in cpu_list[dt_skip:dt_skip + dtc]]
317 [cpu for cpu in cpu_list[dt_skip+sib:dt_skip+sib + dtc]]
319 mt_list = [cpu for cpu in cpu_list[mt_skip:mt_skip + 1]]
320 dt_list = [cpu for cpu in cpu_list[dt_skip:dt_skip + dtc]]
322 return mt_list + dt_list
325 def get_affinity_nf(nodes, node, nf_chains=1, nf_nodes=1, nf_chain=1,
326 nf_node=1, vs_dtc=1, nf_dtc=1, nf_mtcr=2, nf_dtcr=1):
328 """Get affinity of NF (network function). Result will be used to compute
329 the amount of CPUs and also affinity.
331 :param nodes: Physical topology nodes.
332 :param node: SUT node.
333 :param nf_chains: Number of NF chains.
334 :param nf_nodes: Number of NF nodes in chain.
335 :param nf_chain: Chain number indexed from 1.
336 :param nf_node: Node number indexed from 1.
337 :param vs_dtc: Amount of physical cores for vswitch dataplane.
338 :param nf_dtc: Amount of physical cores for NF dataplane.
339 :param nf_mtcr: NF main thread per core ratio.
340 :param nf_dtcr: NF dataplane thread per core ratio.
348 :type nf_dtc: int or float
351 :returns: List of CPUs allocated to NF.
354 skip_cnt = Constants.CPU_CNT_SYSTEM + Constants.CPU_CNT_MAIN + vs_dtc
357 interface_list.append(
358 BuiltIn().get_variable_value('${{{node}_if1}}'.format(node=node)))
359 interface_list.append(
360 BuiltIn().get_variable_value('${{{node}_if2}}'.format(node=node)))
362 cpu_node = Topology.get_interfaces_numa_node(
363 nodes[node], *interface_list)
365 return CpuUtils.cpu_slice_of_list_for_nf(
366 node=nodes[node], cpu_node=cpu_node, nf_chains=nf_chains,
367 nf_nodes=nf_nodes, nf_chain=nf_chain, nf_node=nf_node,
368 nf_mtcr=nf_mtcr, nf_dtcr=nf_dtcr, nf_dtc=nf_dtc, skip_cnt=skip_cnt)