{"thread":{"id":"38751","subject":"[PATCH 2/2] thread-utils.c: detect CPU count on older BSD-like systems","startedAt":"2015-03-08T07:14:36Z","lastAt":"2015-03-08T07:14:37Z","messageCount":2,"participants":["Kyle J. McKay"],"isPatch":true,"patchVersion":1,"patchTotal":2},"messages":[{"id":"257267","messageId":"e85cd4def375e8247ab210983e44e75@74d39fa044aa309eaea14b9f57fe79c","threadId":"38751","inReplyTo":null,"subject":"[PATCH 1/2] configure: support HAVE_BSD_SYSCTL option","fromName":"Kyle J. McKay","fromEmail":"mackyle@gmail.com","sentAt":"2015-03-08T07:14:36Z","receivedAt":"2015-03-08T07:14:36Z","isPatch":true,"sender":{"key":"mackyle@gmail.com","avatar":"https://avatars.githubusercontent.com/u/813346?v=4"},"body":"On BSD-compatible systems some information such as the number\nof available CPUs may only be available via the sysctl function.\n\nAdd support for a HAVE_BSD_SYSCTL option complete with autoconf\nsupport and include the sys/syctl.h header when the option is\nenabled to make the sysctl function available.\n\nSigned-off-by: Kyle J. McKay <mackyle@gmail.com>\n---\n Makefile          |  6 ++++++\n config.mak.uname  |  5 +++++\n configure.ac      | 23 +++++++++++++++++++++++\n git-compat-util.h |  3 +++\n 4 files changed, 37 insertions(+)\n\ndiff --git a/Makefile b/Makefile\nindex 44f1dd10..5f3987fe 100644\n--- a/Makefile\n+++ b/Makefile\n@@ -357,6 +357,8 @@ all::\n # and define it to \"no\" if you need to remove the parentheses () around the\n # constant.  The default is \"auto\", which means to use parentheses if your\n # compiler is detected to support it.\n+#\n+# Define HAVE_BSD_SYSCTL if your platform has a BSD-compatible sysctl function.\n \n GIT-VERSION-FILE: FORCE\n \t@$(SHELL_PATH) ./GIT-VERSION-GEN\n@@ -1431,6 +1433,10 @@ ifdef HAVE_CLOCK_MONOTONIC\n \tBASIC_CFLAGS += -DHAVE_CLOCK_MONOTONIC\n endif\n \n+ifdef HAVE_BSD_SYSCTL\n+\tBASIC_CFLAGS += -DHAVE_BSD_SYSCTL\n+endif\n+\n ifeq ($(TCLTK_PATH),)\n NO_TCLTK = NoThanks\n endif\ndiff --git a/config.mak.uname b/config.mak.uname\nindex b64b63c3..f4e77cb9 100644\n--- a/config.mak.uname\n+++ b/config.mak.uname\n@@ -107,6 +107,7 @@ ifeq ($(uname_S),Darwin)\n \tCOMPAT_OBJS += compat/precompose_utf8.o\n \tBASIC_CFLAGS += -DPRECOMPOSE_UNICODE\n \tBASIC_CFLAGS += -DPROTECT_HFS_DEFAULT=1\n+\tHAVE_BSD_SYSCTL = YesPlease\n endif\n ifeq ($(uname_S),SunOS)\n \tNEEDS_SOCKET = YesPlease\n@@ -199,6 +200,7 @@ ifeq ($(uname_S),FreeBSD)\n \tPYTHON_PATH = /usr/local/bin/python\n \tHAVE_PATHS_H = YesPlease\n \tGMTIME_UNRELIABLE_ERRORS = UnfortunatelyYes\n+\tHAVE_BSD_SYSCTL = YesPlease\n endif\n ifeq ($(uname_S),OpenBSD)\n \tNO_STRCASESTR = YesPlease\n@@ -208,6 +210,7 @@ ifeq ($(uname_S),OpenBSD)\n \tBASIC_CFLAGS += -I/usr/local/include\n \tBASIC_LDFLAGS += -L/usr/local/lib\n \tHAVE_PATHS_H = YesPlease\n+\tHAVE_BSD_SYSCTL = YesPlease\n endif\n ifeq ($(uname_S),MirBSD)\n \tNO_STRCASESTR = YesPlease\n@@ -215,6 +218,7 @@ ifeq ($(uname_S),MirBSD)\n \tUSE_ST_TIMESPEC = YesPlease\n \tNEEDS_LIBICONV = YesPlease\n \tHAVE_PATHS_H = YesPlease\n+\tHAVE_BSD_SYSCTL = YesPlease\n endif\n ifeq ($(uname_S),NetBSD)\n \tifeq ($(shell expr \"$(uname_R)\" : '[01]\\.'),2)\n@@ -225,6 +229,7 @@ ifeq ($(uname_S),NetBSD)\n \tUSE_ST_TIMESPEC = YesPlease\n \tNO_MKSTEMPS = YesPlease\n \tHAVE_PATHS_H = YesPlease\n+\tHAVE_BSD_SYSCTL = YesPlease\n endif\n ifeq ($(uname_S),AIX)\n \tDEFAULT_PAGER = more\ndiff --git a/configure.ac b/configure.ac\nindex 55e5a9b3..bbdde85c 100644\n--- a/configure.ac\n+++ b/configure.ac\n@@ -1046,6 +1046,29 @@ GIT_CONF_SUBST([NO_INITGROUPS])\n #\n # Define NO_ICONV if your libc does not properly support iconv.\n \n+AC_DEFUN([BSD_SYSCTL_SRC], [\n+AC_LANG_PROGRAM([[\n+#include <stddef.h>\n+#include <sys/types.h>\n+#include <sys/sysctl.h>\n+]],[[\n+int val, mib[2];\n+size_t len;\n+mib[0] = CTL_HW;\n+mib[1] = 1;\n+len = sizeof(val);\n+return sysctl(mib, 2, &val, &len, NULL, 0) ? 1 : 0;\n+]])])\n+\n+#\n+# Define HAVE_BSD_SYSCTL=YesPlease if a BSD-compatible sysctl function is available.\n+AC_MSG_CHECKING([for BSD sysctl])\n+AC_COMPILE_IFELSE([BSD_SYSCTL_SRC],\n+\t[AC_MSG_RESULT([yes])\n+\tHAVE_BSD_SYSCTL=YesPlease],\n+\t[AC_MSG_RESULT([no])\n+\tHAVE_BSD_SYSCTL=])\n+GIT_CONF_SUBST([HAVE_BSD_SYSCTL])\n \n ## Other checks.\n # Define USE_PIC if you need the main git objects to be built with -fPIC\ndiff --git a/git-compat-util.h b/git-compat-util.h\nindex a3095be9..50691d3c 100644\n--- a/git-compat-util.h\n+++ b/git-compat-util.h\n@@ -127,6 +127,9 @@\n #else\n #include <poll.h>\n #endif\n+#ifdef HAVE_BSD_SYSCTL\n+#include <sys/sysctl.h>\n+#endif\n \n #if defined(__MINGW32__)\n /* pull in Windows compatibility stuff */\n---\n"},{"id":"257266","messageId":"087595269450d378dbaf0188e8f65c5@74d39fa044aa309eaea14b9f57fe79c","threadId":"38751","inReplyTo":"e85cd4def375e8247ab210983e44e75@74d39fa044aa309eaea14b9f57fe79c","subject":"[PATCH 2/2] thread-utils.c: detect CPU count on older BSD-like systems","fromName":"Kyle J. McKay","fromEmail":"mackyle@gmail.com","sentAt":"2015-03-08T07:14:37Z","receivedAt":"2015-03-08T07:14:37Z","isPatch":true,"sender":{"key":"mackyle@gmail.com","avatar":"https://avatars.githubusercontent.com/u/813346?v=4"},"body":"Not all systems support using sysconf to detect the number\nof available CPU cores.  Older BSD and BSD-derived systems\nonly provide the information via the sysctl function.\n\nIf HAVE_BSD_SYSCTL is defined attempt to retrieve the number\nof available CPU cores using the sysctl function.\n\nIf HAVE_BSD_SYSCTL is not defined or the sysctl function\nfails, we still attempt to get the information via sysconf.\n\nSigned-off-by: Kyle J. McKay <mackyle@gmail.com>\n---\n thread-utils.c | 18 +++++++++++++++++-\n 1 file changed, 17 insertions(+), 1 deletion(-)\n\ndiff --git a/thread-utils.c b/thread-utils.c\nindex 97396a75..a2135e07 100644\n--- a/thread-utils.c\n+++ b/thread-utils.c\n@@ -35,7 +35,23 @@ int online_cpus(void)\n \n \tif (!pstat_getdynamic(&psd, sizeof(psd), (size_t)1, 0))\n \t\treturn (int)psd.psd_proc_cnt;\n-#endif\n+#elif defined(HAVE_BSD_SYSCTL) && defined(HW_NCPU)\n+\tint mib[2];\n+\tsize_t len;\n+\tint cpucount;\n+\n+\tmib[0] = CTL_HW;\n+#  ifdef HW_AVAILCPU\n+\tmib[1] = HW_AVAILCPU;\n+\tlen = sizeof(cpucount);\n+\tif (!sysctl(mib, 2, &cpucount, &len, NULL, 0))\n+\t\treturn cpucount;\n+#  endif /* HW_AVAILCPU */\n+\tmib[1] = HW_NCPU;\n+\tlen = sizeof(cpucount);\n+\tif (!sysctl(mib, 2, &cpucount, &len, NULL, 0))\n+\t\treturn cpucount;\n+#endif /* defined(HAVE_BSD_SYSCTL) && defined(HW_NCPU) */\n \n #ifdef _SC_NPROCESSORS_ONLN\n \tif ((ncpus = (long)sysconf(_SC_NPROCESSORS_ONLN)) > 0)\n---\n"}]}