JIRA: BOTTLENECKS-29
[bottlenecks.git] / vstf / vstf / agent / equalizer / equalizer.py
1 ##############################################################################
2 # Copyright (c) 2015 Huawei Technologies Co.,Ltd and others.
3 #
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 ##############################################################################
9
10 import os
11 import re
12 import subprocess
13 import logging
14
15 log = logging.getLogger(__name__)
16
17
18 def run_cmd(cmd, shell=True):
19     try:
20         ret = subprocess.check_output(cmd, shell=shell)
21     except subprocess.CalledProcessError as e:
22         raise e
23     return ret
24
25
26 class Resource(object):
27     def __init__(self):
28         super(Resource, self).__init__()
29         self.sysfs = "/sys/devices/system/node"
30         self.mapping = {}
31         for node in self._init_numa():
32             self.mapping[node] = {}
33
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)
41
42     def _get_process_mapping(self, numa_node):
43         ret = run_cmd("cat " + self.sysfs + '/' + numa_node + '/cpumap').replace(',', '').lstrip('0')
44         return int(ret, 16)
45
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')
49
50     def _init_numa(self):
51         """the node name is node0, node1......"""
52         try:
53             node_list = os.listdir(self.sysfs)
54         except Exception as e:
55             raise e
56         ret = []
57         partten = re.compile("^node[0-9]{,}$")
58         for node in node_list:
59             if partten.match(node) is None:
60                 continue
61             ret.append(node)
62         return ret
63
64
65 class Equalizer(Resource):
66     def __init__(self):
67         super(Equalizer, self).__init__()
68
69     def topology(self):
70         print self.mapping
71
72
73 e = Equalizer()
74 e.topology()