Merge "Change PTL informatin in INFO"
[bottlenecks.git] / testsuites / vstf / vstf_scripts / 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
28     def __init__(self):
29         super(Resource, self).__init__()
30         self.sysfs = "/sys/devices/system/node"
31         self.mapping = {}
32         for node in self._init_numa():
33             self.mapping[node] = {}
34
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)
42
43     def _get_process_mapping(self, numa_node):
44         ret = run_cmd("cat " + self.sysfs + '/' + numa_node +
45                       '/cpumap').replace(',', '').lstrip('0')
46         return int(ret, 16)
47
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')
52
53     def _init_numa(self):
54         """the node name is node0, node1......"""
55         try:
56             node_list = os.listdir(self.sysfs)
57         except Exception as e:
58             raise e
59         ret = []
60         partten = re.compile("^node[0-9]{,}$")
61         for node in node_list:
62             if partten.match(node) is None:
63                 continue
64             ret.append(node)
65         return ret
66
67
68 class Equalizer(Resource):
69
70     def __init__(self):
71         super(Equalizer, self).__init__()
72
73     def topology(self):
74         print self.mapping
75
76
77 e = Equalizer()
78 e.topology()