debian: New upstream release
[xz/debian.git] / m4 / tuklib_cpucores.m4
blob468c2db639ad6897c3f979102c880b697a155be7
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 #     - 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
18 # COPYING
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; }
43 #else
44 compile error
45 #endif
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>
54 int
55 main(void)
57         cpuset_t set;
58         cpuset_getaffinity(CPU_LEVEL_WHICH, CPU_WHICH_PID, -1,
59                         sizeof(set), &set);
60         return 0;
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([[
72 #ifdef __QNX__
73 compile error
74 #endif
75 #include <sys/types.h>
76 #ifdef HAVE_SYS_PARAM_H
77 #       include <sys/param.h>
78 #endif
79 #include <sys/sysctl.h>
80 int
81 main(void)
83         int name[2] = { CTL_HW, HW_NCPU };
84         int cpus;
85         size_t cpus_size = sizeof(cpus);
86         sysctl(name, 2, &cpus, &cpus_size, NULL, 0);
87         return 0;
89 ]])], [tuklib_cv_cpucores_method=sysctl], [
91 AC_COMPILE_IFELSE([AC_LANG_SOURCE([[
92 #include <unistd.h>
93 int
94 main(void)
96         long i;
97 #ifdef _SC_NPROCESSORS_ONLN
98         /* Many systems using sysconf() */
99         i = sysconf(_SC_NPROCESSORS_ONLN);
100 #else
101         /* IRIX */
102         i = sysconf(_SC_NPROC_ONLN);
103 #endif
104         return 0;
106 ]])], [tuklib_cv_cpucores_method=sysconf], [
108 AC_COMPILE_IFELSE([AC_LANG_SOURCE([[
109 #include <sys/param.h>
110 #include <sys/pstat.h>
113 main(void)
115         struct pst_dynamic pst;
116         pstat_getdynamic(&pst, sizeof(pst), 1, 0);
117         (void)pst.psd_proc_cnt;
118         return 0;
120 ]])], [tuklib_cv_cpucores_method=pstat_getdynamic], [
122         tuklib_cv_cpucores_method=unknown
123 ])])])])])])
125 case $tuklib_cv_cpucores_method in
126         cpuset)
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).])
130                 ;;
131         sysctl)
132                 AC_DEFINE([TUKLIB_CPUCORES_SYSCTL], [1],
133                         [Define to 1 if the number of available CPU cores
134                         can be detected with sysctl().])
135                 ;;
136         sysconf)
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).])
141                 ;;
142         pstat_getdynamic)
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().])
146                 ;;
147 esac
148 ])dnl