mirror of
https://github.com/bitcoin/bitcoin.git
synced 2026-09-13 06:04:42 +02:00
The alias is misleading because automatic selection can also return `HIGH_DEFAULT_DBCACHE`.
96 lines
4.0 KiB
C++
96 lines
4.0 KiB
C++
// Copyright (c) 2021-present The Bitcoin Core developers
|
|
// Distributed under the MIT software license, see the accompanying
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
#include <node/caches.h>
|
|
|
|
#include <common/args.h>
|
|
#include <common/system.h>
|
|
#include <index/txindex.h>
|
|
#include <index/txospenderindex.h>
|
|
#include <kernel/caches.h>
|
|
#include <node/interface_ui.h>
|
|
#include <tinyformat.h>
|
|
#include <util/byte_units.h>
|
|
#include <util/log.h>
|
|
#include <util/overflow.h>
|
|
#include <util/translation.h>
|
|
|
|
#include <algorithm>
|
|
#include <cstdint>
|
|
#include <string>
|
|
|
|
// Unlike for the UTXO database, for the txindex scenario the leveldb cache make
|
|
// a meaningful difference: https://github.com/bitcoin/bitcoin/pull/8273#issuecomment-229601991
|
|
//! Max memory allocated to tx index DB specific cache in bytes.
|
|
static constexpr uint64_t MAX_TX_INDEX_CACHE{1_GiB};
|
|
//! Max memory allocated to all block filter index caches combined in bytes.
|
|
static constexpr uint64_t MAX_FILTER_INDEX_CACHE{1_GiB};
|
|
//! Max memory allocated to tx spenderindex DB specific cache in bytes.
|
|
static constexpr uint64_t MAX_TXOSPENDER_INDEX_CACHE{1_GiB};
|
|
//! Larger default dbcache on 64-bit systems with enough RAM.
|
|
static constexpr uint64_t HIGH_DEFAULT_DBCACHE{1_GiB};
|
|
//! Minimum detected RAM required for HIGH_DEFAULT_DBCACHE.
|
|
static constexpr uint64_t HIGH_DEFAULT_DBCACHE_MIN_TOTAL_RAM{4_GiB};
|
|
|
|
namespace node {
|
|
uint64_t GetDefaultDBCache()
|
|
{
|
|
if constexpr (sizeof(void*) >= 8) {
|
|
if (TryGetTotalRam().value_or(0) >= HIGH_DEFAULT_DBCACHE_MIN_TOTAL_RAM) {
|
|
return HIGH_DEFAULT_DBCACHE;
|
|
}
|
|
}
|
|
return DEFAULT_KERNEL_CACHE;
|
|
}
|
|
|
|
uint64_t CalculateDbCacheBytes(const ArgsManager& args)
|
|
{
|
|
if (auto db_cache{args.GetIntArg("-dbcache")}) {
|
|
if (*db_cache < 0) db_cache = 0;
|
|
const uint64_t db_cache_bytes{SaturatingLeftShift<uint64_t>(*db_cache, 20)};
|
|
return std::max(MIN_DBCACHE_BYTES, std::min(db_cache_bytes, MAX_DBCACHE_BYTES));
|
|
}
|
|
return GetDefaultDBCache();
|
|
}
|
|
|
|
CacheSizes CalculateCacheSizes(const ArgsManager& args, size_t n_indexes)
|
|
{
|
|
uint64_t total_cache{CalculateDbCacheBytes(args)};
|
|
|
|
// Allocate proportional to usage pattern benefit:
|
|
// - txindex (10%): serves getrawtransaction RPCs with mostly unique,
|
|
// non-repetitive lookups across the entire blockchain.
|
|
// - blockfilterindex (5%): serves BIP 157 light clients that repeatedly
|
|
// query recent blocks, benefiting from LevelDB cache, but the
|
|
// working set for a typical 2-week offline gap is ~200kiB, well within 5%
|
|
// of the total cache.
|
|
// - txospenderindex (5%): serves gettxspendingprevout RPCs with very
|
|
// specific, rarely repeated outpoint queries.
|
|
// - coinstatsindex: intentionally not included here, since usage pattern
|
|
// does not seem to suggest it would be necessary to cache.
|
|
IndexCacheSizes index_sizes;
|
|
index_sizes.tx_index = std::min(total_cache * 10 / 100, args.GetBoolArg("-txindex", DEFAULT_TXINDEX) ? MAX_TX_INDEX_CACHE : 0);
|
|
index_sizes.txospender_index = std::min(total_cache * 5 / 100, args.GetBoolArg("-txospenderindex", DEFAULT_TXOSPENDERINDEX) ? MAX_TXOSPENDER_INDEX_CACHE : 0);
|
|
if (n_indexes > 0) {
|
|
uint64_t max_cache = std::min(total_cache * 5 / 100, MAX_FILTER_INDEX_CACHE);
|
|
index_sizes.filter_index = max_cache / n_indexes;
|
|
total_cache -= index_sizes.filter_index * n_indexes;
|
|
}
|
|
total_cache -= index_sizes.tx_index;
|
|
total_cache -= index_sizes.txospender_index;
|
|
return {index_sizes, kernel::CacheSizes{total_cache}};
|
|
}
|
|
|
|
void LogOversizedDbCache(const ArgsManager& args) noexcept
|
|
{
|
|
if (const auto total_ram{TryGetTotalRam()}) {
|
|
const uint64_t db_cache{CalculateDbCacheBytes(args)};
|
|
if (ShouldWarnOversizedDbCache(db_cache, *total_ram)) {
|
|
InitWarning(bilingual_str{tfm::format(_("A %zu MiB dbcache may be too large for a system memory of only %zu MiB."),
|
|
db_cache / 1_MiB, *total_ram / 1_MiB)});
|
|
}
|
|
}
|
|
}
|
|
} // namespace node
|