diff --git a/src/init.cpp b/src/init.cpp --- a/src/init.cpp +++ b/src/init.cpp @@ -1125,6 +1125,7 @@ static void ThreadImport(const Config &config, std::vector vImportFiles) { RenameThread("bitcoin-loadblk"); + ScheduleBatchPriority(); { CImportingNow imp; diff --git a/src/util.h b/src/util.h --- a/src/util.h +++ b/src/util.h @@ -364,4 +364,13 @@ } // namespace util +/** + * 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(); + #endif // BITCOIN_UTIL_H diff --git a/src/util.cpp b/src/util.cpp --- a/src/util.cpp +++ b/src/util.cpp @@ -44,6 +44,7 @@ #include #include +#include #include #include @@ -1311,3 +1312,16 @@ fs::path AbsPathForConfigVal(const fs::path &path, bool net_specific) { return fs::absolute(path, GetDataDir(net_specific)); } + +int ScheduleBatchPriority() { +#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 +}