-
Notifications
You must be signed in to change notification settings - Fork 38.3k
mining: add getMemoryLoad() and track template non-mempool memory footprint #33922
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
base: master
Are you sure you want to change the base?
Changes from all commits
c548d6f
a5eee29
7c4d03d
42e5f9b
e8f8f7f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -67,6 +67,7 @@ | |
| #include <any> | ||
| #include <memory> | ||
| #include <optional> | ||
| #include <ranges> | ||
| #include <utility> | ||
|
|
||
| #include <boost/signals2/signal.hpp> | ||
|
|
@@ -78,6 +79,7 @@ using interfaces::Chain; | |
| using interfaces::FoundBlock; | ||
| using interfaces::Handler; | ||
| using interfaces::MakeSignalHandler; | ||
| using interfaces::MemoryLoad; | ||
| using interfaces::Mining; | ||
| using interfaces::Node; | ||
| using interfaces::WalletLoader; | ||
|
|
@@ -862,7 +864,22 @@ class BlockTemplateImpl : public BlockTemplate | |
| m_block_template(std::move(block_template)), | ||
| m_node(node) | ||
| { | ||
| assert(m_block_template); | ||
| // Don't track the dummy coinbase, because it can be modified in-place | ||
| // by submitSolution() | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| for (const CTransactionRef& tx : Assert(m_block_template)->block.vtx | std::views::drop(1)) { | ||
| m_node.template_tx_refs[tx]++; | ||
| } | ||
| } | ||
|
|
||
| ~BlockTemplateImpl() | ||
| { | ||
| for (const CTransactionRef& tx : m_block_template->block.vtx | std::views::drop(1)) { | ||
| auto ref_count{m_node.template_tx_refs.find(tx)}; | ||
| if (!Assume(ref_count != m_node.template_tx_refs.end())) break; | ||
| if (--ref_count->second == 0) { | ||
ryanofsky marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| m_node.template_tx_refs.erase(ref_count); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| CBlockHeader getBlockHeader() override | ||
|
|
@@ -977,6 +994,12 @@ class MinerImpl : public Mining | |
| return state.IsValid(); | ||
| } | ||
|
|
||
| MemoryLoad getMemoryLoad() override | ||
| { | ||
| CTxMemPool& mempool{*Assert(m_node.mempool)}; | ||
| return {.usage = GetTemplateMemoryUsage(mempool, m_node.template_tx_refs)}; | ||
| } | ||
|
|
||
| NodeContext* context() override { return &m_node; } | ||
| ChainstateManager& chainman() { return *Assert(m_node.chainman); } | ||
| KernelNotifications& notifications() { return *Assert(m_node.notifications); } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -858,7 +858,7 @@ static RPCHelpMan getblocktemplate() | |
| // Update block | ||
| static CBlockIndex* pindexPrev; | ||
| static int64_t time_start; | ||
| static std::unique_ptr<BlockTemplate> block_template; | ||
| std::unique_ptr<BlockTemplate>& block_template{node.gbt_template}; | ||
|
Comment on lines
859
to
+861
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In commit "rpc: move static block_template to node context" (a5eee29) I think it would actually be nice to move all these static variables to a struct or class like @ismaelsadeeq's BlockTemplateCache from #33421. But this could be a followup, and doesn't need to complicate this PR. |
||
| if (!pindexPrev || pindexPrev->GetBlockHash() != tip || | ||
| (mempool.GetTransactionsUpdated() != nTransactionsUpdatedLast && GetTime() - time_start > 5)) | ||
| { | ||
|
|
||
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.
In commit "mining: track non-mempool memory usage" (7c4d03d)
This map can updated from multiple threads, so it needs a mutex to be used safely. I think I'd suggest combining
template_tx_refsandgbt_templatevariables and a mutex into single struct called something likeBlockTemplateStateand adding aunique_ptrto that struct as a member here. The struct could be replaced with a cache class in #33421.