From b00299a2048948e38484e10cda644c3436ff593f Mon Sep 17 00:00:00 2001 From: Martin Quinson Date: Fri, 27 Jan 2012 21:32:42 +0100 Subject: [PATCH 1/1] add a function xbt_os_get_numcores() --- buildtools/Cmake/CompleteInFiles.cmake | 2 ++ src/include/xbt/xbt_os_thread.h | 3 ++- src/portable.h | 7 +++++++ src/xbt/xbt_os_thread.c | 27 ++++++++++++++++++++++++++ 4 files changed, 38 insertions(+), 1 deletion(-) diff --git a/buildtools/Cmake/CompleteInFiles.cmake b/buildtools/Cmake/CompleteInFiles.cmake index 90d3fbce9c..3b7ac73a6c 100644 --- a/buildtools/Cmake/CompleteInFiles.cmake +++ b/buildtools/Cmake/CompleteInFiles.cmake @@ -108,6 +108,8 @@ CHECK_INCLUDE_FILE("unistd.h" HAVE_UNISTD_H) CHECK_INCLUDE_FILE("execinfo.h" HAVE_EXECINFO_H) CHECK_INCLUDE_FILE("signal.h" HAVE_SIGNAL_H) CHECK_INCLUDE_FILE("sys/time.h" HAVE_SYS_TIME_H) +CHECK_INCLUDE_FILE("sys/param.h" HAVE_SYS_PARAM_H) +CHECK_INCLUDE_FILE("sys/sysctl.h" HAVE_SYS_SYSCTL_H) CHECK_INCLUDE_FILE("time.h" HAVE_TIME_H) CHECK_INCLUDE_FILE("dlfcn.h" HAVE_DLFCN_H) CHECK_INCLUDE_FILE("inttypes.h" HAVE_INTTYPES_H) diff --git a/src/include/xbt/xbt_os_thread.h b/src/include/xbt/xbt_os_thread.h index 61d4912064..e744be5ff5 100644 --- a/src/include/xbt/xbt_os_thread.h +++ b/src/include/xbt/xbt_os_thread.h @@ -36,12 +36,13 @@ typedef DWORD xbt_os_thread_key_t; #endif /* Calls pthread_atfork() if present, and else does nothing. - * The only known user of this wrapper is mmalloc_preinit(). + * The only known user of this wrapper is mmalloc_preinit(); This function may disapear in the near future. */ XBT_PUBLIC(int) xbt_os_thread_atfork(void (*prepare)(void), void (*parent)(void), void (*child)(void)); +XBT_PUBLIC(int) xbt_os_get_numcores(void); XBT_PUBLIC(xbt_os_thread_t) xbt_os_thread_create(const char *name, pvoid_f_pvoid_t start_routine, diff --git a/src/portable.h b/src/portable.h index f9dc951ba0..70701e009e 100644 --- a/src/portable.h +++ b/src/portable.h @@ -31,6 +31,13 @@ # include #endif +#ifdef HAVE_SYS_PARAM_H +# include +#endif +#ifdef HAVE_SYS_SYSCTL_H +# include +#endif + /**** **** Networking ****/ diff --git a/src/xbt/xbt_os_thread.c b/src/xbt/xbt_os_thread.c index f34fb398e1..8c9f2b02ca 100644 --- a/src/xbt/xbt_os_thread.c +++ b/src/xbt/xbt_os_thread.c @@ -1163,6 +1163,33 @@ void xbt_os_sem_get_value(xbt_os_sem_t sem, int *svalue) #endif + +/** @brief Returns the amount of cores on the current host */ +int xbt_os_get_numcores(void) { +#ifdef WIN32 + SYSTEM_INFO sysinfo; + GetSystemInfo(&sysinfo); + return sysinfo.dwNumberOfProcessors; +#elif MACOS + int nm[2]; + size_t len = 4; + uint32_t count; + + nm[0] = CTL_HW; nm[1] = HW_AVAILCPU; + sysctl(nm, 2, &count, &len, NULL, 0); + + if(count < 1) { + nm[1] = HW_NCPU; + sysctl(nm, 2, &count, &len, NULL, 0); + if(count < 1) { count = 1; } + } + return count; +#else + return sysconf(_SC_NPROCESSORS_ONLN); +#endif +} + + /***** reentrant mutexes *****/ typedef struct xbt_os_rmutex_ { xbt_os_mutex_t mutex; -- 2.20.1