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.
This commit is contained in:
Evan Klitzke 2018-03-06 12:50:20 -05:00
parent 0a018431c4
commit d54874d795
No known key found for this signature in database
GPG Key ID: 157EFCACBC648422
3 changed files with 25 additions and 0 deletions

View File

@ -625,6 +625,7 @@ void ThreadImport(std::vector<fs::path> vImportFiles)
{ {
const CChainParams& chainparams = Params(); const CChainParams& chainparams = Params();
RenameThread("bitcoin-loadblk"); RenameThread("bitcoin-loadblk");
ScheduleBatchPriority();
{ {
CImportingNow imp; CImportingNow imp;

View File

@ -31,6 +31,7 @@
#include <algorithm> #include <algorithm>
#include <fcntl.h> #include <fcntl.h>
#include <sched.h>
#include <sys/resource.h> #include <sys/resource.h>
#include <sys/stat.h> #include <sys/stat.h>
@ -966,3 +967,17 @@ fs::path AbsPathForConfigVal(const fs::path& path, bool net_specific)
{ {
return fs::absolute(path, GetDataDir(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, &param)) {
LogPrintf("Failed to pthread_setschedparam: %s\n", strerror(errno));
return ret;
}
return 0;
#else
return 1;
#endif
}

View File

@ -357,4 +357,13 @@ std::unique_ptr<T> MakeUnique(Args&&... args)
return std::unique_ptr<T>(new T(std::forward<Args>(args)...)); return std::unique_ptr<T>(new T(std::forward<Args>(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 #endif // BITCOIN_UTIL_H