Skip to content

Commit b119073

Browse files
committed
log tree structure
1 parent 030e649 commit b119073

File tree

3 files changed

+82
-0
lines changed

3 files changed

+82
-0
lines changed

include/xrpl/shamap/SHAMap.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,13 @@ class SHAMap
347347
void
348348
invariants() const;
349349

350+
/** Log tree structure statistics for debugging/monitoring
351+
@param j Journal to log to
352+
@param mapName Name to identify this map in logs
353+
*/
354+
void
355+
logTreeStats(beast::Journal j, std::string const& mapName) const;
356+
350357
private:
351358
using SharedPtrNodeStack = std::stack<
352359
std::pair<intr_ptr::SharedPtr<SHAMapTreeNode>, SHAMapNodeID>>;

src/libxrpl/shamap/SHAMap.cpp

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1240,4 +1240,72 @@ SHAMap::invariants() const
12401240
node->invariants(true);
12411241
}
12421242

1243+
void
1244+
SHAMap::logTreeStats(beast::Journal j, std::string const& mapName) const
1245+
{
1246+
struct Stats
1247+
{
1248+
std::uint32_t totalNodes = 0;
1249+
std::uint32_t innerNodes = 0;
1250+
std::uint32_t leafNodes = 0;
1251+
std::uint32_t maxDepth = 0;
1252+
std::array<std::uint32_t, 65> depthCount;
1253+
Stats()
1254+
{
1255+
depthCount.fill(0);
1256+
}
1257+
} stats;
1258+
1259+
std::function<void(
1260+
intr_ptr::SharedPtr<SHAMapTreeNode> const&, std::uint32_t)>
1261+
traverse;
1262+
traverse = [&](intr_ptr::SharedPtr<SHAMapTreeNode> const& node,
1263+
std::uint32_t depth) {
1264+
if (!node)
1265+
return;
1266+
1267+
stats.totalNodes++;
1268+
stats.depthCount[depth]++;
1269+
stats.maxDepth = std::max(stats.maxDepth, depth);
1270+
1271+
if (node->isInner())
1272+
{
1273+
stats.innerNodes++;
1274+
auto inner = dynamic_cast<SHAMapInnerNode*>(node.get());
1275+
if (inner)
1276+
{
1277+
for (int i = 0; i < branchFactor; ++i)
1278+
{
1279+
if (auto child = inner->getChild(i))
1280+
{
1281+
traverse(child, depth + 1);
1282+
}
1283+
}
1284+
}
1285+
}
1286+
else
1287+
{
1288+
stats.leafNodes++;
1289+
}
1290+
};
1291+
1292+
traverse(root_, 0);
1293+
1294+
JLOG(j.info()) << "SHAMap (" << mapName
1295+
<< ") stats: total_nodes=" << stats.totalNodes
1296+
<< ", inner_nodes=" << stats.innerNodes
1297+
<< ", leaf_nodes=" << stats.leafNodes
1298+
<< ", max_depth=" << stats.maxDepth;
1299+
1300+
std::ostringstream hist;
1301+
hist << "Depth histogram: { ";
1302+
for (std::uint32_t d = 0; d <= stats.maxDepth; ++d)
1303+
{
1304+
if (stats.depthCount[d] > 0)
1305+
hist << d << ": " << stats.depthCount[d] << ", ";
1306+
}
1307+
hist << "}";
1308+
JLOG(j.debug()) << "SHAMap (" << mapName << ") " << hist.str();
1309+
}
1310+
12431311
} // namespace ripple

src/xrpld/app/ledger/detail/LedgerMaster.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,13 @@ LedgerMaster::setValidLedger(std::shared_ptr<Ledger const> const& l)
261261
(void)max_ledger_difference_;
262262
mValidLedgerSeq = l->info().seq;
263263

264+
if (l->info().seq % 100 == 0)
265+
{
266+
beast::Journal statsJournal = app_.journal("SHAMapStats");
267+
l->stateMap().logTreeStats(statsJournal, "AccountStateMap");
268+
l->txMap().logTreeStats(statsJournal, "TransactionMap");
269+
}
270+
264271
app_.getOPs().updateLocalTx(*l);
265272
app_.getSHAMapStore().onLedgerClosed(getValidatedLedger());
266273
mLedgerHistory.validatedLedger(l, consensusHash);

0 commit comments

Comments
 (0)