Line data Source code
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/addressindex.h> 8 : #include <index/spentindex.h> 9 : #include <index/timestampindex.h> 10 : #include <index/txindex.h> 11 : #include <txdb.h> 12 : #include <util/system.h> 13 : 14 : namespace node { 15 3081 : CacheSizes CalculateCacheSizes(const ArgsManager& args, size_t n_indexes) 16 : { 17 3081 : int64_t nTotalCache = (args.GetIntArg("-dbcache", nDefaultDbCache) << 20); 18 3081 : nTotalCache = std::max(nTotalCache, nMinDbCache << 20); // total cache cannot be less than nMinDbCache 19 3081 : nTotalCache = std::min(nTotalCache, nMaxDbCache << 20); // total cache cannot be greater than nMaxDbcache 20 : CacheSizes sizes; 21 3081 : sizes.block_tree_db = std::min(nTotalCache / 8, nMaxBlockDBCache << 20); 22 3081 : nTotalCache -= sizes.block_tree_db; 23 3081 : sizes.tx_index = std::min(nTotalCache / 8, args.GetBoolArg("-txindex", DEFAULT_TXINDEX) ? nMaxTxIndexCache << 20 : 0); 24 3081 : nTotalCache -= sizes.tx_index; 25 3081 : sizes.address_index = std::min(nTotalCache / 8, args.GetBoolArg("-addressindex", DEFAULT_ADDRESSINDEX) ? int64_t(256 << 20) : int64_t(0)); 26 3081 : nTotalCache -= sizes.address_index; 27 3081 : sizes.timestamp_index = std::min(nTotalCache / 64, args.GetBoolArg("-timestampindex", DEFAULT_TIMESTAMPINDEX) ? int64_t(8 << 20) : int64_t(0)); 28 3081 : nTotalCache -= sizes.timestamp_index; 29 3081 : sizes.spent_index = std::min(nTotalCache / 8, args.GetBoolArg("-spentindex", DEFAULT_SPENTINDEX) ? int64_t(128 << 20) : int64_t(0)); 30 3081 : nTotalCache -= sizes.spent_index; 31 3081 : sizes.filter_index = 0; 32 3081 : if (n_indexes > 0) { 33 724 : int64_t max_cache = std::min(nTotalCache / 8, max_filter_index_cache << 20); 34 724 : sizes.filter_index = max_cache / n_indexes; 35 724 : nTotalCache -= sizes.filter_index * n_indexes; 36 724 : } 37 3081 : sizes.coins_db = std::min(nTotalCache / 2, (nTotalCache / 4) + (1 << 23)); // use 25%-50% of the remainder for disk cache 38 3081 : sizes.coins_db = std::min(sizes.coins_db, nMaxCoinsDBCache << 20); // cap total coins db cache 39 3081 : nTotalCache -= sizes.coins_db; 40 3081 : sizes.coins = nTotalCache; // the rest goes to in-memory cache 41 3081 : return sizes; 42 0 : } 43 : } // namespace node