Merge "Add unit test file for ArithmeticRunner"
[yardstick.git] / yardstick / network_services / helpers / cpu.py
1 # Copyright (c) 2016-2017 Intel Corporation
2 #
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
6 #
7 #      http://www.apache.org/licenses/LICENSE-2.0
8 #
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.
14
15
16 import io
17
18
19 class CpuSysCores(object):
20
21     def __init__(self, connection=""):
22         self.core_map = {}
23         self.connection = connection
24
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()
29         return lines
30
31     def _get_core_details(self, lines):
32         core_details = []
33         core_lines = {}
34         for line in lines:
35             if line.strip():
36                     name, value = line.split(":", 1)
37                     core_lines[name.strip()] = value.strip()
38             else:
39                     core_details.append(core_lines)
40                     core_lines = {}
41
42         return core_details
43
44     def get_core_socket(self):
45         lines = self.connection.execute("lscpu")[1].split(u'\n')
46         num_cores = self._get_core_details(lines)
47         for num in num_cores:
48             self.core_map["cores_per_socket"] = num["Core(s) per socket"]
49             self.core_map["thread_per_core"] = num["Thread(s) per core"]
50
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(
59                         core["processor"])
60
61         return self.core_map
62
63     def validate_cpu_cfg(self, vnf_cfg=None):
64         if vnf_cfg is None:
65             vnf_cfg = {
66                 'lb_config': 'SW',
67                 'lb_count': 1,
68                 'worker_config': '1C/1T',
69                 'worker_threads': 1
70             }
71         if self.core_map["thread_per_core"] == 1 and \
72                 vnf_cfg["worker_config"] == "1C/2T":
73             return -1
74
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:
78                 return -1
79
80         return 0