Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
add a function xbt_os_get_numcores()
authorMartin Quinson <martin.quinson@loria.fr>
Fri, 27 Jan 2012 20:32:42 +0000 (21:32 +0100)
committerMartin Quinson <martin.quinson@loria.fr>
Fri, 27 Jan 2012 20:32:42 +0000 (21:32 +0100)
buildtools/Cmake/CompleteInFiles.cmake
src/include/xbt/xbt_os_thread.h
src/portable.h
src/xbt/xbt_os_thread.c

index 90d3fbc..3b7ac73 100644 (file)
@@ -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)
index 61d4912..e744be5 100644 (file)
@@ -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,
index f9dc951..70701e0 100644 (file)
 #  include <unistd.h>
 #endif
 
+#ifdef HAVE_SYS_PARAM_H
+# include <sys/param.h>
+#endif
+#ifdef HAVE_SYS_SYSCTL_H
+# include <sys/sysctl.h>
+#endif
+
 /****
  **** Networking 
  ****/
index f34fb39..8c9f2b0 100644 (file)
@@ -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;