It is located in the directory usertools, which is used to display the structure layout of the CPU in the current system.
First read the maximum number of CPU s supported by the current system.
base_path = "/sys/devices/system/cpu" fd = open("{}/kernel_max".format(base_path)) max_cpus = int(fd.read()) fd.close()
That is, read the files in the sys directory as follows:
$ cat /sys/devices/system/cpu/kernel_max 255 $
Secondly, loop through the cpuN subdirectory under the basic path (i.e. directory / sys/devices/system/cpu). N represents the maximum number of CPUs obtained from 0 to the previous step. The core ID value is obtained from the core ID file in the topology subdirectory, and the socket ID value is obtained from the content of the physical package ID file.
for cpu in xrange(max_cpus + 1): try: fd = open("{}/cpu{}/topology/core_id".format(base_path, cpu)) except IOError: continue 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) if key not in core_map: core_map[key] = [] core_map[key].append(cpu)
That is, traverse CPU n under the directory sys/devices/system/cpu /. N represents 0 to the maximum number of CPUs. Gets the core? ID value corresponding to the CPU. Suppose that the current system has two CPUs. As shown below, the core IDs of both CPUs are 0.
$ cat /sys/devices/system/cpu/cpu0/topology/core_id 0 $ cat /sys/devices/system/cpu/cpu1/topology/core_id 0 $
In addition, the physical package ID, socket, is obtained from the file physical package ID under the topology directory. The socket ID corresponding to CPU 0 of the current system is 0; the socket ID corresponding to CPU 1 is 2, as shown below.
$ cat /sys/devices/system/cpu/cpu0/topology/physical_package_id 0 $ cat /sys/devices/system/cpu/cpu1/topology/physical_package_id 2 $
The following output shows the basic information, mainly including the core cores data content traversed and the contents of the sockets array storing the socket ID.
print(format("=" * (47 + len(base_path)))) print("Core and Socket Information (as reported by '{}')".format(base_path)) print("{}\n".format("=" * (47 + len(base_path)))) print("cores = ", cores) print("sockets = ", sockets) print("")
The final output shows the relationship among CPU, core and socket. That is, when the directory is traversed at the beginning, the contents of the initialized core map array are indexed (socket, core), and the value is CPU.
for c in cores: output = "Core %s" % str(c).ljust(max_core_id_len) for s in sockets: if (s,c) in core_map: output += " " + str(core_map[(s, c)]).ljust(max_core_map_len) else: output += " " * (max_core_map_len + 1) print(output)
The final execution script is as follows:
$ ./cpu_layout.py ====================================================================== Core and Socket Information (as reported by '/sys/devices/system/cpu') ====================================================================== cores = [0] sockets = [0, 2] Socket 0 Socket 2 -------- -------- Core 0 [0] [1] $
DPDK-19.02