From e48bd12f891b4ee69f84b050e5dd53bc078cd589 Mon Sep 17 00:00:00 2001 From: Rafael Folco Date: Tue, 22 Apr 2025 17:12:30 -0300 Subject: [PATCH] Add get-cpu-range.py utility This utility imports the system_topology module to convert a cpu list to range. --- bin/get-cpu-range.py | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100755 bin/get-cpu-range.py diff --git a/bin/get-cpu-range.py b/bin/get-cpu-range.py new file mode 100755 index 0000000..fe0e161 --- /dev/null +++ b/bin/get-cpu-range.py @@ -0,0 +1,40 @@ +#!/bin/python3 + +import os +from pathlib import Path +import sys + +TOOLBOX_HOME = os.environ.get('TOOLBOX_HOME') +if TOOLBOX_HOME is None: + print("This script requires libraries that are provided by the toolbox project.") + print("Toolbox can be acquired from https://github.com/perftool-incubator/toolbox and") + print("then use 'export TOOLBOX_HOME=/path/to/toolbox' so that it can be located.") + exit(1) +else: + p = Path(TOOLBOX_HOME) / 'python' + if not p.exists() or not p.is_dir(): + print("ERROR: /python ('%s') does not exist!" % (p)) + exit(2) + sys.path.append(str(p)) + +from toolbox.system_cpu_topology import system_cpu_topology as topology + +def main(): + if len(sys.argv) != 2: + print("Usage: python get_cpu_range.py ") + print("\tExample: python get_cpu_range.py 0,1,5,10,12,11") + print("\t0-1,5,10-12") + sys.exit(1) + + # argv[1]="1,2,3" + # Convert into a list of integer numbers: cpu_list=[1,2,3] + cpu_list=list(map(int, sys.argv[1].split(','))) + # Get cpu range: cpu=['1-3'] + cpus=topology.formatted_cpu_list(cpu_list) + # Convert back into a cpu range string: cpu_range="1-3" + cpu_range=",".join(map(str, cpus)) + # Stdout: 1-3 + print(cpu_range) + +if __name__ == "__main__": + main()