From d3e40af25970e58bc11e0540846a504cc25ecde2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C5=91rinc?= Date: Wed, 20 May 2026 14:57:19 +0300 Subject: [PATCH] index: shorten indexer thread names `BaseIndex` currently uses the same name for logs, `getindexinfo`, prune locks, and the sync thread. Linux truncates system thread names to 15 visible bytes after the `b-` prefix, so long indexer names are clipped in system tools. Pass a separate thread name explicitly at each `BaseIndex` call site and shorten the OS-visible indexer names to `txidx`, `blkfltbscidx`, `coinstatsidx`, and `txospenderidx`. Add an `Assume` to `ThreadRename()` so future OS-visible thread names keep fitting the same Linux limit. The public index names, `getindexinfo` keys, command-line options, and on-disk paths stay unchanged. Co-authored-by: winterrdog Co-authored-by: Hodlinator <172445034+hodlinator@users.noreply.github.com> Co-authored-by: sedited Co-authored-by: MarcoFalke <*~=`'#}+{/-|&$^_@721217.xyz> --- doc/developer-notes.md | 2 +- src/index/base.cpp | 6 +++--- src/index/base.h | 3 ++- src/index/blockfilterindex.cpp | 11 ++++++++++- src/index/coinstatsindex.cpp | 2 +- src/index/txindex.cpp | 2 +- src/index/txospenderindex.cpp | 2 +- src/test/blockfilter_index_tests.cpp | 5 ++--- src/test/util_threadnames_tests.cpp | 4 ++-- src/util/threadnames.cpp | 2 ++ test/functional/feature_init.py | 8 ++++---- 11 files changed, 29 insertions(+), 18 deletions(-) diff --git a/doc/developer-notes.md b/doc/developer-notes.md index 129fdda7988..962e8851187 100644 --- a/doc/developer-notes.md +++ b/doc/developer-notes.md @@ -708,7 +708,7 @@ and its `cs_KeyStore` lock for example). - [HTTP worker threads (`b-http.xx`)](https://doxygen.bitcoincore.org/httpserver_8cpp.html#http_pool) : Threads to service RPC and REST requests. -- [Indexer threads (`b-txindex`, etc)](https://doxygen.bitcoincore.org/class_base_index.html#index_sync) +- [Indexer threads (`b-txidx`, `b-blkfltbscidx`, `b-coinstatsidx`, `b-txospenderidx`)](https://doxygen.bitcoincore.org/class_base_index.html#index_sync) : One thread per indexer. - [SchedulerThread (`b-scheduler`)](https://doxygen.bitcoincore.org/class_c_scheduler.html#scheduler) diff --git a/src/index/base.cpp b/src/index/base.cpp index bb41cc2a632..906ed265174 100644 --- a/src/index/base.cpp +++ b/src/index/base.cpp @@ -92,8 +92,8 @@ void BaseIndex::DB::WriteBestBlock(CDBBatch& batch, const CBlockLocator& locator batch.Write(DB_BEST_BLOCK, locator); } -BaseIndex::BaseIndex(std::unique_ptr chain, std::string name) - : m_chain{std::move(chain)}, m_name{std::move(name)} {} +BaseIndex::BaseIndex(std::unique_ptr chain, std::string name, std::string thread_name) + : m_chain{std::move(chain)}, m_name{std::move(name)}, m_thread_name{std::move(thread_name)} {} BaseIndex::~BaseIndex() { @@ -460,7 +460,7 @@ bool BaseIndex::StartBackgroundSync() { if (!m_init) throw std::logic_error("Error: Cannot start a non-initialized index"); - m_thread_sync = std::thread(&util::TraceThread, GetName(), [this] { Sync(); }); + m_thread_sync = std::thread(&util::TraceThread, m_thread_name, [this] { Sync(); }); return true; } diff --git a/src/index/base.h b/src/index/base.h index 761b6f56bc2..6d7e86ec0fa 100644 --- a/src/index/base.h +++ b/src/index/base.h @@ -117,6 +117,7 @@ protected: std::unique_ptr m_chain; Chainstate* m_chainstate{nullptr}; const std::string m_name; + const std::string m_thread_name; void BlockConnected(const kernel::ChainstateRole& role, const std::shared_ptr& block, const CBlockIndex* pindex) override; @@ -141,7 +142,7 @@ protected: void SetBestBlockIndex(const CBlockIndex* block); public: - BaseIndex(std::unique_ptr chain, std::string name); + BaseIndex(std::unique_ptr chain, std::string name, std::string thread_name); /// Destructor interrupts sync thread if running and blocks until it exits. virtual ~BaseIndex(); diff --git a/src/index/blockfilterindex.cpp b/src/index/blockfilterindex.cpp index d4fd7025981..5c4a181325f 100644 --- a/src/index/blockfilterindex.cpp +++ b/src/index/blockfilterindex.cpp @@ -61,6 +61,15 @@ constexpr size_t CF_HEADERS_CACHE_MAX_SZ{2000}; namespace { +std::string BlockFilterThreadName(BlockFilterType filter_type) +{ + switch (filter_type) { + case BlockFilterType::BASIC: return "blkfltbscidx"; + case BlockFilterType::INVALID: return ""; + } // no default case, so the compiler can warn about missing cases + assert(false); +} + struct DBVal { uint256 hash; uint256 header; @@ -75,7 +84,7 @@ static std::map g_filter_indexes; BlockFilterIndex::BlockFilterIndex(std::unique_ptr chain, BlockFilterType filter_type, size_t n_cache_size, bool f_memory, bool f_wipe) - : BaseIndex(std::move(chain), BlockFilterTypeName(filter_type) + " block filter index") + : BaseIndex(std::move(chain), BlockFilterTypeName(filter_type) + " block filter index", BlockFilterThreadName(filter_type)) , m_filter_type(filter_type) { const std::string& filter_name = BlockFilterTypeName(filter_type); diff --git a/src/index/coinstatsindex.cpp b/src/index/coinstatsindex.cpp index 80a6d8dfd2b..204f4341a9d 100644 --- a/src/index/coinstatsindex.cpp +++ b/src/index/coinstatsindex.cpp @@ -87,7 +87,7 @@ struct DBVal { std::unique_ptr g_coin_stats_index; CoinStatsIndex::CoinStatsIndex(std::unique_ptr chain, size_t n_cache_size, bool f_memory, bool f_wipe) - : BaseIndex(std::move(chain), "coinstatsindex") + : BaseIndex(std::move(chain), "coinstatsindex", "coinstatsidx") { // An earlier version of the index used "indexes/coinstats" but it contained // a bug and is superseded by a fixed version at "indexes/coinstatsindex". diff --git a/src/index/txindex.cpp b/src/index/txindex.cpp index 4b0c82877ab..5e71e6bb77a 100644 --- a/src/index/txindex.cpp +++ b/src/index/txindex.cpp @@ -66,7 +66,7 @@ void TxIndex::DB::WriteTxs(const std::vector>& v_pos } TxIndex::TxIndex(std::unique_ptr chain, size_t n_cache_size, bool f_memory, bool f_wipe) - : BaseIndex(std::move(chain), "txindex"), m_db(std::make_unique(n_cache_size, f_memory, f_wipe)) + : BaseIndex(std::move(chain), "txindex", "txidx"), m_db(std::make_unique(n_cache_size, f_memory, f_wipe)) {} TxIndex::~TxIndex() = default; diff --git a/src/index/txospenderindex.cpp b/src/index/txospenderindex.cpp index f0d559a958f..3d7b56b6a95 100644 --- a/src/index/txospenderindex.cpp +++ b/src/index/txospenderindex.cpp @@ -60,7 +60,7 @@ struct DBKey { }; TxoSpenderIndex::TxoSpenderIndex(std::unique_ptr chain, size_t n_cache_size, bool f_memory, bool f_wipe) - : BaseIndex(std::move(chain), "txospenderindex"), m_db{std::make_unique(gArgs.GetDataDirNet() / "indexes" / "txospenderindex" / "db", n_cache_size, f_memory, f_wipe)} + : BaseIndex(std::move(chain), "txospenderindex", "txospenderidx"), m_db{std::make_unique(gArgs.GetDataDirNet() / "indexes" / "txospenderindex" / "db", n_cache_size, f_memory, f_wipe)} { if (!m_db->Read("siphash_key", m_siphash_key)) { FastRandomContext rng(false); diff --git a/src/test/blockfilter_index_tests.cpp b/src/test/blockfilter_index_tests.cpp index a48a355d453..6e035c5675f 100644 --- a/src/test/blockfilter_index_tests.cpp +++ b/src/test/blockfilter_index_tests.cpp @@ -338,9 +338,8 @@ private: int m_blocking_height; public: - explicit IndexReorgCrash(std::unique_ptr chain, std::shared_future blocker, - int blocking_height, FakeNodeClock& clock) - : BaseIndex(std::move(chain), "test index"), m_clock(clock), m_blocker(blocker), m_blocking_height(blocking_height) + explicit IndexReorgCrash(std::unique_ptr chain, std::shared_future blocker, int blocking_height, FakeNodeClock& clock) + : BaseIndex(std::move(chain), "test index", "testidx"), m_clock(clock), m_blocker(blocker), m_blocking_height(blocking_height) { const fs::path path = gArgs.GetDataDirNet() / "index"; fs::create_directories(path); diff --git a/src/test/util_threadnames_tests.cpp b/src/test/util_threadnames_tests.cpp index f22eb12dcf1..e10553b8222 100644 --- a/src/test/util_threadnames_tests.cpp +++ b/src/test/util_threadnames_tests.cpp @@ -17,7 +17,7 @@ using util::ToString; BOOST_AUTO_TEST_SUITE(util_threadnames_tests) -const std::string TEST_THREAD_NAME_BASE = "test_thread."; +const std::string TEST_THREAD_NAME_BASE = "test_thrd."; /** * Run a bunch of threads to all call util::ThreadRename. @@ -56,7 +56,7 @@ BOOST_AUTO_TEST_CASE(util_threadnames_test_rename_threaded) BOOST_CHECK_EQUAL(names.size(), 100U); - // Names "test_thread.[n]" should exist for n = [0, 99] + // Names "test_thrd.[n]" should exist for n = [0, 99] for (int i = 0; i < 100; ++i) { BOOST_CHECK(names.contains(TEST_THREAD_NAME_BASE + ToString(i))); } diff --git a/src/util/threadnames.cpp b/src/util/threadnames.cpp index 0615b331994..a9a3649b0b9 100644 --- a/src/util/threadnames.cpp +++ b/src/util/threadnames.cpp @@ -3,6 +3,7 @@ // file COPYING or http://www.opensource.org/licenses/mit-license.php. #include +#include #include #include @@ -53,6 +54,7 @@ static void SetInternalName(const std::string& name) void util::ThreadRename(const std::string& name) { + Assume(name.size() <= 13); // Linux keeps 15 bytes SetThreadName(("b-" + name).c_str()); SetInternalName(name); } diff --git a/test/functional/feature_init.py b/test/functional/feature_init.py index 259e07b1869..526fe63a2bc 100755 --- a/test/functional/feature_init.py +++ b/test/functional/feature_init.py @@ -77,10 +77,10 @@ class InitTest(BitcoinTestFramework): b'net thread start', b'addcon thread start', b'initload thread start', - b'txindex thread start', - b'block filter index thread start', - b'coinstatsindex thread start', - b'txospenderindex thread start', + b'txidx thread start', + b'blkfltbscidx thread start', + b'coinstatsidx thread start', + b'txospenderidx thread start', b'msghand thread start', b'net thread start', b'addcon thread start',