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 # - sysctl(): BSDs, OS/2
14 # - sysconf(): GNU/Linux, Solaris, Tru64, IRIX, AIX, QNX, Cygwin (but
15 # GetSystemInfo() is used on Cygwin)
16 # - pstat_getdynamic(): HP-UX
20 # Author: Lasse Collin
22 # This file has been put into the public domain.
23 # You can do whatever you want with this file.
26 AC_DEFUN_ONCE([TUKLIB_CPUCORES], [
27 AC_REQUIRE([TUKLIB_COMMON])
29 # sys/param.h might be needed by sys/sysctl.h.
30 AC_CHECK_HEADERS([sys/param.h])
32 AC_CACHE_CHECK([how to detect the number of available CPU cores],
33 [tuklib_cv_cpucores_method], [
35 # Maybe checking $host_os would be enough but this matches what
36 # tuklib_cpucores.c does.
38 # NOTE: IRIX has a compiler that doesn't error out with #error, so use
39 # a non-compilable text instead of #error to generate an error.
40 AC_COMPILE_IFELSE([AC_LANG_SOURCE([[
41 #if defined(_WIN32) || defined(__CYGWIN__)
42 int main(void) { return 0; }
46 ]])], [tuklib_cv_cpucores_method=special], [
48 # FreeBSD has both cpuset and sysctl. Look for cpuset first because
49 # it's a better approach.
50 AC_COMPILE_IFELSE([AC_LANG_SOURCE([[
51 #include <sys/param.h>
52 #include <sys/cpuset.h>
58 cpuset_getaffinity(CPU_LEVEL_WHICH, CPU_WHICH_PID, -1,
62 ]])], [tuklib_cv_cpucores_method=cpuset], [
64 # On OS/2, both sysconf() and sysctl() pass the tests in this file,
65 # but only sysctl() works. On QNX it's the opposite: only sysconf() works
66 # (although it assumes that _POSIX_SOURCE, _XOPEN_SOURCE, and _POSIX_C_SOURCE
67 # are undefined or alternatively _QNX_SOURCE is defined).
69 # We test sysctl() first and intentionally break the sysctl() test on QNX
70 # so that sysctl() is never used on QNX.
71 AC_COMPILE_IFELSE([AC_LANG_SOURCE([[
75 #include <sys/types.h>
76 #ifdef HAVE_SYS_PARAM_H
77 # include <sys/param.h>
79 #include <sys/sysctl.h>
83 int name[2] = { CTL_HW, HW_NCPU };
85 size_t cpus_size = sizeof(cpus);
86 sysctl(name, 2, &cpus, &cpus_size, NULL, 0);
89 ]])], [tuklib_cv_cpucores_method=sysctl], [
91 AC_COMPILE_IFELSE([AC_LANG_SOURCE([[
97 #ifdef _SC_NPROCESSORS_ONLN
98 /* Many systems using sysconf() */
99 i = sysconf(_SC_NPROCESSORS_ONLN);
102 i = sysconf(_SC_NPROC_ONLN);
106 ]])], [tuklib_cv_cpucores_method=sysconf], [
108 AC_COMPILE_IFELSE([AC_LANG_SOURCE([[
109 #include <sys/param.h>
110 #include <sys/pstat.h>
115 struct pst_dynamic pst;
116 pstat_getdynamic(&pst, sizeof(pst), 1, 0);
117 (void)pst.psd_proc_cnt;
120 ]])], [tuklib_cv_cpucores_method=pstat_getdynamic], [
122 tuklib_cv_cpucores_method=unknown
125 case $tuklib_cv_cpucores_method in
127 AC_DEFINE([TUKLIB_CPUCORES_CPUSET], [1],
128 [Define to 1 if the number of available CPU cores
129 can be detected with cpuset(2).])
132 AC_DEFINE([TUKLIB_CPUCORES_SYSCTL], [1],
133 [Define to 1 if the number of available CPU cores
134 can be detected with sysctl().])
137 AC_DEFINE([TUKLIB_CPUCORES_SYSCONF], [1],
138 [Define to 1 if the number of available CPU cores
139 can be detected with sysconf(_SC_NPROCESSORS_ONLN)
140 or sysconf(_SC_NPROC_ONLN).])
143 AC_DEFINE([TUKLIB_CPUCORES_PSTAT_GETDYNAMIC], [1],
144 [Define to 1 if the number of available CPU cores
145 can be detected with pstat_getdynamic().])