Merge "yardstick setup ansible, including load_images"
[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 class CpuSysCores(object):
17
18     def __init__(self, connection=""):
19         self.core_map = {}
20         self.connection = connection
21
22     def _open_cpuinfo(self):
23         lines = []
24         lines = self.connection.execute("cat /proc/cpuinfo")[1].split(u'\n')
25         return lines
26
27     def _get_core_details(self, lines):
28         core_details = []
29         core_lines = {}
30         for line in lines:
31             if line.strip():
32                     name, value = line.split(":", 1)
33                     core_lines[name.strip()] = value.strip()
34             else:
35                     core_details.append(core_lines)
36                     core_lines = {}
37
38         return core_details
39
40     def get_core_socket(self):
41         lines = self.connection.execute("lscpu")[1].split(u'\n')
42         num_cores = self._get_core_details(lines)
43         for num in num_cores:
44             self.core_map["cores_per_socket"] = num["Core(s) per socket"]
45             self.core_map["thread_per_core"] = num["Thread(s) per core"]
46
47         lines = self._open_cpuinfo()
48         core_details = self._get_core_details(lines)
49         for core in core_details:
50             for k, v in core.items():
51                 if k == "physical id":
52                     if core["physical id"] not in self.core_map:
53                         self.core_map[core['physical id']] = []
54                     self.core_map[core['physical id']].append(
55                         core["processor"])
56
57         return self.core_map
58
59     def validate_cpu_cfg(self, vnf_cfg=None):
60         if vnf_cfg is None:
61             vnf_cfg = {
62                 'lb_config': 'SW',
63                 'lb_count': 1,
64                 'worker_config': '1C/1T',
65                 'worker_threads': 1
66             }
67         if self.core_map["thread_per_core"] == 1 and \
68                 vnf_cfg["worker_config"] == "1C/2T":
69             return -1
70
71         if vnf_cfg['lb_config'] == 'SW':
72             num_cpu = int(vnf_cfg["worker_threads"]) + 5
73             if int(self.core_map["cores_per_socket"]) < num_cpu:
74                 return -1
75
76         return 0