2016-12-21 15:03:48 +00:00
|
|
|
#!/usr/bin/env python
|
2018-04-09 08:58:37 +00:00
|
|
|
# SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
# Copyright(c) 2010-2014 Intel Corporation
|
|
|
|
# Copyright(c) 2017 Cavium, Inc. All rights reserved.
|
2016-12-21 15:03:48 +00:00
|
|
|
|
|
|
|
from __future__ import print_function
|
2012-12-19 23:00:00 +00:00
|
|
|
import sys
|
2017-04-30 14:11:40 +00:00
|
|
|
try:
|
|
|
|
xrange # Python 2
|
|
|
|
except NameError:
|
|
|
|
xrange = range # Python 3
|
2012-12-19 23:00:00 +00:00
|
|
|
|
|
|
|
sockets = []
|
|
|
|
cores = []
|
|
|
|
core_map = {}
|
2017-03-31 12:21:00 +00:00
|
|
|
base_path = "/sys/devices/system/cpu"
|
|
|
|
fd = open("{}/kernel_max".format(base_path))
|
|
|
|
max_cpus = int(fd.read())
|
2012-12-19 23:00:00 +00:00
|
|
|
fd.close()
|
2017-03-31 12:21:00 +00:00
|
|
|
for cpu in xrange(max_cpus + 1):
|
|
|
|
try:
|
|
|
|
fd = open("{}/cpu{}/topology/core_id".format(base_path, cpu))
|
2017-04-28 10:34:35 +00:00
|
|
|
except IOError:
|
|
|
|
continue
|
2017-03-31 12:21:00 +00:00
|
|
|
except:
|
|
|
|
break
|
|
|
|
core = int(fd.read())
|
|
|
|
fd.close()
|
|
|
|
fd = open("{}/cpu{}/topology/physical_package_id".format(base_path, cpu))
|
|
|
|
socket = int(fd.read())
|
|
|
|
fd.close()
|
|
|
|
if core not in cores:
|
|
|
|
cores.append(core)
|
|
|
|
if socket not in sockets:
|
|
|
|
sockets.append(socket)
|
|
|
|
key = (socket, core)
|
2016-12-21 15:03:47 +00:00
|
|
|
if key not in core_map:
|
|
|
|
core_map[key] = []
|
2017-03-31 12:21:00 +00:00
|
|
|
core_map[key].append(cpu)
|
2012-12-19 23:00:00 +00:00
|
|
|
|
2017-03-31 12:21:00 +00:00
|
|
|
print(format("=" * (47 + len(base_path))))
|
|
|
|
print("Core and Socket Information (as reported by '{}')".format(base_path))
|
|
|
|
print("{}\n".format("=" * (47 + len(base_path))))
|
2016-12-21 15:03:48 +00:00
|
|
|
print("cores = ", cores)
|
|
|
|
print("sockets = ", sockets)
|
|
|
|
print("")
|
2012-12-19 23:00:00 +00:00
|
|
|
|
2014-06-18 04:18:15 +00:00
|
|
|
max_processor_len = len(str(len(cores) * len(sockets) * 2 - 1))
|
2017-04-30 14:11:40 +00:00
|
|
|
max_thread_count = len(list(core_map.values())[0])
|
2017-04-28 10:34:35 +00:00
|
|
|
max_core_map_len = (max_processor_len * max_thread_count) \
|
|
|
|
+ len(", ") * (max_thread_count - 1) \
|
|
|
|
+ len('[]') + len('Socket ')
|
2014-06-18 04:18:15 +00:00
|
|
|
max_core_id_len = len(str(max(cores)))
|
|
|
|
|
2016-12-21 15:03:48 +00:00
|
|
|
output = " ".ljust(max_core_id_len + len('Core '))
|
2012-12-19 23:00:00 +00:00
|
|
|
for s in sockets:
|
2016-12-21 15:03:48 +00:00
|
|
|
output += " Socket %s" % str(s).ljust(max_core_map_len - len('Socket '))
|
|
|
|
print(output)
|
2016-12-21 15:03:47 +00:00
|
|
|
|
2016-12-21 15:03:48 +00:00
|
|
|
output = " ".ljust(max_core_id_len + len('Core '))
|
2012-12-19 23:00:00 +00:00
|
|
|
for s in sockets:
|
2016-12-21 15:03:48 +00:00
|
|
|
output += " --------".ljust(max_core_map_len)
|
|
|
|
output += " "
|
|
|
|
print(output)
|
2012-12-19 23:00:00 +00:00
|
|
|
|
|
|
|
for c in cores:
|
2016-12-21 15:03:48 +00:00
|
|
|
output = "Core %s" % str(c).ljust(max_core_id_len)
|
2016-12-21 15:03:47 +00:00
|
|
|
for s in sockets:
|
2017-04-30 14:11:40 +00:00
|
|
|
if (s,c) in core_map:
|
2017-04-28 10:34:35 +00:00
|
|
|
output += " " + str(core_map[(s, c)]).ljust(max_core_map_len)
|
|
|
|
else:
|
|
|
|
output += " " * (max_core_map_len + 1)
|
2016-12-21 15:03:48 +00:00
|
|
|
print(output)
|