@@ -31,6 +31,7 @@ module Cardano.DbSync.Cache.Types (
3131) where
3232
3333import qualified Cardano.Db as DB
34+ import qualified Cardano.Db.Schema.Variant.TxOut as V
3435import Cardano.DbSync.Cache.FIFO (FIFOCache )
3536import qualified Cardano.DbSync.Cache.FIFO as FIFO
3637import Cardano.DbSync.Cache.LRU (LRUCache )
@@ -82,6 +83,7 @@ data CacheInternal = CacheInternal
8283 , cPrevBlock :: ! (StrictTVar IO (Maybe (DB. BlockId , ByteString )))
8384 , cStats :: ! (StrictTVar IO CacheStatistics )
8485 , cEpoch :: ! (StrictTVar IO CacheEpoch )
86+ , cAddress :: ! (StrictTVar IO (LRUCache ByteString V. AddressId ))
8587 , cTxIds :: ! (StrictTVar IO (FIFOCache (Ledger. TxId StandardCrypto ) DB. TxId ))
8688 }
8789
@@ -96,13 +98,16 @@ data CacheStatistics = CacheStatistics
9698 , multiAssetsQueries :: ! Word64
9799 , prevBlockHits :: ! Word64
98100 , prevBlockQueries :: ! Word64
101+ , addressHits :: ! Word64
102+ , addressQueries :: ! Word64
99103 , txIdsHits :: ! Word64
100104 , txIdsQueries :: ! Word64
101105 }
102106
103107-- CacheCapacity is used to define capacities for different types of cache entries.
104108data CacheCapacity = CacheCapacity
105- { cacheCapacityStake :: ! Word64
109+ { cacheCapacityAddress :: ! Word64
110+ , cacheCapacityStake :: ! Word64
106111 , cacheCapacityDatum :: ! Word64
107112 , cacheCapacityMultiAsset :: ! Word64
108113 , cacheCapacityTx :: ! Word64
@@ -128,7 +133,7 @@ data CacheEpoch = CacheEpoch
128133 deriving (Show )
129134
130135textShowStats :: CacheStatus -> IO Text
131- textShowStats NoCache = pure " NoCache "
136+ textShowStats NoCache = pure " No Caches "
132137textShowStats (ActiveCache ic) = do
133138 isCacheOptimised <- readTVarIO $ cIsCacheOptimised ic
134139 stats <- readTVarIO $ cStats ic
@@ -137,77 +142,77 @@ textShowStats (ActiveCache ic) = do
137142 datums <- readTVarIO (cDatum ic)
138143 mAssets <- readTVarIO (cMultiAssets ic)
139144 txIds <- readTVarIO (cTxIds ic)
145+ address <- readTVarIO (cAddress ic)
140146 pure $
141147 mconcat
142148 [ " \n Cache Statistics:"
143149 , " \n Caches Optimised: " <> textShow isCacheOptimised
144- , " \n Stake Addresses: "
145- , " cache sizes: "
146- , textShow (Map. size $ scStableCache stakeHashRaws)
147- , " and "
148- , textShow (LRU. getSize $ scLruCache stakeHashRaws)
149- , if credsQueries stats == 0
150- then " "
151- else " , hit rate: " <> textShow (100 * credsHits stats `div` credsQueries stats) <> " %"
152- , " , hits: "
153- , textShow (credsHits stats)
154- , " , misses: "
155- , textShow (credsQueries stats - credsHits stats)
156- , " \n Pools: "
157- , " cache size: "
158- , textShow (Map. size pools)
159- , if poolsQueries stats == 0
160- then " "
161- else " , hit rate: " <> textShow (100 * poolsHits stats `div` poolsQueries stats) <> " %"
162- , " , hits: "
163- , textShow (poolsHits stats)
164- , " , misses: "
165- , textShow (poolsQueries stats - poolsHits stats)
166- , " \n Datums: "
167- , " cache capacity: "
168- , textShow (LRU. getCapacity datums)
169- , " , cache size: "
170- , textShow (LRU. getSize datums)
171- , if datumQueries stats == 0
172- then " "
173- else " , hit rate: " <> textShow (100 * datumHits stats `div` datumQueries stats) <> " %"
174- , " , hits: "
175- , textShow (datumHits stats)
176- , " , misses: "
177- , textShow (datumQueries stats - datumHits stats)
178- , " \n Multi Assets: "
179- , " cache capacity: "
180- , textShow (LRU. getCapacity mAssets)
181- , " , cache size: "
182- , textShow (LRU. getSize mAssets)
183- , if multiAssetsQueries stats == 0
184- then " "
185- else " , hit rate: " <> textShow (100 * multiAssetsHits stats `div` multiAssetsQueries stats) <> " %"
186- , " , hits: "
187- , textShow (multiAssetsHits stats)
188- , " , misses: "
189- , textShow (multiAssetsQueries stats - multiAssetsHits stats)
190- , " \n Previous Block: "
191- , if prevBlockQueries stats == 0
192- then " "
193- else " hit rate: " <> textShow (100 * prevBlockHits stats `div` prevBlockQueries stats) <> " %"
194- , " , hits: "
195- , textShow (prevBlockHits stats)
196- , " , misses: "
197- , textShow (prevBlockQueries stats - prevBlockHits stats)
198- , " \n TxId: "
199- , " cache size: "
200- , textShow (FIFO. getSize txIds)
201- , " , cache capacity: "
202- , textShow (FIFO. getCapacity txIds)
203- , if txIdsQueries stats == 0
204- then " "
205- else " , hit rate: " <> textShow (100 * txIdsHits stats `div` txIdsQueries stats) <> " %"
206- , " , hits: "
207- , textShow (txIdsHits stats)
208- , " , misses: "
209- , textShow (txIdsQueries stats - txIdsHits stats)
150+ , textCacheSection " Stake Addresses" (scLruCache stakeHashRaws) (scStableCache stakeHashRaws) (credsHits stats) (credsQueries stats)
151+ , textMapSection " Pools" pools (poolsHits stats) (poolsQueries stats)
152+ , textLruSection " Datums" datums (datumHits stats) (datumQueries stats)
153+ , textLruSection " Addresses" address (addressHits stats) (addressQueries stats)
154+ , textLruSection " Multi Assets" mAssets (multiAssetsHits stats) (multiAssetsQueries stats)
155+ , textPrevBlockSection stats
156+ , textFifoSection " TxId" txIds (txIdsHits stats) (txIdsQueries stats)
210157 ]
158+ where
159+ textCacheSection title cacheLru cacheStable hits queries =
160+ mconcat
161+ [ " \n " <> title <> " : "
162+ , " cache sizes: "
163+ , textShow (Map. size cacheStable)
164+ , " and "
165+ , textShow (LRU. getSize cacheLru)
166+ , hitMissStats hits queries
167+ ]
168+
169+ textMapSection title cache hits queries =
170+ mconcat
171+ [ " \n " <> title <> " : "
172+ , " cache size: "
173+ , textShow (Map. size cache)
174+ , hitMissStats hits queries
175+ ]
176+
177+ textLruSection title cache hits queries =
178+ mconcat
179+ [ " \n " <> title <> " : "
180+ , " cache capacity: "
181+ , textShow (LRU. getCapacity cache)
182+ , " , cache size: "
183+ , textShow (LRU. getSize cache)
184+ , hitMissStats hits queries
185+ ]
186+
187+ textFifoSection title cache hits queries =
188+ mconcat
189+ [ " \n " <> title <> " : "
190+ , " cache size: "
191+ , textShow (FIFO. getSize cache)
192+ , " , cache capacity: "
193+ , textShow (FIFO. getCapacity cache)
194+ , hitMissStats hits queries
195+ ]
196+
197+ textPrevBlockSection stats =
198+ mconcat
199+ [ " \n Previous Block: "
200+ , hitMissStats (prevBlockHits stats) (prevBlockQueries stats)
201+ ]
202+
203+ hitMissStats hits queries =
204+ mconcat
205+ [ hitRate hits queries
206+ , " , hits: "
207+ , textShow hits
208+ , " , misses: "
209+ , textShow (queries - hits)
210+ ]
211+
212+ hitRate hits queries =
213+ if queries == 0
214+ then " "
215+ else " , hit rate: " <> textShow (100 * hits `div` queries) <> " %"
211216
212217useNoCache :: CacheStatus
213218useNoCache = NoCache
@@ -218,6 +223,7 @@ newEmptyCache CacheCapacity {..} = liftIO $ do
218223 cStake <- newTVarIO (StakeCache Map. empty (LRU. empty cacheCapacityStake))
219224 cPools <- newTVarIO Map. empty
220225 cDatum <- newTVarIO (LRU. empty cacheCapacityDatum)
226+ cAddress <- newTVarIO (LRU. empty cacheCapacityAddress)
221227 cMultiAssets <- newTVarIO (LRU. empty cacheCapacityMultiAsset)
222228 cPrevBlock <- newTVarIO Nothing
223229 cStats <- newTVarIO initCacheStatistics
@@ -234,11 +240,12 @@ newEmptyCache CacheCapacity {..} = liftIO $ do
234240 , cPrevBlock = cPrevBlock
235241 , cStats = cStats
236242 , cEpoch = cEpoch
243+ , cAddress = cAddress
237244 , cTxIds = cTxIds
238245 }
239246
240247initCacheStatistics :: CacheStatistics
241- initCacheStatistics = CacheStatistics 0 0 0 0 0 0 0 0 0 0 0 0
248+ initCacheStatistics = CacheStatistics 0 0 0 0 0 0 0 0 0 0 0 0 0 0
242249
243250initCacheEpoch :: CacheEpoch
244251initCacheEpoch = CacheEpoch mempty Nothing
0 commit comments