Skip to content

Commit d54874d

Browse files
committed
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.
1 parent 0a01843 commit d54874d

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
lines changed

src/init.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -625,6 +625,7 @@ void ThreadImport(std::vector<fs::path> vImportFiles)
625625
{
626626
const CChainParams& chainparams = Params();
627627
RenameThread("bitcoin-loadblk");
628+
ScheduleBatchPriority();
628629

629630
{
630631
CImportingNow imp;

src/util.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
#include <algorithm>
3333
#include <fcntl.h>
34+
#include <sched.h>
3435
#include <sys/resource.h>
3536
#include <sys/stat.h>
3637

@@ -966,3 +967,17 @@ fs::path AbsPathForConfigVal(const fs::path& path, bool net_specific)
966967
{
967968
return fs::absolute(path, GetDataDir(net_specific));
968969
}
970+
971+
int ScheduleBatchPriority(void)
972+
{
973+
#ifdef SCHED_BATCH
974+
const static sched_param param{.sched_priority = 0};
975+
if (int ret = pthread_setschedparam(0, SCHED_BATCH, &param)) {
976+
LogPrintf("Failed to pthread_setschedparam: %s\n", strerror(errno));
977+
return ret;
978+
}
979+
return 0;
980+
#else
981+
return 1;
982+
#endif
983+
}

src/util.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,4 +357,13 @@ std::unique_ptr<T> MakeUnique(Args&&... args)
357357
return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
358358
}
359359

360+
/**
361+
* On platforms that support it, tell the kernel the calling thread is
362+
* CPU-intensive and non-interactive. See SCHED_BATCH in sched(7) for details.
363+
*
364+
* @return The return value of sched_setschedule(), or 1 on systems without
365+
* sched_setchedule().
366+
*/
367+
int ScheduleBatchPriority(void);
368+
360369
#endif // BITCOIN_UTIL_H

0 commit comments

Comments
 (0)