Skip to content
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

Improve memory footprint of peer metric #4620

Merged
merged 1 commit into from
Jul 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ouroboros-network/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
* Limit concurrency used by dns resolution. We only resolve up to 8 dns names
concurrently for public / ledger peers and up to 2 for local root peers.
This will affect how quickly node connects to ledger peers when it starts.
* Improved memory footprint of peer metrics (#4620)

## 0.8.1.1

Expand Down
32 changes: 14 additions & 18 deletions ouroboros-network/src/Ouroboros/Network/PeerSelection/PeerMetric.hs
Original file line number Diff line number Diff line change
Expand Up @@ -85,31 +85,31 @@ data PeerMetricsState p = PeerMetricsState {

-- | Header metrics.
--
headerMetrics :: SlotMetric p,
headerMetrics :: !(SlotMetric p),

-- | Fetch metrics.
--
fetchedMetrics :: SlotMetric (p, SizeInBytes),
fetchedMetrics :: !(SlotMetric (p, SizeInBytes)),

-- | Registry recording when a peer joined the board of 'PeerMetrics'. The
-- values are average header and fetched metrics.
--
peerRegistry :: PeerRegistry p,
peerRegistry :: !(PeerRegistry p),

-- | A registry which indicates when the last time a peer was seen.
--
-- If a peer hasn't been seen since the oldest recorded slot number, it will
-- be removed.
--
lastSeenRegistry :: LastSeenRegistry p,
lastSeenRegistry :: !(LastSeenRegistry p),

-- | Latest slot registered in the leader board.
--
lastSlotNo :: SlotNo,
lastSlotNo :: !SlotNo,

-- | Metrics configuration. Its kept here just for convenience.
--
metricsConfig :: PeerMetricsConfiguration
metricsConfig :: !PeerMetricsConfiguration
}


Expand Down Expand Up @@ -260,27 +260,23 @@ insertPeer :: forall p. Ord p
-> SlotNo -- ^ current slot
-> PeerMetricsState p
-> PeerMetricsState p
insertPeer p slotNo
insertPeer p !slotNo
peerMetricsState@PeerMetricsState { lastSeenRegistry, peerRegistry } =
if p `OrdPSQ.member` lastSeenRegistry
then peerMetricsState
else case OrdPSQ.alter f p peerRegistry of
(False, peerRegistry') -> peerMetricsState { peerRegistry = peerRegistry' }
(False, !peerRegistry') -> peerMetricsState { peerRegistry = peerRegistry' }
(True, _peerRegistry') -> peerMetricsState
where
f :: Maybe (SlotNo, AverageMetrics) -> (Bool, Maybe (SlotNo, AverageMetrics))
f a@Just {} = (True, a)
f Nothing = (False, Just ( slotNo
, AverageMetrics {
averageUpstreamyness = avg upstreamenessResults,
averageFetchynessBytes = avg fetchynessBytesResults,
averageFetchynessBlocks = avg fetchynessBlocksResults
}
))
f Nothing = (False, Just $! (slotNo, metrics))
where
upstreamenessResults = upstreamynessImpl peerMetricsState
fetchynessBytesResults = fetchynessBytesImpl peerMetricsState
fetchynessBlocksResults = fetchynessBlocksImpl peerMetricsState
!metrics = AverageMetrics {
averageUpstreamyness = avg (upstreamynessImpl peerMetricsState),
averageFetchynessBytes = avg (fetchynessBytesImpl peerMetricsState),
averageFetchynessBlocks = avg (fetchynessBlocksImpl peerMetricsState)
}

avg :: Map p Int -> Int
avg m | Map.null m = 0
Expand Down
Loading