tuklib_cpucores.m4 revision 1.1.1.2
1#
2# SYNOPSIS
3#
4#   TUKLIB_CPUCORES
5#
6# DESCRIPTION
7#
8#   Check how to find out the number of available CPU cores in the system.
9#   This information is used by tuklib_cpucores.c.
10#
11#   Supported methods:
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
17#
18# COPYING
19#
20#   Author: Lasse Collin
21#
22#   This file has been put into the public domain.
23#   You can do whatever you want with this file.
24#
25
26AC_DEFUN_ONCE([TUKLIB_CPUCORES], [
27AC_REQUIRE([TUKLIB_COMMON])
28
29# sys/param.h might be needed by sys/sysctl.h.
30AC_CHECK_HEADERS([sys/param.h])
31
32AC_CACHE_CHECK([how to detect the number of available CPU cores],
33	[tuklib_cv_cpucores_method], [
34
35# Maybe checking $host_os would be enough but this matches what
36# tuklib_cpucores.c does.
37#
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.
40AC_COMPILE_IFELSE([AC_LANG_SOURCE([[
41#if defined(_WIN32) || defined(__CYGWIN__)
42int main(void) { return 0; }
43#else
44compile error
45#endif
46]])], [tuklib_cv_cpucores_method=special], [
47
48# FreeBSD has both cpuset and sysctl. Look for cpuset first because
49# it's a better approach.
50AC_COMPILE_IFELSE([AC_LANG_SOURCE([[
51#include <sys/param.h>
52#include <sys/cpuset.h>
53
54int
55main(void)
56{
57	cpuset_t set;
58	cpuset_getaffinity(CPU_LEVEL_WHICH, CPU_WHICH_PID, -1,
59			sizeof(set), &set);
60	return 0;
61}
62]])], [tuklib_cv_cpucores_method=cpuset], [
63
64# Look for sysctl() solution first, because on OS/2, both sysconf()
65# and sysctl() pass the tests in this file, but only sysctl()
66# actually works.
67AC_COMPILE_IFELSE([AC_LANG_SOURCE([[
68#include <sys/types.h>
69#ifdef HAVE_SYS_PARAM_H
70#	include <sys/param.h>
71#endif
72#include <sys/sysctl.h>
73int
74main(void)
75{
76	int name[2] = { CTL_HW, HW_NCPU };
77	int cpus;
78	size_t cpus_size = sizeof(cpus);
79	sysctl(name, 2, &cpus, &cpus_size, NULL, 0);
80	return 0;
81}
82]])], [tuklib_cv_cpucores_method=sysctl], [
83
84AC_COMPILE_IFELSE([AC_LANG_SOURCE([[
85#include <unistd.h>
86int
87main(void)
88{
89	long i;
90#ifdef _SC_NPROCESSORS_ONLN
91	/* Many systems using sysconf() */
92	i = sysconf(_SC_NPROCESSORS_ONLN);
93#else
94	/* IRIX */
95	i = sysconf(_SC_NPROC_ONLN);
96#endif
97	return 0;
98}
99]])], [tuklib_cv_cpucores_method=sysconf], [
100
101AC_COMPILE_IFELSE([AC_LANG_SOURCE([[
102#include <sys/param.h>
103#include <sys/pstat.h>
104
105int
106main(void)
107{
108	struct pst_dynamic pst;
109	pstat_getdynamic(&pst, sizeof(pst), 1, 0);
110	(void)pst.psd_proc_cnt;
111	return 0;
112}
113]])], [tuklib_cv_cpucores_method=pstat_getdynamic], [
114
115	tuklib_cv_cpucores_method=unknown
116])])])])])])
117
118case $tuklib_cv_cpucores_method in
119	cpuset)
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).])
123		;;
124	sysctl)
125		AC_DEFINE([TUKLIB_CPUCORES_SYSCTL], [1],
126			[Define to 1 if the number of available CPU cores
127			can be detected with sysctl().])
128		;;
129	sysconf)
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).])
134		;;
135	pstat_getdynamic)
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().])
139		;;
140esac
141])dnl
142