diff --git a/core/application/impl/kagome_application_impl.cpp b/core/application/impl/kagome_application_impl.cpp index f3b6ca35d8..20b999dfc2 100644 --- a/core/application/impl/kagome_application_impl.cpp +++ b/core/application/impl/kagome_application_impl.cpp @@ -75,7 +75,10 @@ namespace kagome::application { } app_state_manager->atLaunch([ctx{io_context}, log{logger_}] { - std::thread asio_runner([ctx{ctx}, log{log}] { ctx->run(); }); + std::thread asio_runner([ctx{ctx}, log{log}] { + soralog::util::setThreadName("kagome"); // explicitly for macos + ctx->run(); + }); asio_runner.detach(); return true; }); diff --git a/core/consensus/grandpa/impl/grandpa_impl.cpp b/core/consensus/grandpa/impl/grandpa_impl.cpp index b70619f83b..e87dfc56d5 100644 --- a/core/consensus/grandpa/impl/grandpa_impl.cpp +++ b/core/consensus/grandpa/impl/grandpa_impl.cpp @@ -94,7 +94,7 @@ namespace kagome::consensus::grandpa { block_tree_(std::move(block_tree)), reputation_repository_(std::move(reputation_repository)), babe_status_observable_(std::move(babe_status_observable)), - execution_thread_pool_{std::make_shared(1ull)}, + execution_thread_pool_{std::make_shared("grandpa", 1ull)}, internal_thread_context_{execution_thread_pool_->handler()}, main_thread_context_{std::move(main_thread_context)}, scheduler_{std::make_shared( diff --git a/core/injector/application_injector.cpp b/core/injector/application_injector.cpp index 421b5dbaf4..f08cd2b7c6 100644 --- a/core/injector/application_injector.cpp +++ b/core/injector/application_injector.cpp @@ -328,10 +328,10 @@ namespace { sptr get_thread_pool(const Injector &injector) { const auto cores = std::thread::hardware_concurrency(); if (cores == 0ul) { - return std::make_shared(5ull); + return std::make_shared("worker", 5ull); } - return std::make_shared(cores); + return std::make_shared("worker", cores); } template diff --git a/core/metrics/impl/exposer_impl.cpp b/core/metrics/impl/exposer_impl.cpp index 55715bfe23..a514db1747 100644 --- a/core/metrics/impl/exposer_impl.cpp +++ b/core/metrics/impl/exposer_impl.cpp @@ -60,8 +60,10 @@ namespace kagome::metrics { acceptor_->local_endpoint().port()); acceptOnce(); - thread_ = std::make_shared( - [context = context_]() { context->run(); }); + thread_ = std::make_shared([context = context_] { + soralog::util::setThreadName("metric-exposer"); + context->run(); + }); thread_->detach(); return true; diff --git a/core/metrics/impl/metrics_watcher.cpp b/core/metrics/impl/metrics_watcher.cpp index 3849554688..7cabdc7e8e 100644 --- a/core/metrics/impl/metrics_watcher.cpp +++ b/core/metrics/impl/metrics_watcher.cpp @@ -34,6 +34,7 @@ namespace kagome::metrics { bool MetricsWatcher::start() { thread_ = std::thread([this] { + soralog::util::setThreadName("metric-watcher"); while (not shutdown_requested_) { auto storage_size_res = measure_storage_size(); if (storage_size_res.has_value()) { diff --git a/core/network/impl/stream_engine.hpp b/core/network/impl/stream_engine.hpp index 96e8b6a98a..b4a3293707 100644 --- a/core/network/impl/stream_engine.hpp +++ b/core/network/impl/stream_engine.hpp @@ -13,7 +13,9 @@ #include #include +#if defined(BACKWARD_HAS_BACKTRACE) #include +#endif #include "libp2p/connection/stream.hpp" #include "libp2p/host/host.hpp" diff --git a/core/offchain/impl/offchain_worker_impl.cpp b/core/offchain/impl/offchain_worker_impl.cpp index 9436c1c38b..3bd8813f9f 100644 --- a/core/offchain/impl/offchain_worker_impl.cpp +++ b/core/offchain/impl/offchain_worker_impl.cpp @@ -63,7 +63,12 @@ namespace kagome::offchain { outcome::result OffchainWorkerImpl::run() { BOOST_ASSERT(not ocw_pool_->getWorker()); - soralog::util::setThreadName("ocw." + std::to_string(block_.number)); + auto at_end = + gsl::finally([prev_thread_name = soralog::util::getThreadName()] { + soralog::util::setThreadName(prev_thread_name); + }); + + soralog::util::setThreadName("ocw.#" + std::to_string(block_.number)); ocw_pool_->addWorker(shared_from_this()); auto remove = gsl::finally([&] { ocw_pool_->removeWorker(); }); diff --git a/core/offchain/impl/runner.hpp b/core/offchain/impl/runner.hpp index d90f4e0f78..3ffdc1648c 100644 --- a/core/offchain/impl/runner.hpp +++ b/core/offchain/impl/runner.hpp @@ -25,7 +25,7 @@ namespace kagome::offchain { : threads_{threads}, free_threads_{threads}, max_tasks_{max_tasks}, - thread_pool_{threads_} {} + thread_pool_{"ocw", threads_} {} void run(Task &&task) { std::unique_lock lock{mutex_}; diff --git a/core/parachain/approval/approval_distribution.cpp b/core/parachain/approval/approval_distribution.cpp index 24e92ee684..6c8bc9340d 100644 --- a/core/parachain/approval/approval_distribution.cpp +++ b/core/parachain/approval/approval_distribution.cpp @@ -474,7 +474,7 @@ namespace kagome::parachain { std::shared_ptr pvf, std::shared_ptr recovery, std::shared_ptr this_context) - : int_pool_{std::make_shared(1ull)}, + : int_pool_{std::make_shared("approval", 1ull)}, internal_context_{int_pool_->handler()}, thread_pool_{std::move(thread_pool)}, thread_pool_context_{thread_pool_->handler()}, diff --git a/core/telemetry/impl/service_impl.cpp b/core/telemetry/impl/service_impl.cpp index c76a94cafc..fa802b58cf 100644 --- a/core/telemetry/impl/service_impl.cpp +++ b/core/telemetry/impl/service_impl.cpp @@ -94,7 +94,10 @@ namespace kagome::telemetry { connections_.emplace_back(std::move(connection)); } worker_thread_ = std::make_shared( - [io_context{io_context_}] { io_context->run(); }); + [io_context{io_context_}] { + soralog::util::setThreadName("telemetry"); + io_context->run(); + }); worker_thread_->detach(); return true; } diff --git a/core/utils/thread_pool.hpp b/core/utils/thread_pool.hpp index 3a1669b2a2..8ed0ff047b 100644 --- a/core/utils/thread_pool.hpp +++ b/core/utils/thread_pool.hpp @@ -13,6 +13,7 @@ #include #include +#include "soralog/util.hpp" #include "utils/non_copyable.hpp" namespace kagome { @@ -89,15 +90,49 @@ namespace kagome { ThreadPool &operator=(ThreadPool &&) = delete; ThreadPool &operator=(const ThreadPool &) = delete; + [[deprecated("Please, use ctor for pool with named threads")]] // explicit ThreadPool(size_t thread_count) : ioc_{std::make_shared()}, work_guard_{ioc_->get_executor()} { BOOST_ASSERT(ioc_); BOOST_ASSERT(thread_count > 0); + static size_t pool_count = 0; + + threads_.reserve(thread_count); + for (size_t i = 0; i < thread_count; ++i) { + threads_.emplace_back( + [io{ioc_}, thread_count, pool_n = ++pool_count, thread_n = i + 1] { + if (thread_count > 1) { + soralog::util::setThreadName( + fmt::format("worker.{}.{}", pool_n, thread_n)); + } else { + soralog::util::setThreadName(fmt::format("worker.{}", pool_n)); + } + io->run(); + }); + } + } + + ThreadPool(std::string_view pool_tag, size_t thread_count) + : ioc_{std::make_shared()}, + work_guard_{ioc_->get_executor()} { + BOOST_ASSERT(ioc_); + BOOST_ASSERT(thread_count > 0); + threads_.reserve(thread_count); for (size_t i = 0; i < thread_count; ++i) { - threads_.emplace_back([io{ioc_}] { io->run(); }); + threads_.emplace_back([io{ioc_}, + pool_tag = std::string(pool_tag), + thread_count, + n = i + 1] { + if (thread_count > 1) { + soralog::util::setThreadName(fmt::format("{}.{}", pool_tag, n)); + } else { + soralog::util::setThreadName(pool_tag); + } + io->run(); + }); } }