Repository: randombit/botan
Pull request #1877 (2019-04-01 19:17:12)

Using available cores rather than total for the pools on linux/android/BSD.
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.

codecov-io: # [Codecov](https://codecov.io/gh/randombit/botan/pull/1877?src=pr&el=h1) Report
> Merging [#1877](https://codecov.io/gh/randombit/botan/pull/1877?src=pr&el=desc) into [master](https://codecov.io/gh/randombit/botan/commit/f7148998ace97f2ad3a583685e6ed3c1f3b74c09?src=pr&el=desc) will **decrease** coverage by `0.01%`.
> The diff coverage is `42.85%`.

[![Impacted file tree graph](https://codecov.io/gh/randombit/botan/pull/1877/graphs/tree.svg?width=650&token=VR6iYmSPH8&height=150&src=pr)](https://codecov.io/gh/randombit/botan/pull/1877?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master    #1877      +/-   ##
==========================================
- Coverage    90.1%   90.09%   -0.02%     
==========================================
  Files         526      526              
  Lines       55936    55942       +6     
  Branches     5825     5826       +1     
==========================================
- Hits        50403    50401       -2     
- Misses       5533     5541       +8
```


| [Impacted Files](https://codecov.io/gh/randombit/botan/pull/1877?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [src/lib/utils/thread\_utils/thread\_pool.cpp](https://codecov.io/gh/randombit/botan/pull/1877/diff?src=pr&el=tree#diff-c3JjL2xpYi91dGlscy90aHJlYWRfdXRpbHMvdGhyZWFkX3Bvb2wuY3Bw) | `90.24% <100%> (ø)` | :arrow_up: |
| [src/lib/utils/os\_utils.cpp](https://codecov.io/gh/randombit/botan/pull/1877/diff?src=pr&el=tree#diff-c3JjL2xpYi91dGlscy9vc191dGlscy5jcHA=) | `66.41% <33.33%> (-1.56%)` | :arrow_down: |
| [src/lib/misc/cryptobox/cryptobox.cpp](https://codecov.io/gh/randombit/botan/pull/1877/diff?src=pr&el=tree#diff-c3JjL2xpYi9taXNjL2NyeXB0b2JveC9jcnlwdG9ib3guY3Bw) | `93.65% <0%> (-1.59%)` | :arrow_down: |
| [src/lib/pubkey/mce/mceliece\_key.cpp](https://codecov.io/gh/randombit/botan/pull/1877/diff?src=pr&el=tree#diff-c3JjL2xpYi9wdWJrZXkvbWNlL21jZWxpZWNlX2tleS5jcHA=) | `85.39% <0%> (-1.13%)` | :arrow_down: |
| [src/lib/tls/tls\_channel.cpp](https://codecov.io/gh/randombit/botan/pull/1877/diff?src=pr&el=tree#diff-c3JjL2xpYi90bHMvdGxzX2NoYW5uZWwuY3Bw) | `80.19% <0%> (-0.34%)` | :arrow_down: |
| [src/cli/tls\_proxy.cpp](https://codecov.io/gh/randombit/botan/pull/1877/diff?src=pr&el=tree#diff-c3JjL2NsaS90bHNfcHJveHkuY3Bw) | `4.97% <0%> (ø)` | :arrow_up: |
| [src/cli/tls\_http\_server.cpp](https://codecov.io/gh/randombit/botan/pull/1877/diff?src=pr&el=tree#diff-c3JjL2NsaS90bHNfaHR0cF9zZXJ2ZXIuY3Bw) | `4.24% <0%> (ø)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/randombit/botan/pull/1877?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/randombit/botan/pull/1877?src=pr&el=footer). Last update [f714899...90cffb8](https://codecov.io/gh/randombit/botan/pull/1877?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/src/cli/tls_http_server.cpp b/src/cli/tls_http_server.cpp
index 0a6428cd6..cc59a71c3 100644
--- a/src/cli/tls_http_server.cpp
+++ b/src/cli/tls_http_server.cpp
@@ -41,12 +41,6 @@
 
 #include "credentials.h"
 
-#if BOOST_VERSION >= 107000
-#define GET_IO_SERVICE(s) ((boost::asio::io_context&)(s).get_executor().context())
-#else
-#define GET_IO_SERVICE(s) ((s).get_io_service())
-#endif
-
 namespace Botan_CLI {
 
 namespace {
@@ -441,7 +435,7 @@ class TLS_Asio_HTTP_Server final
       session::pointer make_session()
          {
          return session::create(
-                   GET_IO_SERVICE(m_acceptor),
+                   m_acceptor.get_io_service(),
                    m_session_manager,
                    m_creds,
                    m_policy);
diff --git a/src/cli/tls_proxy.cpp b/src/cli/tls_proxy.cpp
index 42ce156b1..49ffbe376 100644
--- a/src/cli/tls_proxy.cpp
+++ b/src/cli/tls_proxy.cpp
@@ -33,12 +33,6 @@
 
 #include "credentials.h"
 
-#if BOOST_VERSION >= 107000
-#define GET_IO_SERVICE(s) ((boost::asio::io_context&)(s).get_executor().context())
-#else
-#define GET_IO_SERVICE(s) ((s).get_io_service())
-#endif
-
 namespace Botan_CLI {
 
 namespace {
@@ -376,7 +370,7 @@ class tls_proxy_server final
       session::pointer make_session()
          {
          return session::create(
-                   GET_IO_SERVICE(m_acceptor),
+                   m_acceptor.get_io_service(),
                    m_session_manager,
                    m_creds,
                    m_policy,
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


Test patch:
diff --git a/src/tests/test_os_utils.cpp b/src/tests/test_os_utils.cpp
index 4cbc4fda8..434b9d36c 100644
--- a/src/tests/test_os_utils.cpp
+++ b/src/tests/test_os_utils.cpp
@@ -37,6 +37,7 @@ class OS_Utils_Tests final : public Test
          results.push_back(test_get_process_id());
          results.push_back(test_get_cpu_cycle_counter());
          results.push_back(test_get_high_resolution_clock());
+	 results.push_back(test_get_cpu_numbers());
          results.push_back(test_get_system_timestamp());
          results.push_back(test_memory_locking());
          results.push_back(test_cpu_instruction_probe());
@@ -109,6 +110,18 @@ class OS_Utils_Tests final : public Test
          return result;
          }
 
+      Test::Result test_get_cpu_numbers()
+         {
+         Test::Result result("OS::get_cpu_total/OS::get_cpu_available");
+
+	 size_t tt = Botan::OS::get_cpu_total();
+	 size_t ta = Botan::OS::get_cpu_available();
+
+	 result.test_lte("get_cpu_available not greater than total", ta, tt);
+
+	 return result;
+	 }
+
       Test::Result test_get_system_timestamp()
          {
          // TODO better tests