From ae5f2b6a6cc7b2260e9dff99c1bf378922e0e988 Mon Sep 17 00:00:00 2001 From: James O'Beirne Date: Wed, 13 Jun 2018 14:50:59 -0400 Subject: [PATCH] threads: introduce util/threadnames, refactor thread naming This work is prerequisite to attaching thread names to log lines and deadlock debug utilities. This code allows setting of an "internal" threadname per thread on platforms where thread_local is available. This commit also moves RenameThread() out of a more general module and adds a numeric suffix to disambiguate between threads with the same name. It explicitly names a few main threads using the new util::ThreadRename(). --- src/Makefile.am | 2 ++ src/bitcoind.cpp | 3 +++ src/httpserver.cpp | 11 ++++---- src/init.cpp | 7 ++--- src/qt/bitcoin.cpp | 3 +++ src/test/setup_common.cpp | 2 +- src/util/system.cpp | 20 -------------- src/util/system.h | 6 ++--- src/util/threadnames.cpp | 57 +++++++++++++++++++++++++++++++++++++++ src/util/threadnames.h | 21 +++++++++++++++ src/validation.cpp | 5 ++-- src/validation.h | 2 +- 12 files changed, 103 insertions(+), 36 deletions(-) create mode 100644 src/util/threadnames.cpp create mode 100644 src/util/threadnames.h diff --git a/src/Makefile.am b/src/Makefile.am index 059ed18134..0fefa34cd8 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -210,6 +210,7 @@ BITCOIN_CORE_H = \ util/memory.h \ util/moneystr.h \ util/rbf.h \ + util/threadnames.h \ util/time.h \ util/url.h \ util/validation.h \ @@ -491,6 +492,7 @@ libbitcoin_util_a_SOURCES = \ util/system.cpp \ util/moneystr.cpp \ util/rbf.cpp \ + util/threadnames.cpp \ util/strencodings.cpp \ util/time.cpp \ util/url.cpp \ diff --git a/src/bitcoind.cpp b/src/bitcoind.cpp index dde75c1b12..b31f86cdd9 100644 --- a/src/bitcoind.cpp +++ b/src/bitcoind.cpp @@ -19,6 +19,7 @@ #include #include #include +#include #include #include @@ -64,6 +65,8 @@ static bool AppInit(int argc, char* argv[]) bool fRet = false; + util::ThreadRename("init"); + // // Parameters // diff --git a/src/httpserver.cpp b/src/httpserver.cpp index 5d9c3d2c1a..63639fa3e0 100644 --- a/src/httpserver.cpp +++ b/src/httpserver.cpp @@ -6,6 +6,7 @@ #include #include +#include #include #include #include @@ -17,7 +18,7 @@ #include #include #include -#include +#include #include #include @@ -284,7 +285,7 @@ static void http_reject_request_cb(struct evhttp_request* req, void*) /** Event dispatcher thread */ static bool ThreadHTTP(struct event_base* base) { - RenameThread("bitcoin-http"); + util::ThreadRename("http"); LogPrint(BCLog::HTTP, "Entering http event loop\n"); event_base_dispatch(base); // Event loop will be interrupted by InterruptHTTPServer() @@ -335,9 +336,9 @@ static bool HTTPBindAddresses(struct evhttp* http) } /** Simple wrapper to set thread name and run work queue */ -static void HTTPWorkQueueRun(WorkQueue* queue) +static void HTTPWorkQueueRun(WorkQueue* queue, int worker_num) { - RenameThread("bitcoin-httpworker"); + util::ThreadRename(strprintf("httpworker.%i", worker_num)); queue->Run(); } @@ -430,7 +431,7 @@ void StartHTTPServer() threadHTTP = std::thread(ThreadHTTP, eventBase); for (int i = 0; i < rpcThreads; i++) { - g_thread_http_workers.emplace_back(HTTPWorkQueueRun, workQueue); + g_thread_http_workers.emplace_back(HTTPWorkQueueRun, workQueue, i); } } diff --git a/src/init.cpp b/src/init.cpp index 70459994c7..408a133e4e 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -42,6 +42,7 @@ #include