Skip to content

Commit 2f3ace3

Browse files
committed
db-analyser: support reapplication in --benchmark-ledger-ops
1 parent 42b6061 commit 2f3ace3

File tree

7 files changed

+45
-20
lines changed

7 files changed

+45
-20
lines changed

ouroboros-consensus-cardano/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,9 @@ Lastly the user can provide the analysis that should be run on the chain:
157157
- Ticking the [ledger state](https://github.com/IntersectMBO/ouroboros-consensus/blob/51da3876c01edc2eec250fdc998f6cb33cdc4367/ouroboros-consensus/src/ouroboros-consensus/Ouroboros/Consensus/Ledger/Basics.hs#L174).
158158
- Applying a block.
159159

160+
When the `--reapply` flag is specified, we measure header/block
161+
*re*application instead of full application.
162+
160163
* `--repro-mempool-and-forge NUM` populates the mempool with the transactions
161164
from NUM blocks every time and then runs the forging loop. Useful to inspect
162165
regressions in the forging loop or in the mempool adding/snapshotting logic.

ouroboros-consensus-cardano/app/DBAnalyser/Parsers.hs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,8 @@ parseLimit = asum [
150150

151151
benchmarkLedgerOpsParser :: Parser AnalysisName
152152
benchmarkLedgerOpsParser =
153-
BenchmarkLedgerOps <$> (benchmarkLedgerOpsFlagParser *> pMaybeOutputFile)
153+
benchmarkLedgerOpsFlagParser
154+
*> (BenchmarkLedgerOps <$> pMaybeOutputFile <*> pApplyMode)
154155
where
155156
benchmarkLedgerOpsFlagParser =
156157
flag' BenchmarkLedgerOps $ mconcat [
@@ -160,6 +161,12 @@ benchmarkLedgerOpsParser =
160161
<> " (defaults to stdout)."
161162
]
162163

164+
pApplyMode =
165+
flag LedgerApply LedgerReapply $ mconcat [
166+
long "reapply"
167+
, help $ "Measure header/block *re*application instead of full application."
168+
]
169+
163170
getBlockApplicationMetrics :: Parser AnalysisName
164171
getBlockApplicationMetrics = do
165172
fGetBlockApplicationMetrics <- partialGetBlockApplicationMetricsParser

ouroboros-consensus-cardano/app/db-analyser.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
-- [--full-ledger-validation] |
1313
-- --count-blocks | --checkThunks BLOCK_COUNT |
1414
-- --trace-ledger | --repro-mempool-and-forge INT |
15-
-- --benchmark-ledger-ops [--out-file FILE] |
15+
-- --benchmark-ledger-ops [--out-file FILE] [--reapply] |
1616
-- --get-block-application-metrics NUM [--out-file FILE]]
1717
-- [--num-blocks-to-process INT] COMMAND
1818
module Main (main) where

ouroboros-consensus-cardano/src/unstable-cardano-tools/Cardano/Tools/DBAnalyser/Analysis.hs

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,12 @@ import Ouroboros.Consensus.Block
4949
import Ouroboros.Consensus.Config
5050
import Ouroboros.Consensus.Forecast (forecastFor)
5151
import Ouroboros.Consensus.HeaderValidation (HasAnnTip (..),
52-
HeaderState (..), headerStatePoint, tickHeaderState,
53-
validateHeader)
54-
import Ouroboros.Consensus.Ledger.Abstract (LedgerCfg, LedgerConfig,
55-
applyBlockLedgerResult, applyChainTick, tickThenApply,
56-
tickThenApplyLedgerResult, tickThenReapply)
52+
HeaderState (..), headerStatePoint, revalidateHeader,
53+
tickHeaderState, validateHeader)
54+
import Ouroboros.Consensus.Ledger.Abstract
55+
(ApplyBlock (reapplyBlockLedgerResult), LedgerCfg,
56+
LedgerConfig, applyBlockLedgerResult, applyChainTick,
57+
tickThenApply, tickThenApplyLedgerResult, tickThenReapply)
5758
import Ouroboros.Consensus.Ledger.Basics (LedgerResult (..),
5859
LedgerState, getTipSlot)
5960
import Ouroboros.Consensus.Ledger.Extended
@@ -112,7 +113,7 @@ runAnalysis analysisName = case go analysisName of
112113
go (CheckNoThunksEvery nBks) = mkAnalysis $ checkNoThunksEvery nBks
113114
go TraceLedgerProcessing = mkAnalysis $ traceLedgerProcessing
114115
go (ReproMempoolAndForge nBks) = mkAnalysis $ reproMempoolForge nBks
115-
go (BenchmarkLedgerOps mOutfile) = mkAnalysis $ benchmarkLedgerOps mOutfile
116+
go (BenchmarkLedgerOps mOutfile lgrAppMode) = mkAnalysis $ benchmarkLedgerOps mOutfile lgrAppMode
116117
go (GetBlockApplicationMetrics nrBlocks mOutfile) = mkAnalysis $ getBlockApplicationMetrics nrBlocks mOutfile
117118

