pdump: fix error code check when creating/canceling pthread
[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 #   Copyright(c) 2017 Cavium, Inc. All rights reserved.
8 #   All rights reserved.
9 #
10 #   Redistribution and use in source and binary forms, with or without
11 #   modification, are permitted provided that the following conditions
12 #   are met:
13 #
14 #     * Redistributions of source code must retain the above copyright
15 #       notice, this list of conditions and the following disclaimer.
16 #     * Redistributions in binary form must reproduce the above copyright
17 #       notice, this list of conditions and the following disclaimer in
18 #       the documentation and/or other materials provided with the
19 #       distribution.
20 #     * Neither the name of Intel Corporation nor the names of its
21 #       contributors may be used to endorse or promote products derived
22 #       from this software without specific prior written permission.
23 #
24 #   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 #   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 #   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
27 #   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
28 #   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 #   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
30 #   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 #   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 #   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 #   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
34 #   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 #
36 from __future__ import print_function
37 import sys
38 try:
39     xrange # Python 2
40 except NameError:
41     xrange = range # Python 3
42
43 sockets = []
44 cores = []
45 core_map = {}
46 base_path = "/sys/devices/system/cpu"
47 fd = open("{}/kernel_max".format(base_path))
48 max_cpus = int(fd.read())
49 fd.close()
50 for cpu in xrange(max_cpus + 1):
51     try:
52         fd = open("{}/cpu{}/topology/core_id".format(base_path, cpu))
53     except IOError:
54         continue
55     except:
56         break
57     core = int(fd.read())
58     fd.close()
59     fd = open("{}/cpu{}/topology/physical_package_id".format(base_path, cpu))
60     socket = int(fd.read())
61     fd.close()
62     if core not in cores:
63         cores.append(core)
64     if socket not in sockets:
65         sockets.append(socket)
66     key = (socket, core)
67     if key not in core_map:
68         core_map[key] = []
69     core_map[key].append(cpu)
70
71 print(format("=" * (47 + len(base_path))))
72 print("Core and Socket Information (as reported by '{}')".format(base_path))
73 print("{}\n".format("=" * (47 + len(base_path))))
74 print("cores = ", cores)
75 print("sockets = ", sockets)
76 print("")
77
78 max_processor_len = len(str(len(cores) * len(sockets) * 2 - 1))
79 max_thread_count = len(list(core_map.values())[0])
80 max_core_map_len = (max_processor_len * max_thread_count)  \
81                       + len(", ") * (max_thread_count - 1) \
82                       + len('[]') + len('Socket ')
83 max_core_id_len = len(str(max(cores)))
84
85 output = " ".ljust(max_core_id_len + len('Core '))
86 for s in sockets:
87     output += " Socket %s" % str(s).ljust(max_core_map_len - len('Socket '))
88 print(output)
89
90 output = " ".ljust(max_core_id_len + len('Core '))
91 for s in sockets:
92     output += " --------".ljust(max_core_map_len)
93     output += " "
94 print(output)
95
96 for c in cores:
97     output = "Core %s" % str(c).ljust(max_core_id_len)
98     for s in sockets:
99         if (s,c) in core_map:
100             output += " " + str(core_map[(s, c)]).ljust(max_core_map_len)
101         else:
102             output += " " * (max_core_map_len + 1)
103     print(output)