aboutsummaryrefslogtreecommitdiff
path: root/src/node
diff options
context:
space:
mode:
authorCarl Dong <contact@carldong.me>2021-09-21 11:37:03 -0400
committerCarl Dong <contact@carldong.me>2021-12-07 14:48:49 -0500
commitac4bf138b849a8544798f3891d6623803040c265 (patch)
tree7ae646405acf38322912da24a0838eaa2212c9dc /src/node
parent15f2e33bb3d1ad3bc997f6a84956337f46495091 (diff)
downloadbitcoin-ac4bf138b849a8544798f3891d6623803040c265.tar.xz
node/caches: Extract cache calculation logic
I strongly recommend reviewing with the following git-diff flags: --color-moved=dimmed_zebra --color-moved-ws=allow-indentation-change [META] In a future commit, this function will be re-used in TestingSetup so that the behaviour matches across test and non-test init codepaths.
Diffstat (limited to 'src/node')
-rw-r--r--src/node/caches.cpp38
-rw-r--r--src/node/caches.h22
2 files changed, 60 insertions, 0 deletions
diff --git a/src/node/caches.cpp b/src/node/caches.cpp
new file mode 100644
index 0000000000..be09c7e055
--- /dev/null
+++ b/src/node/caches.cpp
@@ -0,0 +1,38 @@
+// 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", nDefaultDbCache) << 20);
+ nTotalCache = std::max(nTotalCache, nMinDbCache << 20); // total cache cannot be less than nMinDbCache
+ nTotalCache = std::min(nTotalCache, nMaxDbCache << 20); // total cache cannot be greater than nMaxDbcache
+ int64_t nBlockTreeDBCache = std::min(nTotalCache / 8, nMaxBlockDBCache << 20);
+ nTotalCache -= nBlockTreeDBCache;
+ int64_t nTxIndexCache = std::min(nTotalCache / 8, args.GetBoolArg("-txindex", DEFAULT_TXINDEX) ? nMaxTxIndexCache << 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 << 20);
+ filter_index_cache = max_cache / n_indexes;
+ nTotalCache -= filter_index_cache * n_indexes;
+ }
+ int64_t nCoinDBCache = std::min(nTotalCache / 2, (nTotalCache / 4) + (1 << 23)); // use 25%-50% of the remainder for disk cache
+ nCoinDBCache = std::min(nCoinDBCache, nMaxCoinsDBCache << 20); // cap total coins db cache
+ nTotalCache -= nCoinDBCache;
+ int64_t nCoinCacheUsage = nTotalCache; // the rest goes to in-memory cache
+
+ return {
+ nBlockTreeDBCache,
+ nCoinDBCache,
+ nCoinCacheUsage,
+ nTxIndexCache,
+ filter_index_cache,
+ };
+}
diff --git a/src/node/caches.h b/src/node/caches.h
new file mode 100644
index 0000000000..437e7d10e5
--- /dev/null
+++ b/src/node/caches.h
@@ -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> // for size_t
+#include <cstdint> // for int64_t
+
+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