WIP: improve MallocUsage and test in CI #36549

Draft
sipa wants to merge 4 commits from 202610_mallocusage into master AGit
sipa commented 2026-10-16 04:27:29 +00:00 (Migrated from github.com)

This is a WIP to improve the MallocUsage function, with logic to measure actual usage in the unit test code.

This is a WIP to improve the `MallocUsage` function, with logic to measure actual usage in the unit test code.
DrahtBot commented 2026-10-16 04:27:35 +00:00 (Migrated from github.com)

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

External sites

Reviews

See the guideline and AI policy for information on the review process.
A summary of reviews will appear here.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #36087 (util: Add and use AssertUnreachable by maflcko)
  • #35911 (Warn on and add missing [[noreturn]] by fanquake)

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.

<!--e57a25ab6845829454e8d69fc972939a--> The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. <!--006a51241073e994b41acfe9ec718e94--> ### External sites * Code coverage & benchmarks: https://corecheck.dev/bitcoin/bitcoin/pulls/36549 * Ralph review <img src=https://review.fish.foo/assets/bitcoin/bitcoin/36549.png alt="Ralph review status"> https://git.fish.foo/bitcoin/bitcoin/pulls/36549#author-ralph <!--021abf342d371248e50ceaed478a90ca--> ### Reviews See [the guideline](https://github.com/bitcoin/bitcoin/blob/master/CONTRIBUTING.md#code-review) and [AI policy](https://github.com/bitcoin/bitcoin/blob/master/doc/AI_POLICY.md) for information on the review process. A summary of reviews will appear here. <!--174a7506f384e20aa4161008e828411d--> ### Conflicts Reviewers, this pull request conflicts with the following ones: * [#36087](https://github.com/bitcoin/bitcoin/pull/36087) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36087.svg"></sub> (util: Add and use AssertUnreachable by maflcko) * [#35911](https://github.com/bitcoin/bitcoin/pull/35911) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/35911.svg"></sub> (Warn on and add missing `[[noreturn]]` by fanquake) 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. <!--5faf32d7da4f0f540f40219e4f7537a3-->
DrahtBot commented 2026-10-16 04:39:27 +00:00 (Migrated from github.com)

🚧 At least one of the CI tasks failed.
Task macOS-cross to arm64: https://github.com/bitcoin/bitcoin/actions/runs/38850721587/job/116811104368
LLM reason (✨ experimental): CI failed during compilation because RandomSizes in memusage_tests.cpp is unused, and Clang treats that warning as an error.

Hints

Try to run the tests locally, according to the documentation. However, a CI failure may still
happen due to a number of reasons, for example:

  • Possibly due to a silent merge conflict (the changes in this pull request being
    incompatible with the current code in the target branch). If so, make sure to rebase on the latest
    commit of the target branch.

  • A sanitizer issue, which can only be found by compiling with the sanitizer and running the
    affected test.

  • An intermittent issue.

Leave a comment here, if you need help tracking down a confusing failure.

<!--85328a0da195eb286784d51f73fa0af9--> 🚧 At least one of the CI tasks failed. <sub>Task `macOS-cross to arm64`: https://github.com/bitcoin/bitcoin/actions/runs/38850721587/job/116811104368</sub> <sub>LLM reason (✨ experimental): CI failed during compilation because `RandomSizes` in `memusage_tests.cpp` is unused, and Clang treats that warning as an error.</sub> <details><summary>Hints</summary> Try to run the tests locally, according to the documentation. However, a CI failure may still happen due to a number of reasons, for example: * Possibly due to a silent merge conflict (the changes in this pull request being incompatible with the current code in the target branch). If so, make sure to rebase on the latest commit of the target branch. * A sanitizer issue, which can only be found by compiling with the sanitizer and running the affected test. * An intermittent issue. Leave a comment here, if you need help tracking down a confusing failure. </details>
First-time contributor

