summaryrefslogtreecommitdiff
path: root/mysys
diff options
context:
space:
mode:
authorDaniel Black <daniel@linux.ibm.com>2018-11-09 14:38:22 +1100
committerAnel Husakovic <anel@mariadb.org>2020-02-20 08:44:20 +0100
commitfb01cc3766be9131402b82730adfa0e45e6e3511 (patch)
treef3ef6f22014af71162a0579940f782113f6a307f /mysys
parent959fc0c0ccaac0c0946a9c07bceb9b82116c8b47 (diff)
downloadmariadb-git-fb01cc3766be9131402b82730adfa0e45e6e3511.tar.gz
my_getncpus based on threads available
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. Closes PR #922
Diffstat (limited to 'mysys')
-rw-r--r--mysys/my_getncpus.c31
1 files changed, 30 insertions, 1 deletions
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;
}