diff options
author | Daniel Black <daniel@linux.ibm.com> | 2018-11-09 14:38:22 +1100 |
---|---|---|
committer | Anel Husakovic <anel@mariadb.org> | 2020-02-18 00:47:07 +0100 |
commit | f21bf8fce21d0f72e84c4b39c4792ef35a68666f (patch) | |
tree | e91b78cd20bcb6e31aee76514162a8152fd716e5 | |
parent | be77fa914c30e92e33e60feb5a3e098c765798af (diff) | |
download | mariadb-git-bb-5.5-anel-PR922.tar.gz |
my_getncpus based on threads availablebb-5.5-anel-PR922
Detecting the cpus based on sysconf of the online CPUs can significantly
over estimate the number of cpus available.
Wheither via numactl, cgroups, taskset, systemd constraints, docker
containers and probably other mechanisms, the number of threads mysqld
can be run on can be quite less.
As such we use the pthread_getaffinity_np function on Linux and FreeBSD
(identical API) to get the number of CPUs.
The number of CPUs is the default for the thread_pool_size and a too
high default will resulting in large memory usage and high context
switching overhead.
-rw-r--r-- | cmake/os/WindowsCache.cmake | 1 | ||||
-rw-r--r-- | config.h.cmake | 1 | ||||
-rw-r--r-- | configure.cmake | 1 | ||||
-rw-r--r-- | mysys/my_getncpus.c | 31 |
4 files changed, 33 insertions, 1 deletions
diff --git a/cmake/os/WindowsCache.cmake b/cmake/os/WindowsCache.cmake index 81cd916b2fe..b3e7e7067c7 100644 --- a/cmake/os/WindowsCache.cmake +++ b/cmake/os/WindowsCache.cmake @@ -152,6 +152,7 @@ SET(HAVE_PTHREAD_ATTR_SETSCOPE CACHE INTERNAL "") SET(HAVE_PTHREAD_ATTR_SETSTACKSIZE CACHE INTERNAL "") SET(HAVE_PTHREAD_CONDATTR_CREATE CACHE INTERNAL "") SET(HAVE_PTHREAD_CONDATTR_SETCLOCK CACHE INTERNAL "") +SET(HAVE_PTHREAD_GETAFFINITY_NP CACHE INTERNAL "") SET(HAVE_PTHREAD_INIT CACHE INTERNAL "") SET(HAVE_PTHREAD_KEY_DELETE CACHE INTERNAL "") SET(HAVE_PTHREAD_RWLOCK_RDLOCK CACHE INTERNAL "") diff --git a/config.h.cmake b/config.h.cmake index 15070c7ae58..9ccac07a45c 100644 --- a/config.h.cmake +++ b/config.h.cmake @@ -231,6 +231,7 @@ #cmakedefine HAVE_PTHREAD_ATTR_SETSTACKSIZE 1 #cmakedefine HAVE_PTHREAD_CONDATTR_CREATE 1 #cmakedefine HAVE_PTHREAD_CONDATTR_SETCLOCK 1 +#cmakedefine HAVE_PTHREAD_GETAFFINITY_NP 1 #cmakedefine HAVE_PTHREAD_KEY_DELETE 1 #cmakedefine HAVE_PTHREAD_KEY_DELETE 1 #cmakedefine HAVE_PTHREAD_KILL 1 diff --git a/configure.cmake b/configure.cmake index ea1f4d201f0..153d4e8078b 100644 --- a/configure.cmake +++ b/configure.cmake @@ -402,6 +402,7 @@ CHECK_FUNCTION_EXISTS (pthread_attr_getguardsize HAVE_PTHREAD_ATTR_GETGUARDSIZE) CHECK_FUNCTION_EXISTS (pthread_attr_setstacksize HAVE_PTHREAD_ATTR_SETSTACKSIZE) CHECK_FUNCTION_EXISTS (pthread_condattr_create HAVE_PTHREAD_CONDATTR_CREATE) CHECK_FUNCTION_EXISTS (pthread_condattr_setclock HAVE_PTHREAD_CONDATTR_SETCLOCK) +CHECK_FUNCTION_EXISTS (pthread_getaffinity_np HAVE_PTHREAD_GETAFFINITY_NP) CHECK_FUNCTION_EXISTS (pthread_key_delete HAVE_PTHREAD_KEY_DELETE) CHECK_FUNCTION_EXISTS (pthread_rwlock_rdlock HAVE_PTHREAD_RWLOCK_RDLOCK) CHECK_FUNCTION_EXISTS (pthread_sigmask HAVE_PTHREAD_SIGMASK) diff --git a/mysys/my_getncpus.c b/mysys/my_getncpus.c index 38585161c22..0ee03631da8 100644 --- a/mysys/my_getncpus.c +++ b/mysys/my_getncpus.c @@ -21,10 +21,36 @@ #include <unistd.h> #endif +#if defined(__FreeBSD__) && defined(HAVE_PTHREAD_GETAFFINITY_NP) +#include <pthread_np.h> +#include <sys/cpuset.h> +#endif + static int ncpus=0; -int my_getncpus() +int my_getncpus(void) { +#if (defined(__linux__) || defined(__FreeBSD__)) && defined(HAVE_PTHREAD_GETAFFINITY_NP) + cpu_set_t set; + + if (!ncpus) + { + if (pthread_getaffinity_np(pthread_self(), sizeof(set), &set) == 0) + { + ncpus= CPU_COUNT(&set); + } + else + { +#ifdef _SC_NPROCESSORS_ONLN + ncpus= sysconf(_SC_NPROCESSORS_ONLN); +#else + ncpus= 2; +#endif + } + } + +#else /* __linux__ || FreeBSD && HAVE_PTHREAD_GETAFFINITY_NP */ + if (!ncpus) { #ifdef _SC_NPROCESSORS_ONLN @@ -46,5 +72,8 @@ int my_getncpus() ncpus= 2; #endif } + +#endif /* __linux__ || FreeBSD && HAVE_PTHREAD_GETAFFINITY_NP */ + return ncpus; } |