8 # Check how to find out the number of available CPU cores in the system.
9 # This information is used by tuklib_cpucores.c.
12 # - GetSystemInfo(): Windows (including Cygwin)
13 # - sched_getaffinity(): glibc (GNU/Linux, GNU/kFreeBSD)
14 # - cpuset_getaffinity(): FreeBSD
15 # - sysctl(): BSDs, OS/2
16 # - sysconf(): GNU/Linux, Solaris, Tru64, IRIX, AIX, QNX, Cygwin (but
17 # GetSystemInfo() is used on Cygwin)
18 # - pstat_getdynamic(): HP-UX
22 # Author: Lasse Collin
24 # This file has been put into the public domain.
25 # You can do whatever you want with this file.
28 AC_DEFUN_ONCE([TUKLIB_CPUCORES], [
29 AC_REQUIRE([TUKLIB_COMMON])
31 # sys/param.h might be needed by sys/sysctl.h.
32 AC_CHECK_HEADERS([sys/param.h])
34 AC_CACHE_CHECK([how to detect the number of available CPU cores],
35 [tuklib_cv_cpucores_method], [
37 # Maybe checking $host_os would be enough but this matches what
38 # tuklib_cpucores.c does.
40 # NOTE: IRIX has a compiler that doesn't error out with #error, so use
41 # a non-compilable text instead of #error to generate an error.
42 AC_COMPILE_IFELSE([AC_LANG_SOURCE([[
43 #if defined(_WIN32) || defined(__CYGWIN__)
44 int main(void) { return 0; }
48 ]])], [tuklib_cv_cpucores_method=special], [
50 # glibc-based systems (GNU/Linux and GNU/kFreeBSD) have sched_getaffinity().
51 # The CPU_COUNT() macro was added in glibc 2.9 so we try to link the
52 # test program instead of merely compiling it. glibc 2.9 is old enough that
53 # if someone uses the code on older glibc, the fallback to sysconf() should
55 AC_LINK_IFELSE([AC_LANG_SOURCE([[
61 sched_getaffinity(0, sizeof(cpu_mask), &cpu_mask);
62 return CPU_COUNT(&cpu_mask);
64 ]])], [tuklib_cv_cpucores_method=sched_getaffinity], [
66 # FreeBSD has both cpuset and sysctl. Look for cpuset first because
67 # it's a better approach.
69 # This test would match on GNU/kFreeBSD too but it would require
70 # -lfreebsd-glue when linking and thus in the current form this would
71 # fail on GNU/kFreeBSD. The above test for sched_getaffinity() matches
72 # on GNU/kFreeBSD so the test below should never run on that OS.
73 AC_COMPILE_IFELSE([AC_LANG_SOURCE([[
74 #include <sys/param.h>
75 #include <sys/cpuset.h>
81 cpuset_getaffinity(CPU_LEVEL_WHICH, CPU_WHICH_PID, -1,
85 ]])], [tuklib_cv_cpucores_method=cpuset], [
87 # On OS/2, both sysconf() and sysctl() pass the tests in this file,
88 # but only sysctl() works. On QNX it's the opposite: only sysconf() works
89 # (although it assumes that _POSIX_SOURCE, _XOPEN_SOURCE, and _POSIX_C_SOURCE
90 # are undefined or alternatively _QNX_SOURCE is defined).
92 # We test sysctl() first and intentionally break the sysctl() test on QNX
93 # so that sysctl() is never used on QNX.
94 AC_COMPILE_IFELSE([AC_LANG_SOURCE([[
98 #ifdef HAVE_SYS_PARAM_H
99 # include <sys/param.h>
101 #include <sys/sysctl.h>
106 /* This is preferred on OpenBSD, see tuklib_cpucores.c. */
107 int name[2] = { CTL_HW, HW_NCPUONLINE };
109 int name[2] = { CTL_HW, HW_NCPU };
112 size_t cpus_size = sizeof(cpus);
113 sysctl(name, 2, &cpus, &cpus_size, NULL, 0);
116 ]])], [tuklib_cv_cpucores_method=sysctl], [
118 AC_COMPILE_IFELSE([AC_LANG_SOURCE([[
124 #ifdef _SC_NPROCESSORS_ONLN
125 /* Many systems using sysconf() */
126 i = sysconf(_SC_NPROCESSORS_ONLN);
129 i = sysconf(_SC_NPROC_ONLN);
133 ]])], [tuklib_cv_cpucores_method=sysconf], [
135 AC_COMPILE_IFELSE([AC_LANG_SOURCE([[
136 #include <sys/param.h>
137 #include <sys/pstat.h>
142 struct pst_dynamic pst;
143 pstat_getdynamic(&pst, sizeof(pst), 1, 0);
144 (void)pst.psd_proc_cnt;
147 ]])], [tuklib_cv_cpucores_method=pstat_getdynamic], [
149 tuklib_cv_cpucores_method=unknown
152 case $tuklib_cv_cpucores_method in
154 AC_DEFINE([TUKLIB_CPUCORES_SCHED_GETAFFINITY], [1],
155 [Define to 1 if the number of available CPU cores
156 can be detected with sched_getaffinity()])
159 AC_DEFINE([TUKLIB_CPUCORES_CPUSET], [1],
160 [Define to 1 if the number of available CPU cores
161 can be detected with cpuset(2).])
164 AC_DEFINE([TUKLIB_CPUCORES_SYSCTL], [1],
165 [Define to 1 if the number of available CPU cores
166 can be detected with sysctl().])
169 AC_DEFINE([TUKLIB_CPUCORES_SYSCONF], [1],
170 [Define to 1 if the number of available CPU cores
171 can be detected with sysconf(_SC_NPROCESSORS_ONLN)
172 or sysconf(_SC_NPROC_ONLN).])
175 AC_DEFINE([TUKLIB_CPUCORES_PSTAT_GETDYNAMIC], [1],
176 [Define to 1 if the number of available CPU cores
177 can be detected with pstat_getdynamic().])