Skip to content

Commit d4b17c7

Browse files
committed
kernel: Remove batchpriority from kernel library
The current usage of ScheduleBatchPriority is not transparent. Once the thread scheduling is changed, it remains unchanged for the remainder of the thread's lifetime. So move the call from `ImportBlocks` to the init code where it is clearer that its effect lasts for the entire lifetime of the thread. Users of the kernel library might not expect `ImportBlocks` to have an influence on the thread it is called in. Particularly since it is only a compile time option and cannot be controlled at runtime. With this patch users of the kernel library can now choose their own scheduling policy.
1 parent 6f1d906 commit d4b17c7

File tree

3 files changed

+55
-57
lines changed

3 files changed

+55
-57
lines changed

src/Makefile.am

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -975,7 +975,6 @@ libbitcoinkernel_la_SOURCES = \
975975
txdb.cpp \
976976
txmempool.cpp \
977977
uint256.cpp \
978-
util/batchpriority.cpp \
979978
util/chaintype.cpp \
980979
util/check.cpp \
981980
util/feefrac.cpp \

src/init.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
#include <txdb.h>
7070
#include <txmempool.h>
7171
#include <util/asmap.h>
72+
#include <util/batchpriority.h>
7273
#include <util/chaintype.h>
7374
#include <util/check.h>
7475
#include <util/fs.h>
@@ -1735,6 +1736,7 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
17351736
}
17361737

