0e049a69cbd8ce0f680d496d2c7ad969c53392a7
[dpdk.git] / usertools / cpu_layout.py
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 #
35 from __future__ import print_function
36 import sys
37
38 sockets = []
39 cores = []
40 core_map = {}
41
42 fd = open("/proc/cpuinfo")
43 lines = fd.readlines()
44 fd.close()
45
46 core_details = []
47 core_lines = {}
48 for 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
56 for 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
72 print("============================================================")
73 print("Core and Socket Information (as reported by '/proc/cpuinfo')")
74 print("============================================================\n")
75 print("cores = ", cores)
76 print("sockets = ", sockets)
77 print("")
78
79 max_processor_len = len(str(len(cores) * len(sockets) * 2 - 1))
80 max_core_map_len = max_processor_len * 2 + len('[, ]') + len('Socket ')
81 max_core_id_len = len(str(max(cores)))
82
83 output = " ".ljust(max_core_id_len + len('Core '))
84 for s in sockets:
85     output += " Socket %s" % str(s).ljust(max_core_map_len - len('Socket '))
86 print(output)
87
88 output = " ".ljust(max_core_id_len + len('Core '))
89 for s in sockets:
90     output += " --------".ljust(max_core_map_len)
91     output += " "
92 print(output)
93
94 for 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)