Skip to content

Commit 271252c

Browse files
committed
validation, log: extract FlushSnapshotToDisk() function
This moves the flushing and logging into one method and adds logging of time duration and memory for the snapshot chainstate flushing.
1 parent a063647 commit 271252c

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-11
lines changed

src/validation.cpp

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

4843+
static void FlushSnapshotToDisk(CCoinsViewCache& coins_cache, bool snapshot_loaded)
4844+
{
4845+
LogPrintf("[snapshot] flushing %s (%.2f MB)... ", /* Continued */
4846+
snapshot_loaded ? "snapshot chainstate to disk" : "coins cache",
4847+
coins_cache.DynamicMemoryUsage() / (1000 * 1000));
4848+
4849+
const int64_t flush_now{GetTimeMillis()};
4850+
4851+
// TODO: if #17487 is merged, add erase=false here if snapshot is loaded, for better performance.
4852+
coins_cache.Flush();
4853+
4854+
LogPrintf("done (%.2fms)\n", GetTimeMillis() - flush_now);
4855+
}
4856+
48434857
bool ChainstateManager::PopulateAndValidateSnapshot(
48444858
CChainState& snapshot_chainstate,
48454859
CAutoFile& coins_file,
@@ -4877,7 +4891,6 @@ bool ChainstateManager::PopulateAndValidateSnapshot(
48774891
uint64_t coins_left = metadata.m_coins_count;
48784892

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

48834896
while (coins_left > 0) {
@@ -4921,19 +4934,14 @@ bool ChainstateManager::PopulateAndValidateSnapshot(
49214934
const auto snapshot_cache_state = WITH_LOCK(::cs_main,
49224935
return snapshot_chainstate.GetCoinsCacheSizeState());
49234936

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-
4937+
if (snapshot_cache_state >= CoinsCacheSizeState::CRITICAL) {
49304938
// This is a hack - we don't know what the actual best block is, but that
49314939
// doesn't matter for the purposes of flushing the cache here. We'll set this
49324940
// to its correct value (`base_blockhash`) below after the coins are loaded.
49334941
coins_cache.SetBestBlock(GetRandHash());
49344942

4935-
coins_cache.Flush();
4936-
LogPrintf("done (%.2fms)\n", GetTimeMillis() - flush_now);
4943+
// No need to acquire cs_main since this chainstate isn't being used yet.
4944+
FlushSnapshotToDisk(coins_cache, /*snapshot_loaded=*/false);
49374945
}
49384946
}
49394947
}
@@ -4963,9 +4971,8 @@ bool ChainstateManager::PopulateAndValidateSnapshot(
49634971
coins_cache.DynamicMemoryUsage() / (1000 * 1000),
49644972
base_blockhash.ToString());
49654973

4966-
LogPrintf("[snapshot] flushing snapshot chainstate to disk\n");
49674974
// 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.
4975+
FlushSnapshotToDisk(coins_cache, /*snapshot_loaded=*/true);
49694976

49704977
assert(coins_cache.GetBestBlock() == base_blockhash);
49714978

0 commit comments

Comments
 (0)