Skip to content

Conversation

ismaelsadeeq
Copy link
Member

@ismaelsadeeq ismaelsadeeq commented Sep 25, 2023

This PR is a follow-up to fix review comments and a bugfix from #28385

The PR

  • Updated DisconnectedBlockTransactions's MAX_DISCONNECTED_TX_POOL from kb to bytes.

  • Moved DisconnectedBlockTransactions implementation code to kernel/disconnected_transactions.cpp.

  • AddTransactionsFromBlock now assume duplicate transactions are not passed by asserting after inserting each transaction to iters_by_txid.

  • Included a Bug fix: In the current master we are underestimating the memory usage of DisconnectedBlockTransactions.

    • When adding and subtracting cachedInnerUsage we call RecursiveDynamicUsage with CTransaction which invokes this RecursiveDynamicUsage(const CTransaction& tx) version of RecursiveDynamicUsage, the output of that call only account for the memory usage of the inputs and outputs of the CTransaction, this omits the memory usage of the CTransaction object and the control block.
    • This PR fixes this bug by calling RecursiveDynamicUsage with CTransactionRef when adding and subtracting cachedInnerUsage which invokes RecursiveDynamicUsage(const std::shared_ptr<X>& p) version of RecursiveDynamicUsage the output of the calculation accounts for the CTransaction object, the control blocks, inputs and outputs memory usage.
    • see comment
  • Added test for DisconnectedBlockTransactions memory limit.

@DrahtBot
Copy link
Contributor

DrahtBot commented Sep 25, 2023

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

Code Coverage

For detailed information about the code coverage, see the test coverage report.

Reviews

See the guideline for information on the review process.

Type Reviewers
ACK stickies-v, BrandonOdiwuor, glozow
Concept ACK theuni

If your review is incorrectly listed, please react with 👎 to this comment and the bot will ignore it on the next update.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #28690 (build: Introduce internal kernel library by TheCharlatan)

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.

Copy link
Member

@glozow glozow left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for opening a followup! Did you want to pick up the memusage unit test too? (9dcef47 or a new one)

@ismaelsadeeq ismaelsadeeq force-pushed the 09-2023-follow-up-28385 branch from 8c315c7 to 2517862 Compare September 26, 2023 16:45
Copy link
Contributor

@BrandonOdiwuor BrandonOdiwuor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK 2517862

Great job on moving the implementation code from the header file to disconnected_transactions.cpp. This was a necessary step, especially considering how significantly the code had grown.

@ismaelsadeeq ismaelsadeeq force-pushed the 09-2023-follow-up-28385 branch from 2517862 to 436f53f Compare October 5, 2023 10:56
@ismaelsadeeq
Copy link
Member Author

Added a commit to address review comment #28385 (comment)

And rebased due to #28567 being merged for CI to be green

@ismaelsadeeq ismaelsadeeq force-pushed the 09-2023-follow-up-28385 branch from 8742350 to 225c9c3 Compare October 10, 2023 13:33
@ismaelsadeeq
Copy link
Member Author

Thanks for opening a followup! Did you want to pick up the memusage unit test too? (9dcef47 or a new one)

Added the the test.

@glozow glozow added the Tests label Oct 11, 2023
@glozow glozow changed the title Refactor: move DisconnectedBlockTransactions implementation code to disconnected_transactions.cpp tests, refactor: DisconnectedBlockTransactions rewrite followups Oct 11, 2023
@glozow
Copy link
Member

glozow commented Oct 11, 2023

cc @theuni @stickies-v who made some of the suggestions being addressed and reviewed the test previously

Copy link
Contributor

@stickies-v stickies-v left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approach ACK 225c9c3

