Skip to content

Commit

Permalink
thead pool debugging support proposal using native capabilities if po…
Browse files Browse the repository at this point in the history
…ssible.
  • Loading branch information
devnexen committed Sep 29, 2023
1 parent ae37d65 commit 313e439
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 5 deletions.
28 changes: 24 additions & 4 deletions src/lib/utils/os_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,13 @@
#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

#if defined(BOTAN_TARGET_OS_HAS_POSIX1)
#include <errno.h>
#include <pthread.h>
#include <setjmp.h>
#include <signal.h>
#include <stdlib.h>
Expand Down Expand Up @@ -72,6 +69,10 @@ extern "C" char** environ;
#include <sys/prctl.h>
#endif

#if defined(BOTAN_TARGET_IS_FREEBSD) || defined(BOTAN_TARGET_IS_OPENBSD)
#include <pthread_np.h>
#endif

namespace Botan {

// Not defined in OS namespace for historical reasons
Expand Down Expand Up @@ -614,6 +615,25 @@ void OS::page_named(void* page, size_t size) {
#endif
}

#if defined(BOTAN_TARGET_OS_HAS_THREADS)
void OS::set_thread_name(std::thread& thread, const std::string& name) {
#if defined(BOTAN_TARGET_OS_IS_LINUX) || defined(BOTAN_TARGET_OS_IS_FREEBSD)
static_cast<void>(pthread_setname_np(thread.native_handle(), name.c_str()));
#elif defined(BOTAN_TARGET_OS_IS_OPENBSD)
static_cast<void>(pthread_set_name_np(thread.native_handle(), name.c_str()));
#elif defined(BOTAN_TARGET_OS_IS_NETBSD)
static_cast<void>(pthread_set_name_np(thread.native_handle(), "%s", const_cast<char*>(name.c_str())));
#elif defined(BOTAN_TARGET_OS_HAS_WIN32) && defined(BOTAN_BUILD_COMPILER_IS_MSVC)
// Using SetThreadDescription from Win10
BOTAN_UNUSED(thread, name);
#else
// TODO other possible oses ?
// macOs does not seem to allow to name threads other than the current one.
BOTAN_UNUSED(thread, name);
#endif
}
#endif

#if defined(BOTAN_TARGET_OS_HAS_POSIX1) && !defined(BOTAN_TARGET_OS_IS_EMSCRIPTEN)

namespace {
Expand Down
8 changes: 8 additions & 0 deletions src/lib/utils/os_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
#include <string>
#include <vector>

#if defined(BOTAN_TARGET_OS_HAS_THREADS)
#include <thread>
#endif

namespace Botan::OS {

/*
Expand Down Expand Up @@ -148,6 +152,10 @@ void page_allow_access(void* page);
*/
void page_named(void* page, size_t size);

#if defined(BOTAN_TARGET_OS_HAS_THREADS)
void set_thread_name(std::thread& thread, const std::string& name);
#endif

/**
* Run a probe instruction to test for support for a CPU instruction.
* Runs in system-specific env that catches illegal instructions; this
Expand Down
7 changes: 6 additions & 1 deletion src/lib/utils/thread_utils/thread_pool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ Thread_Pool& Thread_Pool::global_instance() {

Thread_Pool::Thread_Pool(std::optional<size_t> opt_pool_size) {
m_shutdown = false;
// On Linux, it is 16 length max, including terminator
const std::string tname = "Botan thread";

if(!opt_pool_size.has_value()) {
return;
Expand All @@ -65,8 +67,11 @@ Thread_Pool::Thread_Pool(std::optional<size_t> opt_pool_size) {
}
}

m_workers.resize(pool_size);

for(size_t i = 0; i != pool_size; ++i) {
m_workers.push_back(std::thread(&Thread_Pool::worker_thread, this));
m_workers[i] = std::thread(&Thread_Pool::worker_thread, this);
OS::set_thread_name(m_workers[i], tname);
}
}

Expand Down

0 comments on commit 313e439

Please sign in to comment.