1*dd083157Smrg# =========================================================================== 2*dd083157Smrg# https://www.gnu.org/software/autoconf-archive/ax_count_cpus.html 3*dd083157Smrg# =========================================================================== 4*dd083157Smrg# 5*dd083157Smrg# SYNOPSIS 6*dd083157Smrg# 7*dd083157Smrg# AX_COUNT_CPUS([ACTION-IF-DETECTED],[ACTION-IF-NOT-DETECTED]) 8*dd083157Smrg# 9*dd083157Smrg# DESCRIPTION 10*dd083157Smrg# 11*dd083157Smrg# Attempt to count the number of logical processor cores (including 12*dd083157Smrg# virtual and HT cores) currently available to use on the machine and 13*dd083157Smrg# place detected value in CPU_COUNT variable. 14*dd083157Smrg# 15*dd083157Smrg# On successful detection, ACTION-IF-DETECTED is executed if present. If 16*dd083157Smrg# the detection fails, then ACTION-IF-NOT-DETECTED is triggered. The 17*dd083157Smrg# default ACTION-IF-NOT-DETECTED is to set CPU_COUNT to 1. 18*dd083157Smrg# 19*dd083157Smrg# LICENSE 20*dd083157Smrg# 21*dd083157Smrg# Copyright (c) 2014,2016 Karlson2k (Evgeny Grin) <k2k@narod.ru> 22*dd083157Smrg# Copyright (c) 2012 Brian Aker <brian@tangent.org> 23*dd083157Smrg# Copyright (c) 2008 Michael Paul Bailey <jinxidoru@byu.net> 24*dd083157Smrg# Copyright (c) 2008 Christophe Tournayre <turn3r@users.sourceforge.net> 25*dd083157Smrg# 26*dd083157Smrg# Copying and distribution of this file, with or without modification, are 27*dd083157Smrg# permitted in any medium without royalty provided the copyright notice 28*dd083157Smrg# and this notice are preserved. This file is offered as-is, without any 29*dd083157Smrg# warranty. 30*dd083157Smrg 31*dd083157Smrg#serial 22 32*dd083157Smrg 33*dd083157Smrg AC_DEFUN([AX_COUNT_CPUS],[dnl 34*dd083157Smrg AC_REQUIRE([AC_CANONICAL_HOST])dnl 35*dd083157Smrg AC_REQUIRE([AC_PROG_EGREP])dnl 36*dd083157Smrg AC_MSG_CHECKING([the number of available CPUs]) 37*dd083157Smrg CPU_COUNT="0" 38*dd083157Smrg 39*dd083157Smrg # Try generic methods 40*dd083157Smrg 41*dd083157Smrg # 'getconf' is POSIX utility, but '_NPROCESSORS_ONLN' and 42*dd083157Smrg # 'NPROCESSORS_ONLN' are platform-specific 43*dd083157Smrg command -v getconf >/dev/null 2>&1 && \ 44*dd083157Smrg CPU_COUNT=`getconf _NPROCESSORS_ONLN 2>/dev/null || getconf NPROCESSORS_ONLN 2>/dev/null` || CPU_COUNT="0" 45*dd083157Smrg AS_IF([[test "$CPU_COUNT" -gt "0" 2>/dev/null || ! command -v nproc >/dev/null 2>&1]],[[: # empty]],[dnl 46*dd083157Smrg # 'nproc' is part of GNU Coreutils and is widely available 47*dd083157Smrg CPU_COUNT=`OMP_NUM_THREADS='' nproc 2>/dev/null` || CPU_COUNT=`nproc 2>/dev/null` || CPU_COUNT="0" 48*dd083157Smrg ])dnl 49*dd083157Smrg 50*dd083157Smrg AS_IF([[test "$CPU_COUNT" -gt "0" 2>/dev/null]],[[: # empty]],[dnl 51*dd083157Smrg # Try platform-specific preferred methods 52*dd083157Smrg AS_CASE([[$host_os]],dnl 53*dd083157Smrg [[*linux*]],[[CPU_COUNT=`lscpu -p 2>/dev/null | $EGREP -e '^@<:@0-9@:>@+,' -c` || CPU_COUNT="0"]],dnl 54*dd083157Smrg [[*darwin*]],[[CPU_COUNT=`sysctl -n hw.logicalcpu 2>/dev/null` || CPU_COUNT="0"]],dnl 55*dd083157Smrg [[freebsd*]],[[command -v sysctl >/dev/null 2>&1 && CPU_COUNT=`sysctl -n kern.smp.cpus 2>/dev/null` || CPU_COUNT="0"]],dnl 56*dd083157Smrg [[netbsd*]], [[command -v sysctl >/dev/null 2>&1 && CPU_COUNT=`sysctl -n hw.ncpuonline 2>/dev/null` || CPU_COUNT="0"]],dnl 57*dd083157Smrg [[solaris*]],[[command -v psrinfo >/dev/null 2>&1 && CPU_COUNT=`psrinfo 2>/dev/null | $EGREP -e '^@<:@0-9@:>@.*on-line' -c 2>/dev/null` || CPU_COUNT="0"]],dnl 58*dd083157Smrg [[mingw*]],[[CPU_COUNT=`ls -qpU1 /proc/registry/HKEY_LOCAL_MACHINE/HARDWARE/DESCRIPTION/System/CentralProcessor/ 2>/dev/null | $EGREP -e '^@<:@0-9@:>@+/' -c` || CPU_COUNT="0"]],dnl 59*dd083157Smrg [[msys*]],[[CPU_COUNT=`ls -qpU1 /proc/registry/HKEY_LOCAL_MACHINE/HARDWARE/DESCRIPTION/System/CentralProcessor/ 2>/dev/null | $EGREP -e '^@<:@0-9@:>@+/' -c` || CPU_COUNT="0"]],dnl 60*dd083157Smrg [[cygwin*]],[[CPU_COUNT=`ls -qpU1 /proc/registry/HKEY_LOCAL_MACHINE/HARDWARE/DESCRIPTION/System/CentralProcessor/ 2>/dev/null | $EGREP -e '^@<:@0-9@:>@+/' -c` || CPU_COUNT="0"]]dnl 61*dd083157Smrg )dnl 62*dd083157Smrg ])dnl 63*dd083157Smrg 64*dd083157Smrg AS_IF([[test "$CPU_COUNT" -gt "0" 2>/dev/null || ! command -v sysctl >/dev/null 2>&1]],[[: # empty]],[dnl 65*dd083157Smrg # Try less preferred generic method 66*dd083157Smrg # 'hw.ncpu' exist on many platforms, but not on GNU/Linux 67*dd083157Smrg CPU_COUNT=`sysctl -n hw.ncpu 2>/dev/null` || CPU_COUNT="0" 68*dd083157Smrg ])dnl 69*dd083157Smrg 70*dd083157Smrg AS_IF([[test "$CPU_COUNT" -gt "0" 2>/dev/null]],[[: # empty]],[dnl 71*dd083157Smrg # Try platform-specific fallback methods 72*dd083157Smrg # They can be less accurate and slower then preferred methods 73*dd083157Smrg AS_CASE([[$host_os]],dnl 74*dd083157Smrg [[*linux*]],[[CPU_COUNT=`$EGREP -e '^processor' -c /proc/cpuinfo 2>/dev/null` || CPU_COUNT="0"]],dnl 75*dd083157Smrg [[*darwin*]],[[CPU_COUNT=`system_profiler SPHardwareDataType 2>/dev/null | $EGREP -i -e 'number of cores:'|cut -d : -f 2 -s|tr -d ' '` || CPU_COUNT="0"]],dnl 76*dd083157Smrg [[freebsd*]],[[CPU_COUNT=`dmesg 2>/dev/null| $EGREP -e '^cpu@<:@0-9@:>@+: '|sort -u|$EGREP -e '^' -c` || CPU_COUNT="0"]],dnl 77*dd083157Smrg [[netbsd*]], [[CPU_COUNT=`command -v cpuctl >/dev/null 2>&1 && cpuctl list 2>/dev/null| $EGREP -e '^@<:@0-9@:>@+ .* online ' -c` || \ 78*dd083157Smrg CPU_COUNT=`dmesg 2>/dev/null| $EGREP -e '^cpu@<:@0-9@:>@+ at'|sort -u|$EGREP -e '^' -c` || CPU_COUNT="0"]],dnl 79*dd083157Smrg [[solaris*]],[[command -v kstat >/dev/null 2>&1 && CPU_COUNT=`kstat -m cpu_info -s state -p 2>/dev/null | $EGREP -c -e 'on-line'` || \ 80*dd083157Smrg CPU_COUNT=`kstat -m cpu_info 2>/dev/null | $EGREP -c -e 'module: cpu_info'` || CPU_COUNT="0"]],dnl 81*dd083157Smrg [[mingw*]],[AS_IF([[CPU_COUNT=`reg query 'HKLM\\Hardware\\Description\\System\\CentralProcessor' 2>/dev/null | $EGREP -e '\\\\@<:@0-9@:>@+$' -c`]],dnl 82*dd083157Smrg [[: # empty]],[[test "$NUMBER_OF_PROCESSORS" -gt "0" 2>/dev/null && CPU_COUNT="$NUMBER_OF_PROCESSORS"]])],dnl 83*dd083157Smrg [[msys*]],[[test "$NUMBER_OF_PROCESSORS" -gt "0" 2>/dev/null && CPU_COUNT="$NUMBER_OF_PROCESSORS"]],dnl 84*dd083157Smrg [[cygwin*]],[[test "$NUMBER_OF_PROCESSORS" -gt "0" 2>/dev/null && CPU_COUNT="$NUMBER_OF_PROCESSORS"]]dnl 85*dd083157Smrg )dnl 86*dd083157Smrg ])dnl 87*dd083157Smrg 88*dd083157Smrg AS_IF([[test "x$CPU_COUNT" != "x0" && test "$CPU_COUNT" -gt 0 2>/dev/null]],[dnl 89*dd083157Smrg AC_MSG_RESULT([[$CPU_COUNT]]) 90*dd083157Smrg m4_ifvaln([$1],[$1],)dnl 91*dd083157Smrg ],[dnl 92*dd083157Smrg m4_ifval([$2],[dnl 93*dd083157Smrg AS_UNSET([[CPU_COUNT]]) 94*dd083157Smrg AC_MSG_RESULT([[unable to detect]]) 95*dd083157Smrg $2 96*dd083157Smrg ], [dnl 97*dd083157Smrg CPU_COUNT="1" 98*dd083157Smrg AC_MSG_RESULT([[unable to detect (assuming 1)]]) 99*dd083157Smrg ])dnl 100*dd083157Smrg ])dnl 101*dd083157Smrg ])dnl 102