dpdk/usertools/cpu_layout.py
<<
>>
Prefs
   1#!/usr/bin/env python3
   2# SPDX-License-Identifier: BSD-3-Clause
   3# Copyright(c) 2010-2014 Intel Corporation
   4# Copyright(c) 2017 Cavium, Inc. All rights reserved.
   5
   6sockets = []
   7cores = []
   8core_map = {}
   9base_path = "/sys/devices/system/cpu"
  10fd = open("{}/kernel_max".format(base_path))
  11max_cpus = int(fd.read())
  12fd.close()
  13for cpu in range(max_cpus + 1):
  14    try:
  15        fd = open("{}/cpu{}/topology/core_id".format(base_path, cpu))
  16    except IOError:
  17        continue
  18    core = int(fd.read())
  19    fd.close()
  20    fd = open("{}/cpu{}/topology/physical_package_id".format(base_path, cpu))
  21    socket = int(fd.read())
  22    fd.close()
  23    if core not in cores:
  24        cores.append(core)
  25    if socket not in sockets:
  26        sockets.append(socket)
  27    key = (socket, core)
  28    if key not in core_map:
  29        core_map[key] = []
  30    core_map[key].append(cpu)
  31
  32print(format("=" * (47 + len(base_path))))
  33print("Core and Socket Information (as reported by '{}')".format(base_path))
  34print("{}\n".format("=" * (47 + len(base_path))))
  35print("cores = ", cores)
  36print("sockets = ", sockets)
  37print("")
  38
  39max_processor_len = len(str(len(cores) * len(sockets) * 2 - 1))
  40max_thread_count = len(list(core_map.values())[0])
  41max_core_map_len = (max_processor_len * max_thread_count)  \
  42                      + len(", ") * (max_thread_count - 1) \
  43                      + len('[]') + len('Socket ')
  44max_core_id_len = len(str(max(cores)))
  45
  46output = " ".ljust(max_core_id_len + len('Core '))
  47for s in sockets:
  48    output += " Socket %s" % str(s).ljust(max_core_map_len - len('Socket '))
  49print(output)
  50
  51output = " ".ljust(max_core_id_len + len('Core '))
  52for s in sockets:
  53    output += " --------".ljust(max_core_map_len)
  54    output += " "
  55print(output)
  56
  57for c in cores:
  58    output = "Core %s" % str(c).ljust(max_core_id_len)
  59    for s in sockets:
  60        if (s, c) in core_map:
  61            output += " " + str(core_map[(s, c)]).ljust(max_core_map_len)
  62        else:
  63            output += " " * (max_core_map_len + 1)
  64    print(output)
  65