[Groonga-commit] groonga/groonga [master] use sysctl() for getting N CPU cores as fallback.

Zurück zum Archiv-Index

null+****@clear***** null+****@clear*****
2010年 8月 9日 (月) 10:15:12 JST


Kouhei Sutou	2010-08-09 01:15:12 +0000 (Mon, 09 Aug 2010)

  New Revision: 545c71f6a15ca4c9bbd5ad5cafa9f9e0da2f5f00

  Log:
    use sysctl() for getting N CPU cores as fallback.

  Modified files:
    configure.ac
    src/groonga.c

  Modified: configure.ac (+1 -1)
===================================================================
--- configure.ac    2010-08-09 00:57:31 +0000 (954b67a)
+++ configure.ac    2010-08-09 01:15:12 +0000 (d2407c5)
@@ -120,7 +120,7 @@ AC_CONFIG_FILES([Makefile
 
 AC_CHECK_HEADERS(sys/mman.h sys/time.h sys/timeb.h sys/param.h sys/types.h pthread.h sys/resource.h)
 AC_CHECK_HEADERS(netdb.h sys/wait.h sys/socket.h netinet/in.h netinet/tcp.h)
-AC_CHECK_HEADERS(ucontext.h signal.h errno.h execinfo.h)
+AC_CHECK_HEADERS(ucontext.h signal.h errno.h execinfo.h sys/sysctl.h)
 AC_CHECK_FUNCS(localtime_r gmtime_r)
 AC_SYS_LARGEFILE
 AC_TYPE_OFF_T

  Modified: src/groonga.c (+19 -1)
===================================================================
--- src/groonga.c    2010-08-09 00:57:31 +0000 (5cdc2b0)
+++ src/groonga.c    2010-08-09 01:15:12 +0000 (5960c81)
@@ -40,6 +40,10 @@
 #include <sys/resource.h>
 #endif /* HAVE_SYS_RESOURCE_H */
 
+#ifdef HAVE_SYS_SYSCTL_H
+#include <sys/sysctl.h>
+#endif /* HAVE_SYS_SYSCTL_H */
+
 #ifndef USE_MSG_NOSIGNAL
 #define MSG_NOSIGNAL 0
 #endif /* USE_MSG_NOSIGNAL */
@@ -1926,7 +1930,7 @@ enum {
   grn_logger_info_set(&grn_gctx, &info);\
 } while(0)
 
-uint32_t
+static uint32_t
 get_core_number(void)
 {
 #ifdef WIN32
@@ -1934,7 +1938,21 @@ get_core_number(void)
   GetSystemInfo(&sinfo);
   return sinfo.dwNumberOfProcessors;
 #else /* WIN32 */
+#  ifdef _SC_NPROCESSORS_CONF
   return sysconf(_SC_NPROCESSORS_CONF);
+#  else
+  int n_processors;
+  size_t length = sizeof(n_processors);
+  int mib[] = {CTL_HW, HW_NCPU};
+  if (sysctl(mib, sizeof(mib) / sizeof(mib[0]),
+             &n_processors, &length, NULL, 0) == 0 &&
+      length == sizeof(n_processors) &&
+      0 < n_processors) {
+    return n_processors;
+  } else {
+    return 1;
+  }
+#  endif /* _SC_NPROCESSORS_CONF */
 #endif /* WIN32 */
 }
 




Groonga-commit メーリングリストの案内
Zurück zum Archiv-Index