Coverage Report

Created: 2026-06-18 19:06

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/root/bitcoin/src/node/caches.cpp
Line
Count
Source
1
// Copyright (c) 2021-present 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 <common/args.h>
8
#include <common/system.h>
9
#include <index/txindex.h>
10
#include <index/txospenderindex.h>
11
#include <kernel/caches.h>
12
#include <node/interface_ui.h>
13
#include <tinyformat.h>
14
#include <util/byte_units.h>
15
#include <util/log.h>
16
17
#include <algorithm>
18
#include <string>
19
20
// Unlike for the UTXO database, for the txindex scenario the leveldb cache make
21
// a meaningful difference: https://github.com/bitcoin/bitcoin/pull/8273#issuecomment-229601991
22
//! Max memory allocated to tx index DB specific cache in bytes.
23
static constexpr size_t MAX_TX_INDEX_CACHE{1_GiB};
24
//! Max memory allocated to all block filter index caches combined in bytes.
25
static constexpr size_t MAX_FILTER_INDEX_CACHE{1_GiB};
26
//! Max memory allocated to tx spenderindex DB specific cache in bytes.
27
static constexpr size_t MAX_TXOSPENDER_INDEX_CACHE{1_GiB};
28
//! Maximum dbcache size on 32-bit systems.
29
static constexpr size_t MAX_32BIT_DBCACHE{1_GiB};
30
//! Larger default dbcache on 64-bit systems with enough RAM.
31
static constexpr size_t HIGH_DEFAULT_DBCACHE{1_GiB};
32
//! Minimum detected RAM required for HIGH_DEFAULT_DBCACHE.
33
static constexpr uint64_t HIGH_DEFAULT_DBCACHE_MIN_TOTAL_RAM{4_GiB};
34
35
namespace node {
36
size_t GetDefaultDBCache()
37
0
{
38
0
    if constexpr (sizeof(void*) >= 8) {
39
0
        if (GetTotalRAM().value_or(0) >= HIGH_DEFAULT_DBCACHE_MIN_TOTAL_RAM) {
  Branch (39:13): [True: 0, False: 0]
40
0
            return HIGH_DEFAULT_DBCACHE;
41
0
        }
42
0
    }
43
0
    return DEFAULT_DB_CACHE;
44
0
}
45
46
size_t CalculateDbCacheBytes(const ArgsManager& args)
47
0
{
48
0
    if (auto db_cache{args.GetIntArg("-dbcache")}) {
  Branch (48:14): [True: 0, False: 0]
49
0
        if (*db_cache < 0) db_cache = 0;
  Branch (49:13): [True: 0, False: 0]
50
0
        const uint64_t db_cache_bytes{SaturatingLeftShift<uint64_t>(*db_cache, 20)};
51
0
        constexpr auto max_db_cache{sizeof(void*) == 4 ? MAX_32BIT_DBCACHE : std::numeric_limits<size_t>::max()};
  Branch (51:37): [Folded - Ignored]
52
0
        return std::max<size_t>(MIN_DB_CACHE, std::min<uint64_t>(db_cache_bytes, max_db_cache));
53
0
    }
54
0
    return GetDefaultDBCache();
55
0
}
56
57
CacheSizes CalculateCacheSizes(const ArgsManager& args, size_t n_indexes)
58
0
{
59
0
    size_t total_cache{CalculateDbCacheBytes(args)};
60
61
    // Allocate proportional to usage pattern benefit:
62
    // - txindex (10%): serves getrawtransaction RPCs with mostly unique,
63
    //   non-repetitive lookups across the entire blockchain.
64
    // - blockfilterindex (5%): serves BIP 157 light clients that repeatedly
65
    //   query recent blocks, benefiting from LevelDB cache, but the
66
    //   working set for a typical 2-week offline gap is ~200kiB, well within 5%
67
    //   of the total cache.
68
    // - txospenderindex (5%): serves gettxspendingprevout RPCs with very
69
    //   specific, rarely repeated outpoint queries.
70
    // - coinstatsindex: intentionally not included here, since usage pattern
71
    //   does not seem to suggest it would be necessary to cache.
72
0
    IndexCacheSizes index_sizes;
73
0
    index_sizes.tx_index = std::min(total_cache * 10 / 100, args.GetBoolArg("-txindex", DEFAULT_TXINDEX) ? MAX_TX_INDEX_CACHE : 0);
  Branch (73:61): [True: 0, False: 0]
74
0
    index_sizes.txospender_index = std::min(total_cache * 5 / 100, args.GetBoolArg("-txospenderindex", DEFAULT_TXOSPENDERINDEX) ? MAX_TXOSPENDER_INDEX_CACHE : 0);
  Branch (74:68): [True: 0, False: 0]
75
0
    if (n_indexes > 0) {
  Branch (75:9): [True: 0, False: 0]
76
0
        size_t max_cache = std::min(total_cache * 5 / 100, MAX_FILTER_INDEX_CACHE);
77
0
        index_sizes.filter_index = max_cache / n_indexes;
78
0
        total_cache -= index_sizes.filter_index * n_indexes;
79
0
    }
80
0
    total_cache -= index_sizes.tx_index;
81
0
    total_cache -= index_sizes.txospender_index;
82
0
    return {index_sizes, kernel::CacheSizes{total_cache}};
83
0
}
84
85
void LogOversizedDbCache(const ArgsManager& args) noexcept
86
0
{
87
0
    if (const auto total_ram{GetTotalRAM()}) {
  Branch (87:20): [True: 0, False: 0]
88
0
        const size_t db_cache{CalculateDbCacheBytes(args)};
89
0
        if (ShouldWarnOversizedDbCache(db_cache, *total_ram)) {
  Branch (89:13): [True: 0, False: 0]
90
0
            InitWarning(bilingual_str{tfm::format(_("A %zu MiB dbcache may be too large for a system memory of only %zu MiB."),
91
0
                        db_cache >> 20, *total_ram >> 20)});
92
0
        }
93
0
    }
94
0
}
95
} // namespace node