-
Notifications
You must be signed in to change notification settings - Fork 770
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
node/caches: Extract cache calculation logic
Summary: Partial backport of [[bitcoin/bitcoin#23280 | core#23280]]: bitcoin/bitcoin@ac4bf13 Depends on D12579. Test Plan: ninja all check-all Reviewers: #bitcoin_abc, sdulfari Reviewed By: #bitcoin_abc, sdulfari Differential Revision: https://reviews.bitcoinabc.org/D12580
- Loading branch information
Showing
4 changed files
with
84 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// Copyright (c) 2021 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 <txdb.h> | ||
#include <util/system.h> | ||
#include <validation.h> | ||
|
||
CacheSizes CalculateCacheSizes(const ArgsManager &args, size_t n_indexes) { | ||
int64_t nTotalCache = | ||
(args.GetIntArg("-dbcache", DEFAULT_DB_CACHE_MB) << 20); | ||
// total cache cannot be less than MIN_DB_CACHE_MB | ||
nTotalCache = std::max(nTotalCache, MIN_DB_CACHE_MB << 20); | ||
// total cache cannot be greater than MAX_DB_CACHE_MB | ||
nTotalCache = std::min(nTotalCache, MAX_DB_CACHE_MB << 20); | ||
int64_t nBlockTreeDBCache = | ||
std::min(nTotalCache / 8, MAX_BLOCK_DB_CACHE_MB << 20); | ||
nTotalCache -= nBlockTreeDBCache; | ||
int64_t nTxIndexCache = | ||
std::min(nTotalCache / 8, args.GetBoolArg("-txindex", DEFAULT_TXINDEX) | ||
? MAX_TX_INDEX_CACHE_MB << 20 | ||
: 0); | ||
nTotalCache -= nTxIndexCache; | ||
int64_t filter_index_cache = 0; | ||
if (n_indexes > 0) { | ||
int64_t max_cache = | ||
std::min(nTotalCache / 8, MAX_FILTER_INDEX_CACHE_MB << 20); | ||
filter_index_cache = max_cache / n_indexes; | ||
nTotalCache -= filter_index_cache * n_indexes; | ||
} | ||
// use 25%-50% of the remainder for disk cache | ||
int64_t nCoinDBCache = | ||
std::min(nTotalCache / 2, (nTotalCache / 4) + (1 << 23)); | ||
// cap total coins db cache | ||
nCoinDBCache = std::min(nCoinDBCache, MAX_COINS_DB_CACHE_MB << 20); | ||
nTotalCache -= nCoinDBCache; | ||
// the rest goes to in-memory cache | ||
int64_t nCoinCacheUsage = nTotalCache; | ||
|
||
return { | ||
nBlockTreeDBCache, nCoinDBCache, nCoinCacheUsage, | ||
nTxIndexCache, filter_index_cache, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// Copyright (c) 2021 The Bitcoin Core developers | ||
// Distributed under the MIT software license, see the accompanying | ||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||
|
||
#ifndef BITCOIN_NODE_CACHES_H | ||
#define BITCOIN_NODE_CACHES_H | ||
|
||
#include <cstddef> | ||
#include <cstdint> | ||
|
||
class ArgsManager; | ||
|
||
struct CacheSizes { | ||
int64_t block_tree_db; | ||
int64_t coins_db; | ||
int64_t coins; | ||
int64_t tx_index; | ||
int64_t filter_index; | ||
}; | ||
CacheSizes CalculateCacheSizes(const ArgsManager &args, size_t n_indexes = 0); | ||
|
||
#endif // BITCOIN_NODE_CACHES_H |