Changeset View
Changeset View
Standalone View
Standalone View
src/validation.cpp
Show First 20 Lines • Show All 2,042 Lines • ▼ Show 20 Lines | MinerFundSuccess: | ||||
nTimeCallbacks += nTime6 - nTime5; | nTimeCallbacks += nTime6 - nTime5; | ||||
LogPrint(BCLog::BENCH, " - Callbacks: %.2fms [%.2fs (%.2fms/blk)]\n", | LogPrint(BCLog::BENCH, " - Callbacks: %.2fms [%.2fs (%.2fms/blk)]\n", | ||||
MILLI * (nTime6 - nTime5), nTimeCallbacks * MICRO, | MILLI * (nTime6 - nTime5), nTimeCallbacks * MICRO, | ||||
nTimeCallbacks * MILLI / nBlocksTotal); | nTimeCallbacks * MILLI / nBlocksTotal); | ||||
return true; | return true; | ||||
} | } | ||||
CoinsCacheSizeState | |||||
CChainState::GetCoinsCacheSizeState(const CTxMemPool &tx_pool) { | |||||
return this->GetCoinsCacheSizeState( | |||||
tx_pool, nCoinCacheUsage, | |||||
gArgs.GetArg("-maxmempool", DEFAULT_MAX_MEMPOOL_SIZE) * 1000000); | |||||
} | |||||
CoinsCacheSizeState | |||||
CChainState::GetCoinsCacheSizeState(const CTxMemPool &tx_pool, | |||||
size_t max_coins_cache_size_bytes, | |||||
size_t max_mempool_size_bytes) { | |||||
int64_t nMempoolUsage = tx_pool.DynamicMemoryUsage(); | |||||
int64_t cacheSize = CoinsTip().DynamicMemoryUsage(); | |||||
int64_t nTotalSpace = | |||||
max_coins_cache_size_bytes + | |||||
std::max<int64_t>(max_mempool_size_bytes - nMempoolUsage, 0); | |||||
//! No need to periodic flush if at least this much space still available. | |||||
static constexpr int64_t MAX_BLOCK_COINSDB_USAGE_BYTES = | |||||
10 * 1024 * 1024; // 10MB | |||||
int64_t large_threshold = std::max( | |||||
(9 * nTotalSpace) / 10, nTotalSpace - MAX_BLOCK_COINSDB_USAGE_BYTES); | |||||
if (cacheSize > nTotalSpace) { | |||||
LogPrintf("Cache size (%s) exceeds total space (%s)\n", cacheSize, | |||||
nTotalSpace); | |||||
return CoinsCacheSizeState::CRITICAL; | |||||
} else if (cacheSize > large_threshold) { | |||||
return CoinsCacheSizeState::LARGE; | |||||
} | |||||
return CoinsCacheSizeState::OK; | |||||
} | |||||
bool CChainState::FlushStateToDisk(const CChainParams &chainparams, | bool CChainState::FlushStateToDisk(const CChainParams &chainparams, | ||||
BlockValidationState &state, | BlockValidationState &state, | ||||
FlushStateMode mode, | FlushStateMode mode, | ||||
int nManualPruneHeight) { | int nManualPruneHeight) { | ||||
int64_t nMempoolUsage = g_mempool.DynamicMemoryUsage(); | |||||
LOCK(cs_main); | LOCK(cs_main); | ||||
assert(this->CanFlushToDisk()); | assert(this->CanFlushToDisk()); | ||||
static int64_t nLastWrite = 0; | static int64_t nLastWrite = 0; | ||||
static int64_t nLastFlush = 0; | static int64_t nLastFlush = 0; | ||||
std::set<int> setFilesToPrune; | std::set<int> setFilesToPrune; | ||||
bool full_flush_completed = false; | bool full_flush_completed = false; | ||||
const size_t coins_count = CoinsTip().GetCacheSize(); | const size_t coins_count = CoinsTip().GetCacheSize(); | ||||
const size_t coins_mem_usage = CoinsTip().DynamicMemoryUsage(); | const size_t coins_mem_usage = CoinsTip().DynamicMemoryUsage(); | ||||
try { | try { | ||||
{ | { | ||||
bool fFlushForPrune = false; | bool fFlushForPrune = false; | ||||
bool fDoFullFlush = false; | bool fDoFullFlush = false; | ||||
CoinsCacheSizeState cache_state = | |||||
GetCoinsCacheSizeState(::g_mempool); | |||||
LOCK(cs_LastBlockFile); | LOCK(cs_LastBlockFile); | ||||
if (fPruneMode && (fCheckForPruning || nManualPruneHeight > 0) && | if (fPruneMode && (fCheckForPruning || nManualPruneHeight > 0) && | ||||
!fReindex) { | !fReindex) { | ||||
if (nManualPruneHeight > 0) { | if (nManualPruneHeight > 0) { | ||||
LOG_TIME_MILLIS("find files to prune (manual)", | LOG_TIME_MILLIS("find files to prune (manual)", | ||||
BCLog::BENCH); | BCLog::BENCH); | ||||
FindFilesToPruneManual(g_chainman, setFilesToPrune, | FindFilesToPruneManual(g_chainman, setFilesToPrune, | ||||
nManualPruneHeight); | nManualPruneHeight); | ||||
Show All 14 Lines | try { | ||||
int64_t nNow = GetTimeMicros(); | int64_t nNow = GetTimeMicros(); | ||||
// Avoid writing/flushing immediately after startup. | // Avoid writing/flushing immediately after startup. | ||||
if (nLastWrite == 0) { | if (nLastWrite == 0) { | ||||
nLastWrite = nNow; | nLastWrite = nNow; | ||||
} | } | ||||
if (nLastFlush == 0) { | if (nLastFlush == 0) { | ||||
nLastFlush = nNow; | nLastFlush = nNow; | ||||
} | } | ||||
int64_t nMempoolSizeMax = | |||||
gArgs.GetArg("-maxmempool", DEFAULT_MAX_MEMPOOL_SIZE) * 1000000; | |||||
int64_t cacheSize = CoinsTip().DynamicMemoryUsage(); | |||||
int64_t nTotalSpace = | |||||
nCoinCacheUsage + | |||||
std::max<int64_t>(nMempoolSizeMax - nMempoolUsage, 0); | |||||
// The cache is large and we're within 10% and 10 MiB of the limit, | // The cache is large and we're within 10% and 10 MiB of the limit, | ||||
// but we have time now (not in the middle of a block processing). | // but we have time now (not in the middle of a block processing). | ||||
bool fCacheLarge = | bool fCacheLarge = mode == FlushStateMode::PERIODIC && | ||||
mode == FlushStateMode::PERIODIC && | cache_state >= CoinsCacheSizeState::LARGE; | ||||
cacheSize > std::max((9 * nTotalSpace) / 10, | |||||
nTotalSpace - | |||||
MAX_BLOCK_COINSDB_USAGE * 1024 * 1024); | |||||
// The cache is over the limit, we have to write now. | // The cache is over the limit, we have to write now. | ||||
bool fCacheCritical = | bool fCacheCritical = mode == FlushStateMode::IF_NEEDED && | ||||
mode == FlushStateMode::IF_NEEDED && cacheSize > nTotalSpace; | cache_state >= CoinsCacheSizeState::CRITICAL; | ||||
// It's been a while since we wrote the block index to disk. Do this | // It's been a while since we wrote the block index to disk. Do this | ||||
// frequently, so we don't need to redownload after a crash. | // frequently, so we don't need to redownload after a crash. | ||||
bool fPeriodicWrite = | bool fPeriodicWrite = | ||||
mode == FlushStateMode::PERIODIC && | mode == FlushStateMode::PERIODIC && | ||||
nNow > nLastWrite + (int64_t)DATABASE_WRITE_INTERVAL * 1000000; | nNow > nLastWrite + (int64_t)DATABASE_WRITE_INTERVAL * 1000000; | ||||
// It's been very long since we flushed the cache. Do this | // It's been very long since we flushed the cache. Do this | ||||
// infrequently, to optimize cache usage. | // infrequently, to optimize cache usage. | ||||
bool fPeriodicFlush = | bool fPeriodicFlush = | ||||
▲ Show 20 Lines • Show All 3,889 Lines • Show Last 20 Lines |