Skip to content

Commit 4716267

Browse files
committed
Use real number of cores for default -par, ignore virtual cores
To determine the default for `-par`, the number of script verification threads, use [boost::thread::physical_concurrency()](http://www.boost.org/doc/libs/1_58_0/doc/html/thread/thread_management.html#thread.thread_management.thread.physical_concurrency) which counts only physical cores, not virtual cores. Virtual cores are roughly a set of cached registers to avoid context switches while threading, they cannot actually perform work, so spawning a verification thread for them could even reduce efficiency and will put undue load on the system. Should fix issue #6358, as well as some other reported system overload issues, especially on Intel processors. The function was only introduced in boost 1.56, so provide a utility function `GetNumCores` to fall back for older Boost versions.
1 parent da77a6f commit 4716267

File tree

5 files changed

+21
-4
lines changed

5 files changed

+21
-4
lines changed

src/init.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ std::string HelpMessage(HelpMessageMode mode)
283283
strUsage += HelpMessageOpt("-loadblock=<file>", _("Imports blocks from external blk000??.dat file") + " " + _("on startup"));
284284
strUsage += HelpMessageOpt("-maxorphantx=<n>", strprintf(_("Keep at most <n> unconnectable transactions in memory (default: %u)"), DEFAULT_MAX_ORPHAN_TRANSACTIONS));
285285
strUsage += HelpMessageOpt("-par=<n>", strprintf(_("Set the number of script verification threads (%u to %d, 0 = auto, <0 = leave that many cores free, default: %d)"),
286-
-(int)boost::thread::hardware_concurrency(), MAX_SCRIPTCHECK_THREADS, DEFAULT_SCRIPTCHECK_THREADS));
286+
-GetNumCores(), MAX_SCRIPTCHECK_THREADS, DEFAULT_SCRIPTCHECK_THREADS));
287287
#ifndef WIN32
288288
strUsage += HelpMessageOpt("-pid=<file>", strprintf(_("Specify pid file (default: %s)"), "bitcoind.pid"));
289289
#endif
@@ -774,7 +774,7 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler)
774774
// -par=0 means autodetect, but nScriptCheckThreads==0 means no concurrency
775775
nScriptCheckThreads = GetArg("-par", DEFAULT_SCRIPTCHECK_THREADS);
776776
if (nScriptCheckThreads <= 0)
777-
nScriptCheckThreads += boost::thread::hardware_concurrency();
777+
nScriptCheckThreads += GetNumCores();
778778
if (nScriptCheckThreads <= 1)
779779
nScriptCheckThreads = 0;
780780
else if (nScriptCheckThreads > MAX_SCRIPTCHECK_THREADS)

src/miner.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -560,7 +560,7 @@ void GenerateBitcoins(bool fGenerate, CWallet* pwallet, int nThreads)
560560
if (Params().DefaultMinerThreads())
561561
nThreads = Params().DefaultMinerThreads();
562562
else
563-
nThreads = boost::thread::hardware_concurrency();
563+
nThreads = GetNumCores();
564564
}
565565

566566
if (minerThreads != NULL)

src/qt/optionsdialog.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ OptionsDialog::OptionsDialog(QWidget *parent, bool enableWallet) :
4242
/* Main elements init */
4343
ui->databaseCache->setMinimum(nMinDbCache);
4444
ui->databaseCache->setMaximum(nMaxDbCache);
45-
ui->threadsScriptVerif->setMinimum(-(int)boost::thread::hardware_concurrency());
45+
ui->threadsScriptVerif->setMinimum(-GetNumCores());
4646
ui->threadsScriptVerif->setMaximum(MAX_SCRIPTCHECK_THREADS);
4747

4848
/* Network elements init */

src/util.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -756,3 +756,13 @@ void SetThreadPriority(int nPriority)
756756
#endif // PRIO_THREAD
757757
#endif // WIN32
758758
}
759+
760+
int GetNumCores()
761+
{
762+
#if BOOST_VERSION >= 105600
763+
return boost::thread::physical_concurrency();
764+
#else // Must fall back to hardware_concurrency, which unfortunately counts virtual cores
765+
return boost::thread::hardware_concurrency();
766+
#endif
767+
}
768+

src/util.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,13 @@ std::string HelpMessageGroup(const std::string& message);
199199
*/
200200
std::string HelpMessageOpt(const std::string& option, const std::string& message);
201201

202+
/**
203+
* Return the number of physical cores available on the current system.
204+
* @note This does not count virtual cores, such as those provided by HyperThreading
205+
* when boost is newer than 1.56.
206+
*/
207+
int GetNumCores();
208+
202209
void SetThreadPriority(int nPriority);
203210
void RenameThread(const char* name);
204211

0 commit comments

Comments
 (0)