Skip to content

Commit

Permalink
workbench: integrate parametrised chain filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
deepfire committed Nov 30, 2021
1 parent 57db158 commit 78348ff
Show file tree
Hide file tree
Showing 10 changed files with 130 additions and 38 deletions.
25 changes: 25 additions & 0 deletions bench/flt-full.json
@@ -0,0 +1,25 @@
[ { "tag": "CSlot"
, "contents":
{ "tag": "EpochGEq"
, "contents": 3
}
}
, { "tag": "CSlot"
, "contents":
{ "tag": "SlotLEq"
, "contents": 37000
}
}
, { "tag": "CBlock"
, "contents":
{ "tag": "BUnitaryChainDelta"
, "contents": true
}
}
, { "tag": "CBlock"
, "contents":
{ "tag": "BFullnessGEq"
, "contents": 0.9
}
}
]
31 changes: 31 additions & 0 deletions bench/flt-midsize.json
@@ -0,0 +1,31 @@
[ { "tag": "CSlot"
, "contents":
{ "tag": "EpochGEq"
, "contents": 3
}
}
, { "tag": "CSlot"
, "contents":
{ "tag": "SlotLEq"
, "contents": 37000
}
}
, { "tag": "CBlock"
, "contents":
{ "tag": "BUnitaryChainDelta"
, "contents": true
}
}
, { "tag": "CBlock"
, "contents":
{ "tag": "BFullnessGEq"
, "contents": 0.1
}
}
, { "tag": "CBlock"
, "contents":
{ "tag": "BFullnessLEq"
, "contents": 0.9
}
}
]
19 changes: 19 additions & 0 deletions bench/flt-range.json
@@ -0,0 +1,19 @@
[ { "tag": "CSlot"
, "contents":
{ "tag": "EpochGEq"
, "contents": 3
}
}
, { "tag": "CSlot"
, "contents":
{ "tag": "SlotLEq"
, "contents": 37000
}
}
, { "tag": "CBlock"
, "contents":
{ "tag": "BUnitaryChainDelta"
, "contents": true
}
}
]
1 change: 1 addition & 0 deletions bench/locli/locli.cabal
Expand Up @@ -45,6 +45,7 @@ library
, bytestring
, cardano-config
, cardano-prelude
, cardano-slotting
, containers
, deepseq
, directory
Expand Down
21 changes: 15 additions & 6 deletions bench/locli/src/Cardano/Analysis/API.hs
Expand Up @@ -16,7 +16,8 @@ import Data.Text.Short (toText)
import Data.Time.Clock (NominalDiffTime)
import Text.Printf (printf)

import Ouroboros.Network.Block (BlockNo(..), SlotNo(..))
import Cardano.Slotting.Slot (EpochNo(..), SlotNo(..))
import Ouroboros.Network.Block (BlockNo(..))

