11use std:: collections:: BTreeMap ;
22use std:: fmt:: Display ;
33use std:: num:: NonZeroUsize ;
4+ use std:: sync:: atomic:: { AtomicU64 , Ordering } ;
45
56use apollo_config:: dumping:: { prepend_sub_config_name, ser_param, SerializeConfig } ;
67use apollo_config:: { ParamPath , ParamPrivacyInput , SerializedParam } ;
@@ -76,8 +77,8 @@ pub struct CachedStorage<S: Storage> {
7677 pub storage : S ,
7778 pub cache : LruCache < DbKey , Option < DbValue > > ,
7879 pub cache_on_write : bool ,
79- reads : u128 ,
80- cached_reads : u128 ,
80+ reads : AtomicU64 ,
81+ cached_reads : AtomicU64 ,
8182 writes : u128 ,
8283 include_inner_stats : bool ,
8384}
@@ -204,8 +205,8 @@ impl<S: Storage> CachedStorage<S> {
204205 storage,
205206 cache : LruCache :: new ( config. cache_size ) ,
206207 cache_on_write : config. cache_on_write ,
207- reads : 0 ,
208- cached_reads : 0 ,
208+ reads : AtomicU64 :: new ( 0 ) ,
209+ cached_reads : AtomicU64 :: new ( 0 ) ,
209210 writes : 0 ,
210211 include_inner_stats : config. include_inner_stats ,
211212 }
@@ -217,14 +218,6 @@ impl<S: Storage> CachedStorage<S> {
217218 }
218219 }
219220
220- pub fn total_reads ( & self ) -> u128 {
221- self . reads
222- }
223-
224- pub fn total_cached_reads ( & self ) -> u128 {
225- self . cached_reads
226- }
227-
228221 pub fn total_writes ( & self ) -> u128 {
229222 self . writes
230223 }
@@ -235,12 +228,12 @@ impl<S: Storage> Storage for CachedStorage<S> {
235228 type Config = CachedStorageConfig < S :: Config > ;
236229
237230 async fn get ( & mut self , key : & DbKey ) -> PatriciaStorageResult < Option < DbValue > > {
238- self . reads += 1 ;
239231 if let Some ( cached_value) = self . cache . get ( key) {
240- self . cached_reads += 1 ;
232+ self . reads . fetch_add ( 1 , Ordering :: Relaxed ) ;
241233 return Ok ( cached_value. clone ( ) ) ;
242234 }
243235
236+ self . cached_reads . fetch_add ( 1 , Ordering :: Relaxed ) ;
244237 let storage_value = self . storage . get ( key) . await ?;
245238 self . cache . put ( key. clone ( ) , storage_value. clone ( ) ) ;
246239 Ok ( storage_value)
@@ -257,19 +250,21 @@ impl<S: Storage> Storage for CachedStorage<S> {
257250 let mut values = vec ! [ None ; keys. len( ) ] ; // The None values are placeholders.
258251 let mut keys_to_fetch = Vec :: new ( ) ;
259252 let mut indices_to_fetch = Vec :: new ( ) ;
253+ let mut cached_reads = 0 ;
260254
261255 for ( index, key) in keys. iter ( ) . enumerate ( ) {
262256 if let Some ( cached_value) = self . cache . get ( key) {
263257 values[ index] = cached_value. clone ( ) ;
258+ cached_reads += 1 ;
264259 } else {
265260 keys_to_fetch. push ( * key) ;
266261 indices_to_fetch. push ( index) ;
267262 }
268263 }
269264
270- self . reads += u128 :: try_from ( keys. len ( ) ) . expect ( "usize should fit in u128 " ) ;
271- self . cached_reads +=
272- u128 :: try_from ( keys . len ( ) - keys_to_fetch . len ( ) ) . expect ( "usize should fit in u128" ) ;
265+ let n_keys = u64 :: try_from ( keys. len ( ) ) . expect ( "usize should fit in u64 " ) ;
266+ self . reads . fetch_add ( n_keys , Ordering :: Relaxed ) ;
267+ self . cached_reads . fetch_add ( cached_reads , Ordering :: Relaxed ) ;
273268
274269 let fetched_values = self . storage . mget ( keys_to_fetch. as_slice ( ) ) . await ?;
275270 indices_to_fetch. iter ( ) . zip ( keys_to_fetch) . zip ( fetched_values) . for_each (
@@ -297,9 +292,11 @@ impl<S: Storage> Storage for CachedStorage<S> {
297292 }
298293
299294 fn get_stats ( & self ) -> PatriciaStorageResult < Self :: Stats > {
295+ let reads = u128:: from ( self . reads . load ( Ordering :: Relaxed ) ) ;
296+ let cached_reads = u128:: from ( self . cached_reads . load ( Ordering :: Relaxed ) ) ;
300297 Ok ( CachedStorageStats {
301- reads : self . reads ,
302- cached_reads : self . cached_reads ,
298+ reads,
299+ cached_reads,
303300 writes : self . writes ,
304301 inner_stats : if self . include_inner_stats {
305302 Some ( self . storage . get_stats ( ) ?)
@@ -310,8 +307,8 @@ impl<S: Storage> Storage for CachedStorage<S> {
310307 }
311308
312309 fn reset_stats ( & mut self ) -> PatriciaStorageResult < ( ) > {
313- self . reads = 0 ;
314- self . cached_reads = 0 ;
310+ self . reads . store ( 0 , Ordering :: Relaxed ) ;
311+ self . cached_reads . store ( 0 , Ordering :: Relaxed ) ;
315312 self . writes = 0 ;
316313 self . storage . reset_stats ( )
317314 }
0 commit comments