test mbuf attach
[dpdk.git] / usertools / cpu_layout.py
1 #!/usr/bin/env python
2 # SPDX-License-Identifier: BSD-3-Clause
3 # Copyright(c) 2010-2014 Intel Corporation
4 # Copyright(c) 2017 Cavium, Inc. All rights reserved.
5
6 from __future__ import print_function
7 import sys
8 try:
9     xrange # Python 2
10 except NameError:
11     xrange = range # Python 3
12
13 if sys.version_info.major < 3:
14     print("WARNING: Python 2 is deprecated for use in DPDK, and will not work in future releases.", file=sys.stderr)
15     print("Please use Python 3 instead", file=sys.stderr)
16
17 sockets = []
18 cores = []
19 core_map = {}
20 base_path = "/sys/devices/system/cpu"
21 fd = open("{}/kernel_max".format(base_path))
22 max_cpus = int(fd.read())
23 fd.close()
24 for cpu in xrange(max_cpus + 1):
25     try:
26         fd = open("{}/cpu{}/topology/core_id".format(base_path, cpu))
27     except IOError:
28         continue
29     except:
30         break
31     core = int(fd.read())
32     fd.close()
33     fd = open("{}/cpu{}/topology/physical_package_id".format(base_path, cpu))
34     socket = int(fd.read())
35     fd.close()
36     if core not in cores:
37         cores.append(core)
38     if socket not in sockets:
39         sockets.append(socket)
40     key = (socket, core)
41     if key not in core_map:
42         core_map[key] = []
43     core_map[key].append(cpu)
44
45 print(format("=" * (47 + len(base_path))))
46 print("Core and Socket Information (as reported by '{}')".format(base_path))
47 print("{}\n".format("=" * (47 + len(base_path))))
48 print("cores = ", cores)
49 print("sockets = ", sockets)
50 print("")
51
52 max_processor_len = len(str(len(cores) * len(sockets) * 2 - 1))
53 max_thread_count = len(list(core_map.values())[0])
54 max_core_map_len = (max_processor_len * max_thread_count)  \
55                       + len(", ") * (max_thread_count - 1) \
56                       + len('[]') + len('Socket ')
57 max_core_id_len = len(str(max(cores)))
58
59 output = " ".ljust(max_core_id_len + len('Core '))
60 for s in sockets:
61     output += " Socket %s" % str(s).ljust(max_core_map_len - len('Socket '))
62 print(output)
63
64 output = " ".ljust(max_core_id_len + len('Core '))
65 for s in sockets:
66     output += " --------".ljust(max_core_map_len)
67     output += " "
68 print(output)
69
70 for c in cores:
71     output = "Core %s" % str(c).ljust(max_core_id_len)
72     for s in sockets:
73         if (s,c) in core_map:
74             output += " " + str(core_map[(s, c)]).ljust(max_core_map_len)
75         else:
76             output += " " * (max_core_map_len + 1)
77     print(output)