|
| 1 | +{-# LANGUAGE FlexibleInstances #-} |
| 2 | +{-# LANGUAGE LambdaCase #-} |
| 3 | +{-# LANGUAGE RecordWildCards #-} |
| 4 | + |
| 5 | +module Cardano.Node.Tracing.Tracers.ForgingStats |
| 6 | + ( ForgingStats (..) |
| 7 | + , calcForgeStats |
| 8 | + ) where |
| 9 | + |
| 10 | +import Cardano.Logging |
| 11 | +import Cardano.Node.Tracing.Tracers.StartLeadershipCheck (ForgeTracerType) |
| 12 | +import Cardano.Slotting.Slot (SlotNo (..)) |
| 13 | +import Ouroboros.Consensus.Node.Tracers |
| 14 | +import qualified Ouroboros.Consensus.Node.Tracers as Consensus |
| 15 | +import Ouroboros.Consensus.Shelley.Node () |
| 16 | + |
| 17 | +import Control.Monad.IO.Class (MonadIO (..)) |
| 18 | +import Data.Aeson (Value (..), (.=)) |
| 19 | + |
| 20 | + |
| 21 | +-------------------------------------------------------------------------------- |
| 22 | +-- ForgingStats Tracer |
| 23 | +-------------------------------------------------------------------------------- |
| 24 | + |
| 25 | +-- | This structure stores counters of blockchain-related events, |
| 26 | +-- per individual thread in fsStats. |
| 27 | +data ForgingStats |
| 28 | + = ForgingStats { |
| 29 | + fsNodeCannotForgeNum :: !Int |
| 30 | + , fsNodeIsLeaderNum :: !Int |
| 31 | + , fsBlocksForgedNum :: !Int |
| 32 | + , fsLastSlot :: !Int |
| 33 | + , fsSlotsMissedNum :: !Int |
| 34 | + } |
| 35 | + |
| 36 | +instance LogFormatting ForgingStats where |
| 37 | + forHuman ForgingStats {..} = |
| 38 | + "Node cannot forge " <> showT fsNodeCannotForgeNum |
| 39 | + <> " node is leader " <> showT fsNodeIsLeaderNum |
| 40 | + <> " blocks forged " <> showT fsBlocksForgedNum |
| 41 | + <> " slots missed " <> showT fsSlotsMissedNum |
| 42 | + forMachine _dtal ForgingStats {..} = |
| 43 | + mconcat [ "kind" .= String "ForgingStats" |
| 44 | + , "nodeCannotForge" .= String (showT fsNodeCannotForgeNum) |
| 45 | + , "nodeIsLeader" .= String (showT fsNodeIsLeaderNum) |
| 46 | + , "blocksForged" .= String (showT fsBlocksForgedNum) |
| 47 | + , "slotsMissed" .= String (showT fsSlotsMissedNum) |
| 48 | + ] |
| 49 | + asMetrics ForgingStats {..} = |
| 50 | + [ IntM "nodeCannotForge" (fromIntegral fsNodeCannotForgeNum) |
| 51 | + , IntM "nodeIsLeader" (fromIntegral fsNodeIsLeaderNum) |
| 52 | + , IntM "blocksForged" (fromIntegral fsBlocksForgedNum) |
| 53 | + , IntM "slotsMissed" (fromIntegral fsSlotsMissedNum) |
| 54 | + ] |
| 55 | + |
| 56 | +instance MetaTrace ForgingStats where |
| 57 | + namespaceFor ForgingStats {} = Namespace [] ["ForgingStats"] |
| 58 | + |
| 59 | + severityFor _ _ = Just Info |
| 60 | + |
| 61 | + documentFor _ = Just |
| 62 | + "nodeCannotForgeNum shows how many times this node could not forge.\ |
| 63 | + \\nnodeIsLeaderNum shows how many times this node was leader.\ |
| 64 | + \\nblocksForgedNum shows how many blocks did forge in this node.\ |
| 65 | + \\nslotsMissed shows how many slots were missed in this node." |
| 66 | + |
| 67 | + metricsDocFor _ = |
| 68 | + [("nodeCannotForge", |
| 69 | + "How many times was this node unable to forge [a block]?") |
| 70 | + ,("nodeIsLeader", |
| 71 | + "How many times was this node slot leader?") |
| 72 | + ,("blocksForged", |
| 73 | + "How many blocks did this node forge?") |
| 74 | + ,("slotsMissed", |
| 75 | + "How many slots did this node miss?") |
| 76 | + ,("lastSlot", |
| 77 | + "") |
| 78 | + ] |
| 79 | + |
| 80 | + allNamespaces = [Namespace [] ["ForgingStats"]] |
| 81 | + |
| 82 | + |
| 83 | +emptyForgingStats :: ForgingStats |
| 84 | +emptyForgingStats = ForgingStats 0 0 0 0 0 |
| 85 | + |
| 86 | +calcForgeStats :: Trace IO ForgingStats |
| 87 | + -> IO (Trace IO (ForgeTracerType blk)) |
| 88 | +calcForgeStats tr = |
| 89 | + let tr' = contramap unfold tr |
| 90 | + in foldCondTraceM calculateForgingStats emptyForgingStats |
| 91 | + (\case |
| 92 | + Left Consensus.TraceStartLeadershipCheck{} -> True |
| 93 | + Left _ -> False |
| 94 | + Right _ -> True |
| 95 | + ) |
| 96 | + tr' |
| 97 | + |
| 98 | +calculateForgingStats :: MonadIO m |
| 99 | + => ForgingStats |
| 100 | + -> LoggingContext |
| 101 | + -> ForgeTracerType blk |
| 102 | + -> m ForgingStats |
| 103 | +calculateForgingStats stats _context |
| 104 | + (Left TraceNodeCannotForge {}) = |
| 105 | + pure $ stats { fsNodeCannotForgeNum = fsNodeCannotForgeNum stats + 1 } |
| 106 | +calculateForgingStats stats _context |
| 107 | + (Left TraceNodeIsLeader {}) = |
| 108 | + pure $ stats { fsNodeIsLeaderNum = fsNodeIsLeaderNum stats + 1 } |
| 109 | +calculateForgingStats stats _context |
| 110 | + (Left TraceForgedBlock {}) = |
| 111 | + pure $ stats { fsBlocksForgedNum = fsBlocksForgedNum stats + 1 } |
| 112 | +calculateForgingStats stats _context |
| 113 | + (Left (TraceNodeNotLeader (SlotNo slot'))) = |
| 114 | + let slot = fromIntegral slot' |
| 115 | + in if fsLastSlot stats == 0 || succ (fsLastSlot stats) == slot |
| 116 | + then pure $ stats { fsLastSlot = slot } |
| 117 | + else |
| 118 | + let missed = (slot - fsLastSlot stats) |
| 119 | + in pure $ stats { fsLastSlot = slot |
| 120 | + , fsSlotsMissedNum = fsSlotsMissedNum stats + missed} |
| 121 | + |
| 122 | +calculateForgingStats stats _context _message = pure stats |
0 commit comments