Commit 56530007 authored by Tamir Duberstein's avatar Tamir Duberstein Committed by Shuah Khan
Browse files

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: default avatarTamir Duberstein <tamird@gmail.com>
Reviewed-by: default avatarDavid Gow <davidgow@google.com>
Signed-off-by: default avatarShuah Khan <skhan@linuxfoundation.org>
parent 875aec23
Loading
Loading
Loading
Loading
+10 −1
Original line number Diff line number Diff line
@@ -312,7 +312,16 @@ def massage_argv(argv: Sequence[str]) -> Sequence[str]:
	return list(map(massage_arg, argv))

def get_default_jobs() -> int:
	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',