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):
28 super(Resource, self).__init__()
29 self.sysfs = "/sys/devices/system/node"
31 for node in self._init_numa():
32 self.mapping[node] = {}
34 process_mapping = self._get_process_mapping(node)
35 for process_index in xrange(0, len(bin(process_mapping)) - 2):
36 if process_mapping & 1 << process_index != 0:
37 core = self._get_core_id(node, process_index)
38 if not self.mapping[node].has_key(core):
39 self.mapping[node][core] = []
40 self.mapping[node][core].append(process_index)
42 def _get_process_mapping(self, numa_node):
43 ret = run_cmd("cat " + self.sysfs + '/' + numa_node + '/cpumap').replace(',', '').lstrip('0')
46 def _get_core_id(self, numa_node, process_index):
47 cmd = "cat " + self.sysfs + '/' + numa_node + '/cpu' + str(process_index) + '/topology/core_id'
48 return run_cmd(cmd).strip('\n')
51 """the node name is node0, node1......"""
53 node_list = os.listdir(self.sysfs)
54 except Exception as e:
57 partten = re.compile("^node[0-9]{,}$")
58 for node in node_list:
59 if partten.match(node) is None:
65 class Equalizer(Resource):
67 super(Equalizer, self).__init__()