tuklib_cpucores.m4 revision 1.1.1.3
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# - 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 19# 20# COPYING 21# 22# Author: Lasse Collin 23# 24# This file has been put into the public domain. 25# You can do whatever you want with this file. 26# 27 28AC_DEFUN_ONCE([TUKLIB_CPUCORES], [ 29AC_REQUIRE([TUKLIB_COMMON]) 30 31# sys/param.h might be needed by sys/sysctl.h. 32AC_CHECK_HEADERS([sys/param.h]) 33 34AC_CACHE_CHECK([how to detect the number of available CPU cores], 35 [tuklib_cv_cpucores_method], [ 36 37# Maybe checking $host_os would be enough but this matches what 38# tuklib_cpucores.c does. 39# 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. 42AC_COMPILE_IFELSE([AC_LANG_SOURCE([[ 43#if defined(_WIN32) || defined(__CYGWIN__) 44int main(void) { return 0; } 45#else 46compile error 47#endif 48]])], [tuklib_cv_cpucores_method=special], [ 49 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. 55AC_LINK_IFELSE([AC_LANG_SOURCE([[ 56#include <sched.h> 57int 58main(void) 59{ 60 cpu_set_t cpu_mask; 61 sched_getaffinity(0, sizeof(cpu_mask), &cpu_mask); 62 return CPU_COUNT(&cpu_mask); 63} 64]])], [tuklib_cv_cpucores_method=sched_getaffinity], [ 65 66# FreeBSD has both cpuset and sysctl. Look for cpuset first because 67# it's a better approach. 68# 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. 73AC_COMPILE_IFELSE([AC_LANG_SOURCE([[ 74#include <sys/param.h> 75#include <sys/cpuset.h> 76 77int 78main(void) 79{ 80 cpuset_t set; 81 cpuset_getaffinity(CPU_LEVEL_WHICH, CPU_WHICH_PID, -1, 82 sizeof(set), &set); 83 return 0; 84} 85]])], [tuklib_cv_cpucores_method=cpuset], [ 86 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). 91# 92# We test sysctl() first and intentionally break the sysctl() test on QNX 93# so that sysctl() is never used on QNX. 94AC_COMPILE_IFELSE([AC_LANG_SOURCE([[ 95#ifdef __QNX__ 96compile error 97#endif 98#include <sys/types.h> 99#ifdef HAVE_SYS_PARAM_H 100# include <sys/param.h> 101#endif 102#include <sys/sysctl.h> 103int 104main(void) 105{ 106 int name[2] = { CTL_HW, HW_NCPU }; 107 int cpus; 108 size_t cpus_size = sizeof(cpus); 109 sysctl(name, 2, &cpus, &cpus_size, NULL, 0); 110 return 0; 111} 112]])], [tuklib_cv_cpucores_method=sysctl], [ 113 114AC_COMPILE_IFELSE([AC_LANG_SOURCE([[ 115#include <unistd.h> 116int 117main(void) 118{ 119 long i; 120#ifdef _SC_NPROCESSORS_ONLN 121 /* Many systems using sysconf() */ 122 i = sysconf(_SC_NPROCESSORS_ONLN); 123#else 124 /* IRIX */ 125 i = sysconf(_SC_NPROC_ONLN); 126#endif 127 return 0; 128} 129]])], [tuklib_cv_cpucores_method=sysconf], [ 130 131AC_COMPILE_IFELSE([AC_LANG_SOURCE([[ 132#include <sys/param.h> 133#include <sys/pstat.h> 134 135int 136main(void) 137{ 138 struct pst_dynamic pst; 139 pstat_getdynamic(&pst, sizeof(pst), 1, 0); 140 (void)pst.psd_proc_cnt; 141 return 0; 142} 143]])], [tuklib_cv_cpucores_method=pstat_getdynamic], [ 144 145 tuklib_cv_cpucores_method=unknown 146])])])])])])]) 147 148case $tuklib_cv_cpucores_method in 149 sched_getaffinity) 150 AC_DEFINE([TUKLIB_CPUCORES_SCHED_GETAFFINITY], [1], 151 [Define to 1 if the number of available CPU cores 152 can be detected with sched_getaffinity()]) 153 ;; 154 cpuset) 155 AC_DEFINE([TUKLIB_CPUCORES_CPUSET], [1], 156 [Define to 1 if the number of available CPU cores 157 can be detected with cpuset(2).]) 158 ;; 159 sysctl) 160 AC_DEFINE([TUKLIB_CPUCORES_SYSCTL], [1], 161 [Define to 1 if the number of available CPU cores 162 can be detected with sysctl().]) 163 ;; 164 sysconf) 165 AC_DEFINE([TUKLIB_CPUCORES_SYSCONF], [1], 166 [Define to 1 if the number of available CPU cores 167 can be detected with sysconf(_SC_NPROCESSORS_ONLN) 168 or sysconf(_SC_NPROC_ONLN).]) 169 ;; 170 pstat_getdynamic) 171 AC_DEFINE([TUKLIB_CPUCORES_PSTAT_GETDYNAMIC], [1], 172 [Define to 1 if the number of available CPU cores 173 can be detected with pstat_getdynamic().]) 174 ;; 175esac 176])dnl 177