Skip to content

Commit f2706f4

Browse files
authored
Merge pull request #6295 from IntersectMBO/bench-mmap
bench | mmap
2 parents 2a08c8c + ff88a3d commit f2706f4

File tree

5 files changed

+34
-9
lines changed

5 files changed

+34
-9
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,11 @@ stateDiagram-v2
4545

4646
The process for getting a `cardano-node` executable can be found in the
4747
[Cardano Developer
48-
Portal](https://developers.cardano.org/docs/get-started/cardano-node/installing-cardano-node).
48+
Portal](https://developers.cardano.org/docs/operate-a-stake-pool/node-operations/installing-cardano-node).
4949

5050
The configuration and files required to run a `cardano-node` in one of the
5151
supported networks are described also in the [Cardano Developer
52-
Portal](https://developers.cardano.org/docs/get-started/cardano-node/running-cardano).
52+
Portal](https://developers.cardano.org/docs/operate-a-stake-pool/node-operations/running-cardano).
5353

5454
# Using `cardano-node` and dependencies as a library
5555

cardano-node/cardano-node.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ library
175175
, io-classes >= 1.5
176176
, iohk-monitoring ^>= 0.2
177177
, microlens
178+
, mmap
178179
, network-mux
179180
, iproute
180181
, lobemo-backend-aggregation

cardano-node/src/Cardano/Node/Protocol/Shelley.hs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ import Control.Monad
4848
import qualified Data.Aeson as Aeson
4949
import qualified Data.ByteString as BS
5050
import qualified Data.Text as T
51+
import System.Directory (getFileSize)
52+
import qualified System.IO.MMap as MMap
5153

5254
------------------------------------------------------------------------------
5355
-- Shelley protocol
@@ -94,12 +96,33 @@ readGenesis :: GenesisFile
9496
(ShelleyGenesis, GenesisHash)
9597
readGenesis = readGenesisAny
9698

99+
-- | Read a genesis file using a memory-safe strategy.
100+
--
101+
-- Genesis files are used in testing and benchmarking to create testnets with
102+
-- large datasets. To be able to read these files in memory constrained
103+
-- environments we use a conditional loading strategy: if file is below ~10
104+
-- megabytes it is entirely read into memory, otherwise the 'ByteString' is
105+
-- created using `mmap` that uses the virtual memory subsystem to do on-demand
106+
-- loading.
107+
-- With current usage and how benchmakring is done only Shelley and Conway are
108+
-- affected (Shelley's `initialFunds` and Conway's "delegs" and "initialDReps").
97109
readGenesisAny :: FromJSON genesis
98110
=> GenesisFile
99111
-> Maybe GenesisHash
100112
-> ExceptT GenesisReadError IO (genesis, GenesisHash)
101113
readGenesisAny (GenesisFile file) mExpectedGenesisHash = do
102-
content <- handleIOExceptT (GenesisReadFileError file) $ BS.readFile file
114+
content <- handleIOExceptT (GenesisReadFileError file) $ do
115+
-- Size of the file in 8-bit bytes.
116+
size <- getFileSize file
117+
{-- Mainnet files for reference:
118+
-byron-genesis.json: 1056360 (1.1M)
119+
-shelley-genesis.json: 2486 (2.5K)
120+
-alonzo-genesis.json: 9459 (9.3K)
121+
-conway-genesis.json: 4168 (4.1K)
122+
--}
123+
if size >= 10485760 -- 10 megabytes.
124+
then MMap.mmapFileByteString file Nothing -- MMap version.
125+
else BS.readFile file -- Non-lazy version.
103126
genesisHash <- checkExpectedGenesisHash content mExpectedGenesisHash
104127
genesis <- firstExceptT (GenesisDecodeError file) $ hoistEither $
105128
Aeson.eitherDecodeStrict' content

cardano-node/src/Cardano/Node/Startup.hs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -155,12 +155,13 @@ data BasicInfoCommon = BasicInfoCommon {
155155
, biNodeStartTime :: UTCTime
156156
}
157157

158+
-- Fields of this type are made strict to be sure no path from GC roots to genesis is retained
158159
data BasicInfoShelleyBased = BasicInfoShelleyBased {
159-
bisEra :: Text
160-
, bisSystemStartTime :: UTCTime
161-
, bisSlotLength :: NominalDiffTime
162-
, bisEpochLength :: Word64
163-
, bisSlotsPerKESPeriod :: Word64
160+
bisEra :: !Text
161+
, bisSystemStartTime :: !UTCTime
162+
, bisSlotLength :: !NominalDiffTime
163+
, bisEpochLength :: !Word64
164+
, bisSlotsPerKESPeriod :: !Word64
164165
}
165166

166167
data BasicInfoByron = BasicInfoByron {

configuration/cardano/mainnet-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ MaxKnownMajorProtocolVersion: 2
3939
PeerSharing: True
4040

4141
# The following control the number of outbound connections to strictly our upstream peers
42-
# cf. https://developers.cardano.org/docs/get-started/cardano-node/p2p
42+
# cf. https://developers.cardano.org/docs/operate-a-stake-pool/node-operations/topology
4343
# defaults are provided by ouroboros-network, but can be overridden by uncommenting below:
4444
# SyncTargetNumberOfActiveBigLedgerPeers: 30
4545
# SyncTargetNumberOfActivePeers: 5

0 commit comments

Comments
 (0)