diff options
author | Jack Lloyd <[email protected]> | 2019-04-07 09:32:11 -0400 |
---|---|---|
committer | Jack Lloyd <[email protected]> | 2019-04-07 09:32:11 -0400 |
commit | 71944443d8f06a13673c0ab1fc66170c4e5732bb (patch) | |
tree | 0b264186b818c70df2e28e1c5c4305be8b9103fb /src | |
parent | f7148998ace97f2ad3a583685e6ed3c1f3b74c09 (diff) | |
parent | 358be63d14df48872e85a6c09cfa9a89404d2d2e (diff) |
Merge GH #1877 Use sysconf to detect CPU counts
Diffstat (limited to 'src')
-rw-r--r-- | src/lib/utils/os_utils.cpp | 34 | ||||
-rw-r--r-- | src/lib/utils/os_utils.h | 3 | ||||
-rw-r--r-- | src/lib/utils/thread_utils/thread_pool.cpp | 2 | ||||
-rw-r--r-- | src/tests/test_os_utils.cpp | 13 |
4 files changed, 51 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 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 |