From: "Robin Jarry" <rjarry@redhat.com>
To: "Anatoly Burakov" <anatoly.burakov@intel.com>, <dev@dpdk.org>
Cc: <bruce.richardson@intel.com>
Subject: Re: [PATCH v1 1/2] usertools/cpu_layout: update coding style
Date: Mon, 19 Aug 2024 11:26:10 +0200 [thread overview]
Message-ID: <D3JS40KOCC9J.5TMGBER6V9W2@redhat.com> (raw)
In-Reply-To: <e51d00fea7c535404dda4d63e483639b13c48b2d.1723634354.git.anatoly.burakov@intel.com>
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 <anatoly.burakov@intel.com>
> ---
Hi Anatoly,
thanks for the patch. Did you format the code using black/ruff? I'd like
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.
>
> -sockets = []
> -cores = []
> -core_map = {}
> -base_path = "/sys/devices/system/cpu"
> -fd = open("{}/kernel_max".format(base_path))
> -max_cpus = int(fd.read())
> -fd.close()
> -for cpu in range(max_cpus + 1):
> - try:
> - fd = open("{}/cpu{}/topology/core_id".format(base_path, cpu))
> - 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)
> - 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)
> -
> -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("")
> -
> -max_processor_len = len(str(len(cores) * len(sockets) * 2 - 1))
> -max_thread_count = len(list(core_map.values())[0])
> -max_core_map_len = (max_processor_len * max_thread_count) \
> - + len(", ") * (max_thread_count - 1) \
> - + len('[]') + len('Socket ')
> -max_core_id_len = len(str(max(cores)))
> -
> -output = " ".ljust(max_core_id_len + len('Core '))
> -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 '))
> -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)
> +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
script. It will not be used as an importable library.
> + """Expand a range string into a list of integers."""
> + # 0,1-3 => [0, 1-3]
> + ranges = rstr.split(",")
> + valset: List[int] = []
> + for r in ranges:
> + # 1-3 => [1, 2, 3]
> + if "-" in r:
> + start, end = r.split("-")
> + valset.extend(range(int(start), int(end) + 1))
> else:
> - output += " " * (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 = row
> + w_first, *w_rest = col_widths
> + first_end = " " * 4
> + rest_end = " " * 10
> +
> + print(first.ljust(w_first), end=first_end)
> + for cell, width in zip(rest, w_rest):
> + print(cell.rjust(width), end=rest_end)
> + print()
> +
> +
> +def _print_section(heading: str) -> None:
> + sep = "=" * len(heading)
> + print(sep)
> + print(heading)
> + print(sep)
> + print()
> +
> +
> +def _main() -> None:
> + sockets_s: Set[int] = set()
> + cores_s: Set[int] = set()
> + core_map: Dict[Tuple[int, int], List[int]] = {}
> + base_path = "/sys/devices/system/cpu"
> +
> + cpus = _range_expand(_read_sysfs(f"{base_path}/online"))
> +
> + for cpu in cpus:
> + lcore_base = f"{base_path}/cpu{cpu}"
> + core = int(_read_sysfs(f"{lcore_base}/topology/core_id"))
> + socket = int(_read_sysfs(f"{lcore_base}/topology/physical_package_id"))
> +
> + cores_s.add(core)
> + sockets_s.add(socket)
> + key = (socket, core)
> + core_map.setdefault(key, [])
> + core_map[key].append(cpu)
> +
> + cores = sorted(cores_s)
> + sockets = sorted(sockets_s)
> +
> + _print_section("Core and Socket Information "
> + f"(as reported by '{base_path}')")
> +
> + print("cores = ", cores)
> + print("sockets = ", sockets)
> + print()
> +
> + # Core, [Socket, Socket, ...]
> + heading_strs = "", *[f"Socket {s}" for s in sockets]
> + sep_strs = tuple("-" * len(hstr) for hstr in heading_strs)
> + rows: List[Tuple[str, ...]] = []
> +
> + for c in cores:
> + # Core,
> + row: Tuple[str, ...] = (f"Core {c}",)
> +
> + # [lcores, lcores, ...]
> + for s in sockets:
> + try:
> + lcores = core_map[(s, c)]
> + row += (f"{lcores}",)
> + except KeyError:
> + row += ("",)
> + rows += [row]
> +
> + # find max widths for each column, including header and rows
> + max_widths = [
> + 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__ == "__main__":
> + _main()
> --
> 2.43.5
next prev parent reply other threads:[~2024-08-19 9:26 UTC|newest]
Thread overview: 64+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-08-14 11:19 Anatoly Burakov
2024-08-14 11:19 ` [PATCH v1 2/2] usertools/cpu_layout: print out NUMA nodes Anatoly Burakov
2024-08-16 12:16 ` [PATCH v2 1/4] usertools/cpu_layout: update coding style Anatoly Burakov
2024-08-16 12:16 ` [PATCH v2 2/4] usertools/cpu_layout: print out NUMA nodes Anatoly Burakov
2024-08-19 11:23 ` Robin Jarry
2024-08-16 12:16 ` [PATCH v2 3/4] usertools/dpdk-hugepages.py: sort by NUMA node Anatoly Burakov
2024-08-16 12:21 ` Burakov, Anatoly
2024-08-19 11:32 ` Robin Jarry
2024-08-20 9:04 ` Burakov, Anatoly
2024-08-20 9:06 ` Robin Jarry
2024-08-16 12:16 ` [PATCH v2 4/4] usertools/dpdk-devbind: print " Anatoly Burakov
2024-08-19 11:34 ` Robin Jarry
2024-08-20 9:08 ` Burakov, Anatoly
2024-08-20 9:28 ` Robin Jarry
2024-08-20 9:40 ` Burakov, Anatoly
2024-08-20 9:49 ` Robin Jarry
2024-08-20 9:56 ` Burakov, Anatoly
2024-08-20 10:00 ` Robin Jarry
2024-08-19 11:11 ` [PATCH v2 1/4] usertools/cpu_layout: update coding style Robin Jarry
2024-08-20 9:12 ` Burakov, Anatoly
2024-08-20 9:20 ` Robin Jarry
2024-08-20 9:31 ` Burakov, Anatoly
2024-08-20 9:45 ` Robin Jarry
2024-08-20 9:56 ` Burakov, Anatoly
2024-08-19 9:26 ` Robin Jarry [this message]
2024-08-19 9:36 ` [PATCH v1 1/2] " Burakov, Anatoly
2024-08-19 15:13 ` Stephen Hemminger
2024-08-20 15:35 ` [PATCH v3 1/4] " Anatoly Burakov
2024-08-20 15:35 ` [PATCH v3 2/4] usertools/cpu_layout: print out NUMA nodes Anatoly Burakov
2024-08-20 19:22 ` Robin Jarry
2024-08-21 8:49 ` Burakov, Anatoly
2024-08-20 15:35 ` [PATCH v3 3/4] usertools/dpdk-hugepages.py: update coding style Anatoly Burakov
2024-08-20 15:52 ` Stephen Hemminger
2024-08-21 8:53 ` Burakov, Anatoly
2024-08-20 15:57 ` Robin Jarry
2024-08-21 8:52 ` Burakov, Anatoly
2024-08-21 9:06 ` Burakov, Anatoly
2024-08-21 9:16 ` Burakov, Anatoly
2024-08-21 9:22 ` Robin Jarry
2024-08-20 15:35 ` [PATCH v3 4/4] usertools/dpdk-devbind: print NUMA node Anatoly Burakov
2024-08-20 15:59 ` [PATCH v3 1/4] usertools/cpu_layout: update coding style Robin Jarry
2024-08-21 8:49 ` Burakov, Anatoly
2024-08-21 9:22 ` [PATCH v4 " Anatoly Burakov
2024-08-21 9:22 ` [PATCH v4 2/4] usertools/cpu_layout: print out NUMA nodes Anatoly Burakov
2024-08-21 9:22 ` [PATCH v4 3/4] usertools/dpdk-hugepages.py: update coding style Anatoly Burakov
2024-08-21 9:26 ` Robin Jarry
2024-08-21 9:39 ` Burakov, Anatoly
2024-08-21 9:22 ` [PATCH v4 4/4] usertools/dpdk-devbind: print NUMA node Anatoly Burakov
2024-08-21 9:44 ` [PATCH v5 1/4] usertools/cpu_layout: update coding style Anatoly Burakov
2024-08-21 9:44 ` [PATCH v5 2/4] usertools/cpu_layout: print out NUMA nodes Anatoly Burakov
2024-08-21 11:43 ` Robin Jarry
2024-08-21 9:44 ` [PATCH v5 3/4] usertools/dpdk-hugepages.py: update coding style Anatoly Burakov
2024-08-21 11:43 ` Robin Jarry
2024-08-21 9:44 ` [PATCH v5 4/4] usertools/dpdk-devbind: print NUMA node Anatoly Burakov
2024-08-21 11:44 ` Robin Jarry
2024-08-21 10:16 ` [PATCH v5 1/4] usertools/cpu_layout: update coding style Robin Jarry
2024-08-22 10:38 ` [PATCH v6 " Anatoly Burakov
2024-08-22 10:38 ` [PATCH v6 2/4] usertools/cpu_layout: print out NUMA nodes Anatoly Burakov
2024-08-22 17:43 ` Robin Jarry
2024-10-12 17:56 ` Stephen Hemminger
2024-08-22 10:38 ` [PATCH v6 3/4] usertools/dpdk-hugepages.py: update coding style Anatoly Burakov
2024-08-22 10:38 ` [PATCH v6 4/4] usertools/dpdk-devbind: print NUMA node Anatoly Burakov
2024-10-12 17:57 ` Stephen Hemminger
2024-10-12 17:57 ` [PATCH v6 1/4] usertools/cpu_layout: update coding style Stephen Hemminger
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=D3JS40KOCC9J.5TMGBER6V9W2@redhat.com \
--to=rjarry@redhat.com \
--cc=anatoly.burakov@intel.com \
--cc=bruce.richardson@intel.com \
--cc=dev@dpdk.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).