aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCarl Dong <contact@carldong.me>2022-06-30 23:10:55 -0400
committerCarl Dong <contact@carldong.me>2022-08-03 12:02:31 -0400
commit08dbc6ef72db48168dc03991f5f838dae42c8dfd (patch)
treef78f5551eea5874f068a05f3bbab1a9563f01493
parent0dbce4b1034b53d19b88af332385a006098b6d48 (diff)
downloadbitcoin-08dbc6ef72db48168dc03991f5f838dae42c8dfd.tar.xz
cuckoocache: Return approximate memory size
Returning the approximate total size eliminates the need for InitS*Cache() to do nElems*sizeof(uint256). The cuckoocache has a better idea of this information.
-rw-r--r--src/bitcoin-chainstate.cpp4
-rw-r--r--src/cuckoocache.h14
-rw-r--r--src/init.cpp5
-rw-r--r--src/script/sigcache.cpp12
-rw-r--r--src/script/sigcache.h2
-rw-r--r--src/test/util/setup_common.cpp4
-rw-r--r--src/validation.cpp10
-rw-r--r--src/validation.h2
8 files changed, 33 insertions, 20 deletions
diff --git a/src/bitcoin-chainstate.cpp b/src/bitcoin-chainstate.cpp
index 4656cb23e7..9386936b81 100644
--- a/src/bitcoin-chainstate.cpp
+++ b/src/bitcoin-chainstate.cpp
@@ -62,8 +62,8 @@ int main(int argc, char* argv[])
// Necessary for CheckInputScripts (eventually called by ProcessNewBlock),
// which will try the script cache first and fall back to actually
// performing the check with the signature cache.
- InitSignatureCache();
- InitScriptExecutionCache();
+ Assert(InitSignatureCache());
+ Assert(InitScriptExecutionCache());
// SETUP: Scheduling and Background Signals
diff --git a/src/cuckoocache.h b/src/cuckoocache.h
index d0dc61c7e6..5fc852439f 100644
--- a/src/cuckoocache.h
+++ b/src/cuckoocache.h
@@ -336,8 +336,8 @@ public:
uint32_t setup(uint32_t new_size)
{
// depth_limit must be at least one otherwise errors can occur.
- depth_limit = static_cast<uint8_t>(std::log2(static_cast<float>(std::max((uint32_t)2, new_size))));
size = std::max<uint32_t>(2, new_size);
+ depth_limit = static_cast<uint8_t>(std::log2(static_cast<float>(size)));
table.resize(size);
collection_flags.setup(size);
epoch_flags.resize(size);
@@ -357,12 +357,16 @@ public:
*
* @param bytes the approximate number of bytes to use for this data
* structure
- * @returns the maximum number of elements storable (see setup()
- * documentation for more detail)
+ * @returns A pair of the maximum number of elements storable (see setup()
+ * documentation for more detail) and the approxmiate total size of these
+ * elements in bytes.
*/
- uint32_t setup_bytes(size_t bytes)
+ std::pair<uint32_t, size_t> setup_bytes(size_t bytes)
{
- return setup(bytes/sizeof(Element));
+ auto num_elems = setup(bytes/sizeof(Element));
+
+ size_t approx_size_bytes = num_elems * sizeof(Element);
+ return std::make_pair(num_elems, approx_size_bytes);
}
/** insert loops at most depth_limit times trying to insert a hash
diff --git a/src/init.cpp b/src/init.cpp
index e3d13ad972..53139924b6 100644
--- a/src/init.cpp
+++ b/src/init.cpp
@@ -1115,8 +1115,9 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
args.GetArg("-datadir", ""), fs::PathToString(fs::current_path()));
}
- InitSignatureCache();
- InitScriptExecutionCache();
+ if (!InitSignatureCache() || !InitScriptExecutionCache()) {
+ return InitError(strprintf(_("Unable to allocate memory for -maxsigcachesize: '%s' MiB"), args.GetIntArg("-maxsigcachesize", DEFAULT_MAX_SIG_CACHE_SIZE)));
+ }
int script_threads = args.GetIntArg("-par", DEFAULT_SCRIPTCHECK_THREADS);
if (script_threads <= 0) {
diff --git a/src/script/sigcache.cpp b/src/script/sigcache.cpp
index 4e2acc463a..7cee55a431 100644
--- a/src/script/sigcache.cpp
+++ b/src/script/sigcache.cpp
@@ -75,7 +75,7 @@ public:
std::unique_lock<std::shared_mutex> lock(cs_sigcache);
setValid.insert(entry);
}
- uint32_t setup_bytes(size_t n)
+ std::pair<uint32_t, size_t> setup_bytes(size_t n)
{
return setValid.setup_bytes(n);
}
@@ -92,14 +92,18 @@ static CSignatureCache signatureCache;
// To be called once in AppInitMain/BasicTestingSetup to initialize the
// signatureCache.
-void InitSignatureCache()
+bool InitSignatureCache()
{
// nMaxCacheSize is unsigned. If -maxsigcachesize is set to zero,
// setup_bytes creates the minimum possible cache (2 elements).
size_t nMaxCacheSize = std::min(std::max((int64_t)0, gArgs.GetIntArg("-maxsigcachesize", DEFAULT_MAX_SIG_CACHE_SIZE) / 2), MAX_MAX_SIG_CACHE_SIZE) * ((size_t) 1 << 20);
- size_t nElems = signatureCache.setup_bytes(nMaxCacheSize);
+
+ auto setup_results = signatureCache.setup_bytes(nMaxCacheSize);
+
+ const auto [num_elems, approx_size_bytes] = setup_results;
LogPrintf("Using %zu MiB out of %zu/2 requested for signature cache, able to store %zu elements\n",
- (nElems*sizeof(uint256)) >>20, (nMaxCacheSize*2)>>20, nElems);
+ approx_size_bytes >> 20, (nMaxCacheSize * 2) >> 20, num_elems);
+ return true;
}
bool CachingTransactionSignatureChecker::VerifyECDSASignature(const std::vector<unsigned char>& vchSig, const CPubKey& pubkey, const uint256& sighash) const
diff --git a/src/script/sigcache.h b/src/script/sigcache.h
index 1e21d6f340..edcba2543c 100644
--- a/src/script/sigcache.h
+++ b/src/script/sigcache.h
@@ -33,6 +33,6 @@ public:
bool VerifySchnorrSignature(Span<const unsigned char> sig, const XOnlyPubKey& pubkey, const uint256& sighash) const override;
};
-void InitSignatureCache();
+[[nodiscard]] bool InitSignatureCache();
#endif // BITCOIN_SCRIPT_SIGCACHE_H
diff --git a/src/test/util/setup_common.cpp b/src/test/util/setup_common.cpp
index dc6e000c65..ef2c6af660 100644
--- a/src/test/util/setup_common.cpp
+++ b/src/test/util/setup_common.cpp
@@ -133,8 +133,8 @@ BasicTestingSetup::BasicTestingSetup(const std::string& chainName, const std::ve
m_node.kernel = std::make_unique<kernel::Context>();
SetupEnvironment();
SetupNetworking();
- InitSignatureCache();
- InitScriptExecutionCache();
+ Assert(InitSignatureCache());
+ Assert(InitScriptExecutionCache());
m_node.chain = interfaces::MakeChain(m_node);
fCheckBlockIndex = true;
static bool noui_connected = false;
diff --git a/src/validation.cpp b/src/validation.cpp
index d018d185e9..d0c84ba317 100644
--- a/src/validation.cpp
+++ b/src/validation.cpp
@@ -1656,7 +1656,7 @@ bool CScriptCheck::operator()() {
static CuckooCache::cache<uint256, SignatureCacheHasher> g_scriptExecutionCache;
static CSHA256 g_scriptExecutionCacheHasher;
-void InitScriptExecutionCache() {
+bool InitScriptExecutionCache() {
// Setup the salted hasher
uint256 nonce = GetRandHash();
// We want the nonce to be 64 bytes long to force the hasher to process
@@ -1667,9 +1667,13 @@ void InitScriptExecutionCache() {
// nMaxCacheSize is unsigned. If -maxsigcachesize is set to zero,
// setup_bytes creates the minimum possible cache (2 elements).
size_t nMaxCacheSize = std::min(std::max((int64_t)0, gArgs.GetIntArg("-maxsigcachesize", DEFAULT_MAX_SIG_CACHE_SIZE) / 2), MAX_MAX_SIG_CACHE_SIZE) * ((size_t) 1 << 20);
- size_t nElems = g_scriptExecutionCache.setup_bytes(nMaxCacheSize);
+
+ auto setup_results = g_scriptExecutionCache.setup_bytes(nMaxCacheSize);
+
+ const auto [num_elems, approx_size_bytes] = setup_results;
LogPrintf("Using %zu MiB out of %zu/2 requested for script execution cache, able to store %zu elements\n",
- (nElems*sizeof(uint256)) >>20, (nMaxCacheSize*2)>>20, nElems);
+ approx_size_bytes >> 20, (nMaxCacheSize * 2) >> 20, num_elems);
+ return true;
}
/**
diff --git a/src/validation.h b/src/validation.h
index df9146852f..92ade4cf94 100644
--- a/src/validation.h
+++ b/src/validation.h
@@ -323,7 +323,7 @@ public:
};
/** Initializes the script-execution cache */
-void InitScriptExecutionCache();
+[[nodiscard]] bool InitScriptExecutionCache();
/** Functions for validating blocks and updating the block tree */