@@ -25,6 +25,13 @@ const CACHE_DIR: &str = "cache";
25
25
// Cacheable things.
26
26
const MODULES_DIR : & str = "modules" ;
27
27
28
+ /// Statistics about the usage of a cache instance. Those values are node
29
+ /// specific and must not be used in a consensus critical context.
30
+ /// When a node is hit by a client for simulations or other queries, hits and misses
31
+ /// increase. Also a node restart will reset the values.
32
+ ///
33
+ /// All values should be increment using saturated addition to ensure the node does not
34
+ /// crash in case the stats exceed the integer limit.
28
35
#[ derive( Debug , Default , Clone , Copy ) ]
29
36
pub struct Stats {
30
37
pub hits_pinned_memory_cache : u32 ,
@@ -228,7 +235,7 @@ where
228
235
229
236
// Try to get module from the memory cache
230
237
if let Some ( module) = cache. memory_cache . load ( checksum) ? {
231
- cache. stats . hits_memory_cache += 1 ;
238
+ cache. stats . hits_memory_cache = cache . stats . hits_memory_cache . saturating_add ( 1 ) ;
232
239
return cache
233
240
. pinned_memory_cache
234
241
. store ( checksum, module. module , module. size ) ;
@@ -237,7 +244,7 @@ where
237
244
// Try to get module from file system cache
238
245
let store = make_runtime_store ( Some ( cache. instance_memory_limit ) ) ;
239
246
if let Some ( module) = cache. fs_cache . load ( checksum, & store) ? {
240
- cache. stats . hits_fs_cache += 1 ;
247
+ cache. stats . hits_fs_cache = cache . stats . hits_fs_cache . saturating_add ( 1 ) ;
241
248
let module_size = loupe:: size_of_val ( & module) ;
242
249
return cache
243
250
. pinned_memory_cache
@@ -295,20 +302,21 @@ where
295
302
let mut cache = self . inner . lock ( ) . unwrap ( ) ;
296
303
// Try to get module from the pinned memory cache
297
304
if let Some ( module) = cache. pinned_memory_cache . load ( checksum) ? {
298
- cache. stats . hits_pinned_memory_cache += 1 ;
305
+ cache. stats . hits_pinned_memory_cache =
306
+ cache. stats . hits_pinned_memory_cache . saturating_add ( 1 ) ;
299
307
return Ok ( module) ;
300
308
}
301
309
302
310
// Get module from memory cache
303
311
if let Some ( module) = cache. memory_cache . load ( checksum) ? {
304
- cache. stats . hits_memory_cache += 1 ;
312
+ cache. stats . hits_memory_cache = cache . stats . hits_memory_cache . saturating_add ( 1 ) ;
305
313
return Ok ( module. module ) ;
306
314
}
307
315
308
316
// Get module from file system cache
309
317
let store = make_runtime_store ( Some ( cache. instance_memory_limit ) ) ;
310
318
if let Some ( module) = cache. fs_cache . load ( checksum, & store) ? {
311
- cache. stats . hits_fs_cache += 1 ;
319
+ cache. stats . hits_fs_cache = cache . stats . hits_fs_cache . saturating_add ( 1 ) ;
312
320
let module_size = loupe:: size_of_val ( & module) ;
313
321
cache
314
322
. memory_cache
@@ -322,7 +330,7 @@ where
322
330
// serialization format. If you do not replay all transactions, previous calls of `save_wasm`
323
331
// stored the old module format.
324
332
let wasm = self . load_wasm_with_path ( & cache. wasm_path , checksum) ?;
325
- cache. stats . misses += 1 ;
333
+ cache. stats . misses = cache . stats . misses . saturating_add ( 1 ) ;
326
334
let module = compile ( & wasm, Some ( cache. instance_memory_limit ) , & [ ] ) ?;
327
335
cache. fs_cache . store ( checksum, & module) ?;
328
336
let module_size = loupe:: size_of_val ( & module) ;
0 commit comments