xref: /dpdk/usertools/cpu_layout.py (revision 10b71caecbe1cddcbb65c050ca775fba575e88db)
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
6from __future__ import print_function
7import sys
8try:
9    xrange # Python 2
10except NameError:
11    xrange = range # Python 3
12
13if 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
17sockets = []
18cores = []
19core_map = {}
20base_path = "/sys/devices/system/cpu"
21fd = open("{}/kernel_max".format(base_path))
22max_cpus = int(fd.read())
23fd.close()
24for 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
45print(format("=" * (47 + len(base_path))))
46print("Core and Socket Information (as reported by '{}')".format(base_path))
47print("{}\n".format("=" * (47 + len(base_path))))
48print("cores = ", cores)
49print("sockets = ", sockets)
50print("")
51
52max_processor_len = len(str(len(cores) * len(sockets) * 2 - 1))
53max_thread_count = len(list(core_map.values())[0])
54max_core_map_len = (max_processor_len * max_thread_count)  \
55                      + len(", ") * (max_thread_count - 1) \
56                      + len('[]') + len('Socket ')
57max_core_id_len = len(str(max(cores)))
58
59output = " ".ljust(max_core_id_len + len('Core '))
60for s in sockets:
61    output += " Socket %s" % str(s).ljust(max_core_map_len - len('Socket '))
62print(output)
63
64output = " ".ljust(max_core_id_len + len('Core '))
65for s in sockets:
66    output += " --------".ljust(max_core_map_len)
67    output += " "
68print(output)
69
70for 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)
78