Skip to content

Commit 04c808a

Browse files
committed
txgraph: expose memory usage estimate function (feature)
1 parent 7680bb8 commit 04c808a

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

src/test/fuzz/txgraph.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1012,6 +1012,21 @@ FUZZ_TARGET(txgraph)
10121012
}
10131013
assert(!top_sim.IsOversized());
10141014
break;
1015+
} else if (command-- == 0) {
1016+
// GetMainMemoryUsage().
1017+
auto usage = real->GetMainMemoryUsage();
1018+
// Test stability.
1019+
if (alt) {
1020+
auto usage2 = real->GetMainMemoryUsage();
1021+
assert(usage == usage2);
1022+
}
1023+
// Only empty graphs have 0 memory usage.
1024+
if (main_sim.GetTransactionCount() == 0) {
1025+
assert(usage == 0);
1026+
} else {
1027+
assert(usage > 0);
1028+
}
1029+
break;
10151030
}
10161031
}
10171032
}

src/txgraph.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -629,6 +629,8 @@ class TxGraphImpl final : public TxGraph
629629
std::unique_ptr<BlockBuilder> GetBlockBuilder() noexcept final;
630630
std::pair<std::vector<Ref*>, FeePerWeight> GetWorstMainChunk() noexcept final;
631631

632+
size_t GetMainMemoryUsage() noexcept final;
633+
632634
void SanityCheck() const final;
633635
};
634636

@@ -2980,6 +2982,21 @@ std::vector<TxGraph::Ref*> TxGraphImpl::Trim() noexcept
29802982
return ret;
29812983
}
29822984

2985+
size_t TxGraphImpl::GetMainMemoryUsage() noexcept
2986+
{
2987+
// Make sure splits/merges are applied, as memory usage may not be representative otherwise.
2988+
SplitAll(/*up_to_level=*/0);
2989+
ApplyDependencies(/*level=*/0);
2990+
// Compute memory usage
2991+
size_t usage = /* From clusters */
2992+
m_main_clusterset.m_cluster_usage +
2993+
/* From Entry objects. */
2994+
sizeof(Entry) * m_main_clusterset.m_txcount +
2995+
/* From the chunk index. */
2996+
memusage::DynamicUsage(m_main_chunkindex);
2997+
return usage;
2998+
}
2999+
29833000
} // namespace
29843001

29853002
TxGraph::Ref::~Ref()

src/txgraph.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,13 @@ class TxGraph
202202
* graph must not be oversized. If the graph is empty, {{}, FeePerWeight{}} is returned. */
203203
virtual std::pair<std::vector<Ref*>, FeePerWeight> GetWorstMainChunk() noexcept = 0;
204204

205+
/** Get the approximate memory usage for this object, just counting the main graph. If a
206+
* staging graph is present, return a number corresponding to memory usage after
207+
* AbortStaging() would be called. BlockBuilders' memory usage, memory usage of internally
208+
* queued operations, and memory due to temporary caches, is not included here. Can always be
209+
* called. */
210+
virtual size_t GetMainMemoryUsage() noexcept = 0;
211+
205212
/** Perform an internal consistency check on this object. */
206213
virtual void SanityCheck() const = 0;
207214

0 commit comments

Comments
 (0)