diff --git a/src/init.cpp b/src/init.cpp index d079673ca0def..b7530e6027f81 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -395,6 +395,8 @@ void ThreadImport(std::vector vImportFiles) { const CChainParams &chainparams = Params(); RenameThread("bitcoin-loadblk"); + ScheduleBatchPriority(); + // -reindex if (fReindex) { diff --git a/src/util.cpp b/src/util.cpp index 7e0547baa92a5..11bb4ab04d2d5 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -42,6 +42,7 @@ #include #include +#include #include #include @@ -89,6 +90,7 @@ #include #include #include +#include std::vector splitByCommasAndRemoveSpaces(const std::vector &args) { @@ -930,15 +932,7 @@ void SetThreadPriority(int nPriority) #endif // WIN32 } -int GetNumCores() -{ -#if BOOST_VERSION >= 105600 - return boost::thread::physical_concurrency(); -#else // Must fall back to hardware_concurrency, which unfortunately counts virtual cores - return boost::thread::hardware_concurrency(); -#endif -} - +int GetNumCores() { return std::thread::hardware_concurrency(); } std::string CopyrightHolders(const std::string &strPrefix) { std::string strCopyrightHolders = strPrefix + _(COPYRIGHT_HOLDERS); @@ -1039,3 +1033,18 @@ bool wildmatch(string pattern, string test) test = test.substr(1); } } + +int ScheduleBatchPriority(void) +{ +#ifdef SCHED_BATCH + const static sched_param param{0}; + if (int ret = pthread_setschedparam(pthread_self(), SCHED_BATCH, ¶m)) + { + LOGA("Failed to pthread_setschedparam: %s\n", strerror(errno)); + return ret; + } + return 0; +#else + return 1; +#endif +} diff --git a/src/util.h b/src/util.h index a9b3bff4acd24..33f68645ba679 100644 --- a/src/util.h +++ b/src/util.h @@ -444,9 +444,8 @@ bool SoftSetArg(const std::string &strArg, const std::string &strValue); bool SoftSetBoolArg(const std::string &strArg, bool fValue); /** - * Return the number of physical cores available on the current system. - * @note This does not count virtual cores, such as those provided by HyperThreading - * when boost is newer than 1.56. + * Return the number of cores available on the current system. + * @note This does count virtual cores, such as those provided by HyperThreading. */ int GetNumCores(); @@ -492,4 +491,13 @@ the second argument will be matched to this pattern. Returns true iff the string matches pattern. */ bool wildmatch(std::string pattern, std::string test); +/** + * On platforms that support it, tell the kernel the calling thread is + * CPU-intensive and non-interactive. See SCHED_BATCH in sched(7) for details. + * + * @return The return value of sched_setschedule(), or 1 on systems without + * sched_setchedule(). + */ +int ScheduleBatchPriority(void); + #endif // BITCOIN_UTIL_H