aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/utils
diff options
context:
space:
mode:
authorDavid Carlier <devnexen@gmail.com>2019-04-01 20:14:47 +0100
committerDavid Carlier <devnexen@gmail.com>2019-04-07 13:59:09 +0100
commit358be63d14df48872e85a6c09cfa9a89404d2d2e (patch)
tree36c8faf2a90db58129d9d025e0e2b1963fd51dc9 /src/lib/utils
parent7beb65eb60767330b1d0acc76517109f54c61e0d (diff)
Using available cores rather than total for the pools.
Issue with the C++ api it might not reflect the reality of the H/W, and on Android, in battery saving mode for example, might be more reasonable to use what the system really offers.
Diffstat (limited to 'src/lib/utils')
-rw-r--r--src/lib/utils/os_utils.cpp34
-rw-r--r--src/lib/utils/os_utils.h3
-rw-r--r--src/lib/utils/thread_utils/thread_pool.cpp2
3 files changed, 38 insertions, 1 deletions
diff --git a/src/lib/utils/os_utils.cpp b/src/lib/utils/os_utils.cpp
index aa599e4b0..eb459b8fb 100644
--- a/src/lib/utils/os_utils.cpp
+++ b/src/lib/utils/os_utils.cpp
@@ -15,6 +15,10 @@
#include <chrono>
#include <cstdlib>
+#if defined(BOTAN_TARGET_OS_HAS_THREADS)
+ #include <thread>
+#endif
+
#if defined(BOTAN_TARGET_OS_HAS_EXPLICIT_BZERO)
#include <string.h>
#endif
@@ -163,6 +167,36 @@ uint64_t OS::get_cpu_cycle_counter()
return rtc;
}
+size_t OS::get_cpu_total()
+ {
+ size_t tt = 1;
+#if defined(BOTAN_TARGET_OS_HAS_POSIX1) && defined(_SC_NPROCESSORS_CONF)
+ long res;
+ if ((res = ::sysconf(_SC_NPROCESSORS_CONF)) <= 0)
+ {
+ return 1;
+ }
+ tt = static_cast<size_t>(res);
+#elif defined(BOTAN_TARGET_OS_HAS_THREADS)
+ tt = static_cast<size_t>(std::thread::hardware_concurrency());
+#endif
+ return tt;
+ }
+
+size_t OS::get_cpu_available()
+ {
+#if defined(BOTAN_TARGET_OS_HAS_POSIX1) && defined(_SC_NPROCESSORS_ONLN)
+ long res;
+ if ((res = ::sysconf(_SC_NPROCESSORS_ONLN)) <= 0)
+ {
+ return 1;
+ }
+ return static_cast<size_t>(res);
+#else
+ return get_cpu_total();
+#endif
+ }
+
uint64_t OS::get_high_resolution_clock()
{
if(uint64_t cpu_clock = OS::get_cpu_cycle_counter())
diff --git a/src/lib/utils/os_utils.h b/src/lib/utils/os_utils.h
index 82f3aad04..f597387c7 100644
--- a/src/lib/utils/os_utils.h
+++ b/src/lib/utils/os_utils.h
@@ -50,6 +50,9 @@ bool running_in_privileged_state();
*/
uint64_t BOTAN_TEST_API get_cpu_cycle_counter();
+size_t BOTAN_TEST_API get_cpu_total();
+size_t BOTAN_TEST_API get_cpu_available();
+
/*
* @return best resolution timestamp available
*
diff --git a/src/lib/utils/thread_utils/thread_pool.cpp b/src/lib/utils/thread_utils/thread_pool.cpp
index 4ccefe8dc..405f79585 100644
--- a/src/lib/utils/thread_utils/thread_pool.cpp
+++ b/src/lib/utils/thread_utils/thread_pool.cpp
@@ -22,7 +22,7 @@ Thread_Pool::Thread_Pool(size_t pool_size)
{
if(pool_size == 0)
{
- pool_size = std::thread::hardware_concurrency();
+ pool_size = OS::get_cpu_available();
/*
* For large machines don't create too many threads, unless