17371738
chainman.m_thread_load = std::thread(&util::TraceThread, "initload", [=, &chainman, &args, &node] {
1739+
ScheduleBatchPriority();
17381740
// Import blocks
17391741
ImportBlocks(chainman, vImportFiles);
17401742
if (args.GetBoolArg("-stopafterblockimport", DEFAULT_STOPAFTERBLOCKIMPORT)) {

src/node/blockstorage.cpp

Lines changed: 53 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1175,69 +1175,66 @@ class ImportingNow
11751175

11761176
void ImportBlocks(ChainstateManager& chainman, std::vector<fs::path> vImportFiles)
11771177
{
1178-
ScheduleBatchPriority();
1179-
1180-
{
1181-
ImportingNow imp{chainman.m_blockman.m_importing};
1182-
1183-
// -reindex
1184-
if (fReindex) {
1185-
int nFile = 0;
1186-
// Map of disk positions for blocks with unknown parent (only used for reindex);
1187-
// parent hash -> child disk position, multiple children can have the same parent.
1188-
std::multimap<uint256, FlatFilePos> blocks_with_unknown_parent;
1189-
while (true) {
1190-
FlatFilePos pos(nFile, 0);
1191-
if (!fs::exists(chainman.m_blockman.GetBlockPosFilename(pos))) {
1192-
break; // No block files left to reindex
1193-
}
1194-
AutoFile file{chainman.m_blockman.OpenBlockFile(pos, true)};
1195-
if (file.IsNull()) {
1196-
break; // This error is logged in OpenBlockFile
1197-
}
1198-
LogPrintf("Reindexing block file blk%05u.dat...\n", (unsigned int)nFile);
1199-
chainman.LoadExternalBlockFile(file, &pos, &blocks_with_unknown_parent);
1200-
if (chainman.m_interrupt) {
1201-
LogPrintf("Interrupt requested. Exit %s\n", __func__);
1202-
return;
1203-
}
1204-
nFile++;
1178+
ImportingNow imp{chainman.m_blockman.m_importing};
1179+
1180+
// -reindex
1181+
if (fReindex) {
1182+
int nFile = 0;
1183+
// Map of disk positions for blocks with unknown parent (only used for reindex);
1184+
// parent hash -> child disk position, multiple children can have the same parent.
1185+
std::multimap<uint256, FlatFilePos> blocks_with_unknown_parent;
1186+
while (true) {
1187+
FlatFilePos pos(nFile, 0);
1188+
if (!fs::exists(chainman.m_blockman.GetBlockPosFilename(pos))) {
1189+
break; // No block files left to reindex
12051190
}
1206-
WITH_LOCK(::cs_main, chainman.m_blockman.m_block_tree_db->WriteReindexing(false));
1207-
fReindex = false;
1208-
LogPrintf("Reindexing finished\n");
1209-
// To avoid ending up in a situation without genesis block, re-try initializing (no-op if reindexing worked):
1210-
chainman.ActiveChainstate().LoadGenesisBlock();
1191+
AutoFile file{chainman.m_blockman.OpenBlockFile(pos, true)};
1192+
if (file.IsNull()) {
1193+
break; // This error is logged in OpenBlockFile
1194+
}
1195+
LogPrintf("Reindexing block file blk%05u.dat...\n", (unsigned int)nFile);
1196+
chainman.LoadExternalBlockFile(file, &pos, &blocks_with_unknown_parent);
1197+
if (chainman.m_interrupt) {
1198+
LogPrintf("Interrupt requested. Exit %s\n", __func__);
1199+
return;
1200+
}
1201+
nFile++;
12111202
}
1212-
1213-
// -loadblock=
1214-
for (const fs::path& path : vImportFiles) {
1215-
AutoFile file{fsbridge::fopen(path, "rb")};
1216-
if (!file.IsNull()) {
1217-
LogPrintf("Importing blocks file %s...\n", fs::PathToString(path));
1218-
chainman.LoadExternalBlockFile(file);
1219-
if (chainman.m_interrupt) {
1220-
LogPrintf("Interrupt requested. Exit %s\n", __func__);
1221-
return;
1222-
}
1223-
} else {
1224-
LogPrintf("Warning: Could not open blocks file %s\n", fs::PathToString(path));
1203+
WITH_LOCK(::cs_main, chainman.m_blockman.m_block_tree_db->WriteReindexing(false));
1204+
fReindex = false;
1205+
LogPrintf("Reindexing finished\n");
1206+
// To avoid ending up in a situation without genesis block, re-try initializing (no-op if reindexing worked):
1207+
chainman.ActiveChainstate().LoadGenesisBlock();
1208+
}
1209+
1210+
// -loadblock=
1211+
for (const fs::path& path : vImportFiles) {
1212+
AutoFile file{fsbridge::fopen(path, "rb")};
1213+
if (!file.IsNull()) {
1214+
LogPrintf("Importing blocks file %s...\n", fs::PathToString(path));
1215+
chainman.LoadExternalBlockFile(file);
1216+
if (chainman.m_interrupt) {
1217+
LogPrintf("Interrupt requested. Exit %s\n", __func__);
1218+
return;
12251219
}
1220+
} else {
1221+
LogPrintf("Warning: Could not open blocks file %s\n", fs::PathToString(path));
12261222
}
1223+
}
12271224

1228-
// scan for better chains in the block chain database, that are not yet connected in the active best chain
1225+
// scan for better chains in the block chain database, that are not yet connected in the active best chain
12291226

1230-
// We can't hold cs_main during ActivateBestChain even though we're accessing
1231-
// the chainman unique_ptrs since ABC requires us not to be holding cs_main, so retrieve
1232-
// the relevant pointers before the ABC call.
1233-
for (Chainstate* chainstate : WITH_LOCK(::cs_main, return chainman.GetAll())) {
1234-
BlockValidationState state;
1235-
if (!chainstate->ActivateBestChain(state, nullptr)) {
1236-
chainman.GetNotifications().fatalError(strprintf(_("Failed to connect best block (%s)."), state.ToString()));
1237-
return;
1238-
}
1227+
// We can't hold cs_main during ActivateBestChain even though we're accessing
1228+
// the chainman unique_ptrs since ABC requires us not to be holding cs_main, so retrieve
1229+
// the relevant pointers before the ABC call.
1230+
for (Chainstate* chainstate : WITH_LOCK(::cs_main, return chainman.GetAll())) {
1231+
BlockValidationState state;
1232+
if (!chainstate->ActivateBestChain(state, nullptr)) {
1233+
chainman.GetNotifications().fatalError(strprintf(_("Failed to connect best block (%s)."), state.ToString()));
1234+
return;
12391235
}
1240-
} // End scope of ImportingNow
1236+
}
1237+
// End scope of ImportingNow
12411238
}
12421239

12431240
std::ostream& operator<<(std::ostream& os, const BlockfileType& type) {

0 commit comments

Comments
 (0)