1 # SPDX-License-Identifier: 0BSD
3 #############################################################################
11 # Check how to find out the number of available CPU cores in the system.
12 # This information is used by tuklib_cpucores.c.
15 # - GetSystemInfo(): Windows (including Cygwin)
16 # - sched_getaffinity(): glibc (GNU/Linux, GNU/kFreeBSD)
17 # - cpuset_getaffinity(): FreeBSD
18 # - sysctl(): BSDs, OS/2
19 # - sysconf(): GNU/Linux, Solaris, Tru64, IRIX, AIX, QNX, Cygwin (but
20 # GetSystemInfo() is used on Cygwin)
21 # - pstat_getdynamic(): HP-UX
23 #############################################################################
25 # Author: Lasse Collin
27 #############################################################################
29 AC_DEFUN_ONCE([TUKLIB_CPUCORES], [
30 AC_REQUIRE([TUKLIB_COMMON])
32 # sys/param.h might be needed by sys/sysctl.h.
33 AC_CHECK_HEADERS([sys/param.h])
35 AC_CACHE_CHECK([how to detect the number of available CPU cores],
36 [tuklib_cv_cpucores_method], [
38 # Maybe checking $host_os would be enough but this matches what
39 # tuklib_cpucores.c does.
41 # NOTE: IRIX has a compiler that doesn't error out with #error, so use
42 # a non-compilable text instead of #error to generate an error.
43 AC_COMPILE_IFELSE([AC_LANG_SOURCE([[
44 #if defined(_WIN32) || defined(__CYGWIN__)
45 int main(void) { return 0; }
49 ]])], [tuklib_cv_cpucores_method=special], [
51 # glibc-based systems (GNU/Linux and GNU/kFreeBSD) have sched_getaffinity().
52 # The CPU_COUNT() macro was added in glibc 2.9 so we try to link the
53 # test program instead of merely compiling it. glibc 2.9 is old enough that
54 # if someone uses the code on older glibc, the fallback to sysconf() should
56 AC_LINK_IFELSE([AC_LANG_SOURCE([[
62 sched_getaffinity(0, sizeof(cpu_mask), &cpu_mask);
63 return CPU_COUNT(&cpu_mask);
65 ]])], [tuklib_cv_cpucores_method=sched_getaffinity], [
67 # FreeBSD has both cpuset and sysctl. Look for cpuset first because
68 # it's a better approach.
70 # This test would match on GNU/kFreeBSD too but it would require
71 # -lfreebsd-glue when linking and thus in the current form this would
72 # fail on GNU/kFreeBSD. The above test for sched_getaffinity() matches
73 # on GNU/kFreeBSD so the test below should never run on that OS.
74 AC_LINK_IFELSE([AC_LANG_SOURCE([[
75 #include <sys/param.h>
76 #include <sys/cpuset.h>
82 cpuset_getaffinity(CPU_LEVEL_WHICH, CPU_WHICH_PID, -1,
86 ]])], [tuklib_cv_cpucores_method=cpuset], [
88 # On OS/2, both sysconf() and sysctl() pass the tests in this file,
89 # but only sysctl() works. On QNX it's the opposite: only sysconf() works
90 # (although it assumes that _POSIX_SOURCE, _XOPEN_SOURCE, and _POSIX_C_SOURCE
91 # are undefined or alternatively _QNX_SOURCE is defined).
93 # We test sysctl() first and intentionally break the sysctl() test on QNX
94 # so that sysctl() is never used on QNX.
95 AC_LINK_IFELSE([AC_LANG_SOURCE([[
99 #ifdef HAVE_SYS_PARAM_H
100 # include <sys/param.h>
102 #include <sys/sysctl.h>
107 /* This is preferred on OpenBSD, see tuklib_cpucores.c. */
108 int name[2] = { CTL_HW, HW_NCPUONLINE };
110 int name[2] = { CTL_HW, HW_NCPU };
113 size_t cpus_size = sizeof(cpus);
114 sysctl(name, 2, &cpus, &cpus_size, NULL, 0);
117 ]])], [tuklib_cv_cpucores_method=sysctl], [
119 AC_LINK_IFELSE([AC_LANG_SOURCE([[
125 #ifdef _SC_NPROCESSORS_ONLN
126 /* Many systems using sysconf() */
127 i = sysconf(_SC_NPROCESSORS_ONLN);
130 i = sysconf(_SC_NPROC_ONLN);
134 ]])], [tuklib_cv_cpucores_method=sysconf], [
136 AC_LINK_IFELSE([AC_LANG_SOURCE([[
137 #include <sys/param.h>
138 #include <sys/pstat.h>
143 struct pst_dynamic pst;
144 pstat_getdynamic(&pst, sizeof(pst), 1, 0);
145 (void)pst.psd_proc_cnt;
148 ]])], [tuklib_cv_cpucores_method=pstat_getdynamic], [
150 tuklib_cv_cpucores_method=unknown
153 case $tuklib_cv_cpucores_method in
155 AC_DEFINE([TUKLIB_CPUCORES_SCHED_GETAFFINITY], [1],
156 [Define to 1 if the number of available CPU cores
157 can be detected with sched_getaffinity()])
160 AC_DEFINE([TUKLIB_CPUCORES_CPUSET], [1],
161 [Define to 1 if the number of available CPU cores
162 can be detected with cpuset(2).])
165 AC_DEFINE([TUKLIB_CPUCORES_SYSCTL], [1],
166 [Define to 1 if the number of available CPU cores
167 can be detected with sysctl().])
170 AC_DEFINE([TUKLIB_CPUCORES_SYSCONF], [1],
171 [Define to 1 if the number of available CPU cores
172 can be detected with sysconf(_SC_NPROCESSORS_ONLN)
173 or sysconf(_SC_NPROC_ONLN).])
176 AC_DEFINE([TUKLIB_CPUCORES_PSTAT_GETDYNAMIC], [1],
177 [Define to 1 if the number of available CPU cores
178 can be detected with pstat_getdynamic().])