Set thread names for better debugging

This commit is contained in:
SChernykh 2025-03-13 11:19:58 +01:00
parent 37e8cc01cf
commit d1dd95d9c9
10 changed files with 59 additions and 0 deletions

View file

@ -345,6 +345,16 @@ int main(int argc, char* argv[])
return 0;
}" HAVE_RES_QUERY)
check_c_source_compiles("
#define _GNU_SOURCE
#include <pthread.h>
int main()
{
pthread_setname_np(pthread_self(), \"Main\");
return 0;
}" HAVE_PTHREAD_SETNAME_NP)
set(CMAKE_REQUIRED_LIBRARIES)
if (HAVE_BUILTIN_CLZLL)
@ -366,6 +376,10 @@ if (HAVE_RES_QUERY)
endif()
endif()
if (HAVE_PTHREAD_SETNAME_NP)
add_definitions(/DHAVE_PTHREAD_SETNAME_NP)
endif()
add_definitions("-DRAPIDJSON_PARSE_DEFAULT_FLAGS=kParseTrailingCommasFlag")
add_executable(${CMAKE_PROJECT_NAME} ${HEADERS} ${SOURCES})

View file

@ -250,6 +250,8 @@ private:
NOINLINE void run()
{
set_thread_name("Logger");
do {
uv_mutex_lock(&m_mutex);
if (m_readPos == m_writePos.load()) {

View file

@ -404,6 +404,8 @@ void MergeMiningClientJSON_RPC::loop(void* data)
{
LOGINFO(1, "event loop started");
set_thread_name("MM JSON RPC");
MergeMiningClientJSON_RPC* client = static_cast<MergeMiningClientJSON_RPC*>(data);
int err = uv_run(&client->m_loop, UV_RUN_DEFAULT);

View file

@ -1953,6 +1953,8 @@ int p2pool::run()
loop->data = nullptr;
GetLoopUserData(loop);
set_thread_name("Main");
try {
get_info();
load_found_blocks();

View file

@ -244,6 +244,7 @@ void RandomX_Hasher::set_seed(const hash& seed)
{
// Background doesn't work very well with xmrig mining on all cores
//make_thread_background();
set_thread_name("Dataset init");
randomx_init_dataset(m_dataset, m_cache[m_index], a, b - a);
});
}
@ -426,6 +427,8 @@ void RandomX_Hasher_RPC::loop(void* data)
{
LOGINFO(1, "event loop started");
set_thread_name("RX hash RPC");
RandomX_Hasher_RPC* hasher = static_cast<RandomX_Hasher_RPC*>(data);
int err = uv_run(&hasher->m_loop, UV_RUN_DEFAULT);

View file

@ -2370,6 +2370,8 @@ void SideChain::launch_precalc(const PoolBlock* block)
void SideChain::precalc_worker()
{
set_thread_name("Precalc");
std::vector<std::pair<size_t, const Wallet*>> wallets;
wallets.reserve(m_chainWindowSize);

View file

@ -615,6 +615,17 @@ void TCPServer::loop(void* data)
TCPServer* server = static_cast<TCPServer*>(data);
log_category_prefix = server->get_log_category();
{
char buf[64] = {};
log::Stream s(buf);
s << log_category_prefix;
if (s.m_pos > 0) {
buf[s.m_pos - 1] = '\0';
}
set_thread_name(buf);
}
LOGINFO(1, "event loop started");
server_event_loop_thread = data;

View file

@ -846,4 +846,21 @@ NOINLINE PerfTimer::~PerfTimer()
LOGINFO(m_level, m_name << " took " << dt.count() << " ms");
}
void set_thread_name(const char* name)
{
#if (UV_VERSION_MAJOR > 1) || ((UV_VERSION_MAJOR == 1) && (UV_VERSION_MINOR >= 50))
const int err = uv_thread_setname(name);
if (err) {
LOGERR(1, "uv_thread_setname failed for " << name << ", error " << uv_err_name(err));
}
#elif defined(HAVE_PTHREAD_SETNAME_NP)
const int err = pthread_setname_np(pthread_self(), name);
if (err) {
LOGERR(1, "pthread_setname_np failed for " << name << ", error " << err);
}
#else
(void)name;
#endif
}
} // namespace p2pool

View file

@ -232,4 +232,6 @@ void parallel_run(uv_loop_t* loop, T&& callback, bool wait = false)
}
}
void set_thread_name(const char* name);
} // namespace p2pool

View file

@ -130,6 +130,8 @@ void ZMQReader::monitor_thread(void* arg)
{
LOGINFO(1, "monitor thread ready");
set_thread_name("ZMQ monitor");
ZMQReader* r = reinterpret_cast<ZMQReader*>(arg);
do {} while (!r->m_stopped && r->m_monitor->m_connected && r->m_monitor->check_event(-1));
@ -151,6 +153,8 @@ void ZMQReader::run()
m_workerThreadRunning = true;
ON_SCOPE_LEAVE([this]() { m_workerThreadRunning = false; });
set_thread_name("ZMQ worker");
zmq_msg_t message = {};
try {