summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTamir Duberstein <tamird@gmail.com>2024-11-02 08:09:48 -0400
committerShuah Khan <skhan@linuxfoundation.org>2025-01-14 14:56:49 -0700
commit56530007cac0630140a0846dd6110d60105a58ac (patch)
tree7924e4d81d10e6b90e111fce40ffd56bbc652d73
parent875aec2357cd22d412226e4134d1106248b0eb9d (diff)
kunit: add fallback for os.sched_getaffinity
Python 3.13 added os.process_cpu_count as a cross-platform alternative for the Linux-only os.sched_getaffinity. Use it when it's available and provide a fallback when it's not. This allows kunit to run on macOS. Signed-off-by: Tamir Duberstein <tamird@gmail.com> Reviewed-by: David Gow <davidgow@google.com> Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
-rwxr-xr-xtools/testing/kunit/kunit.py11
1 files changed, 10 insertions, 1 deletions
diff --git a/tools/testing/kunit/kunit.py b/tools/testing/kunit/kunit.py
index 676fa99a8b19..7f9ae55fd6d5 100755
--- a/tools/testing/kunit/kunit.py
+++ b/tools/testing/kunit/kunit.py
@@ -312,7 +312,16 @@ def massage_argv(argv: Sequence[str]) -> Sequence[str]:
return list(map(massage_arg, argv))
def get_default_jobs() -> int:
- return len(os.sched_getaffinity(0))
+ if sys.version_info >= (3, 13):
+ if (ncpu := os.process_cpu_count()) is not None:
+ return ncpu
+ raise RuntimeError("os.process_cpu_count() returned None")
+ # See https://github.com/python/cpython/blob/b61fece/Lib/os.py#L1175-L1186.
+ if sys.platform != "darwin":
+ return len(os.sched_getaffinity(0))
+ if (ncpu := os.cpu_count()) is not None:
+ return ncpu
+ raise RuntimeError("os.cpu_count() returned None")
def add_common_opts(parser: argparse.ArgumentParser) -> None:
parser.add_argument('--build_dir',