Even though they're both correct and an improvement, I'd drop 6f5768d and 89c994a. For both, the fixups are arbitrary and incomplete (e.g. there are plenty of other places where we don't use list initialization in validation.cpp, so why are only these two being fixed? "It's a follow-up" is a bit of a wonky rationale. I'd say let's not do it, or do it consistently and ideally enforce it (which I don't think is suitable for this PR). Likewise for the clang-format fixes. Inconsistent style-only refactors are generally a hard sell.

note: I reviewed 72ebef1 with --color-moved=zebra --color-moved-ws="allow-indentation-change" (always useful to mention those things in PR description to help reviewers be more productive)

Copy link
Contributor

@stickies-v stickies-v Oct 12, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note for other reviewers: these are the memory allocation numbers I'm getting on my machine:

(lldb) p MAP_1
(const size_t) 32
(lldb) p MAP_100
(const size_t) 832
(lldb) p TX_USAGE
(const size_t) 640
(lldb) p ENTRY_USAGE_OVERESTIMATE
(const size_t) 896

@ismaelsadeeq
Copy link
Member Author

Thanks for your review @stickies-v, will drop the commits as you suggested and address comments.

@ismaelsadeeq ismaelsadeeq force-pushed the 09-2023-follow-up-28385 branch 2 times, most recently from 6d922c3 to 8faa980 Compare October 13, 2023 16:15
@ismaelsadeeq
Copy link
Member Author

ismaelsadeeq commented Oct 13, 2023

Force pushed 225c9c3 to 8faa980

@theuni
Copy link
Member

theuni commented Oct 13, 2023

ACK the first 3 commits. I'll pass on re-reviewing the test commit and confusing myself about memory accounting again :)

In `AddTransactionsToBlock` description comment we have the asuumption
that callers will never pass multiple transactions with the same txid
We are asserting to assume that does not happen.
@ismaelsadeeq ismaelsadeeq force-pushed the 09-2023-follow-up-28385 branch from 8faa980 to 3c00943 Compare October 18, 2023 16:16
@ismaelsadeeq ismaelsadeeq force-pushed the 09-2023-follow-up-28385 branch from 3c00943 to e9906c1 Compare October 18, 2023 17:05
Copy link
Contributor

@stickies-v stickies-v left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK e9906c1

Just 2 nits, happy to quickly re-ack since no other acks yet.

edit: PR description needs updating, would change title to tests, bugfix and add the bugfix in the description, as well as the "assume duplicate transactions are not added to iters_by_txid" commit

@DrahtBot DrahtBot requested review from BrandonOdiwuor and theuni and removed request for BrandonOdiwuor October 19, 2023 13:32
@ismaelsadeeq ismaelsadeeq force-pushed the 09-2023-follow-up-28385 branch from e9906c1 to 2c40895 Compare October 19, 2023 14:55
@ismaelsadeeq ismaelsadeeq changed the title tests, refactor: DisconnectedBlockTransactions rewrite followups tests, bug fix: DisconnectedBlockTransactions rewrite followups Oct 19, 2023
stickies-v and others added 2 commits October 19, 2023 16:14
With `queuedTx` owning the `CTransactionRef` shared ptrs, they (and
the managed objects) are entirely allocated on the heap. In
`DisconnectedBlockTransactions::DynamicMemoryUsage`, we account for
the 2 pointers that make up the shared_ptr, but not for the managed
object (CTransaction) or the control block.

Prior to this commit, by calculating the `RecursiveDynamicUsage` on
a `CTransaction` whenever modifying `cachedInnerUsage`, we account
for the dynamic usage of the `CTransaction`, i.e. the `vins` and
`vouts` vectors, but we do not account for the `CTransaction`
object itself, nor for the `CTransactionRef` control block.

This means prior to this commit, `DynamicMemoryUsage` underestimates
dynamic memory usage by not including the `CTransaction` objects and
the shared ptr control blocks.

Fix this by calculating `RecursiveDynamicUsage` on the
`CTransactionRef` instead of the `CTransaction` whenever modifying
`cachedInnerUsage`.
@ismaelsadeeq ismaelsadeeq force-pushed the 09-2023-follow-up-28385 branch from 2c40895 to 9b3da70 Compare October 19, 2023 15:15
@ismaelsadeeq
Copy link
Member Author

edit: PR description needs updating, would change title to tests, bugfix and add the bugfix in the description, as well as the "assume duplicate transactions are not added to iters_by_txid" commit

Thank you @stickies-v , I updated the PR description and address review comments.

Copy link
Contributor

@stickies-v stickies-v left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK 9b3da70 - nice work!

@DrahtBot DrahtBot requested review from BrandonOdiwuor and removed request for BrandonOdiwuor October 19, 2023 19:33
Copy link
Contributor

@BrandonOdiwuor BrandonOdiwuor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

re ACK 9b3da70

Copy link
Member

@glozow glozow left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK 9b3da70

@@ -34,7 +34,7 @@ std::vector<CTransactionRef> DisconnectedBlockTransactions::LimitMemoryUsage()

while (!queuedTx.empty() && DynamicMemoryUsage() > m_max_mem_usage) {
evicted.emplace_back(queuedTx.front());
cachedInnerUsage -= RecursiveDynamicUsage(*queuedTx.front());
cachedInnerUsage -= RecursiveDynamicUsage(queuedTx.front());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For future reference for b2d0447

Imagining DynamicMemoryUsage for a DisconnectedBlockTransactions with 1 transaction (ignoring the iters_by_txid part since that doesn't change),

before

DynamicUsage(queuedTx) + cachedInnerUsage
=MallocUsage(sizeof(list_node<CTransactionRef>) + cachedInnerUsage
=MallocUsage(sizeof(4 pointers) + RecursiveDynamicUsage(CTransaction)
=MallocUsage(sizeof(4 pointers) + DynamicUsage(vin) + DynamicUsage(vout) + sum([RecursiveDynamicUsage(input) for input in vin]) + sum([RecursiveDynamicUsage(output) for output in vout])

after

DynamicUsage(queuedTx) + cachedInnerUsage
=MallocUsage(sizeof(list_node<CTransactionRef>) + cachedInnerUsage
=MallocUsage(sizeof(4 pointers) + RecursiveDynamicUsage(CTransactionRef)
=MallocUsage(sizeof(4 pointers) + DynamicUsage(CTransactionRef) + RecursiveDynamicUsage(CTransaction)
=MallocUsage(sizeof(4 pointers) + MallocUsage(sizeof(CTransaction)) + MallocUsage(sizeof(stl_shared_counter)) + RecursiveDynamicUsage(CTransaction)
=MallocUsage(sizeof(4 pointers) + MallocUsage(sizeof(CTransaction)) + MallocUsage(sizeof(stl_shared_counter)) + DynamicUsage(vin) + DynamicUsage(vout) + sum([RecursiveDynamicUsage(input) for input in vin]) + sum([RecursiveDynamicUsage(output) for output in vout])

So we account for

  • each list node, which is 2 pointers + a shared pointer object (2 pointers)
  • each shared pointer's dynamically allocated memory i.e. the CTransaction object + its always dynamically allocated stuff (the vectors) + the control block

@glozow glozow merged commit 023418a into bitcoin:master Nov 2, 2023
@ismaelsadeeq ismaelsadeeq deleted the 09-2023-follow-up-28385 branch June 27, 2024 11:16
@bitcoin bitcoin locked and limited conversation to collaborators Jun 27, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants