Skip to content

Commit

Permalink
Add tracing events for pipelining
Browse files Browse the repository at this point in the history
  • Loading branch information
amesgen authored and nfrisby committed Apr 19, 2022
1 parent d38e6d6 commit 60f0771
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ module Ouroboros.Consensus.Storage.ChainDB.Impl.ChainSel (
import Control.Exception (assert)
import Control.Monad.Except
import Control.Monad.Trans.State.Strict
import Control.Tracer (Tracer, contramap, traceWith)
import Control.Tracer (Tracer, contramap, nullTracer, traceWith)
import Data.Function (on)
import Data.List (partition, sortBy)
import Data.List.NonEmpty (NonEmpty)
Expand Down Expand Up @@ -206,6 +206,7 @@ initialChainSelection immutableDB volatileDB lgrDB tracer cfg varInvalid
, trace = traceWith
(contramap (InitChainSelValidation) tracer)
-- initial chain selection is not concerned about pipelining
, tracePipelining = traceWith nullTracer
, varTentativeState
, varTentativeHeader
, punish = Nothing
Expand Down Expand Up @@ -538,6 +539,8 @@ chainSelectionForBlock cdb@CDB{..} blockCache hdr punish = do
, curChainAndLedger = curChainAndLedger
, trace =
traceWith (contramap (TraceAddBlockEvent . AddBlockValidation) cdbTracer)
, tracePipelining =
traceWith (contramap (TraceAddBlockEvent . PipeliningEvent) cdbTracer)
, punish = Just (p, punish)
}

Expand Down Expand Up @@ -764,6 +767,7 @@ chainSelectionForBlock cdb@CDB{..} blockCache hdr punish = do
return (curChain, newChain, events)

trace $ mkTraceEvent events (mkNewTipInfo newLedger) curChain newChain
trace $ PipeliningEvent OutdatedTentativeHeader
traceWith cdbTraceLedger newLedger

return $ castPoint $ AF.headPoint newChain
Expand Down Expand Up @@ -810,6 +814,7 @@ getKnownHeaderThroughCache volatileDB hash = gets (Map.lookup hash) >>= \case
data ChainSelEnv m blk = ChainSelEnv
{ lgrDB :: LgrDB m blk
, trace :: TraceValidationEvent blk -> m ()
, tracePipelining :: TracePipeliningEvent blk -> m ()
, bcfg :: BlockConfig blk
, varInvalid :: StrictTVar m (WithFingerprint (InvalidBlocks blk))
, varFutureBlocks :: StrictTVar m (FutureBlocks m blk)
Expand Down Expand Up @@ -935,26 +940,30 @@ chainSelection chainSelEnv chainDiffs =
-- | Set and return the tentative header, if applicable. Also, notify
-- the tentative followers.
setTentativeHeader :: m (Maybe (Header blk))
setTentativeHeader = atomically $ do
setTentativeHeader = do
mTentativeHeader <-
(\ts -> isPipelineable bcfg ts candidate)
<$> readTVar varTentativeState
<$> readTVarIO varTentativeState
whenJust mTentativeHeader $ \tentativeHeader -> do
writeTVar varTentativeHeader $ Just $! tentativeHeader
let tentativeChain = curChain AF.:> tentativeHeader
forTentativeFollowers $ \followerHandle ->
fhSwitchFork followerHandle curTipPoint tentativeChain
atomically $ do
writeTVar varTentativeHeader $ Just $! tentativeHeader
let tentativeChain = curChain AF.:> tentativeHeader
forTentativeFollowers $ \followerHandle ->
fhSwitchFork followerHandle curTipPoint tentativeChain
tracePipelining $ SetTentativeHeader tentativeHeader
pure mTentativeHeader

-- | Clear a tentative header that turned out to be invalid. Also, roll
-- back the tentative followers.
clearTentativeHeader :: Header blk -> m ()
clearTentativeHeader tentativeHeader = atomically $ do
writeTVar varTentativeHeader Nothing
writeTVar varTentativeState $
LastInvalidTentative (selectView bcfg tentativeHeader)
forTentativeFollowers $ \followerHandle ->
fhSwitchFork followerHandle curTipPoint curChain
clearTentativeHeader tentativeHeader = do
atomically $ do
writeTVar varTentativeHeader Nothing
writeTVar varTentativeState $
LastInvalidTentative (selectView bcfg tentativeHeader)
forTentativeFollowers $ \followerHandle ->
fhSwitchFork followerHandle curTipPoint curChain
tracePipelining $ TrapTentativeHeader tentativeHeader

curTipPoint = castPoint $ AF.headPoint curChain
forTentativeFollowers f = getTentativeFollowers >>= mapM_ f
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ module Ouroboros.Consensus.Storage.ChainDB.Impl.Types (
, TraceInitChainSelEvent (..)
, TraceIteratorEvent (..)
, TraceOpenEvent (..)
, TracePipeliningEvent (..)
, TraceValidationEvent (..)
) where

Expand Down Expand Up @@ -644,6 +645,11 @@ data TraceAddBlockEvent blk =
-- This is done for all blocks from the future each time a new block is
-- added.
| ChainSelectionForFutureBlock (RealPoint blk)

-- | The tentative header (in the context of diffusion pipelining) has been
-- updated.
| PipeliningEvent (TracePipeliningEvent blk)

deriving (Generic)

deriving instance
Expand Down Expand Up @@ -696,6 +702,18 @@ deriving instance
, LedgerSupportsProtocol blk
) => Show (TraceValidationEvent blk)

data TracePipeliningEvent blk =
-- | A new tentative header got set.
SetTentativeHeader (Header blk)
-- | The body of tentative block turned out to be invalid.
| TrapTentativeHeader (Header blk)
-- | We selected a new (better) chain, which cleared the previous tentative
-- header (if any).
| OutdatedTentativeHeader

deriving stock instance Eq (Header blk) => Eq (TracePipeliningEvent blk)
deriving stock instance Show (Header blk) => Show (TracePipeliningEvent blk)

data TraceInitChainSelEvent blk =
StartedInitChainSelection
-- ^ An event traced when inital chain selection has started during the
Expand Down

0 comments on commit 60f0771

Please sign in to comment.