fuzz coverage

Coverage Report

Created: 2025-10-29 15:27

/Users/eugenesiegel/btc/bitcoin/src/node/caches.cpp
Line
Count
Source (jump to first uncovered line)
1
// Copyright (c) 2021-2022 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 <kernel/caches.h>
11
#include <logging.h>
12
#include <node/interface_ui.h>
13
#include <tinyformat.h>
14
#include <util/byte_units.h>
15
16
#include <algorithm>
17
#include <string>
18
19
// Unlike for the UTXO database, for the txindex scenario the leveldb cache make
20
// a meaningful difference: https://github.com/bitcoin/bitcoin/pull/8273#issuecomment-229601991
21
//! Max memory allocated to tx index DB specific cache in bytes.
22
static constexpr size_t MAX_TX_INDEX_CACHE{1024_MiB};
23
//! Max memory allocated to all block filter index caches combined in bytes.
24
static constexpr size_t MAX_FILTER_INDEX_CACHE{1024_MiB};
25
//! Maximum dbcache size on 32-bit systems.
26
static constexpr size_t MAX_32BIT_DBCACHE{1024_MiB};
27
28
namespace node {
29
size_t CalculateDbCacheBytes(const ArgsManager& args)
30
51.2k
{
31
51.2k
    if (auto db_cache{args.GetIntArg("-dbcache")}) {
32
0
        if (*db_cache < 0) db_cache = 0;
33
0
        const uint64_t db_cache_bytes{SaturatingLeftShift<uint64_t>(*db_cache, 20)};
34
0
        constexpr auto max_db_cache{sizeof(void*) == 4 ? MAX_32BIT_DBCACHE : std::numeric_limits<size_t>::max()};
35
0
        return std::max<size_t>(MIN_DB_CACHE, std::min<uint64_t>(db_cache_bytes, max_db_cache));
36
0
    }
37
51.2k
    return DEFAULT_DB_CACHE;
38
51.2k
}
39
40
CacheSizes CalculateCacheSizes(const ArgsManager& args, size_t n_indexes)
41
51.2k
{
42
51.2k
    size_t total_cache{CalculateDbCacheBytes(args)};
43
44
51.2k
    IndexCacheSizes index_sizes;
45
51.2k
    index_sizes.tx_index = std::min(total_cache / 8, args.GetBoolArg("-txindex", DEFAULT_TXINDEX) ? 
MAX_TX_INDEX_CACHE0
: 0);
46
51.2k
    total_cache -= index_sizes.tx_index;
47
51.2k
    if (n_indexes > 0) {
48
0
        size_t max_cache = std::min(total_cache / 8, MAX_FILTER_INDEX_CACHE);
49
0
        index_sizes.filter_index = max_cache / n_indexes;
50
0
        total_cache -= index_sizes.filter_index * n_indexes;
51
0
    }
52
51.2k
    return {index_sizes, kernel::CacheSizes{total_cache}};
53
51.2k
}
54
55
void LogOversizedDbCache(const ArgsManager& args) noexcept
56
0
{
57
0
    if (const auto total_ram{GetTotalRAM()}) {
58
0
        const size_t db_cache{CalculateDbCacheBytes(args)};
59
0
        if (ShouldWarnOversizedDbCache(db_cache, *total_ram)) {
60
0
            InitWarning(bilingual_str{tfm::format(_("A %zu MiB dbcache may be too large for a system memory of only %zu MiB."),
61
0
                        db_cache >> 20, *total_ram >> 20)});
62
0
        }
63
0
    }
64
0
}
65
} // namespace node