From 706cb50041ef79d7aee7f8495281c6abaa440c1c Mon Sep 17 00:00:00 2001 From: Csaba Kiraly Date: Tue, 8 Oct 2024 10:25:42 +0200 Subject: [PATCH] add debugPrintLoop to print neighborhood info Signed-off-by: Csaba Kiraly --- codexdht/private/eth/p2p/discoveryv5/protocol.nim | 14 ++++++++++++++ .../private/eth/p2p/discoveryv5/routing_table.nim | 5 +++++ 2 files changed, 19 insertions(+) diff --git a/codexdht/private/eth/p2p/discoveryv5/protocol.nim b/codexdht/private/eth/p2p/discoveryv5/protocol.nim index 1efecb7..5220243 100644 --- a/codexdht/private/eth/p2p/discoveryv5/protocol.nim +++ b/codexdht/private/eth/p2p/discoveryv5/protocol.nim @@ -126,6 +126,7 @@ const RevalidateMax = 10000 ## Revalidation of a peer is done between min and max milliseconds. ## value in milliseconds IpMajorityInterval = 5.minutes ## Interval for checking the latest IP:Port + DebugPrintInterval = 5.minutes ## Interval to print neighborhood with stats ## majority and updating this when SPR auto update is set. InitialLookups = 1 ## Amount of lookups done when populating the routing table ResponseTimeout* = 1.seconds ## timeout for the response of a request-response @@ -167,6 +168,7 @@ type refreshLoop: Future[void] revalidateLoop: Future[void] ipMajorityLoop: Future[void] + debugPrintLoop: Future[void] lastLookup: chronos.Moment bootstrapRecords*: seq[SignedPeerRecord] ipVote: IpVote @@ -1039,6 +1041,17 @@ proc ipMajorityLoop(d: Protocol) {.async.} = trace "ipMajorityLoop canceled" trace "ipMajorityLoop exited!" +proc debugPrintLoop(d: Protocol) {.async.} = + ## Loop which prints the neighborhood with stats + while true: + await sleepAsync(DebugPrintInterval) + for b in d.routingTable.buckets: + debug "bucket", depth = b.getDepth, + len = b.nodes.len, standby = b.replacementLen + for n in b.nodes: + debug "node", n, rttMin = n.stats.rttMin.int, rttAvg = n.stats.rttAvg.int, + bwMaxMbps = (n.stats.bwMax / 1e6).round(3), bwAvgMbps = (n.stats.bwAvg / 1e6).round(3) + func init*( T: type DiscoveryConfig, tableIpLimit: uint, @@ -1176,6 +1189,7 @@ proc start*(d: Protocol) {.async.} = d.refreshLoop = refreshLoop(d) d.revalidateLoop = revalidateLoop(d) d.ipMajorityLoop = ipMajorityLoop(d) + d.debugPrintLoop = debugPrintLoop(d) await d.providers.start() diff --git a/codexdht/private/eth/p2p/discoveryv5/routing_table.nim b/codexdht/private/eth/p2p/discoveryv5/routing_table.nim index a890863..25f6049 100644 --- a/codexdht/private/eth/p2p/discoveryv5/routing_table.nim +++ b/codexdht/private/eth/p2p/discoveryv5/routing_table.nim @@ -180,6 +180,8 @@ proc midpoint(k: KBucket): NodeId = proc len(k: KBucket): int = k.nodes.len +proc replacementLen*(k: KBucket): int = k.replacementCache.len + proc tail(k: KBucket): Node = k.nodes[high(k.nodes)] proc ipLimitInc(r: var RoutingTable, b: KBucket, n: Node): bool = @@ -281,6 +283,9 @@ proc computeSharedPrefixBits(nodes: openArray[NodeId]): int = # Reaching this would mean that all node ids are equal. doAssert(false, "Unable to calculate number of shared prefix bits") +proc getDepth*(b: KBucket) : int = + computeSharedPrefixBits(@[b.istart, b.iend]) + proc init*(T: type RoutingTable, localNode: Node, bitsPerHop = DefaultBitsPerHop, ipLimits = DefaultTableIpLimits, rng: ref HmacDrbgContext, distanceCalculator = XorDistanceCalculator): T =