From 6aa7c187d19b8537040fbb4debfad94845775c91 Mon Sep 17 00:00:00 2001 From: Jack Grigg Date: Fri, 13 May 2022 21:03:57 +0000 Subject: [PATCH 1/2] Shorten thread name prefix Threads can be a maximum of 15 characters, so using `zc-` instead of `zcash-` as a prefix means we can fit more information into the thread names. --- src/httpserver.cpp | 4 ++-- src/init.cpp | 4 +++- src/main.cpp | 2 +- src/metrics.cpp | 3 --- src/util.h | 2 +- src/wallet/walletdb.cpp | 2 +- 6 files changed, 8 insertions(+), 9 deletions(-) diff --git a/src/httpserver.cpp b/src/httpserver.cpp index 968d9a5f7..27c8b0a23 100644 --- a/src/httpserver.cpp +++ b/src/httpserver.cpp @@ -289,7 +289,7 @@ static void http_reject_request_cb(struct evhttp_request* req, void*) /** Event dispatcher thread */ static bool ThreadHTTP(struct event_base* base, struct evhttp* http) { - RenameThread("zcash-http"); + RenameThread("zc-http-server"); LogPrint("http", "Entering http event loop\n"); event_base_dispatch(base); // Event loop will be interrupted by InterruptHTTPServer() @@ -339,7 +339,7 @@ static bool HTTPBindAddresses(struct evhttp* http) /** Simple wrapper to set thread name and run work queue */ static void HTTPWorkQueueRun(WorkQueue* queue) { - RenameThread("zcash-httpworker"); + RenameThread("zc-http-worker"); queue->Run(); } diff --git a/src/init.cpp b/src/init.cpp index 7cc7698f3..6d07f39c6 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -1438,7 +1438,9 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler) !fPrintToConsole && !GetBoolArg("-daemon", false)) { // Start the persistent metrics interface ConnectMetricsScreen(); - threadGroup.create_thread(&ThreadShowMetricsScreen); + threadGroup.create_thread( + boost::bind(&TraceThread, "metrics-ui", &ThreadShowMetricsScreen) + ); } // Initialize Zcash circuit parameters diff --git a/src/main.cpp b/src/main.cpp index 8e8856b7c..77f9093f7 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3029,7 +3029,7 @@ bool FindUndoPos(CValidationState &state, int nFile, CDiskBlockPos &pos, unsigne static CCheckQueue scriptcheckqueue(128); void ThreadScriptCheck() { - RenameThread("zcash-scriptch"); + RenameThread("zc-scriptcheck"); scriptcheckqueue.Thread(); } diff --git a/src/metrics.cpp b/src/metrics.cpp index 42603b0b1..231f5e457 100644 --- a/src/metrics.cpp +++ b/src/metrics.cpp @@ -601,9 +601,6 @@ bool enableVTMode() void ThreadShowMetricsScreen() { - // Make this thread recognisable as the metrics screen thread - RenameThread("zcash-metrics-screen"); - // Determine whether we should render a persistent UI or rolling metrics bool isTTY = isatty(STDOUT_FILENO); bool isScreen = GetBoolArg("-metricsui", isTTY); diff --git a/src/util.h b/src/util.h index 36f7ef2dc..ae850d385 100644 --- a/src/util.h +++ b/src/util.h @@ -192,7 +192,7 @@ void RenameThread(const char* name); */ template void TraceThread(const char* name, Callable func) { - std::string s = strprintf("zcash-%s", name); + std::string s = strprintf("zc-%s", name); RenameThread(s.c_str()); try { diff --git a/src/wallet/walletdb.cpp b/src/wallet/walletdb.cpp index c130551db..93e40ca48 100644 --- a/src/wallet/walletdb.cpp +++ b/src/wallet/walletdb.cpp @@ -1134,7 +1134,7 @@ DBErrors CWalletDB::ZapWalletTx(CWallet* pwallet, vector& vWtx) void ThreadFlushWalletDB(const string& strFile) { // Make this thread recognisable as the wallet flushing thread - RenameThread("zcash-wallet"); + RenameThread("zc-wallet-flush"); static bool fOneThread; if (fOneThread) From 1f1810c37d00cb46d00d8553e6de3c6fdb991010 Mon Sep 17 00:00:00 2001 From: Jack Grigg Date: Fri, 13 May 2022 21:05:11 +0000 Subject: [PATCH 2/2] Name currently-unnamed threads that we can rename After this, the only threads that aren't named are the LevelDB background threads. --- Cargo.lock | 1 + Cargo.toml | 3 +++ src/asyncrpcqueue.cpp | 3 +++ src/bitcoind.cpp | 4 ++++ src/init.cpp | 4 ++++ src/rust/include/rust/init.h | 19 +++++++++++++++++++ src/rust/src/init_ffi.rs | 7 +++++++ src/rust/src/rustzcash.rs | 1 + 8 files changed, 42 insertions(+) create mode 100644 src/rust/include/rust/init.h create mode 100644 src/rust/src/init_ffi.rs diff --git a/Cargo.lock b/Cargo.lock index 2080a8daa..336fc9664 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -982,6 +982,7 @@ dependencies = [ "orchard", "rand 0.8.5", "rand_core 0.6.3", + "rayon", "secp256k1", "secrecy", "subtle", diff --git a/Cargo.toml b/Cargo.toml index c83841bb9..37a006ea3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -57,6 +57,9 @@ zcash_proofs = "0.6" ed25519-zebra = "3" zeroize = "1.4.2" +# Rust threading +rayon = "1.5" + # Metrics ipnet = "2" metrics = "0.17" diff --git a/src/asyncrpcqueue.cpp b/src/asyncrpcqueue.cpp index 361063ba5..ddee22df3 100644 --- a/src/asyncrpcqueue.cpp +++ b/src/asyncrpcqueue.cpp @@ -3,6 +3,7 @@ // file COPYING or https://www.opensource.org/licenses/mit-license.php . #include "asyncrpcqueue.h" +#include "util.h" static std::atomic workerCounter(0); @@ -26,6 +27,8 @@ AsyncRPCQueue::~AsyncRPCQueue() { * A worker will execute this method on a new thread */ void AsyncRPCQueue::run(size_t workerId) { + std::string s = strprintf("zc-asyncrpc-%s", workerId); + RenameThread(s.c_str()); while (true) { AsyncRPCOperationId key; diff --git a/src/bitcoind.cpp b/src/bitcoind.cpp index 122bb05c2..c8bb23b30 100644 --- a/src/bitcoind.cpp +++ b/src/bitcoind.cpp @@ -43,6 +43,10 @@ static bool fDaemon; void WaitForShutdown(boost::thread_group* threadGroup) { + // This is the main process thread, but after this point, all we do here is wait for + // shutdown, and then shut down the node. + RenameThread("zc-wait-to-stop"); + bool fShutdown = ShutdownRequested(); // Tell the main threads to shutdown. while (!fShutdown) diff --git a/src/init.cpp b/src/init.cpp index 6d07f39c6..57d76c77b 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -66,6 +66,7 @@ #include "zmq/zmqnotificationinterface.h" #endif +#include #include #include "librustzcash.h" @@ -1071,6 +1072,9 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler) std::set_new_handler(new_handler_terminate); + // Set up global Rayon threadpool. + zcashd_init_rayon_threadpool(); + // ********************************************************* Step 2: parameter interactions const CChainParams& chainparams = Params(); diff --git a/src/rust/include/rust/init.h b/src/rust/include/rust/init.h new file mode 100644 index 000000000..7eae676b3 --- /dev/null +++ b/src/rust/include/rust/init.h @@ -0,0 +1,19 @@ +// Copyright (c) 2022 The Zcash developers +// Distributed under the MIT software license, see the accompanying +// file COPYING or https://www.opensource.org/licenses/mit-license.php . + +#ifndef ZCASH_RUST_INCLUDE_RUST_INIT_H +#define ZCASH_RUST_INCLUDE_RUST_INIT_H + +#ifdef __cplusplus +extern "C" { +#endif + +/// Initializes the global Rayon threadpool. +void zcashd_init_rayon_threadpool(); + +#ifdef __cplusplus +} +#endif + +#endif // ZCASH_RUST_INCLUDE_RUST_INIT_H diff --git a/src/rust/src/init_ffi.rs b/src/rust/src/init_ffi.rs new file mode 100644 index 000000000..8ef139c9c --- /dev/null +++ b/src/rust/src/init_ffi.rs @@ -0,0 +1,7 @@ +#[no_mangle] +pub extern "C" fn zcashd_init_rayon_threadpool() { + rayon::ThreadPoolBuilder::new() + .thread_name(|i| format!("zc-rayon-{}", i)) + .build_global() + .expect("Only initialized once"); +} diff --git a/src/rust/src/rustzcash.rs b/src/rust/src/rustzcash.rs index f5b85fd67..c7443c74d 100644 --- a/src/rust/src/rustzcash.rs +++ b/src/rust/src/rustzcash.rs @@ -77,6 +77,7 @@ mod builder_ffi; mod history_ffi; mod incremental_merkle_tree; mod incremental_merkle_tree_ffi; +mod init_ffi; mod orchard_ffi; mod orchard_keys_ffi; mod transaction_ffi;