xref: /dpdk/usertools/cpu_layout.py (revision f5057be340e44f3edc0fe90fa875eb89a4c49b4f)
1#!/usr/bin/env python3
2# SPDX-License-Identifier: BSD-3-Clause
3# Copyright(c) 2010-2014 Intel Corporation
4# Copyright(c) 2017 Cavium, Inc. All rights reserved.
5
6sockets = []
7cores = []
8core_map = {}
9base_path = "/sys/devices/system/cpu"
10fd = open("{}/kernel_max".format(base_path))
11max_cpus = int(fd.read())
12fd.close()
13for cpu in range(max_cpus + 1):
14    try:
15        fd = open("{}/cpu{}/topology/core_id".format(base_path, cpu))
16    except IOError:
17        continue
18    except:
19        break
20    core = int(fd.read())
21    fd.close()
22    fd = open("{}/cpu{}/topology/physical_package_id".format(base_path, cpu))
23    socket = int(fd.read())
24    fd.close()
25    if core not in cores:
26        cores.append(core)
27    if socket not in sockets:
28        sockets.append(socket)
29    key = (socket, core)
30    if key not in core_map:
31        core_map[key] = []
32    core_map[key].append(cpu)
33
34print(format("=" * (47 + len(base_path))))
35print("Core and Socket Information (as reported by '{}')".format(base_path))
36print("{}\n".format("=" * (47 + len(base_path))))
37print("cores = ", cores)
38print("sockets = ", sockets)
39print("")
40
41max_processor_len = len(str(len(cores) * len(sockets) * 2 - 1))
42max_thread_count = len(list(core_map.values())[0])
43max_core_map_len = (max_processor_len * max_thread_count)  \
44                      + len(", ") * (max_thread_count - 1) \
45                      + len('[]') + len('Socket ')
46max_core_id_len = len(str(max(cores)))
47
48output = " ".ljust(max_core_id_len + len('Core '))
49for s in sockets:
50    output += " Socket %s" % str(s).ljust(max_core_map_len - len('Socket '))
51print(output)
52
53output = " ".ljust(max_core_id_len + len('Core '))
54for s in sockets:
55    output += " --------".ljust(max_core_map_len)
56    output += " "
57print(output)
58
59for c in cores:
60    output = "Core %s" % str(c).ljust(max_core_id_len)
61    for s in sockets:
62        if (s,c) in core_map:
63            output += " " + str(core_map[(s, c)]).ljust(max_core_map_len)
64        else:
65            output += " " * (max_core_map_len + 1)
66    print(output)
67