DPDK用户工具cpu

    xiaoxiao2022-07-13  154

     

    其位于目录usertools下,用于显示当前系统中CPU的结构布局。

    首先读取当前系统支持的最大CPU数量。

    base_path = "/sys/devices/system/cpu" fd = open("{}/kernel_max".format(base_path)) max_cpus = int(fd.read()) fd.close()

    即读取如下的sys目录中的文件:

    $ cat /sys/devices/system/cpu/kernel_max 255 $

    其次,循环遍历基础路径下(即目录/sys/devices/system/cpu)的cpuN子目录,N表示从0到上一步获得的最大CPU数量值。由其中的topology子目录下的core_id文件获取到核心ID值,由文件physical_package_id的内容得到socket ID值。

    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)

    即在目录sys/devices/system/cpu/下遍历cpuN,N表示0到最大CPU数量。取得CPU对应的core_id值。假设当前系统有2个CPU,如下得到两个CPU的CORE ID都为0。

    $ cat /sys/devices/system/cpu/cpu0/topology/core_id 0 $ cat /sys/devices/system/cpu/cpu1/topology/core_id 0 $

    并且,由topology目录下的文件physical_package_id获取到物理封装ID,即socket。当前系统的CPU 0所对应的socket ID为0;CPU 1所对应的socket ID为2,如下所示。

    $ cat /sys/devices/system/cpu/cpu0/topology/physical_package_id 0 $ cat /sys/devices/system/cpu/cpu1/topology/physical_package_id 2 $

    以下输出显示基本信息,主要为遍历到的核心cores数据内容,以及保存socket ID的sockets数组内容。

    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("")

    最后的输出,显示CPU、core与socket三者之间的关系,即在最开始遍历目录时,初始化好的core_map数组的内容,其索引时(socket, core),值为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)

    最终执行脚本显示如下:

    $ ./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

     

    最新回复(0)