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>
---
tools/testing/kunit/kunit.py | 11 ++++++++++-
1 file changed, 10 insertions(+), 1 deletion(-)
diff --git a/tools/testing/kunit/kunit.py b/tools/testing/kunit/kunit.py
index bc74088c458aee20b1a21fdeb9f3cb01ab20fec4..3a8cbb868ac559f68d047e38be92f7c64a3314ea 100755
--- a/tools/testing/kunit/kunit.py
+++ b/tools/testing/kunit/kunit.py
@@ -303,7 +303,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',
--
2.47.0
On Sat, 2 Nov 2024 at 20:10, Tamir Duberstein <tamird@gmail.com> wrote:
>
> 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>
> ---
Looks plausible enough to me. Thanks very much!
Reviewed-by: David Gow <davidgow@google.com>
Cheers,
-- David
> tools/testing/kunit/kunit.py | 11 ++++++++++-
> 1 file changed, 10 insertions(+), 1 deletion(-)
>
> diff --git a/tools/testing/kunit/kunit.py b/tools/testing/kunit/kunit.py
> index bc74088c458aee20b1a21fdeb9f3cb01ab20fec4..3a8cbb868ac559f68d047e38be92f7c64a3314ea 100755
> --- a/tools/testing/kunit/kunit.py
> +++ b/tools/testing/kunit/kunit.py
> @@ -303,7 +303,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',
>
> --
> 2.47.0
>
On Tue, Nov 5, 2024 at 3:36 AM David Gow <davidgow@google.com> wrote: > > On Sat, 2 Nov 2024 at 20:10, Tamir Duberstein <tamird@gmail.com> wrote: > > > > 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> > > --- > > Looks plausible enough to me. Thanks very much! > > Reviewed-by: David Gow <davidgow@google.com> > > Cheers, > -- David Thanks David! While the next patch is still plausibly undergoing discussion, would it be possible to pick this one up? Without it kunit.py is not usable on macOS.
+Shuah Khan <skhan@linuxfoundation.org> On Sat, Dec 7, 2024 at 1:52 PM Tamir Duberstein <tamird@gmail.com> wrote: > > Thanks David! While the next patch is still plausibly undergoing > discussion, would it be possible to pick this one up? Without it > kunit.py is not usable on macOS. Shuah, could you pick this if you agree? Cheers, Tamir
© 2016 - 2026 Red Hat, Inc.