From d54874d795a523d7cad5f4a9f4270145e56238f8 Mon Sep 17 00:00:00 2001 From: Evan Klitzke Date: Tue, 6 Mar 2018 12:50:20 -0500 Subject: Set SCHED_BATCH priority on the loadblk thread. While reading another PR I saw a mention of #6358. The use case for SCHED_BATCH is to hint to the kernel that the thread is running a non-interactive workload that consumes a lot of CPU time. This is helpful on desktop machines where the loadblk thread can interfere with interactive applications. More details can be found in the sched(7) man page. --- src/init.cpp | 1 + src/util.cpp | 15 +++++++++++++++ src/util.h | 9 +++++++++ 3 files changed, 25 insertions(+) (limited to 'src') diff --git a/src/init.cpp b/src/init.cpp index e5443eef0e..62767edbf8 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -625,6 +625,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 494d5c4eaf..842d4f6e19 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -31,6 +31,7 @@ #include #include +#include #include #include @@ -966,3 +967,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 04ff44f218..a9f64a0566 100644 --- a/src/util.h +++ b/src/util.h @@ -357,4 +357,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 -- cgit v1.2.3