1 # Copyright (c) 2016-2017 Intel Corporation
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
19 class CpuSysCores(object):
21 def __init__(self, connection=""):
23 self.connection = connection
25 def _open_cpuinfo(self):
26 cpuinfo = io.BytesIO()
27 self.connection.get_file_obj("/proc/cpuinfo", cpuinfo)
28 lines = cpuinfo.getvalue().decode('utf-8').splitlines()
31 def _get_core_details(self, lines):
36 name, value = line.split(":", 1)
37 core_lines[name.strip()] = value.strip()
39 core_details.append(core_lines)
44 def get_core_socket(self):
45 lines = self.connection.execute("lscpu")[1].split(u'\n')
46 num_cores = self._get_core_details(lines)
48 self.core_map["cores_per_socket"] = num["Core(s) per socket"]
49 self.core_map["thread_per_core"] = num["Thread(s) per core"]
51 lines = self._open_cpuinfo()
52 core_details = self._get_core_details(lines)
53 for core in core_details:
54 for k, v in core.items():
55 if k == "physical id":
56 if core["physical id"] not in self.core_map:
57 self.core_map[core['physical id']] = []
58 self.core_map[core['physical id']].append(
63 def validate_cpu_cfg(self, vnf_cfg=None):
68 'worker_config': '1C/1T',
71 if self.core_map["thread_per_core"] == 1 and \
72 vnf_cfg["worker_config"] == "1C/2T":
75 if vnf_cfg['lb_config'] == 'SW':
76 num_cpu = int(vnf_cfg["worker_threads"]) + 5
77 if int(self.core_map["cores_per_socket"]) < num_cpu: