Mesa (main): util: Fix setting nr_cpus on some BSD variants

GitLab Mirror gitlab-mirror at kemper.freedesktop.org
Tue Jun 15 20:26:14 UTC 2021


Module: Mesa
Branch: main
Commit: 5623c75e40b38017873df09954b5f65dc84dcde5
URL:    http://cgit.freedesktop.org/mesa/mesa/commit/?id=5623c75e40b38017873df09954b5f65dc84dcde5

Author: Ian Romanick <ian.d.romanick at intel.com>
Date:   Tue Jun  8 15:39:20 2021 -0700

util: Fix setting nr_cpus on some BSD variants

Linux, FreeBSD, and DragonFly should have _SC_NOPROCESSORS_ONLN.  NetBSD
and OpenBSD should have HW_NCPUONLINE.  This is what FFmpeg uses on
those platforms.

FreeBSD sysconf(3) manual page:

https://www.freebsd.org/cgi/man.cgi?query=sysconf&sektion=3&apropos=0&manpath=freebsd

The FFmpeg patch is at:

https://patchwork.ffmpeg.org/project/ffmpeg/patch/YGi4sJx3trG3Yn7c@humpty.home.comstyle.com/

OpenBSD sysctl(2) manual page:

https://man.openbsd.org/sysctl.2

Acked-by: Marek Olšák <marek.olsak at amd.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/11228>

---

 src/util/u_cpu_detect.c | 17 +++++++++++++++--
 1 file changed, 15 insertions(+), 2 deletions(-)

diff --git a/src/util/u_cpu_detect.c b/src/util/u_cpu_detect.c
index 5890fcf59f3..03a1d79fd58 100644
--- a/src/util/u_cpu_detect.c
+++ b/src/util/u_cpu_detect.c
@@ -565,7 +565,20 @@ util_cpu_detect_once(void)
       util_cpu_caps.nr_cpus = MAX2(1, system_info.dwNumberOfProcessors);
    }
 #elif defined(PIPE_OS_UNIX)
-#  if defined(_SC_NPROCESSORS_ONLN)
+   /* Linux, FreeBSD, DragonFly, and Mac OS X should have
+    * _SC_NOPROCESSORS_ONLN.  NetBSD and OpenBSD should have HW_NCPUONLINE.
+    * This is what FFmpeg uses on those platforms.
+    */
+#  if defined(PIPE_OS_BSD) && defined(HW_NCPUONLINE)
+   {
+      const int mib[] = { CTL_HW, HW_NCPUONLINE };
+      int ncpu;
+      int len = sizeof(ncpu);
+
+      sysctl(mib, 2, &ncpu, &len, NULL, 0);
+      util_cpu_caps.nr_cpus = ncpu;
+   }
+#  elif defined(_SC_NPROCESSORS_ONLN)
    util_cpu_caps.nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
    if (util_cpu_caps.nr_cpus == ~0)
       util_cpu_caps.nr_cpus = 1;
@@ -578,7 +591,7 @@ util_cpu_detect_once(void)
       sysctl(mib, 2, &ncpu, &len, NULL, 0);
       util_cpu_caps.nr_cpus = ncpu;
    }
-#  endif
+#  endif /* defined(PIPE_OS_BSD) */
 #endif /* defined(PIPE_OS_UNIX) */
 
    if (util_cpu_caps.nr_cpus == 0)



More information about the mesa-commit mailing list