Skip to content
Open
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
30 changes: 30 additions & 0 deletions ouroboros-consensus.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -945,6 +945,36 @@ benchmark mempool-bench
unstable-mempool-test-utils,
with-utf8,

benchmark mempool-state-bench
import: common-bench
type: exitcode-stdio-1.0
hs-source-dirs:
ouroboros-consensus/bench/mempool-state-bench
ouroboros-consensus/bench/mempool-bench

main-is: Main.hs
other-modules:
Bench.Consensus.Mempool.TestBlock

ghc-options: -threaded -rtsopts -with-rtsopts=-N4

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the reasoning behind choosing -N4?

build-depends:
async,
base,
cardano-ledger-core,
cardano-slotting,
containers,
contra-tracer,
deepseq,
io-classes:si-timers,
mempack,
nothunks,
ouroboros-consensus,
serialise,
time,
transformers,
tree-diff,
unstable-consensus-testlib,

benchmark ChainSync-client-bench
import: common-bench
type: exitcode-stdio-1.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ module Bench.Consensus.Mempool.TestBlock

-- * Initial parameters
, initialLedgerState
, mkInitialLedgerState

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(This Comment is here only because this line is rendered nearest the commit message.)

The Add mempool-state-bench: concurrent shared-state access benchmark commit message includes

Adds mkInitialLedgerState + advanceTip helpers to the bench TestBlock,
and docs/mempool-double-buffer-plan.md describing the design + plan.

But, eg, I don't see a docs/mempool-double-buffer-plan.md in the PR.

Would you ask Claude to remove such "dangling" references to your conversation from this PR's commit messages and code comments, since this PR doesn't retain the context of your conversation with Claude?

, advanceTip
, sampleLedgerConfig

-- * Transactions
Expand All @@ -37,6 +39,8 @@ import Data.MemPack
import Data.Set (Set)
import qualified Data.Set as Set
import Data.TreeDiff (ToExpr)
import Data.Word (Word64)
import GHC.Clock (getMonotonicTimeNSec)
import GHC.Generics (Generic)
import NoThunks.Class (NoThunks)
import qualified Ouroboros.Consensus.Block as Block
Expand All @@ -48,7 +52,10 @@ import Ouroboros.Consensus.Ledger.Tables
import qualified Ouroboros.Consensus.Ledger.Tables.Diff as Diff
import qualified Ouroboros.Consensus.Ledger.Tables.Utils as Ledger
import Ouroboros.Consensus.Util.IndexedMemPack (IndexedMemPack (..))
import System.Environment (lookupEnv)
import System.IO.Unsafe (unsafePerformIO)
import Test.Util.TestBlock hiding (TestBlock)
import Text.Read (readMaybe)

{-------------------------------------------------------------------------------
MempoolTestBlock
Expand Down Expand Up @@ -86,15 +93,29 @@ mkTx cons prod =
-------------------------------------------------------------------------------}

initialLedgerState :: LedgerState (TestBlockWith Tx) ValuesMK
initialLedgerState =
initialLedgerState = mkInitialLedgerState []

