Skip to content

Commit 767c012

Browse files
author
MarcoFalke
committed
Merge bitcoin/bitcoin#23738: validation, log: improve logging of ChainstateManager snapshot persistance
50209a4 validation, doc: remove TODO comment (Jon Atack) 8e37fa8 validation, log: improve logging in FlushSnapshotToDisk() (Jon Atack) 271252c validation, log: extract FlushSnapshotToDisk() function (Jon Atack) Pull request description: Use the `LOG_TIME_MILLIS_WITH_CATEGORY_MSG_ONCE` macro to improve the logging of ChainstateManager snapshot persistance, log task start and completion separately and no longer manually track the duration, as suggested by Marco Falke in bitcoin/bitcoin#22872 (comment). Extract the flushing into one function, which clarifies the logic, extends the improved logging to both flushing call sites, and allows logging the prefix `FlushSnapshotToDisk`, which is similar to `FlushStateToDisk`. before ``` [snapshot] flushing coins cache (0 MB)... done (0.00ms) [snapshot] flushing snapshot chainstate to disk ``` after ``` FlushSnapshotToDisk: flushing coins cache (0 MB) started ... FlushSnapshotToDisk: completed (0.00ms) FlushSnapshotToDisk: saving snapshot chainstate (0 MB) started ... FlushSnapshotToDisk: completed (0.00ms) ``` The logging can be observed in the output of ``` ./src/test/test_bitcoin -t validation_chainstate_tests -- DEBUG_LOG_OUT ``` Top commit has no ACKs. Tree-SHA512: 5d954cd8c7455f8625152a43663a237f04717bb834aed62925a56e17c711fca6ccfc03783970b6b0bde44f64617d804b423a7048287c06ee816db36247acf272
2 parents bf66e25 + 50209a4 commit 767c012

File tree

1 file changed

+15
-11
lines changed

1 file changed

+15
-11
lines changed

src/validation.cpp

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4840,6 +4840,17 @@ bool ChainstateManager::ActivateSnapshot(
48404840
return true;
48414841
}
48424842

4843+
static void FlushSnapshotToDisk(CCoinsViewCache& coins_cache, bool snapshot_loaded)
4844+
{
4845+
LOG_TIME_MILLIS_WITH_CATEGORY_MSG_ONCE(
4846+
strprintf("%s (%.2f MB)",
4847+
snapshot_loaded ? "saving snapshot chainstate" : "flushing coins cache",
4848+
coins_cache.DynamicMemoryUsage() / (1000 * 1000)),
4849+
BCLog::LogFlags::ALL);
4850+
4851+
coins_cache.Flush();
4852+
}
4853+
48434854
bool ChainstateManager::PopulateAndValidateSnapshot(
48444855
CChainState& snapshot_chainstate,
48454856
CAutoFile& coins_file,
@@ -4877,7 +4888,6 @@ bool ChainstateManager::PopulateAndValidateSnapshot(
48774888
uint64_t coins_left = metadata.m_coins_count;
48784889

48794890
LogPrintf("[snapshot] loading coins from snapshot %s\n", base_blockhash.ToString());
4880-
int64_t flush_now{0};
48814891
int64_t coins_processed{0};
48824892

48834893
while (coins_left > 0) {
@@ -4921,19 +4931,14 @@ bool ChainstateManager::PopulateAndValidateSnapshot(
49214931
const auto snapshot_cache_state = WITH_LOCK(::cs_main,
49224932
return snapshot_chainstate.GetCoinsCacheSizeState());
49234933

4924-
if (snapshot_cache_state >=
4925-
CoinsCacheSizeState::CRITICAL) {
4926-
LogPrintf("[snapshot] flushing coins cache (%.2f MB)... ", /* Continued */
4927-
coins_cache.DynamicMemoryUsage() / (1000 * 1000));
4928-
flush_now = GetTimeMillis();
4929-
4934+
if (snapshot_cache_state >= CoinsCacheSizeState::CRITICAL) {
49304935
// This is a hack - we don't know what the actual best block is, but that
49314936
// doesn't matter for the purposes of flushing the cache here. We'll set this
49324937
// to its correct value (`base_blockhash`) below after the coins are loaded.
49334938
coins_cache.SetBestBlock(GetRandHash());
49344939

4935-
coins_cache.Flush();
4936-
LogPrintf("done (%.2fms)\n", GetTimeMillis() - flush_now);
4940+
// No need to acquire cs_main since this chainstate isn't being used yet.
4941+
FlushSnapshotToDisk(coins_cache, /*snapshot_loaded=*/false);
49374942
}
49384943
}
49394944
}
@@ -4963,9 +4968,8 @@ bool ChainstateManager::PopulateAndValidateSnapshot(
49634968
coins_cache.DynamicMemoryUsage() / (1000 * 1000),
49644969
base_blockhash.ToString());
49654970

4966-
LogPrintf("[snapshot] flushing snapshot chainstate to disk\n");
49674971
// No need to acquire cs_main since this chainstate isn't being used yet.
4968-
coins_cache.Flush(); // TODO: if #17487 is merged, add erase=false here for better performance.
4972+
FlushSnapshotToDisk(coins_cache, /*snapshot_loaded=*/true);
49694973

49704974
assert(coins_cache.GetBestBlock() == base_blockhash);
49714975

0 commit comments

Comments
 (0)