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 9256545811; Mon, 19 Aug 2024 11:26:18 +0200 (CEST) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 444434028A; Mon, 19 Aug 2024 11:26:18 +0200 (CEST) Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by mails.dpdk.org (Postfix) with ESMTP id 7069540267 for ; Mon, 19 Aug 2024 11:26:15 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1724059574; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=zzlIJmfD/gtTlqzoWky5109Js/6aatLM9KZ0YRjduD4=; b=OBWyNep9TbotcfHQYHmkASjee23wNS6mW9UQALfGzVci2Nw5fez8k6jSYjN9vE5AxsxOSX j7AMlPQObEU33TLZf+XizzvCj7683ml2ddPZRbD8FsEVmo60CDyUoI79+MxES0HE6GEmZz UaEBLnyDgtcqCdrhpPhoR9HIn+k9irc= Received: from mail-wr1-f70.google.com (mail-wr1-f70.google.com [209.85.221.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-255-dAIDKbBROduiliDaueCdyw-1; Mon, 19 Aug 2024 05:26:13 -0400 X-MC-Unique: dAIDKbBROduiliDaueCdyw-1 Received: by mail-wr1-f70.google.com with SMTP id ffacd0b85a97d-3718d9d9267so1890149f8f.0 for ; Mon, 19 Aug 2024 02:26:12 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724059572; x=1724664372; h=in-reply-to:references:user-agent:to:from:cc:subject:message-id :date:content-transfer-encoding:mime-version:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=zzlIJmfD/gtTlqzoWky5109Js/6aatLM9KZ0YRjduD4=; b=NhHsEUldXndQIPd7isCBFg7ykNq8+N8J3ummsK0WNeo4PHihXwCymBXzOIXMN0H83h Y0rUjrcgV96r+XNq85afe0Z1cd50rGxRUDiNjHGGsw15GeloCNXMv3+vVaTgjMSukCCq WKka35QQXSt/pPQasoLjvKHMg9XYadSpd3Yg13o5xgjrf6xaXpD30QtfT5KObNxA2p3A W4XW3OdJs5nPewdaXoNl8UMky09cTCU/H75DkpJ0UCUC57K9TGWhd/E3z75XclDLpf5Q F+D9AQlRo4n35ruKFFRlCAw4Gn4WDbg+kAsPmR5yq0aEAoe8BeZg7Gsecgn50/Xpw7ZG 4gZA== X-Forwarded-Encrypted: i=1; AJvYcCVg9vhbBU8N54uMO7FAfF8uQstJkkDHUPsqWXQnDspAVSh0u7kHj0EgrVYi8vAz8KiunG5bRlCwwBpwJOE= X-Gm-Message-State: AOJu0Yxk46r5VzWgdS30pXTiVBXcJ1qmwT5cBkd2JT4/tHP4G1+rLNmM Qp5LbVj3ZXBoVl2J54tbKZwX5wA4RVuU8a1OcwcMZCZJqMfvHlw5taTFL9R6dCHUkOpEhfSTme+ lSkztRhcaals+6Xo20wvlXp8FK752xBoJ7KXd0n4p/IbZBNNcf9c= X-Received: by 2002:a5d:452b:0:b0:371:8c06:82ea with SMTP id ffacd0b85a97d-371943150a3mr5877591f8f.1.1724059571542; Mon, 19 Aug 2024 02:26:11 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEqi0hgVYh1bEm2vJeh+txwyiIdAf5Fz/X5dpJh0qtYQZDjYLRe1ATYNX4D4yfTlkHT0CNoog== X-Received: by 2002:a5d:452b:0:b0:371:8c06:82ea with SMTP id ffacd0b85a97d-371943150a3mr5877577f8f.1.1724059571026; Mon, 19 Aug 2024 02:26:11 -0700 (PDT) Received: from localhost (2a01cb00025433006239e1f47a0b2371.ipv6.abo.wanadoo.fr. [2a01:cb00:254:3300:6239:e1f4:7a0b:2371]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-429ded28cdasm155003775e9.16.2024.08.19.02.26.10 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 19 Aug 2024 02:26:10 -0700 (PDT) Mime-Version: 1.0 Date: Mon, 19 Aug 2024 11:26:10 +0200 Message-Id: Subject: Re: [PATCH v1 1/2] usertools/cpu_layout: update coding style Cc: From: "Robin Jarry" To: "Anatoly Burakov" , User-Agent: aerc/0.18.2-23-g4abdbd4014f5-dirty References: In-Reply-To: X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=UTF-8; format=Flowed 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 Anatoly Burakov, Aug 14, 2024 at 13:19: > Update coding style: > > - make it PEP-484 compliant > - address all flake8, mypy etc. warnings > - use f-strings in place of old-style string interpolation > - refactor printing to make the code more readable > > Signed-off-by: Anatoly Burakov > --- Hi Anatoly, thanks for the patch. Did you format the code using black/ruff? I'd like=20 to start keeping python code formatting consistent across user tools. > usertools/cpu_layout.py | 162 ++++++++++++++++++++++++++-------------- > 1 file changed, 104 insertions(+), 58 deletions(-) > > diff --git a/usertools/cpu_layout.py b/usertools/cpu_layout.py > index 891b9238fa..843b29a134 100755 > --- a/usertools/cpu_layout.py > +++ b/usertools/cpu_layout.py > @@ -3,62 +3,108 @@ > # Copyright(c) 2010-2014 Intel Corporation > # Copyright(c) 2017 Cavium, Inc. All rights reserved. > =20 > -sockets =3D [] > -cores =3D [] > -core_map =3D {} > -base_path =3D "/sys/devices/system/cpu" > -fd =3D open("{}/kernel_max".format(base_path)) > -max_cpus =3D int(fd.read()) > -fd.close() > -for cpu in range(max_cpus + 1): > - try: > - fd =3D open("{}/cpu{}/topology/core_id".format(base_path, cpu)) > - except IOError: > - continue > - core =3D int(fd.read()) > - fd.close() > - fd =3D open("{}/cpu{}/topology/physical_package_id".format(base_path= , cpu)) > - socket =3D int(fd.read()) > - fd.close() > - if core not in cores: > - cores.append(core) > - if socket not in sockets: > - sockets.append(socket) > - key =3D (socket, core) > - if key not in core_map: > - core_map[key] =3D [] > - core_map[key].append(cpu) > - > -print(format("=3D" * (47 + len(base_path)))) > -print("Core and Socket Information (as reported by '{}')".format(base_pa= th)) > -print("{}\n".format("=3D" * (47 + len(base_path)))) > -print("cores =3D ", cores) > -print("sockets =3D ", sockets) > -print("") > - > -max_processor_len =3D len(str(len(cores) * len(sockets) * 2 - 1)) > -max_thread_count =3D len(list(core_map.values())[0]) > -max_core_map_len =3D (max_processor_len * max_thread_count) \ > - + len(", ") * (max_thread_count - 1) \ > - + len('[]') + len('Socket ') > -max_core_id_len =3D len(str(max(cores))) > - > -output =3D " ".ljust(max_core_id_len + len('Core ')) > -for s in sockets: > - output +=3D " Socket %s" % str(s).ljust(max_core_map_len - len('Sock= et ')) > -print(output) > - > -output =3D " ".ljust(max_core_id_len + len('Core ')) > -for s in sockets: > - output +=3D " --------".ljust(max_core_map_len) > - output +=3D " " > -print(output) > - > -for c in cores: > - output =3D "Core %s" % str(c).ljust(max_core_id_len) > - for s in sockets: > - if (s, c) in core_map: > - output +=3D " " + str(core_map[(s, c)]).ljust(max_core_map_l= en) > +from typing import List, Set, Dict, Tuple > + > + > +def _range_expand(rstr: str) -> List[int]: I don't see any reason to prefix the symbols with an underscore in this=20 script. It will not be used as an importable library. > + """Expand a range string into a list of integers.""" > + # 0,1-3 =3D> [0, 1-3] > + ranges =3D rstr.split(",") > + valset: List[int] =3D [] > + for r in ranges: > + # 1-3 =3D> [1, 2, 3] > + if "-" in r: > + start, end =3D r.split("-") > + valset.extend(range(int(start), int(end) + 1)) > else: > - output +=3D " " * (max_core_map_len + 1) > - print(output) > + valset.append(int(r)) > + return valset > + > + > +def _read_sysfs(path: str) -> str: > + with open(path) as fd: > + return fd.read().strip() > + > + > +def _print_row(row: Tuple[str, ...], col_widths: List[int]) -> None: > + first, *rest =3D row > + w_first, *w_rest =3D col_widths > + first_end =3D " " * 4 > + rest_end =3D " " * 10 > + > + print(first.ljust(w_first), end=3Dfirst_end) > + for cell, width in zip(rest, w_rest): > + print(cell.rjust(width), end=3Drest_end) > + print() > + > + > +def _print_section(heading: str) -> None: > + sep =3D "=3D" * len(heading) > + print(sep) > + print(heading) > + print(sep) > + print() > + > + > +def _main() -> None: > + sockets_s: Set[int] =3D set() > + cores_s: Set[int] =3D set() > + core_map: Dict[Tuple[int, int], List[int]] =3D {} > + base_path =3D "/sys/devices/system/cpu" > + > + cpus =3D _range_expand(_read_sysfs(f"{base_path}/online")) > + > + for cpu in cpus: > + lcore_base =3D f"{base_path}/cpu{cpu}" > + core =3D int(_read_sysfs(f"{lcore_base}/topology/core_id")) > + socket =3D int(_read_sysfs(f"{lcore_base}/topology/physical_pack= age_id")) > + > + cores_s.add(core) > + sockets_s.add(socket) > + key =3D (socket, core) > + core_map.setdefault(key, []) > + core_map[key].append(cpu) > + > + cores =3D sorted(cores_s) > + sockets =3D sorted(sockets_s) > + > + _print_section("Core and Socket Information " > + f"(as reported by '{base_path}')") > + > + print("cores =3D ", cores) > + print("sockets =3D ", sockets) > + print() > + > + # Core, [Socket, Socket, ...] > + heading_strs =3D "", *[f"Socket {s}" for s in sockets] > + sep_strs =3D tuple("-" * len(hstr) for hstr in heading_strs) > + rows: List[Tuple[str, ...]] =3D [] > + > + for c in cores: > + # Core, > + row: Tuple[str, ...] =3D (f"Core {c}",) > + > + # [lcores, lcores, ...] > + for s in sockets: > + try: > + lcores =3D core_map[(s, c)] > + row +=3D (f"{lcores}",) > + except KeyError: > + row +=3D ("",) > + rows +=3D [row] > + > + # find max widths for each column, including header and rows > + max_widths =3D [ > + max([len(tup[col_idx]) for tup in rows + [heading_strs]]) > + for col_idx in range(len(heading_strs)) > + ] > + > + # print out table taking row widths into account > + _print_row(heading_strs, max_widths) > + _print_row(sep_strs, max_widths) > + for row in rows: > + _print_row(row, max_widths) > + > + > +if __name__ =3D=3D "__main__": > + _main() > --=20 > 2.43.5