|
7 | 7 | #include <common/args.h> |
8 | 8 | #include <index/txindex.h> |
9 | 9 | #include <kernel/caches.h> |
| 10 | +#include <logging.h> |
| 11 | +#include <util/byte_units.h> |
10 | 12 |
|
11 | 13 | #include <algorithm> |
12 | 14 | #include <string> |
13 | 15 |
|
14 | 16 | // Unlike for the UTXO database, for the txindex scenario the leveldb cache make |
15 | 17 | // a meaningful difference: https://github.com/bitcoin/bitcoin/pull/8273#issuecomment-229601991 |
16 | | -//! Max memory allocated to tx index DB specific cache in MiB. |
17 | | -static constexpr int64_t MAX_TX_INDEX_CACHE{1024}; |
18 | | -//! Max memory allocated to all block filter index caches combined in MiB. |
19 | | -static constexpr int64_t MAX_FILTER_INDEX_CACHE{1024}; |
| 18 | +//! Max memory allocated to tx index DB specific cache in bytes. |
| 19 | +static constexpr size_t MAX_TX_INDEX_CACHE{1024_MiB}; |
| 20 | +//! Max memory allocated to all block filter index caches combined in bytes. |
| 21 | +static constexpr size_t MAX_FILTER_INDEX_CACHE{1024_MiB}; |
20 | 22 |
|
21 | 23 | namespace node { |
22 | 24 | CacheSizes CalculateCacheSizes(const ArgsManager& args, size_t n_indexes) |
23 | 25 | { |
24 | | - int64_t nTotalCache = (args.GetIntArg("-dbcache", DEFAULT_DB_CACHE) << 20); |
25 | | - nTotalCache = std::max(nTotalCache, MIN_DB_CACHE << 20); |
26 | | - IndexCacheSizes sizes; |
27 | | - sizes.tx_index = std::min(nTotalCache / 8, args.GetBoolArg("-txindex", DEFAULT_TXINDEX) ? MAX_TX_INDEX_CACHE << 20 : 0); |
28 | | - nTotalCache -= sizes.tx_index; |
| 26 | + // Convert -dbcache from MiB units to bytes. The total cache is floored by MIN_DB_CACHE and capped by max size_t value. |
| 27 | + size_t total_cache{DEFAULT_DB_CACHE}; |
| 28 | + if (std::optional<int64_t> db_cache = args.GetIntArg("-dbcache")) { |
| 29 | + if (*db_cache < 0) db_cache = 0; |
| 30 | + uint64_t db_cache_bytes = SaturatingLeftShift<uint64_t>(*db_cache, 20); |
| 31 | + total_cache = std::max<size_t>(MIN_DB_CACHE, std::min<uint64_t>(db_cache_bytes, std::numeric_limits<size_t>::max())); |
| 32 | + } |
| 33 | + |
| 34 | + IndexCacheSizes index_sizes; |
| 35 | + index_sizes.tx_index = std::min(total_cache / 8, args.GetBoolArg("-txindex", DEFAULT_TXINDEX) ? MAX_TX_INDEX_CACHE : 0); |
| 36 | + total_cache -= index_sizes.tx_index; |
29 | 37 | if (n_indexes > 0) { |
30 | | - int64_t max_cache = std::min(nTotalCache / 8, MAX_FILTER_INDEX_CACHE << 20); |
31 | | - sizes.filter_index = max_cache / n_indexes; |
32 | | - nTotalCache -= sizes.filter_index * n_indexes; |
| 38 | + size_t max_cache = std::min(total_cache / 8, MAX_FILTER_INDEX_CACHE); |
| 39 | + index_sizes.filter_index = max_cache / n_indexes; |
| 40 | + total_cache -= index_sizes.filter_index * n_indexes; |
33 | 41 | } |
34 | | - return {sizes, kernel::CacheSizes{static_cast<size_t>(nTotalCache)}}; |
| 42 | + return {index_sizes, kernel::CacheSizes{total_cache}}; |
35 | 43 | } |
36 | 44 | } // namespace node |
0 commit comments