2022-12-24 23:49:50 +00:00
|
|
|
// Copyright (c) 2021-2022 The Bitcoin Core developers
|
2021-09-21 11:37:03 -04:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
|
|
|
|
|
#include <node/caches.h>
|
|
|
|
|
|
2023-03-23 12:23:29 +01:00
|
|
|
#include <common/args.h>
|
2022-10-03 15:53:22 +01:00
|
|
|
#include <index/txindex.h>
|
2024-11-22 20:51:34 +01:00
|
|
|
#include <kernel/caches.h>
|
2024-12-17 21:58:47 +01:00
|
|
|
#include <logging.h>
|
|
|
|
|
#include <util/byte_units.h>
|
2021-09-21 11:37:03 -04:00
|
|
|
|
2024-11-22 20:51:34 +01:00
|
|
|
#include <algorithm>
|
|
|
|
|
#include <string>
|
|
|
|
|
|
2024-11-22 22:44:17 +01:00
|
|
|
// 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
|
2024-12-17 21:58:47 +01:00
|
|
|
//! Max memory allocated to tx index DB specific cache in bytes.
|
|
|
|
|
static constexpr size_t MAX_TX_INDEX_CACHE{1024_MiB};
|
|
|
|
|
//! Max memory allocated to all block filter index caches combined in bytes.
|
|
|
|
|
static constexpr size_t MAX_FILTER_INDEX_CACHE{1024_MiB};
|
2024-11-22 22:44:17 +01:00
|
|
|
|
2021-11-12 10:06:00 -05:00
|
|
|
namespace node {
|
2021-09-21 11:37:03 -04:00
|
|
|
CacheSizes CalculateCacheSizes(const ArgsManager& args, size_t n_indexes)
|
|
|
|
|
{
|
2024-12-17 21:58:47 +01:00
|
|
|
// Convert -dbcache from MiB units to bytes. The total cache is floored by MIN_DB_CACHE and capped by max size_t value.
|
|
|
|
|
size_t total_cache{DEFAULT_DB_CACHE};
|
|
|
|
|
if (std::optional<int64_t> db_cache = args.GetIntArg("-dbcache")) {
|
|
|
|
|
if (*db_cache < 0) db_cache = 0;
|
|
|
|
|
uint64_t db_cache_bytes = SaturatingLeftShift<uint64_t>(*db_cache, 20);
|
|
|
|
|
total_cache = std::max<size_t>(MIN_DB_CACHE, std::min<uint64_t>(db_cache_bytes, std::numeric_limits<size_t>::max()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
IndexCacheSizes index_sizes;
|
|
|
|
|
index_sizes.tx_index = std::min(total_cache / 8, args.GetBoolArg("-txindex", DEFAULT_TXINDEX) ? MAX_TX_INDEX_CACHE : 0);
|
|
|
|
|
total_cache -= index_sizes.tx_index;
|
2021-09-21 11:37:03 -04:00
|
|
|
if (n_indexes > 0) {
|
2024-12-17 21:58:47 +01:00
|
|
|
size_t max_cache = std::min(total_cache / 8, MAX_FILTER_INDEX_CACHE);
|
|
|
|
|
index_sizes.filter_index = max_cache / n_indexes;
|
|
|
|
|
total_cache -= index_sizes.filter_index * n_indexes;
|
2021-09-21 11:37:03 -04:00
|
|
|
}
|
2024-12-17 21:58:47 +01:00
|
|
|
return {index_sizes, kernel::CacheSizes{total_cache}};
|
2021-09-21 11:37:03 -04:00
|
|
|
}
|
2021-11-12 10:06:00 -05:00
|
|
|
} // namespace node
|