Base: 924f4c4ac78b0cd3b917c694f42c0651a6f3003a
Head: 6140d4c45155987c461b928ad9e94abf714208d6

Findings

Major

Skip allocator-layout comparisons under Valgrind (src/test/memusage_tests.cpp:237, head)

The native Valgrind CI job runs this test under default Memcheck without enabling a compiler sanitizer, so SANITIZER_ALLOCATOR is false. Memcheck replaces the allocator, and its redzones change the small-block address gaps that this test compares against the glibc model. The mmap-threshold setting does not bypass that replacement. Skip this comparison under Valgrind or exclude the allocator-layout suite from that job while retaining native coverage.

Minor

Include string terminators in allocation estimates (src/memusage.h:80, head)

The tighter rounding exposes a missing byte in DynamicUsage(const std::string&), which still passes capacity() rather than capacity() + 1 for heap-backed strings. On 64-bit glibc, a non-SSO capacity of 24 is charged MallocUsage(24) == 32, while the characters and terminator require at least 25 bytes, charged 48 by this model. The base formula returned 48, so this change introduces an undercount at that boundary. Buffered logging uses this estimate for memory-limit accounting, and symmetric insertion and removal do not correct the underestimated total. Pass capacity() + 1 after the SSO check and add container-level boundary coverage. The behavior change is established, but no specific production message producing this boundary capacity was identified, so production impact is unconfirmed.

Review coverage was incomplete. adversarial: The inspection limit prevented checking TxOrphanage::Announcement::GetMemUsage and its accounting/eviction paths; the effect of changed estimates on orphan-resource limits remains unverified. adversarial_glm: fill_mempool callers other than mempool_limit.py were not individually re-read; the IndexError reachability in finding 1 rests on the helper's documented -maxmempool=5 contract and on the fact that the pre-change code (fixed 72 batches, assert on eviction) already forces every current caller to use a ~5 MB pool, otherwise those tests would fail today. If any caller sets a larger pool, the failure mode would change from a failed assertion to an IndexError. adversarial_glm: The musl class-index formula (memusage.h:109-111) was verified by spot-checking representative slot values at each doubling boundary (including the extremes 161, 65520, 74880, 87360, 104832, 131055) rather than exhaustively; no out-of-bounds index is reachable since i<=44 before adjustment and i<=47 after. adversarial_glm: The COST/SLOT tables, the Windows LFH bucket sizes, and the macOS medium-range 8 MiB boundary are taken from the PR's stated measurements and cannot be confirmed statically. verifier: The supported FreeBSD test path and unconditional accuracy assertions were verified, but the native allocator layout needed to establish the claimed BSD failures was not independently verified. verifier: The musl COST constants, Windows LFH and VirtualAlloc accounting, and macOS 8 MiB medium-allocation boundary remain dependent on the supplied measurements. The musl lookup's index bounds and class-selection arithmetic were checked. verifier: Buffered logging was traced as a consumer of string accounting, but no specific production message was established to produce the capacity-24 trigger. verifier: Unresolved candidate: The checkout establishes the factual mismatch in scope: memusage.h retains an explicitly unmeasured historical estimate for BSDs, the new suite enforces narrow bounds without a platform exclusion, and doc/build-freebsd.md supports FreeBSD and recommends CTest. However, the claimed native address-gap values and resulting failures were not independently verified against allocator sources or measurements. Those are needed to publish the concrete supported-platform failure rather than merely request additional validation of an acknowledged limitation. ALL_SIZES is not itself the root cause, since random mode also enforces the same bounds; the separate runtime-cost objection has no established material regression beyond the supplied timing.

Full review report