import Cardano.Analysis.ChainFilter
import Cardano.Analysis.Profile
Expand All @@ -33,7 +34,8 @@ import Data.Distribution
-- | Results of block propagation analysis.
data BlockPropagation
= BlockPropagation
{ bpForgerForges :: !(Distribution Float NominalDiffTime)
{ bpSlotRange :: !(SlotNo, SlotNo) -- ^ Analysis range, inclusive.
, bpForgerForges :: !(Distribution Float NominalDiffTime)
, bpForgerAdoptions :: !(Distribution Float NominalDiffTime)
, bpForgerAnnouncements :: !(Distribution Float NominalDiffTime)
, bpForgerSends :: !(Distribution Float NominalDiffTime)
Expand All @@ -56,6 +58,7 @@ data BlockEvents
, beBlockPrev :: !Hash
, beBlockNo :: !BlockNo
, beSlotNo :: !SlotNo
, beEpochNo :: !EpochNo
, beForge :: !BlockForge
, beObservations :: [BlockObservation]
, bePropagation :: !(Distribution Float NominalDiffTime)
Expand Down Expand Up @@ -133,11 +136,17 @@ testBlockEvents Profile{genesis=GenesisProfile{..}}
BlockEvents{beForge=BlockForge{..},..} = \case
CBlock flt -> case flt of
BUnitaryChainDelta -> bfChainDelta == 1
BFullnessAbove f ->
bfBlockSize >= floor ((fromIntegral max_block_size :: Double) * f)
BFullnessGEq f ->
bfBlockSize > floor ((fromIntegral max_block_size :: Double) * f)
BFullnessLEq f ->
bfBlockSize < floor ((fromIntegral max_block_size :: Double) * f)
BSizeGEq x -> bfBlockSize >= fromIntegral x
BSizeLEq x -> bfBlockSize <= fromIntegral x
CSlot flt -> case flt of
SSince s -> beSlotNo >= s
SUntil s -> beSlotNo <= s
SlotGEq s -> beSlotNo >= s
SlotLEq s -> beSlotNo <= s
EpochGEq e -> beEpochNo >= e
EpochLEq e -> beEpochNo <= e
_ -> True

isValidBlockEvent :: Profile -> [ChainFilter] -> BlockEvents -> Bool
Expand Down
16 changes: 12 additions & 4 deletions bench/locli/src/Cardano/Analysis/BlockProp.hs
Expand Up @@ -18,7 +18,7 @@ module Cardano.Analysis.BlockProp
(blockProp)
where

import Prelude (String, (!!), error, head, id, show, tail)
import Prelude (String, (!!), error, head, last, id, show, tail)
import Cardano.Prelude hiding (head, show)

import Control.Arrow ((***), (&&&))
Expand All @@ -43,7 +43,8 @@ import Data.Time.Clock (NominalDiffTime, UTCTime, addUTCTime, diffUTCTi

import Text.Printf (printf)

import Ouroboros.Network.Block (BlockNo(..), SlotNo(..))
import Cardano.Slotting.Slot (EpochNo(..), SlotNo(..))
import Ouroboros.Network.Block (BlockNo(..))

import Data.Accum
import Data.Distribution
Expand All @@ -66,6 +67,7 @@ data ForgerEvents a
, bfeBlockNo :: !BlockNo
, bfeSlotNo :: !SlotNo
, bfeSlotStart :: !SlotStart
, bfeEpochNo :: !EpochNo
, bfeBlockSize :: !(Maybe Int)
, bfeForged :: !(Maybe a)
, bfeAdopted :: !(Maybe a)
Expand Down Expand Up @@ -259,7 +261,10 @@ doBlockProp p cFilters eventMaps = do
putStrLn ("tip block: " <> show tipBlock :: String)
putStrLn ("chain length: " <> show (length chain) :: String)
pure BlockPropagation
{ bpForgerForges = forgerEventsCDF (Just . bfForged . beForge)
{ bpSlotRange = (,)
(head chainV & beSlotNo)
(last chainV & beSlotNo)
, bpForgerForges = forgerEventsCDF (Just . bfForged . beForge)
, bpForgerAdoptions = forgerEventsCDF ((\x ->
if bfChainDelta x == 1
then Just (bfAdopted x)
Expand Down Expand Up @@ -349,6 +354,7 @@ doBlockProp p cFilters eventMaps = do
, beBlockPrev = bfeBlockPrev
, beBlockNo = bfeBlockNo
, beSlotNo = bfeSlotNo
, beEpochNo = bfeEpochNo
, beForge =
BlockForge
{ bfForger = host
Expand Down Expand Up @@ -424,7 +430,7 @@ blockEventMapsFromLogObjects ci (f@(unJsonLogfile -> fp), xs) =
machBlockMap = foldl (blockPropMachEventsStep ci f) mempty xs

blockPropMachEventsStep :: ChainInfo -> JsonLogfile -> MachBlockMap UTCTime -> LogObject -> MachBlockMap UTCTime
blockPropMachEventsStep ci (JsonLogfile fp) bMap lo = case lo of
blockPropMachEventsStep ci@CInfo{..} (JsonLogfile fp) bMap lo = case lo of
-- 0. Notice (observer only)
LogObject{loAt, loHost, loBody=LOChainSyncClientSeenHeader{loBlock,loBlockNo,loSlotNo}} ->
let mbe0 = Map.lookup loBlock bMap
Expand Down Expand Up @@ -471,6 +477,8 @@ blockPropMachEventsStep ci (JsonLogfile fp) bMap lo = case lo of
, bfeBlockNo = loBlockNo
, bfeSlotNo = loSlotNo
, bfeSlotStart = slotStart ci loSlotNo
, bfeEpochNo = EpochNo $
fst (unSlotNo loSlotNo `divMod` epoch_length gsis)
, bfeBlockSize = Nothing
, bfeForged = Just loAt
, bfeAdopted = Nothing
Expand Down
9 changes: 5 additions & 4 deletions bench/locli/src/Cardano/Analysis/ChainFilter.hs
Expand Up @@ -22,10 +22,11 @@ data ChainFilter

-- | Block classification -- primary for validity as subjects of analysis.
data BlockCond
= BUnitaryChainDelta
-- ^ All timings account for processing of a single block.
| BFullnessAbove Double
-- ^ Block fullness is above fraction.
= BUnitaryChainDelta -- ^ All timings account for processing of a single block.
| BFullnessGEq Double -- ^ Block fullness is above fraction.
| BFullnessLEq Double -- ^ Block fullness is below fraction.
| BSizeGEq Word64
| BSizeLEq Word64
deriving (FromJSON, Generic, NFData, Show, ToJSON)


Expand Down
10 changes: 6 additions & 4 deletions bench/locli/src/Cardano/Analysis/Driver.hs
Expand Up @@ -88,8 +88,8 @@ runAnalysisCommand (MachineTimelineCmd genesisFile metaFile mChFiltersFile logfi
AE.eitherDecode @Genesis <$> LBS.readFile (unJsonGenesisFile genesisFile))
chFilters <- fmap (fromMaybe []) $
forM mChFiltersFile $
\(JsonSelectorFile f) -> do
firstExceptT (RunMetaParseError metaFile . T.pack)
\jf@(JsonSelectorFile f) -> do
firstExceptT (ChainFiltersParseError jf . T.pack)
(newExceptT $
AE.eitherDecode @[ChainFilter] <$> LBS.readFile f)

Expand All @@ -106,8 +106,8 @@ runAnalysisCommand (BlockPropagationCmd genesisFile metaFile mChFiltersFile logf
AE.eitherDecode @Genesis <$> LBS.readFile (unJsonGenesisFile genesisFile))
chFilters <- fmap (fromMaybe []) $
forM mChFiltersFile $
\(JsonSelectorFile f) -> do
firstExceptT (RunMetaParseError metaFile . T.pack)
\jf@(JsonSelectorFile f) -> do
firstExceptT (ChainFiltersParseError jf . T.pack)
(newExceptT $
AE.eitherDecode @[ChainFilter] <$> LBS.readFile f)

Expand Down Expand Up @@ -243,6 +243,8 @@ runMachineTimeline chainInfo logfiles chFilters MachineTimelineOutputFiles{..} =
renderPrettyMachTimeline xs s (TextOutputFile f) =
withFile f WriteMode $ \hnd -> do
progress "pretty-timeline" (Q f)
hPutStrLn hnd . T.pack $
printf "--- input: %s" f
mapM_ (T.hPutStrLn hnd)
(renderDistributions p RenderPretty s)
mapM_ (T.hPutStrLn hnd)
Expand Down
23 changes: 15 additions & 8 deletions bench/locli/src/Cardano/Unlog/SlotStats.hs
Expand Up @@ -4,7 +4,8 @@
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# OPTIONS_GHC -Wno-deprecations #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# OPTIONS_GHC -Wno-deprecations -Wno-orphans #-}
{- HLINT ignore "Use head" -}
module Cardano.Unlog.SlotStats (module Cardano.Unlog.SlotStats) where

Expand All @@ -18,7 +19,7 @@ import Data.List.Split (splitOn)
import Data.Time.Clock (UTCTime, NominalDiffTime)
import Text.Printf

import Ouroboros.Network.Block (SlotNo(..))
import Cardano.Slotting.Slot (EpochNo (..), SlotNo (..))

import Data.Accum
import Cardano.Analysis.Profile
Expand Down Expand Up @@ -56,17 +57,23 @@ data SlotStats
deriving (Generic, Show)

data SlotCond
= SSince SlotNo
| SUntil SlotNo
| SHasLeaders
= SlotGEq SlotNo
| SlotLEq SlotNo
| EpochGEq EpochNo
| EpochLEq EpochNo
| SlotHasLeaders
deriving (FromJSON, Generic, NFData, Show, ToJSON)

deriving instance NFData EpochNo

testSlotStats :: Profile -> SlotStats -> SlotCond -> Bool
testSlotStats Profile{genesis=GenesisProfile{}}
SlotStats{..} = \case
SSince s -> slSlot >= s
SUntil s -> slSlot <= s
SHasLeaders -> slCountLeads > 0
SlotGEq s -> slSlot >= s
SlotLEq s -> slSlot <= s
EpochGEq s -> fromIntegral slEpoch >= s
EpochLEq s -> fromIntegral slEpoch <= s
SlotHasLeaders -> slCountLeads > 0

instance RenderTimeline SlotStats where
rtFields _ =
Expand Down
13 changes: 1 addition & 12 deletions nix/workbench/analyse.sh
Expand Up @@ -20,12 +20,9 @@ analyse() {
local skip_preparation= time= dump_logobjects= self_args=() locli_args=() since_slot= until_slot= fullness_above=
while test $# -gt 0
do case "$1" in
--chain-filters ) locli_args+=($1 $2); self_args+=($1 $2); shift;;
--reanalyse | --re ) skip_preparation='true'; self_args+=($1);;
--dump-logobjects ) dump_logobjects='true'; self_args+=($1);;
--block-fullness-above )
locli_args+=($1 $2); self_args+=($1 $2); fullness_above=$2; shift;;
--since-slot ) locli_args+=($1 $2); self_args+=($1 $2); since_slot=$2; shift;;
--until-slot ) locli_args+=($1 $2); self_args+=($1 $2); until_slot=$2; shift;;
* ) break;; esac; shift; done

local op=${1:-$(usage_analyse)}; shift
Expand Down Expand Up @@ -82,19 +79,11 @@ case "$op" in
--genesis "$dir"/genesis-shelley.json
--run-metafile "$dir"/meta.json
## ->
--blocks-unitary-chain-delta
## ->
--timeline-pretty "$adir"/block-propagation.txt
--analysis-json "$adir"/block-propagation.json
)
if test -n "$dump_logobjects"; then
locli_args+=(--logobjects-json "$adir"/logs-cluster.logobjects.json); fi
if test -n "$fullness_above"; then
locli_arge+=(--block-fullness-above $fullness_above); fi
if test -n "$since_slot"; then
locli_args+=(--since-slot $since_slot); fi
if test -n "$until_slot"; then
locli_args+=(--until-slot $until_slot); fi

time locli 'analyse' 'block-propagation' \
"${locli_args[@]}" "$adir"/*.flt.json
Expand Down

0 comments on commit 78348ff

Please sign in to comment.