Skip to content

Commit 2ed74a4

Browse files
committed
Merge #16945: refactor: introduce CChainState::GetCoinsCacheSizeState
02b9511 tests: add tests for GetCoinsCacheSizeState (James O'Beirne) b17e91d refactoring: introduce CChainState::GetCoinsCacheSizeState (James O'Beirne) Pull request description: This is part of the [assumeutxo project](https://github.com/bitcoin/bitcoin/projects/11): Parent PR: #15606 Issue: #15605 Specification: https://github.com/jamesob/assumeutxo-docs/tree/master/proposal --- This pulls out the routine for detection of how full the coins cache is from FlushStateToDisk. We use this logic independently when deciding when to flush the coins cache during UTXO snapshot activation ([see here](bitcoin/bitcoin@231fb5f#diff-24efdb00bfbe56b140fb006b562cc70bR5275)). ACKs for top commit: ariard: Code review ACK 02b9511. ryanofsky: Code review ACK 02b9511. Just rebase, new COIN_SIZE comment, and new test message since last review Tree-SHA512: 8bdd78bf68a4a5d33a776e73fcc2857f050d6d102caa4997ed19ca25468c1358e6e728199d61b423033c02e6bc8f00a1d9da52cf17a2d37d70860fca9237ea7c
2 parents f2f9fdf + 02b9511 commit 2ed74a4

File tree

5 files changed

+230
-8
lines changed

5 files changed

+230
-8
lines changed

src/Makefile.test.include

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ BITCOIN_TESTS =\
185185
test/uint256_tests.cpp \
186186
test/util_tests.cpp \
187187
test/validation_block_tests.cpp \
188+
test/validation_flush_tests.cpp \
188189
test/versionbits_tests.cpp
189190

190191
if ENABLE_PROPERTY_TESTS

src/test/validation_flush_tests.cpp

Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
// Copyright (c) 2019 The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
//
5+
#include <txmempool.h>
6+
#include <validation.h>
7+
#include <sync.h>
8+
#include <test/util/setup_common.h>
9+
10+
#include <boost/test/unit_test.hpp>
11+
12+
BOOST_FIXTURE_TEST_SUITE(validation_flush_tests, BasicTestingSetup)
13+
14+
//! Test utilities for detecting when we need to flush the coins cache based
15+
//! on estimated memory usage.
16+
//!
17+
//! @sa CChainState::GetCoinsCacheSizeState()
18+
//!
19+
BOOST_AUTO_TEST_CASE(getcoinscachesizestate)
20+
{
21+
BlockManager blockman{};
22+
CChainState chainstate{blockman};
23+
chainstate.InitCoinsDB(/*cache_size_bytes*/ 1 << 10, /*in_memory*/ true, /*should_wipe*/ false);
24+
WITH_LOCK(::cs_main, chainstate.InitCoinsCache());
25+
CTxMemPool tx_pool{};
26+
27+
constexpr bool is_64_bit = sizeof(void*) == 8;
28+
29+
LOCK(::cs_main);
30+
auto& view = chainstate.CoinsTip();
31+
32+
//! Create and add a Coin with DynamicMemoryUsage of 80 bytes to the given view.
33+
auto add_coin = [](CCoinsViewCache& coins_view) -> COutPoint {
34+
Coin newcoin;
35+
uint256 txid = InsecureRand256();
36+
COutPoint outp{txid, 0};
37+
newcoin.nHeight = 1;
38+
newcoin.out.nValue = InsecureRand32();
39+
newcoin.out.scriptPubKey.assign((uint32_t)56, 1);
40+
coins_view.AddCoin(outp, std::move(newcoin), false);
41+
42+
return outp;
43+
};
44+
45+
// The number of bytes consumed by coin's heap data, i.e. CScript
46+
// (prevector<28, unsigned char>) when assigned 56 bytes of data per above.
47+
//
48+
// See also: Coin::DynamicMemoryUsage().
49+
constexpr int COIN_SIZE = is_64_bit ? 80 : 64;
50+
51+
auto print_view_mem_usage = [](CCoinsViewCache& view) {
52+
BOOST_TEST_MESSAGE("CCoinsViewCache memory usage: " << view.DynamicMemoryUsage());
53+
};
54+
55+
constexpr size_t MAX_COINS_CACHE_BYTES = 1024;
56+
57+
// Without any coins in the cache, we shouldn't need to flush.
58+
BOOST_CHECK_EQUAL(
59+
chainstate.GetCoinsCacheSizeState(tx_pool, MAX_COINS_CACHE_BYTES, /*max_mempool_size_bytes*/ 0),
60+
CoinsCacheSizeState::OK);
61+
62+
// If the initial memory allocations of cacheCoins don't match these common
63+
// cases, we can't really continue to make assertions about memory usage.
64+
// End the test early.
65+
if (view.DynamicMemoryUsage() != 32 && view.DynamicMemoryUsage() != 16) {
66+
// Add a bunch of coins to see that we at least flip over to CRITICAL.
67+
68+
for (int i{0}; i < 1000; ++i) {
69+
COutPoint res = add_coin(view);
70+
BOOST_CHECK_EQUAL(view.AccessCoin(res).DynamicMemoryUsage(), COIN_SIZE);
71+
}
72+
73+
BOOST_CHECK_EQUAL(
74+
chainstate.GetCoinsCacheSizeState(tx_pool, MAX_COINS_CACHE_BYTES, /*max_mempool_size_bytes*/ 0),
75+
CoinsCacheSizeState::CRITICAL);
76+
77+
BOOST_TEST_MESSAGE("Exiting cache flush tests early due to unsupported arch");
78+
return;
79+
}
80+
81+
print_view_mem_usage(view);
82+
BOOST_CHECK_EQUAL(view.DynamicMemoryUsage(), is_64_bit ? 32 : 16);
83+
84+
// We should be able to add COINS_UNTIL_CRITICAL coins to the cache before going CRITICAL.
85+
// This is contingent not only on the dynamic memory usage of the Coins
86+
// that we're adding (COIN_SIZE bytes per), but also on how much memory the
87+
// cacheCoins (unordered_map) preallocates.
88+
//
89+
// I came up with the count by examining the printed memory usage of the
90+
// CCoinsCacheView, so it's sort of arbitrary - but it shouldn't change
91+
// unless we somehow change the way the cacheCoins map allocates memory.
92+
//
93+
constexpr int COINS_UNTIL_CRITICAL = is_64_bit ? 4 : 5;
94+
95+
for (int i{0}; i < COINS_UNTIL_CRITICAL; ++i) {
96+
COutPoint res = add_coin(view);
97+
print_view_mem_usage(view);
98+
BOOST_CHECK_EQUAL(view.AccessCoin(res).DynamicMemoryUsage(), COIN_SIZE);
99+
BOOST_CHECK_EQUAL(
100+
chainstate.GetCoinsCacheSizeState(tx_pool, MAX_COINS_CACHE_BYTES, /*max_mempool_size_bytes*/ 0),
101+
CoinsCacheSizeState::OK);
102+
}
103+
104+
// Adding an additional coin will push us over the edge to CRITICAL.
105+
add_coin(view);
106+
print_view_mem_usage(view);
107+
108+
auto size_state = chainstate.GetCoinsCacheSizeState(
109+
tx_pool, MAX_COINS_CACHE_BYTES, /*max_mempool_size_bytes*/ 0);
110+
111+
if (!is_64_bit && size_state == CoinsCacheSizeState::LARGE) {
112+
// On 32 bit hosts, we may hit LARGE before CRITICAL.
113+
add_coin(view);
114+
print_view_mem_usage(view);
115+
}
116+
117+
BOOST_CHECK_EQUAL(
118+
chainstate.GetCoinsCacheSizeState(tx_pool, MAX_COINS_CACHE_BYTES, /*max_mempool_size_bytes*/ 0),
119+
CoinsCacheSizeState::CRITICAL);
120+
121+
// Passing non-zero max mempool usage should allow us more headroom.
122+
BOOST_CHECK_EQUAL(
123+
chainstate.GetCoinsCacheSizeState(tx_pool, MAX_COINS_CACHE_BYTES, /*max_mempool_size_bytes*/ 1 << 10),
124+
CoinsCacheSizeState::OK);
125+
126+
for (int i{0}; i < 3; ++i) {
127+
add_coin(view);
128+
print_view_mem_usage(view);
129+
BOOST_CHECK_EQUAL(
130+
chainstate.GetCoinsCacheSizeState(tx_pool, MAX_COINS_CACHE_BYTES, /*max_mempool_size_bytes*/ 1 << 10),
131+
CoinsCacheSizeState::OK);
132+
}
133+
134+
// Adding another coin with the additional mempool room will put us >90%
135+
// but not yet critical.
136+
add_coin(view);
137+
print_view_mem_usage(view);
138+
139+
// Only perform these checks on 64 bit hosts; I haven't done the math for 32.
140+
if (is_64_bit) {
141+
float usage_percentage = (float)view.DynamicMemoryUsage() / (MAX_COINS_CACHE_BYTES + (1 << 10));
142+
BOOST_TEST_MESSAGE("CoinsTip usage percentage: " << usage_percentage);
143+
BOOST_CHECK(usage_percentage >= 0.9);
144+
BOOST_CHECK(usage_percentage < 1);
145+
BOOST_CHECK_EQUAL(
146+
chainstate.GetCoinsCacheSizeState(tx_pool, MAX_COINS_CACHE_BYTES, 1 << 10),
147+
CoinsCacheSizeState::LARGE);
148+
}
149+
150+
// Using the default max_* values permits way more coins to be added.
151+
for (int i{0}; i < 1000; ++i) {
152+
add_coin(view);
153+
BOOST_CHECK_EQUAL(
154+
chainstate.GetCoinsCacheSizeState(tx_pool),
155+
CoinsCacheSizeState::OK);
156+
}
157+
158+
// Flushing the view doesn't take us back to OK because cacheCoins has
159+
// preallocated memory that doesn't get reclaimed even after flush.
160+
161+
BOOST_CHECK_EQUAL(
162+
chainstate.GetCoinsCacheSizeState(tx_pool, MAX_COINS_CACHE_BYTES, 0),
163+
CoinsCacheSizeState::CRITICAL);
164+
165+
view.SetBestBlock(InsecureRand256());
166+
BOOST_CHECK(view.Flush());
167+
print_view_mem_usage(view);
168+
169+
BOOST_CHECK_EQUAL(
170+
chainstate.GetCoinsCacheSizeState(tx_pool, MAX_COINS_CACHE_BYTES, 0),
171+
CoinsCacheSizeState::CRITICAL);
172+
}
173+
174+
BOOST_AUTO_TEST_SUITE_END()

src/txdb.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ class CBlockIndex;
2020
class CCoinsViewDBCursor;
2121
class uint256;
2222

23-
//! No need to periodic flush if at least this much space still available.
24-
static constexpr int MAX_BLOCK_COINSDB_USAGE = 10;
2523
//! -dbcache default (MiB)
2624
static const int64_t nDefaultDbCache = 450;
2725
//! -dbbatchsize default (bytes)

src/validation.cpp

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2187,13 +2187,44 @@ bool CChainState::ConnectBlock(const CBlock& block, BlockValidationState& state,
21872187
return true;
21882188
}
21892189

2190+
CoinsCacheSizeState CChainState::GetCoinsCacheSizeState(const CTxMemPool& tx_pool)
2191+
{
2192+
return this->GetCoinsCacheSizeState(
2193+
tx_pool,
2194+
nCoinCacheUsage,
2195+
gArgs.GetArg("-maxmempool", DEFAULT_MAX_MEMPOOL_SIZE) * 1000000);
2196+
}
2197+
2198+
CoinsCacheSizeState CChainState::GetCoinsCacheSizeState(
2199+
const CTxMemPool& tx_pool,
2200+
size_t max_coins_cache_size_bytes,
2201+
size_t max_mempool_size_bytes)
2202+
{
2203+
int64_t nMempoolUsage = tx_pool.DynamicMemoryUsage();
2204+
int64_t cacheSize = CoinsTip().DynamicMemoryUsage();
2205+
int64_t nTotalSpace =
2206+
max_coins_cache_size_bytes + std::max<int64_t>(max_mempool_size_bytes - nMempoolUsage, 0);
2207+
2208+
//! No need to periodic flush if at least this much space still available.
2209+
static constexpr int64_t MAX_BLOCK_COINSDB_USAGE_BYTES = 10 * 1024 * 1024; // 10MB
2210+
int64_t large_threshold =
2211+
std::max((9 * nTotalSpace) / 10, nTotalSpace - MAX_BLOCK_COINSDB_USAGE_BYTES);
2212+
2213+
if (cacheSize > nTotalSpace) {
2214+
LogPrintf("Cache size (%s) exceeds total space (%s)\n", cacheSize, nTotalSpace);
2215+
return CoinsCacheSizeState::CRITICAL;
2216+
} else if (cacheSize > large_threshold) {
2217+
return CoinsCacheSizeState::LARGE;
2218+
}
2219+
return CoinsCacheSizeState::OK;
2220+
}
2221+
21902222
bool CChainState::FlushStateToDisk(
21912223
const CChainParams& chainparams,
21922224
BlockValidationState &state,
21932225
FlushStateMode mode,
21942226
int nManualPruneHeight)
21952227
{
2196-
int64_t nMempoolUsage = mempool.DynamicMemoryUsage();
21972228
LOCK(cs_main);
21982229
assert(this->CanFlushToDisk());
21992230
static int64_t nLastWrite = 0;
@@ -2208,6 +2239,7 @@ bool CChainState::FlushStateToDisk(
22082239
{
22092240
bool fFlushForPrune = false;
22102241
bool fDoFullFlush = false;
2242+
CoinsCacheSizeState cache_state = GetCoinsCacheSizeState(::mempool);
22112243
LOCK(cs_LastBlockFile);
22122244
if (fPruneMode && (fCheckForPruning || nManualPruneHeight > 0) && !fReindex) {
22132245
if (nManualPruneHeight > 0) {
@@ -2236,13 +2268,10 @@ bool CChainState::FlushStateToDisk(
22362268
if (nLastFlush == 0) {
22372269
nLastFlush = nNow;
22382270
}
2239-
int64_t nMempoolSizeMax = gArgs.GetArg("-maxmempool", DEFAULT_MAX_MEMPOOL_SIZE) * 1000000;
2240-
int64_t cacheSize = CoinsTip().DynamicMemoryUsage();
2241-
int64_t nTotalSpace = nCoinCacheUsage + std::max<int64_t>(nMempoolSizeMax - nMempoolUsage, 0);
22422271
// 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).
2243-
bool fCacheLarge = mode == FlushStateMode::PERIODIC && cacheSize > std::max((9 * nTotalSpace) / 10, nTotalSpace - MAX_BLOCK_COINSDB_USAGE * 1024 * 1024);
2272+
bool fCacheLarge = mode == FlushStateMode::PERIODIC && cache_state >= CoinsCacheSizeState::LARGE;
22442273
// The cache is over the limit, we have to write now.
2245-
bool fCacheCritical = mode == FlushStateMode::IF_NEEDED && cacheSize > nTotalSpace;
2274+
bool fCacheCritical = mode == FlushStateMode::IF_NEEDED && cache_state >= CoinsCacheSizeState::CRITICAL;
22462275
// 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.
22472276
bool fPeriodicWrite = mode == FlushStateMode::PERIODIC && nNow > nLastWrite + (int64_t)DATABASE_WRITE_INTERVAL * 1000000;
22482277
// It's been very long since we flushed the cache. Do this infrequently, to optimize cache usage.

src/validation.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,15 @@ class CoinsViews {
530530
void InitCache() EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
531531
};
532532

533+
enum class CoinsCacheSizeState
534+
{
535+
//! The coins cache is in immediate need of a flush.
536+
CRITICAL = 2,
537+
//! The cache is at >= 90% capacity.
538+
LARGE = 1,
539+
OK = 0
540+
};
541+
533542
/**
534543
* CChainState stores and provides an API to update our local knowledge of the
535544
* current best chain.
@@ -721,6 +730,17 @@ class CChainState {
721730
/** Update the chain tip based on database information, i.e. CoinsTip()'s best block. */
722731
bool LoadChainTip(const CChainParams& chainparams) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
723732

733+
//! Dictates whether we need to flush the cache to disk or not.
734+
//!
735+
//! @return the state of the size of the coins cache.
736+
CoinsCacheSizeState GetCoinsCacheSizeState(const CTxMemPool& tx_pool)
737+
EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
738+
739+
CoinsCacheSizeState GetCoinsCacheSizeState(
740+
const CTxMemPool& tx_pool,
741+
size_t max_coins_cache_size_bytes,
742+
size_t max_mempool_size_bytes) EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
743+
724744
private:
725745
bool ActivateBestChainStep(BlockValidationState& state, const CChainParams& chainparams, CBlockIndex* pindexMostWork, const std::shared_ptr<const CBlock>& pblock, bool& fInvalidFound, ConnectTrace& connectTrace) EXCLUSIVE_LOCKS_REQUIRED(cs_main, ::mempool.cs);
726746
bool ConnectTip(BlockValidationState& state, const CChainParams& chainparams, CBlockIndex* pindexNew, const std::shared_ptr<const CBlock>& pblock, ConnectTrace& connectTrace, DisconnectedBlockTransactions& disconnectpool) EXCLUSIVE_LOCKS_REQUIRED(cs_main, ::mempool.cs);

0 commit comments

Comments
 (0)