Skip to content

Commit 5653000

Browse files
tamirdshuahkh
authored andcommitted
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>
1 parent 875aec2 commit 5653000

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

tools/testing/kunit/kunit.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,16 @@ def massage_arg(arg: str) -> str:
312312
return list(map(massage_arg, argv))
313313

314314
def get_default_jobs() -> int:
315-
return len(os.sched_getaffinity(0))
315+
if sys.version_info >= (3, 13):
316+
if (ncpu := os.process_cpu_count()) is not None:
317+
return ncpu
318+
raise RuntimeError("os.process_cpu_count() returned None")
319+
# See https://github.com/python/cpython/blob/b61fece/Lib/os.py#L1175-L1186.
320+
if sys.platform != "darwin":
321+
return len(os.sched_getaffinity(0))
322+
if (ncpu := os.cpu_count()) is not None:
323+
return ncpu
324+
raise RuntimeError("os.cpu_count() returned None")
316325

317326
def add_common_opts(parser: argparse.ArgumentParser) -> None:
318327
parser.add_argument('--build_dir',

0 commit comments

Comments
 (0)