Skip to content

Commit dbb3113

Browse files
committed
Merge bitcoin#30083: kernel: Remove batchpriority from kernel library
d4b17c7 kernel: Remove batchpriority from kernel library (TheCharlatan) Pull request description: 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 freely choose their own scheduling policy. This PR is easier reviewed with `git diff --color-moved-ws=ignore-all-space --color-moved=dimmed-zebra` --- This PR is part of the [libbitcoinkernel project](bitcoin#27587). ACKs for top commit: maflcko: ACK d4b17c7 📭 ryanofsky: Code review ACK d4b17c7, just added suggested comment since last review hebasto: ACK d4b17c7, I have reviewed the code and it looks OK. Tree-SHA512: cafedecd9affad58ddd7f30f68bba71291ca951bb186ff4b2da04b7f21f0b26e5e3143846d032b9e391bd5ce6c7466b97aa3758d2a85ebd7353eb8b69139641a
2 parents 7fcf4e9 + d4b17c7 commit dbb3113

File tree

3 files changed

+55
-57
lines changed

3 files changed

+55
-57
lines changed

src/Makefile.am

-1
Original file line numberDiff line numberDiff line change
@@ -974,7 +974,6 @@ libbitcoinkernel_la_SOURCES = \
974974
txdb.cpp \
975975
txmempool.cpp \
976976
uint256.cpp \
977-
util/batchpriority.cpp \
978977
util/chaintype.cpp \
979978
util/check.cpp \
980979
util/feefrac.cpp \

src/init.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
#include <txdb.h>
7272
#include <txmempool.h>
7373
#include <util/asmap.h>
74+
#include <util/batchpriority.h>
7475
#include <util/chaintype.h>
7576
#include <util/check.h>
7677
#include <util/fs.h>
@@ -1741,6 +1742,7 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
17411742
}
17421743

17431744
chainman.m_thread_load = std::thread(&util::TraceThread, "initload", [=, &chainman, &args, &node] {
1745+
ScheduleBatchPriority();
17441746
// Import blocks
17451747
ImportBlocks(chainman, vImportFiles);
17461748
if (args.GetBoolArg("-stopafterblockimport", DEFAULT_STOPAFTERBLOCKIMPORT)) {

src/node/blockstorage.cpp

+53-56
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)