<!-- forgejo-review-bot:bitcoin/bitcoin --> Base: `924f4c4ac78b0cd3b917c694f42c0651a6f3003a` Head: `6140d4c45155987c461b928ad9e94abf714208d6` ##### Findings **Major** **Skip allocator-layout comparisons under Valgrind** (src/test/memusage_tests.cpp:237, head) The native Valgrind CI job runs this test under default Memcheck without enabling a compiler sanitizer, so `SANITIZER_ALLOCATOR` is false. Memcheck replaces the allocator, and its redzones change the small-block address gaps that this test compares against the glibc model. The mmap-threshold setting does not bypass that replacement. Skip this comparison under Valgrind or exclude the allocator-layout suite from that job while retaining native coverage. **Minor** **Include string terminators in allocation estimates** (src/memusage.h:80, head) The tighter rounding exposes a missing byte in `DynamicUsage(const std::string&)`, which still passes `capacity()` rather than `capacity() + 1` for heap-backed strings. On 64-bit glibc, a non-SSO capacity of 24 is charged `MallocUsage(24) == 32`, while the characters and terminator require at least 25 bytes, charged 48 by this model. The base formula returned 48, so this change introduces an undercount at that boundary. Buffered logging uses this estimate for memory-limit accounting, and symmetric insertion and removal do not correct the underestimated total. Pass `capacity() + 1` after the SSO check and add container-level boundary coverage. The behavior change is established, but no specific production message producing this boundary capacity was identified, so production impact is unconfirmed. Review coverage was incomplete. adversarial: The inspection limit prevented checking TxOrphanage::Announcement::GetMemUsage and its accounting/eviction paths; the effect of changed estimates on orphan-resource limits remains unverified. adversarial_glm: fill_mempool callers other than mempool_limit.py were not individually re-read; the IndexError reachability in finding 1 rests on the helper's documented -maxmempool=5 contract and on the fact that the pre-change code (fixed 72 batches, assert on eviction) already forces every current caller to use a ~5 MB pool, otherwise those tests would fail today. If any caller sets a larger pool, the failure mode would change from a failed assertion to an IndexError. adversarial_glm: The musl class-index formula (memusage.h:109-111) was verified by spot-checking representative slot values at each doubling boundary (including the extremes 161, 65520, 74880, 87360, 104832, 131055) rather than exhaustively; no out-of-bounds index is reachable since i<=44 before adjustment and i<=47 after. adversarial_glm: The COST/SLOT tables, the Windows LFH bucket sizes, and the macOS medium-range 8 MiB boundary are taken from the PR's stated measurements and cannot be confirmed statically. verifier: The supported FreeBSD test path and unconditional accuracy assertions were verified, but the native allocator layout needed to establish the claimed BSD failures was not independently verified. verifier: The musl COST constants, Windows LFH and VirtualAlloc accounting, and macOS 8 MiB medium-allocation boundary remain dependent on the supplied measurements. The musl lookup's index bounds and class-selection arithmetic were checked. verifier: Buffered logging was traced as a consumer of string accounting, but no specific production message was established to produce the capacity-24 trigger. verifier: Unresolved candidate: The checkout establishes the factual mismatch in scope: memusage.h retains an explicitly unmeasured historical estimate for BSDs, the new suite enforces narrow bounds without a platform exclusion, and doc/build-freebsd.md supports FreeBSD and recommends CTest. However, the claimed native address-gap values and resulting failures were not independently verified against allocator sources or measurements. Those are needed to publish the concrete supported-platform failure rather than merely request additional validation of an acknowledged limitation. ALL_SIZES is not itself the root cause, since random mode also enforces the same bounds; the separate runtime-cost objection has no established material regression beyond the supplied timing. [Full review report](<https://review.fish.foo/traces/36549-6140d4c45155987c461b928ad9e94abf714208d6-866-8.html>)
DrahtBot commented 2026-11-20 22:41:32 +00:00 (Migrated from github.com)

🐙 This pull request conflicts with the target branch and needs rebase.

<!--cf906140f33d8803c4a75a2196329ecb--> 🐙 This pull request conflicts with the target branch and [needs rebase](https://github.com/bitcoin/bitcoin/blob/master/CONTRIBUTING.md#rebasing-changes).
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
bitcoin/bitcoin!36549
No description provided.