Skip to content

Commit becd8dd

Browse files
committed
Merge #12618: Set SCHED_BATCH priority on the loadblk thread.
d54874d Set SCHED_BATCH priority on the loadblk thread. (Evan Klitzke) Pull request description: Today I came across #10271, and while reading the discussion #6358 was linked to. Linux systems have a `SCHED_BATCH` scheduler priority that is useful for threads like loadblk. You can find the full details at [sched(7)](http://man7.org/linux/man-pages/man7/sched.7.html), but I'll quote the relevant part of the man page below: > ...this policy will cause the scheduler to always assume that the thread is CPU-intensive. Consequently, the scheduler will apply a small scheduling penalty with respect to wakeup behavior, so that this thread is mildly disfavored in scheduling decisions. > > This policy is useful for workloads that are noninteractive, but do not want to lower their nice value, and for workloads that want a deterministic scheduling policy without interactivity causing extra preemptions (between the workload's tasks). I think this change is useful independently of #10271 and irrespective of whether that change is merged. Under normal operation the loadblk thread will just import `mempool.dat`. However, if Bitcoin is started with `-reindex` or `-reindex-chainstate` this thread will use a great deal of CPU while it rebuilds the chainstate database (and the block database in the case of `-reindex`). By setting `SCHED_BATCH` this thread is less likely to interfere with interactive tasks (e.g. the user's web browser, text editor, etc.). I'm leaving the nice value unchanged (which also affects scheduling decisions) because I think that's better set by the user. Likewise I'm not using [ioprio_set(2)](http://man7.org/linux/man-pages/man2/ioprio_set.2.html) because it can cause the thread to become completely I/O starved (and knowledgeable users can use `ionice(1)` anyway). Tree-SHA512: ea8f7d3921ed5708948809da771345cdc33efd7ba3323e9dfec07a25bc21e8612e2676f9c178e2710c7bc437e8c9cafc5e0463613688fea5699b6e8e2fec6cff
2 parents b2e5fe8 + d54874d commit becd8dd

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
@@ -632,6 +632,7 @@ void ThreadImport(std::vector<fs::path> vImportFiles)
632632
{
633633
const CChainParams& chainparams = Params();
634634
RenameThread("bitcoin-loadblk");
635+
ScheduleBatchPriority();
635636

636637
{
637638
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

@@ -1039,3 +1040,17 @@ fs::path AbsPathForConfigVal(const fs::path& path, bool net_specific)
10391040
{
10401041
return fs::absolute(path, GetDataDir(net_specific));
10411042
}
1043+
1044+
int ScheduleBatchPriority(void)
1045+
{
1046+
#ifdef SCHED_BATCH
1047+
const static sched_param param{.sched_priority = 0};
1048+
if (int ret = pthread_setschedparam(0, SCHED_BATCH, &param)) {
1049+
LogPrintf("Failed to pthread_setschedparam: %s\n", strerror(errno));
1050+
return ret;
1051+
}
1052+
return 0;
1053+
#else
1054+
return 1;
1055+
#endif
1056+
}

src/util.h

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

384+
/**
385+
* On platforms that support it, tell the kernel the calling thread is
386+
* CPU-intensive and non-interactive. See SCHED_BATCH in sched(7) for details.
387+
*
388+
* @return The return value of sched_setschedule(), or 1 on systems without
389+
* sched_setchedule().
390+
*/
391+
int ScheduleBatchPriority(void);
392+
384393
#endif // BITCOIN_UTIL_H

0 commit comments

Comments
 (0)