txgraph: expose memory usage estimate function (feature)

This commit is contained in:
Pieter Wuille 2025-08-05 11:06:15 -04:00
parent 7680bb8fd4
commit 04c808ac4c
3 changed files with 39 additions and 0 deletions

View File

@ -1012,6 +1012,21 @@ FUZZ_TARGET(txgraph)
}
assert(!top_sim.IsOversized());
break;
} else if (command-- == 0) {
// GetMainMemoryUsage().
auto usage = real->GetMainMemoryUsage();
// Test stability.
if (alt) {
auto usage2 = real->GetMainMemoryUsage();
assert(usage == usage2);
}
// Only empty graphs have 0 memory usage.
if (main_sim.GetTransactionCount() == 0) {
assert(usage == 0);
} else {
assert(usage > 0);
}
break;
}
}
}

View File

@ -629,6 +629,8 @@ public:
std::unique_ptr<BlockBuilder> GetBlockBuilder() noexcept final;
std::pair<std::vector<Ref*>, FeePerWeight> GetWorstMainChunk() noexcept final;
size_t GetMainMemoryUsage() noexcept final;
void SanityCheck() const final;
};
@ -2980,6 +2982,21 @@ std::vector<TxGraph::Ref*> TxGraphImpl::Trim() noexcept
return ret;
}
size_t TxGraphImpl::GetMainMemoryUsage() noexcept
{
// Make sure splits/merges are applied, as memory usage may not be representative otherwise.
SplitAll(/*up_to_level=*/0);
ApplyDependencies(/*level=*/0);
// Compute memory usage
size_t usage = /* From clusters */
m_main_clusterset.m_cluster_usage +
/* From Entry objects. */
sizeof(Entry) * m_main_clusterset.m_txcount +
/* From the chunk index. */
memusage::DynamicUsage(m_main_chunkindex);
return usage;
}
} // namespace
TxGraph::Ref::~Ref()

View File

@ -202,6 +202,13 @@ public:
* graph must not be oversized. If the graph is empty, {{}, FeePerWeight{}} is returned. */
virtual std::pair<std::vector<Ref*>, FeePerWeight> GetWorstMainChunk() noexcept = 0;
/** Get the approximate memory usage for this object, just counting the main graph. If a
* staging graph is present, return a number corresponding to memory usage after
* AbortStaging() would be called. BlockBuilders' memory usage, memory usage of internally
* queued operations, and memory due to temporary caches, is not included here. Can always be
* called. */
virtual size_t GetMainMemoryUsage() noexcept = 0;
/** Perform an internal consistency check on this object. */
virtual void SanityCheck() const = 0;