118119
mkAnalysis ::
@@ -536,13 +537,15 @@ benchmarkLedgerOps ::
536537
( HasAnalysis blk
537538
, LedgerSupportsProtocol blk
538539
)
539-
=> Maybe FilePath -> Analysis blk StartFromLedgerState
540-
benchmarkLedgerOps mOutfile AnalysisEnv {db, registry, startFrom, cfg, limit} = do
540+
=> Maybe FilePath
541+
-> LedgerApplicationMode
542+
-> Analysis blk StartFromLedgerState
543+
benchmarkLedgerOps mOutfile ledgerAppMode AnalysisEnv {db, registry, startFrom, cfg, limit} = do
541544
-- We default to CSV when the no output file is provided (and thus the results are output to stdout).
542545
outFormat <- F.getOutputFormat mOutfile
543546

544547
withFile mOutfile $ \outFileHandle -> do
545-
F.writeMetadata outFileHandle outFormat
548+
F.writeMetadata outFileHandle outFormat ledgerAppMode
546549
F.writeHeader outFileHandle outFormat
547550

548551
void $ processAll db registry GetBlock startFrom limit initLedger (process outFileHandle outFormat)
@@ -639,10 +642,13 @@ benchmarkLedgerOps mOutfile AnalysisEnv {db, registry, startFrom, cfg, limit} =
639642
LedgerView (BlockProtocol blk)
640643
-> Ticked (HeaderState blk)
641644
-> IO (HeaderState blk)
642-
applyTheHeader ledgerView tickedHeaderState = do
645+
applyTheHeader ledgerView tickedHeaderState = case ledgerAppMode of
646+
LedgerApply ->
643647
case runExcept $ validateHeader cfg ledgerView (getHeader blk) tickedHeaderState of
644648
Left err -> fail $ "benchmark doesn't support invalid headers: " <> show rp <> " " <> show err
645649
Right x -> pure x
650+
LedgerReapply ->
651+
pure $! revalidateHeader cfg ledgerView (getHeader blk) tickedHeaderState
646652

647653
tickTheLedgerState ::
648654
SlotNo
@@ -654,10 +660,13 @@ benchmarkLedgerOps mOutfile AnalysisEnv {db, registry, startFrom, cfg, limit} =
654660
applyTheBlock ::
655661
Ticked (LedgerState blk)
656662
-> IO (LedgerState blk)
657-
applyTheBlock tickedLedgerSt = do
663+
applyTheBlock tickedLedgerSt = case ledgerAppMode of
664+
LedgerApply ->
658665
case runExcept (lrResult <$> applyBlockLedgerResult lcfg blk tickedLedgerSt) of
659666
Left err -> fail $ "benchmark doesn't support invalid blocks: " <> show rp <> " " <> show err
660667
Right x -> pure x
668+
LedgerReapply ->
669+
pure $! lrResult $ reapplyBlockLedgerResult lcfg blk tickedLedgerSt
661670

662671
withFile :: Maybe FilePath -> (IO.Handle -> IO r) -> IO r
663672
withFile (Just outfile) = IO.withFile outfile IO.WriteMode

