Name threads by role (#1258)

Threads !
This commit is contained in:
Roy Keene 2018-10-04 17:13:50 -05:00 committed by GitHub
commit 051df7be9b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 165 additions and 10 deletions

View file

@ -1,11 +1,11 @@
if (${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
set (platform_sources plat/default/priority.cpp plat/posix/perms.cpp)
set (platform_sources plat/default/priority.cpp plat/posix/perms.cpp plat/darwin/thread_role.cpp)
elseif (${CMAKE_SYSTEM_NAME} MATCHES "Windows")
set (platform_sources plat/windows/priority.cpp plat/windows/perms.cpp)
set (platform_sources plat/windows/priority.cpp plat/windows/perms.cpp plat/windows/thread_role.cpp)
elseif (${CMAKE_SYSTEM_NAME} MATCHES "Linux")
set (platform_sources plat/linux/priority.cpp plat/posix/perms.cpp)
set (platform_sources plat/linux/priority.cpp plat/posix/perms.cpp plat/linux/thread_role.cpp)
elseif (${CMAKE_SYSTEM_NAME} MATCHES "FreeBSD")
set (platform_sources plat/default/priority.cpp plat/posix/perms.cpp)
set (platform_sources plat/default/priority.cpp plat/posix/perms.cpp plat/freebsd/thread_role.cpp)
else ()
error ("Unknown platform: ${CMAKE_SYSTEM_NAME}")
endif ()

View file

@ -0,0 +1,7 @@
#include <pthread.h>
#include <rai/lib/utility.hpp>
void rai::thread_role::set_name (std::string thread_name)
{
pthread_setname_np (thread_name.c_str ());
}

View file

@ -0,0 +1,8 @@
#include <pthread.h>
#include <pthread_np.h>
#include <rai/lib/utility.hpp>
void rai::thread_role::set_name (std::string thread_name)
{
pthread_set_name_np (pthread_self (), thread_name.c_str ());
}

View file

@ -0,0 +1,7 @@
#include <pthread.h>
#include <rai/lib/utility.hpp>
void rai::thread_role::set_name (std::string thread_name)
{
pthread_setname_np (pthread_self (), thread_name.c_str ());
}

View file

@ -0,0 +1,18 @@
#include <windows.h>
#include <processthreadsapi.h>
#include <rai/lib/utility.hpp>
typedef HRESULT (*SetThreadDescription_t) (HANDLE, PCWSTR);
void rai::thread_role::set_name (std::string thread_name)
{
SetThreadDescription_t SetThreadDescription_local;
SetThreadDescription_local = (SetThreadDescription_t)GetProcAddress (GetModuleHandle (TEXT ("kernel32.dll")), "SetThreadDescription");
if (SetThreadDescription_local)
{
std::wstring thread_name_wide (thread_name.begin (), thread_name.end ());
SetThreadDescription_local (GetCurrentThread (), thread_name_wide.c_str ());
}
}

View file

@ -1,6 +1,77 @@
#include <iostream>
#include <rai/lib/utility.hpp>
namespace rai
{
namespace thread_role
{
/*
* rai::thread_role namespace
*
* Manage thread role
*/
static thread_local rai::thread_role::name current_thread_role = rai::thread_role::name::unknown;
rai::thread_role::name get (void)
{
return current_thread_role;
}
void set (rai::thread_role::name role)
{
std::string thread_role_name_string;
switch (role)
{
case rai::thread_role::name::unknown:
thread_role_name_string = "<unknown>";
break;
case rai::thread_role::name::io:
thread_role_name_string = "I/O";
break;
case rai::thread_role::name::work:
thread_role_name_string = "Work pool";
break;
case rai::thread_role::name::packet_processing:
thread_role_name_string = "Pkt processing";
break;
case rai::thread_role::name::alarm:
thread_role_name_string = "Alarm";
break;
case rai::thread_role::name::vote_processing:
thread_role_name_string = "Vote processing";
break;
case rai::thread_role::name::block_processing:
thread_role_name_string = "Blck processing";
break;
case rai::thread_role::name::announce_loop:
thread_role_name_string = "Announce loop";
break;
case rai::thread_role::name::wallet_actions:
thread_role_name_string = "Wallet actions";
break;
case rai::thread_role::name::bootstrap_initiator:
thread_role_name_string = "Bootstrap init";
break;
}
/*
* We want to constrain the thread names to 15
* characters, since this is the smallest maximum
* length supported by the platforms we support
* (specifically, Linux)
*/
assert (thread_role_name_string.size () < 16);
rai::thread_role::set_name (thread_role_name_string);
rai::thread_role::current_thread_role = role;
}
}
}
/*
* Backing code for "release_assert", which is itself a macro
*/
void release_assert_internal (bool check, const char * check_expr, const char * file, unsigned int line)
{
if (check)

View file

@ -22,6 +22,29 @@ void set_secure_perm_directory (boost::filesystem::path const & path, boost::sys
void set_secure_perm_file (boost::filesystem::path const & path);
void set_secure_perm_file (boost::filesystem::path const & path, boost::system::error_code & ec);
/*
* Functions for understanding the role of the current thread
*/
namespace thread_role
{
enum class name
{
unknown,
io,
work,
packet_processing,
alarm,
vote_processing,
block_processing,
announce_loop,
wallet_actions,
bootstrap_initiator,
};
rai::thread_role::name get (void);
void set (rai::thread_role::name);
void set_name (std::string);
}
template <typename... T>
class observer_set
{

View file

@ -36,6 +36,7 @@ opencl (opencl_a)
for (auto i (0); i < count; ++i)
{
auto thread (std::thread ([this, i]() {
rai::thread_role::set (rai::thread_role::name::work);
rai::work_thread_reprioritize ();
loop (i);
}));

View file

@ -1126,7 +1126,10 @@ void rai::bootstrap_attempt::add_bulk_push_target (rai::block_hash const & head,
rai::bootstrap_initiator::bootstrap_initiator (rai::node & node_a) :
node (node_a),
stopped (false),
thread ([this]() { run_bootstrap (); })
thread ([this]() {
rai::thread_role::set (rai::thread_role::name::bootstrap_initiator);
run_bootstrap ();
})
{
}

View file

@ -49,6 +49,7 @@ on (true)
for (size_t i = 0; i < node.config.io_threads; ++i)
{
packet_processing_threads.push_back (std::thread ([this]() {
rai::thread_role::set (rai::thread_role::name::packet_processing);
try
{
process_packets ();
@ -706,7 +707,10 @@ bool rai::operation::operator> (rai::operation const & other_a) const
rai::alarm::alarm (boost::asio::io_service & service_a) :
service (service_a),
thread ([this]() { run (); })
thread ([this]() {
rai::thread_role::set (rai::thread_role::name::alarm);
run ();
})
{
}
@ -1324,7 +1328,10 @@ node (node_a),
started (false),
stopped (false),
active (false),
thread ([this]() { process_loop (); })
thread ([this]() {
rai::thread_role::set (rai::thread_role::name::vote_processing);
process_loop ();
})
{
std::unique_lock<std::mutex> lock (mutex);
while (!started)
@ -1799,7 +1806,10 @@ port_mapping (*this),
vote_processor (*this),
warmed_up (0),
block_processor (*this),
block_processor_thread ([this]() { this->block_processor.process_blocks (); }),
block_processor_thread ([this]() {
rai::thread_role::set (rai::thread_role::name::block_processing);
this->block_processor.process_blocks ();
}),
online_reps (*this),
stats (config.stat_config)
{
@ -4189,7 +4199,10 @@ rai::active_transactions::active_transactions (rai::node & node_a) :
node (node_a),
started (false),
stopped (false),
thread ([this]() { announce_loop (); })
thread ([this]() {
rai::thread_role::set (rai::thread_role::name::announce_loop);
announce_loop ();
})
{
std::unique_lock<std::mutex> lock (mutex);
while (!started)
@ -4230,6 +4243,7 @@ rai::thread_runner::thread_runner (boost::asio::io_service & service_a, unsigned
for (auto i (0); i < service_threads_a; ++i)
{
threads.push_back (std::thread ([&service_a]() {
rai::thread_role::set (rai::thread_role::name::io);
try
{
service_a.run ();

View file

@ -1238,7 +1238,10 @@ observer ([](bool) {}),
node (node_a),
env (boost::polymorphic_downcast<rai::mdb_store *> (node_a.store_impl.get ())->env),
stopped (false),
thread ([this]() { do_wallet_actions (); })
thread ([this]() {
rai::thread_role::set (rai::thread_role::name::wallet_actions);
do_wallet_actions ();
})
{
if (!error_a)
{