-- | Like 'initialLedgerState' but seeded with a set of available tokens (the
-- UTxO). Chains of transactions can then be built by consuming a seed token and
-- producing the next one.
mkInitialLedgerState :: [Token] -> LedgerState (TestBlockWith Tx) ValuesMK
mkInitialLedgerState toks =
TestLedger
{ lastAppliedPoint = Block.GenesisPoint
, payloadDependentState =
TestPLDS
{ getTestPLDS = ValuesMK Map.empty
{ getTestPLDS = ValuesMK (Map.fromList [(t, ()) | t <- toks])
}
}

-- | Move the tip to a fresh point (distinct per @n@) while keeping the ledger
-- tables unchanged. Used to force the mempool to resync/revalidate against a
-- "new" tip without invalidating any of its transactions.
advanceTip ::

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this function ever called on its own result? Without paging the TestBlock context back into my brain, this function's RHS seems very bogus. I could imagine that's OK, if the function's output is never retained in some accumulating state.

If it doesn't accumulate, please rename it in a way that no longer suggests its accumulative. And maybe point out in this comment where/when it's result is overwritten.

Word64 -> LedgerState (TestBlockWith Tx) ValuesMK -> LedgerState (TestBlockWith Tx) ValuesMK
advanceTip n st =
st{lastAppliedPoint = Block.blockPoint (firstBlockWithPayload n (Tx Set.empty Set.empty))}

sampleLedgerConfig :: Ledger.LedgerConfig TestBlock
sampleLedgerConfig =
testBlockLedgerConfigFrom $
Expand Down Expand Up @@ -223,17 +244,62 @@ txSize (TestBlockGenTx tx) =
fromIntegral $
1 + length (consumed tx) + length (produced tx)

-- | Simulated CPU cost, in microseconds, of /fully/ validating a transaction
-- (the script and signature checks a real ledger performs in 'applyTx'). Read
-- once from @MEMPOOL_APPLY_CPU_US@; defaults to @0@ so ordinary tests and the
-- criterion @mempool-bench@ are unaffected.
--
-- Together with 'reapplyCpuMicros' this lets a benchmark reproduce the
-- real-node relationship @reapply ≪ apply@: reapplication skips the expensive
-- checks, so a mempool sync (which only reapplies) is strictly cheaper per tx
-- than ingestion (which fully validates). The mempool-state-bench relies on
-- this for its sync-vs-ingest convergence to be faithful.
{-# NOINLINE applyCpuMicros #-}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want 0 defaults for these? Maybe we can grab some numbers from a relevant report Brian Bush wrote?

applyCpuMicros :: Int
applyCpuMicros = envInt "MEMPOOL_APPLY_CPU_US" 0

-- | Simulated CPU cost, in microseconds, of /reapplying/ an already-validated
-- transaction. Read once from @MEMPOOL_REAPPLY_CPU_US@; defaults to @0@. Keep
-- this well below 'applyCpuMicros' to model @reapply ≪ apply@.
{-# NOINLINE reapplyCpuMicros #-}
reapplyCpuMicros :: Int
reapplyCpuMicros = envInt "MEMPOOL_REAPPLY_CPU_US" 0

{-# NOINLINE envInt #-}
envInt :: String -> Int -> Int
envInt k d = unsafePerformIO $ maybe d id . (>>= readMaybe) <$> lookupEnv k

-- | Busy-wait (burning CPU, /not/ sleeping — validation contends for cores)
-- for @us@ microseconds, then return @tx@. The result is @tx@ itself and the
-- caller feeds it into the ledger transition, so the spin depends on the tx
-- and cannot be shared across calls or optimised away.
{-# NOINLINE burnCpuMicros #-}
burnCpuMicros :: Int -> Tx -> Tx
burnCpuMicros us tx
| us <= 0 = tx
| otherwise = unsafePerformIO $ do
let targetNs = fromIntegral us * 1000 :: Word64
start <- getMonotonicTimeNSec
let go = do
now <- getMonotonicTimeNSec
if now - start >= targetNs then pure tx else go
go

instance Ledger.LedgerSupportsMempool TestBlock where
applyTx _cfg _shouldIntervene _slot (TestBlockGenTx tx) tickedSt =
except $
fmap ((,ValidatedGenTx (TestBlockGenTx tx)) . Ledger.trackingToDiffs) $
applyDirectlyToPayloadDependentState tickedSt tx

reapplyTx cfg slot (ValidatedGenTx genTx) tickedSt =
Ledger.applyDiffs tickedSt . fst
<$> Ledger.applyTx cfg Ledger.DoNotIntervene slot genTx tickedSt

-- FIXME: it is ok to use 'DoNotIntervene' here?
-- Pay the (simulated) full-validation cost. 'burnCpuMicros' returns the
-- tx we then apply, so it is forced as part of producing the result.
applyDirectlyToPayloadDependentState tickedSt (burnCpuMicros applyCpuMicros tx)

-- Reapplication does /not/ route through 'applyTx' (which would pay the full
-- validation cost); it runs the ledger transition directly, paying only the
-- much cheaper 'reapplyCpuMicros'.
reapplyTx _cfg _slot (ValidatedGenTx (TestBlockGenTx tx)) tickedSt =
except $
Ledger.applyDiffs tickedSt . Ledger.trackingToDiffs
<$> applyDirectlyToPayloadDependentState tickedSt (burnCpuMicros reapplyCpuMicros tx)

txForgetValidated (ValidatedGenTx tx) = tx

Expand Down
Loading
Loading