1 ##############################################################################
2 # Copyright (c) 2015 Huawei Technologies Co.,Ltd and others.
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
15 log = logging.getLogger(__name__)
18 def run_cmd(cmd, shell=True):
20 ret = subprocess.check_output(cmd, shell=shell)
21 except subprocess.CalledProcessError as e:
26 class Resource(object):
29 super(Resource, self).__init__()
30 self.sysfs = "/sys/devices/system/node"
32 for node in self._init_numa():
33 self.mapping[node] = {}
35 process_mapping = self._get_process_mapping(node)
36 for process_index in xrange(0, len(bin(process_mapping)) - 2):
37 if process_mapping & 1 << process_index != 0:
38 core = self._get_core_id(node, process_index)
39 if core not in self.mapping[node]:
40 self.mapping[node][core] = []
41 self.mapping[node][core].append(process_index)
43 def _get_process_mapping(self, numa_node):
44 ret = run_cmd("cat " + self.sysfs + '/' + numa_node +
45 '/cpumap').replace(',', '').lstrip('0')
48 def _get_core_id(self, numa_node, process_index):
49 cmd = "cat " + self.sysfs + '/' + numa_node + \
50 '/cpu' + str(process_index) + '/topology/core_id'
51 return run_cmd(cmd).strip('\n')
54 """the node name is node0, node1......"""
56 node_list = os.listdir(self.sysfs)
57 except Exception as e:
60 partten = re.compile("^node[0-9]{,}$")
61 for node in node_list:
62 if partten.match(node) is None:
68 class Equalizer(Resource):
71 super(Equalizer, self).__init__()