-
Notifications
You must be signed in to change notification settings - Fork 38.6k
kernel: Move kernel-related cache constants to kernel cache #31483
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. Code Coverage & BenchmarksFor details see: https://corecheck.dev/bitcoin/bitcoin/pulls/31483. ReviewsSee the guideline for information on the review process.
If your review is incorrectly listed, please react with 👎 to this comment and the bot will ignore it on the next update. ConflictsReviewers, this pull request conflicts with the following ones:
If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first. |
62da6a0 to
173477b
Compare
stickies-v
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Concept ACK. Makes sense to separate this.
173477b to
f454987
Compare
|
Thanks for the review @stickies-v, Updated 173477b -> f454987 (kernel_cache_sizes_0 -> kernel_cache_sizes_1, compare)
|
f454987 to
2c49e0b
Compare
|
Thanks for the comments @stickies-v, Updated f454987 -> 2c49e0b (kernel_cache_sizes_1 -> kernel_cache_sizes_2, compare)
|
stickies-v
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ACK 2c49e0b
The only behaviour change is a very slight difference in cache allocation by moving block_tree_db allocation after index allocation. Because block_tree_db is capped at 2MiB, the maximum impact this change can have is very limited. Even at the most extreme case with -dbcache=4 -txindex -blockfilterindex=1, the results are not meaningfully different:
On master:
2024-12-17T14:25:35Z Cache configuration:
2024-12-17T14:25:35Z * Using 0.5 MiB for block index database
2024-12-17T14:25:35Z * Using 0.4 MiB for transaction index database
2024-12-17T14:25:35Z * Using 0.4 MiB for basic block filter index database
2024-12-17T14:25:35Z * Using 1.3 MiB for chain state database
2024-12-17T14:25:35Z * Using 1.3 MiB for in-memory UTXO set (plus up to 286.1 MiB of unused mempool space)
With this PR:
2024-12-17T14:27:38Z Cache configuration:
2024-12-17T14:27:38Z * Using 0.4 MiB for block index database
2024-12-17T14:27:38Z * Using 0.5 MiB for transaction index database
2024-12-17T14:27:38Z * Using 0.4 MiB for basic block filter index database
2024-12-17T14:27:38Z * Using 1.3 MiB for chain state database
2024-12-17T14:27:38Z * Using 1.3 MiB for in-memory UTXO set (plus up to 286.1 MiB of unused mempool space)
2c49e0b to
8e929e3
Compare
|
Updated 2c49e0b -> 8e929e3 (kernel_cache_sizes_2 -> kernel_cache_sizes_3, compare)
|
stickies-v
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
re-ACK 8e929e3 - just a small doc change.
ryanofsky
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code review ACK 8e929e3. All these changes make sense and definitely make the kernel API nicer.
I do also think it would be an improvement to change all the int64_t fields to size_t since they just get casted to size_t in the end anyway, and because size_t is more self-documenting and makes it clearer the sizes are in bytes.
8e929e3 to
7517126
Compare
|
Thanks for the review @ryanofsky!
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These have nothing to do with the txdb, so move them out and into the node caches.
They are not related to the txdb, so a better place for them is the new kernel and node cache file. Re-use the default amount of kernel cache for the default node cache.
This avoids having to rely on implicit casts when passing them to the various functions allocating the caches. This also ensures that if the requested amount of db_cache does not fit in a size_t, it is clamped to the maximum value of a size_t. Also take this opportunity to make the total amounts of cache in the chainstate manager a size_t too.
430d5fe to
2a92702
Compare
|
Updated 430d5fe -> 2a92702 (kernel_cache_sizes_16 -> kernel_cache_sizes_17, compare)
Yes, that was my motivation for the change back to sizeof(). |
|
re-ACK 2a92702 No changes except for outstanding review comments to simplify |
ryanofsky
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code review ACK 2a92702. Changes since last review are fixing size options to use size_t instead of int64_t again, simplifying CheckedLeftShift more, and making other minor suggested cleanups
hodlinator
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
re-ACK 2a92702
- Excellent with added fuzz test.
- Added support for shifting negative numbers.
- Adopting
size_tearlier for some cases, reducing churn.
Succeeded building on 32-bit (CentOS).
Nit: commit message 65cde36
- kernel: Move default cache constants to caches
+ kernel: Move default cache constants to caches.h| constexpr std::optional<T> CheckedLeftShift(T input, unsigned shift) noexcept | ||
| { | ||
| if (shift == 0 || input == 0) return input; | ||
| // Avoid undefined c++ behaviour if shift is >= number of bits in T. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
mega-nit in case you re-touch:
| // Avoid undefined c++ behaviour if shift is >= number of bits in T. | |
| // Avoid undefined C++ behaviour if shift is >= number of bits in T. |
|
|
||
| auto widen = [](T value) -> W { return value; }; | ||
| auto clamp = [](W value) -> W { return std::clamp<W>(value, min, max); }; | ||
| auto check = [](W value) -> std::optional<W> { if (value >= min && value <= max) return value; else return std::nullopt; }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: Naked else without braces is contrary to developer-notes.md:
bitcoin/doc/developer-notes.md
Lines 76 to 79 in 98939ce
| - If an `if` only has a single-statement `then`-clause, it can appear | |
| on the same line as the `if`, without braces. In every other case, | |
| braces are required, and the `then` and `else` clauses must appear | |
| correctly indented on a new line. |
Consider using ternary operator:
| auto check = [](W value) -> std::optional<W> { if (value >= min && value <= max) return value; else return std::nullopt; }; | |
| auto check = [](W value) -> std::optional<W> { return (value >= min && value <= max) ? std::optional{value} : std::nullopt; }; |
src/node/caches.cpp
Outdated
| if (n_indexes > 0) { | ||
| int64_t max_cache = std::min(nTotalCache / 8, max_filter_index_cache << 20); | ||
| int64_t max_cache = std::min(nTotalCache / 8, MAX_FILTER_INDEX_CACHE << 20); | ||
| sizes.filter_index = max_cache / n_indexes; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit in commit 8826cae:
(We're assigning what seems to be an int64_t to size_t filter_index here. I tried adding curlies on the right side of the assignment and compiling for 32-bit but was unable to trigger narrowing errors. Maybe the compiler is clever enough to see that (MAX_FILTER_INDEX_CACHE << 20) < std::numeric_limits<size_t>::max()).
| #include <stdexcept> | ||
|
|
||
| //! Overflow-safe conversion of MiB to bytes. | ||
| constexpr size_t operator"" _MiB(unsigned long long mebibytes) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
src/util/byte_units.h:13:29: error: identifier '_MiB' preceded by whitespace in a literal operator declaration is deprecated [-Werror,-Wdeprecated-literal-operator]
13 | constexpr size_t operator"" _MiB(unsigned long long mebibytes)
| ~~~~~~~~~~~^~~~
| operator""_MiB
1 error generated.
clang 20.0.0
Fixed in #31691
Carrying non-kernel related fields in the cache sizes for the indexes is confusing for kernel library users. The cache sizes are set currently with magic numbers in bitcoin-chainstate. The comments for the cache size calculations are not completely clear. The constants for the cache sizes are also currently in
txdb.h, which is not an ideal place for holding all cache size related constants.Solve these things by moving the kernel-specific cache size fields to their own struct and moving the constants to either the node or the kernel cache sizes.
This slightly changes the way the cache is allocated if (and only if) the txindex and/or blockfilterindex is used. Since they are now given precedence over the block tree db cache, this results in a bit less cache being allocated to the block tree db, coinsdb and coins caches. The effect is negligible though, i.e. cache sizes with default dbcache reported through the logs are:
master:
this PR:
This PR is part of the libbitcoinkernel project.