tuklib_integer.h: Fix a recent copypaste error in Clang detection.
[xz.git] / m4 / tuklib_cpucores.m4
blob873812d66b6ca7c2d75b81f9b1e163f6828f503a
2 # SYNOPSIS
4 #   TUKLIB_CPUCORES
6 # DESCRIPTION
8 #   Check how to find out the number of available CPU cores in the system.
9 #   This information is used by tuklib_cpucores.c.
11 #   Supported methods:
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
20 # COPYING
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; }
45 #else
46 compile error
47 #endif
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
54 # be good enough.
55 AC_LINK_IFELSE([AC_LANG_SOURCE([[
56 #include <sched.h>
57 int
58 main(void)
60         cpu_set_t cpu_mask;
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>
77 int
78 main(void)
80         cpuset_t set;
81         cpuset_getaffinity(CPU_LEVEL_WHICH, CPU_WHICH_PID, -1,
82                         sizeof(set), &set);
83         return 0;
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([[
95 #ifdef __QNX__
96 compile error
97 #endif
98 #ifdef HAVE_SYS_PARAM_H
99 #       include <sys/param.h>
100 #endif
101 #include <sys/sysctl.h>
103 main(void)
105 #ifdef HW_NCPUONLINE
106         /* This is preferred on OpenBSD, see tuklib_cpucores.c. */
107         int name[2] = { CTL_HW, HW_NCPUONLINE };
108 #else
109         int name[2] = { CTL_HW, HW_NCPU };
110 #endif
111         int cpus;
112         size_t cpus_size = sizeof(cpus);
113         sysctl(name, 2, &cpus, &cpus_size, NULL, 0);
114         return 0;
116 ]])], [tuklib_cv_cpucores_method=sysctl], [
118 AC_COMPILE_IFELSE([AC_LANG_SOURCE([[
119 #include <unistd.h>
121 main(void)
123         long i;
124 #ifdef _SC_NPROCESSORS_ONLN
125         /* Many systems using sysconf() */
126         i = sysconf(_SC_NPROCESSORS_ONLN);
127 #else
128         /* IRIX */
129         i = sysconf(_SC_NPROC_ONLN);
130 #endif
131         return 0;
133 ]])], [tuklib_cv_cpucores_method=sysconf], [
135 AC_COMPILE_IFELSE([AC_LANG_SOURCE([[
136 #include <sys/param.h>
137 #include <sys/pstat.h>
140 main(void)
142         struct pst_dynamic pst;
143         pstat_getdynamic(&pst, sizeof(pst), 1, 0);
144         (void)pst.psd_proc_cnt;
145         return 0;
147 ]])], [tuklib_cv_cpucores_method=pstat_getdynamic], [
149         tuklib_cv_cpucores_method=unknown
150 ])])])])])])])
152 case $tuklib_cv_cpucores_method in
153         sched_getaffinity)
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()])
157                 ;;
158         cpuset)
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).])
162                 ;;
163         sysctl)
164                 AC_DEFINE([TUKLIB_CPUCORES_SYSCTL], [1],
165                         [Define to 1 if the number of available CPU cores
166                         can be detected with sysctl().])
167                 ;;
168         sysconf)
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).])
173                 ;;
174         pstat_getdynamic)
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().])
178                 ;;
179 esac
180 ])dnl