From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id 2180F4297F; Tue, 18 Apr 2023 18:31:32 +0200 (CEST) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id E1C3840A84; Tue, 18 Apr 2023 18:31:31 +0200 (CEST) Received: from mail-pl1-f172.google.com (mail-pl1-f172.google.com [209.85.214.172]) by mails.dpdk.org (Postfix) with ESMTP id 9EA1A4014F for ; Tue, 18 Apr 2023 18:31:30 +0200 (CEST) Received: by mail-pl1-f172.google.com with SMTP id d9443c01a7336-1a6ebc66ca4so8435845ad.3 for ; Tue, 18 Apr 2023 09:31:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=networkplumber-org.20221208.gappssmtp.com; s=20221208; t=1681835490; x=1684427490; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:subject:cc:to:from:date:from:to:cc:subject:date :message-id:reply-to; bh=zmDbhhD87z6yHKPfAtMLTgMPBGaRkp+BTFG868r271k=; b=U8JOVRT13rhmMAPa/ESo39/QblGxuDKQs4oMQl4FtC2Ucmfcf4Kge1354tPldBoF1t oegJjaK2pwmQMEFmqmvdGw8eyKEZEORpZdmIBMZcC4EJ0Tqu33bp6MxrPTAMqbph9MIF qF9cUBOiBkT81u7j/w322lkIW6Pvy6UsSWP/QLlLCb2NZ7YnyBPnMufHCgn9HrhWXQx4 VljLg5Q/fS3tcZYe5PjkdvAIJcGvdRQ9UiTssYzjiRa9ZebEhFYM/LxZQnkbNTPGdsla b1LzcNYgMZD/KBiwcOu2SqHekeVajXBWSnT72l1jxl2Kjfan2hxlc94ssKNmkZHqJf8z hbgQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681835490; x=1684427490; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:subject:cc:to:from:date:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=zmDbhhD87z6yHKPfAtMLTgMPBGaRkp+BTFG868r271k=; b=KrS5axEoyfgYZSo1R+k4eB8ZnTQQkDkTl6AQxKDTVJCAckt82EB8Y3CH9CPdPyqJGZ nvJPl3xwOgs6uk1/RR61t5sPvDtJp/fORo83pa9DtCZ8LcqJFgVKlKrUAU22d9uLQa8b VjxtVcvFvqcneNxR3K5IqSnFJ6IVzescOhAdcKtg0M2szqDqdXODMYFO0EMhSPcFB9WK /sqJSetHEYSD/689nn6VfhqwkUB75UEjZwMjFqjS8iypgA+LZsPRgB5CtcLH1JJ6EdYG rRKswn5Gn/kB7OF07Pvn92WOc8RkPe1flr/r/g0MH9V2EuE8MkXYqwdrji8hE+12IQbH Z96g== X-Gm-Message-State: AAQBX9eJmtI+jyBHiKTJ2yI7BjyeTPCvEcYbKo7ndzaZANLRN4iXQem8 bPFL5twCV8s67Eog4zGZ57q+lS7QEPQVkHjzcu8kjw== X-Google-Smtp-Source: AKy350boaL0W3Cvh1m0OoTI4N0j08pp8gD4OJ3o18g+eQxiEPQ0EDS+MnHFnBjmPCGVOBhsyGfE46A== X-Received: by 2002:a17:902:d18a:b0:1a6:5493:a3e8 with SMTP id m10-20020a170902d18a00b001a65493a3e8mr2331945plb.41.1681835489607; Tue, 18 Apr 2023 09:31:29 -0700 (PDT) Received: from hermes.local (204-195-120-218.wavecable.com. [204.195.120.218]) by smtp.gmail.com with ESMTPSA id jf7-20020a170903268700b001a2135e7eabsm9850080plb.16.2023.04.18.09.31.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 18 Apr 2023 09:31:28 -0700 (PDT) Date: Tue, 18 Apr 2023 09:31:25 -0700 From: Stephen Hemminger To: Wenzhuo Lu Cc: dev@dpdk.org Subject: Re: [PATCH] usertools: enhance CPU layout Message-ID: <20230418093125.03619b89@hermes.local> In-Reply-To: <1681795541-68384-1-git-send-email-wenzhuo.lu@intel.com> References: <1681795541-68384-1-git-send-email-wenzhuo.lu@intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org On Tue, 18 Apr 2023 13:25:41 +0800 Wenzhuo Lu wrote: > fd = open("{}/cpu{}/topology/physical_package_id".format(base_path, cpu)) > socket = int(fd.read()) > fd.close() > + fd = open("{}/cpu{}/topology/die_id".format(base_path, cpu)) > + die = int(fd.read()) > + fd.close() > + fd = open("{}/cpu{}/topology/thread_siblings_list".format(base_path, cpu)) > + threads_share = str(fd.read()) > + fd.close() > + fd = open("{}/cpu{}/cache/index2/shared_cpu_list".format(base_path, cpu)) > + l2_cache_share = str(fd.read()) > + fd.close() This code would read easier and follow more pythonish convention by using "with" and us f-strings. Are all these API's available on oldest supported kernel version for DPDK? 4.14 And there is a lot of duplicated code which indicates that a helper function would help. Not sure that the naming with -P and -E are generic enough, looks like something Intel specific?? Also, your code causes more python lint warnings about things like: usertools/cpu_layout.py:49:0: C0325: Unnecessary parens after 'if' keyword (superfluous-parens) Something like this? diff --git a/usertools/cpu_layout.py b/usertools/cpu_layout.py index 891b9238fa19..3423a735ccf6 100755 --- a/usertools/cpu_layout.py +++ b/usertools/cpu_layout.py @@ -1,38 +1,82 @@ #!/usr/bin/env python3 # SPDX-License-Identifier: BSD-3-Clause -# Copyright(c) 2010-2014 Intel Corporation +# Copyright(c) 2010-2023 Intel Corporation # Copyright(c) 2017 Cavium, Inc. All rights reserved. sockets = [] +dies = [] cores = [] +module_id = [] core_map = {} +core_p_e = {} +title_len = 47 +die_len = 8 +module_no = 0 +meaningful_module = [] base_path = "/sys/devices/system/cpu" -fd = open("{}/kernel_max".format(base_path)) -max_cpus = int(fd.read()) -fd.close() + + +def sysfs_cpu(attribute): + '''read string from sysfs cpu info''' + with open(f"{base_path}/{attribute}") as sysfs_fd: + return sysfs_fd.read() + + +max_cpus = int(sysfs_cpu("kernel_max")) + for cpu in range(max_cpus + 1): + topology = f"cpu{cpu}/topology" try: - fd = open("{}/cpu{}/topology/core_id".format(base_path, cpu)) + fd = open(f"{base_path}/{topology}/core_id") + core = int(fd.read()) + fd.close() except IOError: continue - 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) + socket = int(sysfs_cpu(f"{topology}/physical_package_id")) if socket not in sockets: sockets.append(socket) - key = (socket, core) + die = int(sysfs_cpu(f"{topology}/die_id")) + if die not in dies: + dies.append(die) + key = (socket, die, core) if key not in core_map: core_map[key] = [] + + threads_share = sysfs_cpu(f"{topology}/thread_siblings_list") + l2_cache_share = sysfs_cpu(f"cpu{cpu}/cache/index2/shared_cpu_list") + if threads_share == l2_cache_share: + p_e = '-P' + module_id.append(-1) + else: + module_tmp = [] + p_e = '-E' + for i in l2_cache_share: + if not i.isdigit(): + break + module_tmp.append(i) + if cpu == int("".join(module_tmp)): + module_id.append(module_no) + module_no += 1 + else: + module_id.append(-1) + key_p_e = (die, core) + if key_p_e not in core_p_e: + core_p_e[key_p_e] = p_e core_map[key].append(cpu) -print(format("=" * (47 + len(base_path)))) +print(format("=" * (title_len + len(base_path)))) print("Core and Socket Information (as reported by '{}')".format(base_path)) -print("{}\n".format("=" * (47 + len(base_path)))) +print("{}\n".format("=" * (title_len + len(base_path)))) print("cores = ", cores) + +for i in module_id: + if i != -1: + meaningful_module.append(i) +print("modules = ", meaningful_module) +print("dies = ", dies) print("sockets = ", sockets) print("") @@ -43,22 +87,30 @@ + len('[]') + len('Socket ') max_core_id_len = len(str(max(cores))) -output = " ".ljust(max_core_id_len + len('Core ')) +socket_space_len = max_core_id_len + len('Core ') + die_len + len('-P') +output = " ".ljust(socket_space_len) for s in sockets: output += " Socket %s" % str(s).ljust(max_core_map_len - len('Socket ')) print(output) -output = " ".ljust(max_core_id_len + len('Core ')) +output = " ".ljust(socket_space_len) for s in sockets: output += " --------".ljust(max_core_map_len) output += " " print(output) -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) +for d in dies: + print("Die", die) + for c in cores: + if module_id[core_map[(sockets[0], d, c)][0]] != -1: + print(" Module", module_id[core_map[(sockets[0], d, c)][0]]) + output = " ".ljust(die_len) + output += "Core" + output += core_p_e[(d, c)] + output += " %s" % str(c).ljust(max_core_id_len) + for s in sockets: + if (s, d, c) in core_map: + output += " " + str(core_map[(s, d, c)]).ljust(max_core_map_len) + else: + output += " " * (max_core_map_len + 1) + print(output) -- 2.39.2