From 3a779907816ca2a86f8d3577cde4eaabc64f8e15 Mon Sep 17 00:00:00 2001 From: Nik Bougalis Date: Sun, 15 Dec 2019 20:37:10 -0800 Subject: [PATCH] Improve automatic I/O thread tuning algorithm --- src/ripple/app/main/Application.cpp | 17 +++++++++++++++-- src/ripple/app/main/BasicApp.cpp | 18 ++++++++++-------- 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/src/ripple/app/main/Application.cpp b/src/ripple/app/main/Application.cpp index 21fd48bb9..705617dd6 100644 --- a/src/ripple/app/main/Application.cpp +++ b/src/ripple/app/main/Application.cpp @@ -399,7 +399,15 @@ public: #if RIPPLE_SINGLE_IO_SERVICE_THREAD return 1; #else - return (config.NODE_SIZE >= 2) ? 2 : 1; + auto const cores = std::thread::hardware_concurrency(); + + // Use a single thread when running on under-provisioned systems + // or if we are configured to use minimal resources. + if ((cores == 1) || ((config.NODE_SIZE == 0) && (cores == 2))) + return 1; + + // Otherwise, prefer two threads. + return 2; #endif } @@ -414,7 +422,6 @@ public: , config_ (std::move(config)) , logs_ (std::move(logs)) , timeKeeper_ (std::move(timeKeeper)) - , m_journal (logs_->journal("Application")) // PerfLog must be started before any other threads are launched. @@ -1321,6 +1328,12 @@ bool ApplicationImp::setup() JLOG(m_journal.info()) << "process starting: " << BuildInfo::getFullVersionString(); + if (numberOfThreads(*config_) < 2) + { + JLOG (m_journal.warn()) << + "Limited to a single I/O service thread by system configuration."; + } + // Optionally turn off logging to console. logs_->silent (config_->silent()); diff --git a/src/ripple/app/main/BasicApp.cpp b/src/ripple/app/main/BasicApp.cpp index cf54d3beb..d86447509 100644 --- a/src/ripple/app/main/BasicApp.cpp +++ b/src/ripple/app/main/BasicApp.cpp @@ -24,20 +24,22 @@ BasicApp::BasicApp(std::size_t numberOfThreads) { work_.emplace (io_service_); threads_.reserve(numberOfThreads); + while(numberOfThreads--) - threads_.emplace_back( - [this, numberOfThreads]() + { + threads_.emplace_back([this, numberOfThreads]() { - beast::setCurrentThreadName( - std::string("io_service #") + - std::to_string(numberOfThreads)); - this->io_service_.run(); + beast::setCurrentThreadName("io svc #" + + std::to_string(numberOfThreads)); + this->io_service_.run(); }); + } } BasicApp::~BasicApp() { work_ = boost::none; - for (auto& _ : threads_) - _.join(); + + for (auto& t : threads_) + t.join(); }