ouroboros-consensus-cardano/src/unstable-cardano-tools/Cardano/Tools/DBAnalyser/Analysis/BenchmarkLedgerOps/FileWriting.hs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import Cardano.Tools.DBAnalyser.Analysis.BenchmarkLedgerOps.SlotDataPo
1616
(SlotDataPoint)
1717
import qualified Cardano.Tools.DBAnalyser.Analysis.BenchmarkLedgerOps.SlotDataPoint as DP
1818
import qualified Cardano.Tools.DBAnalyser.CSV as CSV
19+
import Cardano.Tools.DBAnalyser.Types (LedgerApplicationMode)
1920
import Data.Aeson as Aeson
2021
import qualified Data.ByteString.Lazy as BSL
2122
import System.FilePath.Posix (takeExtension)
@@ -86,10 +87,10 @@ writeDataPoint outFileHandle JSON slotDataPoint =
8687

8788
-- | Write metadata to a JSON file if this is the selected
8889
-- format. Perform a no-op otherwise.
89-
writeMetadata :: IO.Handle -> OutputFormat -> IO ()
90-
writeMetadata _outFileHandle CSV = pure ()
91-
writeMetadata outFileHandle JSON =
92-
BenchmarkLedgerOps.Metadata.getMetadata
90+
writeMetadata :: IO.Handle -> OutputFormat -> LedgerApplicationMode -> IO ()
91+
writeMetadata _outFileHandle CSV _lgrAppMode = pure ()
92+
writeMetadata outFileHandle JSON lgrAppMode =
93+
BenchmarkLedgerOps.Metadata.getMetadata lgrAppMode
9394
>>= BSL.hPut outFileHandle . Aeson.encode
9495

9596
{-------------------------------------------------------------------------------

ouroboros-consensus-cardano/src/unstable-cardano-tools/Cardano/Tools/DBAnalyser/Analysis/BenchmarkLedgerOps/Metadata.hs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ module Cardano.Tools.DBAnalyser.Analysis.BenchmarkLedgerOps.Metadata (
1515
, getMetadata
1616
) where
1717

18+
import Cardano.Tools.DBAnalyser.Types (LedgerApplicationMode (..))
1819
import Cardano.Tools.GitRev (gitRev)
1920
import Data.Aeson (ToJSON)
2021
import qualified Data.Aeson as Aeson
@@ -35,13 +36,14 @@ data Metadata = Metadata {
3536
, operatingSystem :: String
3637
, machineArchitecture :: String
3738
, gitRevison :: String
39+
, ledgerApplicationMode :: String
3840
} deriving (Generic, Show, Eq)
3941

4042
instance ToJSON Metadata where
4143
toEncoding = Aeson.genericToEncoding Aeson.defaultOptions
4244

43-
getMetadata :: IO Metadata
44-
getMetadata = do
45+
getMetadata :: LedgerApplicationMode -> IO Metadata
46+
getMetadata lgrAppMode = do
4547
rtsFlags <- RTS.getRTSFlags
4648
pure $ Metadata {
4749
rtsGCMaxStkSize = RTS.maxStkSize $ RTS.gcFlags rtsFlags
@@ -53,4 +55,7 @@ getMetadata = do
5355
, operatingSystem = System.Info.os
5456
, machineArchitecture = System.Info.arch
5557
, gitRevison = T.unpack gitRev
58+
, ledgerApplicationMode = case lgrAppMode of
59+
LedgerApply -> "full-application"
60+
LedgerReapply -> "reapplication"
5661
}

ouroboros-consensus-cardano/src/unstable-cardano-tools/Cardano/Tools/DBAnalyser/Types.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ data AnalysisName =
2828
| CountBlocks
2929
| CheckNoThunksEvery Word64
3030
| TraceLedgerProcessing
31-
| BenchmarkLedgerOps (Maybe FilePath)
31+
| BenchmarkLedgerOps (Maybe FilePath) LedgerApplicationMode
3232
| ReproMempoolAndForge Int
3333
-- | Compute different block application metrics every 'NumberOfBlocks'.
3434
--

0 commit comments

Comments
 (0)