xref: /f-stack/dpdk/usertools/cpu_layout.py (revision 8d76b62e)
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
13sockets = []
14cores = []
15core_map = {}
16base_path = "/sys/devices/system/cpu"
17fd = open("{}/kernel_max".format(base_path))
18max_cpus = int(fd.read())
19fd.close()
20for cpu in xrange(max_cpus + 1):
21    try:
22        fd = open("{}/cpu{}/topology/core_id".format(base_path, cpu))
23    except IOError:
24        continue
25    core = int(fd.read())
26    fd.close()
27    fd = open("{}/cpu{}/topology/physical_package_id".format(base_path, cpu))
28    socket = int(fd.read())
29    fd.close()
30    if core not in cores:
31        cores.append(core)
32    if socket not in sockets:
33        sockets.append(socket)
34    key = (socket, core)
35    if key not in core_map:
36        core_map[key] = []
37    core_map[key].append(cpu)
38
39print(format("=" * (47 + len(base_path))))
40print("Core and Socket Information (as reported by '{}')".format(base_path))
41print("{}\n".format("=" * (47 + len(base_path))))
42print("cores = ", cores)
43print("sockets = ", sockets)
44print("")
45
46max_processor_len = len(str(len(cores) * len(sockets) * 2 - 1))
47max_thread_count = len(list(core_map.values())[0])
48max_core_map_len = (max_processor_len * max_thread_count)  \
49                      + len(", ") * (max_thread_count - 1) \
50                      + len('[]') + len('Socket ')
51max_core_id_len = len(str(max(cores)))
52
53output = " ".ljust(max_core_id_len + len('Core '))
54for s in sockets:
55    output += " Socket %s" % str(s).ljust(max_core_map_len - len('Socket '))
56print(output)
57
58output = " ".ljust(max_core_id_len + len('Core '))
59for s in sockets:
60    output += " --------".ljust(max_core_map_len)
61    output += " "
62print(output)
63
64for c in cores:
65    output = "Core %s" % str(c).ljust(max_core_id_len)
66    for s in sockets:
67        if (s, c) in core_map:
68            output += " " + str(core_map[(s, c)]).ljust(max_core_map_len)
69        else:
70            output += " " * (max_core_map_len + 1)
71    print(output)
72