0
0
Fork 0
mirror of https://github.com/bitcoin/bitcoin.git synced 2025-03-06 14:19:59 -05:00

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.
This commit is contained in:
Carl Dong 2022-06-30 23:10:55 -04:00
parent 0dbce4b103
commit 08dbc6ef72
8 changed files with 33 additions and 20 deletions

View file

@ -62,8 +62,8 @@ int main(int argc, char* argv[])
// Necessary for CheckInputScripts (eventually called by ProcessNewBlock), // Necessary for CheckInputScripts (eventually called by ProcessNewBlock),
// which will try the script cache first and fall back to actually // which will try the script cache first and fall back to actually
// performing the check with the signature cache. // performing the check with the signature cache.
InitSignatureCache(); Assert(InitSignatureCache());
InitScriptExecutionCache(); Assert(InitScriptExecutionCache());
// SETUP: Scheduling and Background Signals // SETUP: Scheduling and Background Signals

View file

@ -336,8 +336,8 @@ public:
uint32_t setup(uint32_t new_size) uint32_t setup(uint32_t new_size)
{ {
// depth_limit must be at least one otherwise errors can occur. // 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); size = std::max<uint32_t>(2, new_size);
depth_limit = static_cast<uint8_t>(std::log2(static_cast<float>(size)));
table.resize(size); table.resize(size);
collection_flags.setup(size); collection_flags.setup(size);
epoch_flags.resize(size); epoch_flags.resize(size);
@ -357,12 +357,16 @@ public:
* *
* @param bytes the approximate number of bytes to use for this data * @param bytes the approximate number of bytes to use for this data
* structure * structure
* @returns the maximum number of elements storable (see setup() * @returns A pair of the maximum number of elements storable (see setup()
* documentation for more detail) * 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 /** insert loops at most depth_limit times trying to insert a hash

View file

@ -1115,8 +1115,9 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
args.GetArg("-datadir", ""), fs::PathToString(fs::current_path())); args.GetArg("-datadir", ""), fs::PathToString(fs::current_path()));
} }
InitSignatureCache(); if (!InitSignatureCache() || !InitScriptExecutionCache()) {
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); int script_threads = args.GetIntArg("-par", DEFAULT_SCRIPTCHECK_THREADS);
if (script_threads <= 0) { if (script_threads <= 0) {

View file

@ -75,7 +75,7 @@ public:
std::unique_lock<std::shared_mutex> lock(cs_sigcache); std::unique_lock<std::shared_mutex> lock(cs_sigcache);
setValid.insert(entry); 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); return setValid.setup_bytes(n);
} }
@ -92,14 +92,18 @@ static CSignatureCache signatureCache;
// To be called once in AppInitMain/BasicTestingSetup to initialize the // To be called once in AppInitMain/BasicTestingSetup to initialize the
// signatureCache. // signatureCache.
void InitSignatureCache() bool InitSignatureCache()
{ {
// nMaxCacheSize is unsigned. If -maxsigcachesize is set to zero, // nMaxCacheSize is unsigned. If -maxsigcachesize is set to zero,
// setup_bytes creates the minimum possible cache (2 elements). // 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 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", 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 bool CachingTransactionSignatureChecker::VerifyECDSASignature(const std::vector<unsigned char>& vchSig, const CPubKey& pubkey, const uint256& sighash) const

View file

@ -33,6 +33,6 @@ public:
bool VerifySchnorrSignature(Span<const unsigned char> sig, const XOnlyPubKey& pubkey, const uint256& sighash) const override; 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 #endif // BITCOIN_SCRIPT_SIGCACHE_H

View file

@ -133,8 +133,8 @@ BasicTestingSetup::BasicTestingSetup(const std::string& chainName, const std::ve
m_node.kernel = std::make_unique<kernel::Context>(); m_node.kernel = std::make_unique<kernel::Context>();
SetupEnvironment(); SetupEnvironment();
SetupNetworking(); SetupNetworking();
InitSignatureCache(); Assert(InitSignatureCache());
InitScriptExecutionCache(); Assert(InitScriptExecutionCache());
m_node.chain = interfaces::MakeChain(m_node); m_node.chain = interfaces::MakeChain(m_node);
fCheckBlockIndex = true; fCheckBlockIndex = true;
static bool noui_connected = false; static bool noui_connected = false;

View file

@ -1656,7 +1656,7 @@ bool CScriptCheck::operator()() {
static CuckooCache::cache<uint256, SignatureCacheHasher> g_scriptExecutionCache; static CuckooCache::cache<uint256, SignatureCacheHasher> g_scriptExecutionCache;
static CSHA256 g_scriptExecutionCacheHasher; static CSHA256 g_scriptExecutionCacheHasher;
void InitScriptExecutionCache() { bool InitScriptExecutionCache() {
// Setup the salted hasher // Setup the salted hasher
uint256 nonce = GetRandHash(); uint256 nonce = GetRandHash();
// We want the nonce to be 64 bytes long to force the hasher to process // 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, // nMaxCacheSize is unsigned. If -maxsigcachesize is set to zero,
// setup_bytes creates the minimum possible cache (2 elements). // 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 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", 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;
} }
/** /**

View file

@ -323,7 +323,7 @@ public:
}; };
/** Initializes the script-execution cache */ /** Initializes the script-execution cache */
void InitScriptExecutionCache(); [[nodiscard]] bool InitScriptExecutionCache();
/** Functions for validating blocks and updating the block tree */ /** Functions for validating blocks and updating the block tree */