Bitcoin ABC  0.28.12
P2P Digital Currency
caches.cpp
Go to the documentation of this file.
1 // Copyright (c) 2021 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
5 #include <node/caches.h>
6 
7 #include <index/txindex.h>
8 #include <txdb.h>
9 #include <util/system.h>
10 
11 namespace node {
12 CacheSizes CalculateCacheSizes(const ArgsManager &args, size_t n_indexes) {
13  int64_t nTotalCache =
14  (args.GetIntArg("-dbcache", DEFAULT_DB_CACHE_MB) << 20);
15  // total cache cannot be less than MIN_DB_CACHE_MB
16  nTotalCache = std::max(nTotalCache, MIN_DB_CACHE_MB << 20);
17  // total cache cannot be greater than MAX_DB_CACHE_MB
18  nTotalCache = std::min(nTotalCache, MAX_DB_CACHE_MB << 20);
19 
20  CacheSizes sizes;
21  sizes.block_tree_db =
22  std::min(nTotalCache / 8, MAX_BLOCK_DB_CACHE_MB << 20);
23  nTotalCache -= sizes.block_tree_db;
24  sizes.tx_index =
25  std::min(nTotalCache / 8, args.GetBoolArg("-txindex", DEFAULT_TXINDEX)
26  ? MAX_TX_INDEX_CACHE_MB << 20
27  : 0);
28  nTotalCache -= sizes.tx_index;
29  sizes.filter_index = 0;
30 
31  if (n_indexes > 0) {
32  int64_t max_cache =
33  std::min(nTotalCache / 8, MAX_FILTER_INDEX_CACHE_MB << 20);
34  sizes.filter_index = max_cache / n_indexes;
35  nTotalCache -= sizes.filter_index * n_indexes;
36  }
37 
38  // use 25%-50% of the remainder for disk cache
39  sizes.coins_db = std::min(nTotalCache / 2, (nTotalCache / 4) + (1 << 23));
40  // cap total coins db cache
41  sizes.coins_db = std::min(sizes.coins_db, MAX_COINS_DB_CACHE_MB << 20);
42  nTotalCache -= sizes.coins_db;
43  // the rest goes to in-memory cache
44  sizes.coins = nTotalCache;
45 
46  return sizes;
47 }
48 } // namespace node
int64_t GetIntArg(const std::string &strArg, int64_t nDefault) const
Return integer argument or default value.
Definition: system.cpp:635
bool GetBoolArg(const std::string &strArg, bool fDefault) const
Return boolean argument or default value.
Definition: system.cpp:665
Definition: init.h:28
CacheSizes CalculateCacheSizes(const ArgsManager &args, size_t n_indexes)
Definition: caches.cpp:12
int64_t tx_index
Definition: caches.h:18
int64_t coins
Definition: caches.h:17
int64_t block_tree_db
Definition: caches.h:15
int64_t filter_index
Definition: caches.h:19
int64_t coins_db
Definition: caches.h:16
static constexpr int64_t MAX_TX_INDEX_CACHE_MB
Max memory allocated to block tree DB specific cache, if -txindex (MiB)
Definition: txdb.h:49
static constexpr int64_t MAX_DB_CACHE_MB
max. -dbcache (MiB)
Definition: txdb.h:38
static constexpr int64_t MAX_BLOCK_DB_CACHE_MB
Max memory allocated to block tree DB specific cache, if no -txindex (MiB)
Definition: txdb.h:44
static constexpr int64_t MIN_DB_CACHE_MB
min. -dbcache (MiB)
Definition: txdb.h:36
static constexpr int64_t MAX_COINS_DB_CACHE_MB
Max memory allocated to coin DB specific cache (MiB)
Definition: txdb.h:53
static constexpr int64_t DEFAULT_DB_CACHE_MB
-dbcache default (MiB)
Definition: txdb.h:40
static constexpr int64_t MAX_FILTER_INDEX_CACHE_MB
Max memory allocated to all block filter index caches combined in MiB.
Definition: txdb.h:51
static constexpr bool DEFAULT_TXINDEX
Definition: txindex.h:15