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#     - sysctl(): BSDs, OS/2
13#     - sysconf(): GNU/Linux, Solaris, Tru64, IRIX, AIX, Cygwin
14#     - pstat_getdynamic(): HP-UX
15#
16# COPYING
17#
18#   Author: Lasse Collin
19#
20#   This file has been put into the public domain.
21#   You can do whatever you want with this file.
22#
23
24AC_DEFUN_ONCE([TUKLIB_CPUCORES], [
25AC_REQUIRE([TUKLIB_COMMON])
26
27# sys/param.h might be needed by sys/sysctl.h.
28AC_CHECK_HEADERS([sys/param.h])
29
30AC_CACHE_CHECK([how to detect the number of available CPU cores],
31	[tuklib_cv_cpucores_method], [
32
33# Look for sysctl() solution first, because on OS/2, both sysconf()
34# and sysctl() pass the tests in this file, but only sysctl()
35# actually works.
36AC_COMPILE_IFELSE([AC_LANG_SOURCE([[
37#include <sys/types.h>
38#ifdef HAVE_SYS_PARAM_H
39#	include <sys/param.h>
40#endif
41#include <sys/sysctl.h>
42int
43main(void)
44{
45	int name[2] = { CTL_HW, HW_NCPU };
46	int cpus;
47	size_t cpus_size = sizeof(cpus);
48	sysctl(name, 2, &cpus, &cpus_size, NULL, 0);
49	return 0;
50}
51]])], [tuklib_cv_cpucores_method=sysctl], [
52
53AC_COMPILE_IFELSE([AC_LANG_SOURCE([[
54#include <unistd.h>
55int
56main(void)
57{
58	long i;
59#ifdef _SC_NPROCESSORS_ONLN
60	/* Many systems using sysconf() */
61	i = sysconf(_SC_NPROCESSORS_ONLN);
62#else
63	/* IRIX */
64	i = sysconf(_SC_NPROC_ONLN);
65#endif
66	return 0;
67}
68]])], [tuklib_cv_cpucores_method=sysconf], [
69
70AC_COMPILE_IFELSE([AC_LANG_SOURCE([[
71#include <sys/param.h>
72#include <sys/pstat.h>
73
74int
75main(void)
76{
77	struct pst_dynamic pst;
78	pstat_getdynamic(&pst, sizeof(pst), 1, 0);
79	(void)pst.psd_proc_cnt;
80	return 0;
81}
82]])], [tuklib_cv_cpucores_method=pstat_getdynamic], [
83
84	tuklib_cv_cpucores_method=unknown
85])])])])
86
87case $tuklib_cv_cpucores_method in
88	sysctl)
89		AC_DEFINE([TUKLIB_CPUCORES_SYSCTL], [1],
90			[Define to 1 if the number of available CPU cores
91			can be detected with sysctl().])
92		;;
93	sysconf)
94		AC_DEFINE([TUKLIB_CPUCORES_SYSCONF], [1],
95			[Define to 1 if the number of available CPU cores
96			can be detected with sysconf(_SC_NPROCESSORS_ONLN)
97			or sysconf(_SC_NPROC_ONLN).])
98		;;
99	pstat_getdynamic)
100		AC_DEFINE([TUKLIB_CPUCORES_PSTAT_GETDYNAMIC], [1],
101			[Define to 1 if the number of available CPU cores
102			can be detected with pstat_getdynamic().])
103		;;
104esac
105])dnl
106