aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/utils/os_utils.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/utils/os_utils.cpp')
-rw-r--r--src/lib/utils/os_utils.cpp34
1 files changed, 34 insertions, 0 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())