xref: /dpdk/usertools/cpu_layout.py (revision 0857b942113874c69dc3db5df11a828ee3cc9b6b)
1#!/usr/bin/env python
2
3#
4#   BSD LICENSE
5#
6#   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
7#   All rights reserved.
8#
9#   Redistribution and use in source and binary forms, with or without
10#   modification, are permitted provided that the following conditions
11#   are met:
12#
13#     * Redistributions of source code must retain the above copyright
14#       notice, this list of conditions and the following disclaimer.
15#     * Redistributions in binary form must reproduce the above copyright
16#       notice, this list of conditions and the following disclaimer in
17#       the documentation and/or other materials provided with the
18#       distribution.
19#     * Neither the name of Intel Corporation nor the names of its
20#       contributors may be used to endorse or promote products derived
21#       from this software without specific prior written permission.
22#
23#   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24#   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25#   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26#   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27#   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28#   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29#   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30#   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31#   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32#   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33#   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34#
35from __future__ import print_function
36import sys
37
38sockets = []
39cores = []
40core_map = {}
41
42fd = open("/proc/cpuinfo")
43lines = fd.readlines()
44fd.close()
45
46core_details = []
47core_lines = {}
48for line in lines:
49    if len(line.strip()) != 0:
50        name, value = line.split(":", 1)
51        core_lines[name.strip()] = value.strip()
52    else:
53        core_details.append(core_lines)
54        core_lines = {}
55
56for core in core_details:
57    for field in ["processor", "core id", "physical id"]:
58        if field not in core:
59            print("Error getting '%s' value from /proc/cpuinfo" % field)
60            sys.exit(1)
61        core[field] = int(core[field])
62
63    if core["core id"] not in cores:
64        cores.append(core["core id"])
65    if core["physical id"] not in sockets:
66        sockets.append(core["physical id"])
67    key = (core["physical id"], core["core id"])
68    if key not in core_map:
69        core_map[key] = []
70    core_map[key].append(core["processor"])
71
72print("============================================================")
73print("Core and Socket Information (as reported by '/proc/cpuinfo')")
74print("============================================================\n")
75print("cores = ", cores)
76print("sockets = ", sockets)
77print("")
78
79max_processor_len = len(str(len(cores) * len(sockets) * 2 - 1))
80max_core_map_len = max_processor_len * 2 + len('[, ]') + len('Socket ')
81max_core_id_len = len(str(max(cores)))
82
83output = " ".ljust(max_core_id_len + len('Core '))
84for s in sockets:
85    output += " Socket %s" % str(s).ljust(max_core_map_len - len('Socket '))
86print(output)
87
88output = " ".ljust(max_core_id_len + len('Core '))
89for s in sockets:
90    output += " --------".ljust(max_core_map_len)
91    output += " "
92print(output)
93
94for c in cores:
95    output = "Core %s" % str(c).ljust(max_core_id_len)
96    for s in sockets:
97        output += " " + str(core_map[(s, c)]).ljust(max_core_map_len)
98    print(output)
99