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, 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 # Look for sysctl() solution first, because on OS/2, both sysconf()
65 # and sysctl() pass the tests in this file, but only sysctl()
67 AC_COMPILE_IFELSE([AC_LANG_SOURCE([[
68 #include <sys/types.h>
69 #ifdef HAVE_SYS_PARAM_H
70 # include <sys/param.h>
72 #include <sys/sysctl.h>
76 int name[2] = { CTL_HW, HW_NCPU };
78 size_t cpus_size = sizeof(cpus);
79 sysctl(name, 2, &cpus, &cpus_size, NULL, 0);
82 ]])], [tuklib_cv_cpucores_method=sysctl], [
84 AC_COMPILE_IFELSE([AC_LANG_SOURCE([[
90 #ifdef _SC_NPROCESSORS_ONLN
91 /* Many systems using sysconf() */
92 i = sysconf(_SC_NPROCESSORS_ONLN);
95 i = sysconf(_SC_NPROC_ONLN);
99 ]])], [tuklib_cv_cpucores_method=sysconf], [
101 AC_COMPILE_IFELSE([AC_LANG_SOURCE([[
102 #include <sys/param.h>
103 #include <sys/pstat.h>
108 struct pst_dynamic pst;
109 pstat_getdynamic(&pst, sizeof(pst), 1, 0);
110 (void)pst.psd_proc_cnt;
113 ]])], [tuklib_cv_cpucores_method=pstat_getdynamic], [
115 tuklib_cv_cpucores_method=unknown
118 case $tuklib_cv_cpucores_method in
120 AC_DEFINE([TUKLIB_CPUCORES_CPUSET], [1],
121 [Define to 1 if the number of available CPU cores
122 can be detected with cpuset(2).])
125 AC_DEFINE([TUKLIB_CPUCORES_SYSCTL], [1],
126 [Define to 1 if the number of available CPU cores
127 can be detected with sysctl().])
130 AC_DEFINE([TUKLIB_CPUCORES_SYSCONF], [1],
131 [Define to 1 if the number of available CPU cores
132 can be detected with sysconf(_SC_NPROCESSORS_ONLN)
133 or sysconf(_SC_NPROC_ONLN).])
136 AC_DEFINE([TUKLIB_CPUCORES_PSTAT_GETDYNAMIC], [1],
137 [Define to 1 if the number of available CPU cores
138 can be detected with pstat_getdynamic().])