Initial commit
[ta/infra-ansible.git] / roles / allocate_cpu_cores / library / get_cpu_topology.py
1 #!/usr/bin/python
2
3 # Copyright 2019 Nokia
4
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 #     http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 import glob
18
19 from ansible.module_utils.basic import *
20
21 def mask2list(path):
22     r = set()
23
24     with open(path, 'r') as f:
25         masks = [int(x, 16) for x in f.read().split(',')]
26
27     masks.reverse()
28
29     i = 0
30     for m in masks:
31         for j in range(32):
32             if m & (1 << j):
33                 r.add(i)
34             i += 1
35
36     return sorted(r)
37
38 def get_node_cores(nodepath):
39     r = []
40     s = set()
41
42     for cpu in mask2list(nodepath + '/cpumap'):
43         if cpu not in s:
44             v = mask2list(nodepath + '/cpu%d/topology/thread_siblings' % cpu)
45             r.append(v)
46             s.update(v)
47
48     return r
49
50 def main():
51     module = AnsibleModule(argument_spec = { 'var': { 'required': True, 'type': 'str' } })
52
53     pattern = '/sys/devices/system/node/node*'
54     r = {}
55
56     for nodepath in glob.glob(pattern):
57         nodeid = nodepath[len(pattern) - 1:]
58         r['numa' + nodeid] = get_node_cores(nodepath)
59
60     module.exit_json(changed = False, ansible_facts = { module.params['var']: r })
61
62 if __name__ == '__main__':
63     main()