diff --git a/src/init.cpp b/src/init.cpp index 880c8bce1..811e59e36 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -632,6 +632,7 @@ void ThreadImport(std::vector vImportFiles) { const CChainParams& chainparams = Params(); RenameThread("bitcoin-loadblk"); + ScheduleBatchPriority(); { CImportingNow imp; diff --git a/src/util.cpp b/src/util.cpp index 46054f502..8b36c3e5f 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -31,6 +31,7 @@ #include #include +#include #include #include @@ -1039,3 +1040,17 @@ fs::path AbsPathForConfigVal(const fs::path& path, bool net_specific) { return fs::absolute(path, GetDataDir(net_specific)); } + +int ScheduleBatchPriority(void) +{ +#ifdef SCHED_BATCH + const static sched_param param{.sched_priority = 0}; + if (int ret = pthread_setschedparam(0, SCHED_BATCH, ¶m)) { + LogPrintf("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 7114c0acc..f625a61d8 100644 --- a/src/util.h +++ b/src/util.h @@ -381,4 +381,13 @@ std::unique_ptr MakeUnique(Args&&... args) return std::unique_ptr(new T(std::forward(args)...)); } +/** + * 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