Skip to content

Commit f3f11c9

Browse files
committed
Include the size estimate into the pinned metrics
1 parent 2cc4f97 commit f3f11c9

File tree

1 file changed

+30
-11
lines changed

1 file changed

+30
-11
lines changed

packages/vm/src/cache.rs

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,20 @@ pub struct Metrics {
5353
pub size_memory_cache: usize,
5454
}
5555

56+
#[derive(Debug, Clone)]
57+
pub struct PerModuleMetrics {
58+
/// Hits (i.e. loads) of the module from the cache
59+
pub hits: u32,
60+
/// Size the module takes up in memory
61+
pub size: usize,
62+
}
63+
5664
#[derive(Debug, Clone)]
5765
pub struct PinnedMetrics {
5866
// It is *intentional* that this is only a vector
5967
// We don't need a potentially expensive hashing algorithm here
6068
// The checksums are sourced from a hashmap already, ensuring uniqueness of the checksums
61-
pub hits_per_contract: Vec<(Checksum, u32)>,
69+
pub per_module: Vec<(Checksum, PerModuleMetrics)>,
6270
}
6371

6472
#[derive(Clone, Debug)]
@@ -192,13 +200,20 @@ where
192200

193201
pub fn pinned_metrics(&self) -> PinnedMetrics {
194202
let cache = self.inner.lock().unwrap();
195-
let hits_per_contract = cache
203+
let per_module = cache
196204
.pinned_memory_cache
197205
.iter()
198-
.map(|(checksum, module)| (*checksum, module.hits))
206+
.map(|(checksum, module)| {
207+
let metrics = PerModuleMetrics {
208+
hits: module.hits,
209+
size: module.module.size_estimate,
210+
};
211+
212+
(*checksum, metrics)
213+
})
199214
.collect();
200215

201-
PinnedMetrics { hits_per_contract }
216+
PinnedMetrics { per_module }
202217
}
203218

204219
pub fn metrics(&self) -> Metrics {
@@ -1436,34 +1451,38 @@ mod tests {
14361451
cache.pin(&checksum).unwrap();
14371452

14381453
let pinned_metrics = cache.pinned_metrics();
1439-
assert_eq!(pinned_metrics.hits_per_contract, [(checksum, 0)]);
1454+
assert_eq!(pinned_metrics.per_module.len(), 1);
1455+
assert_eq!(pinned_metrics.per_module[0].0, checksum);
1456+
assert_eq!(pinned_metrics.per_module[0].1.hits, 0);
14401457

14411458
let backend = mock_backend(&[]);
14421459
let _ = cache
14431460
.get_instance(&checksum, backend, TESTING_OPTIONS)
14441461
.unwrap();
14451462

14461463
let pinned_metrics = cache.pinned_metrics();
1447-
assert_eq!(pinned_metrics.hits_per_contract, [(checksum, 1)]);
1464+
assert_eq!(pinned_metrics.per_module.len(), 1);
1465+
assert_eq!(pinned_metrics.per_module[0].0, checksum);
1466+
assert_eq!(pinned_metrics.per_module[0].1.hits, 1);
14481467

14491468
let empty_checksum = cache.save_wasm(EMPTY_CONTRACT).unwrap();
14501469
cache.pin(&empty_checksum).unwrap();
14511470

14521471
let pinned_metrics = cache.pinned_metrics();
1453-
assert_eq!(pinned_metrics.hits_per_contract.len(), 2);
1472+
assert_eq!(pinned_metrics.per_module.len(), 2);
14541473

14551474
let get_module_hits = |checksum| {
14561475
pinned_metrics
1457-
.hits_per_contract
1476+
.per_module
14581477
.iter()
14591478
.find(|(iter_checksum, _module)| *iter_checksum == checksum)
14601479
.map(|(_checksum, module)| module)
1461-
.copied()
1480+
.cloned()
14621481
.unwrap()
14631482
};
14641483

1465-
assert_eq!(get_module_hits(checksum), 1);
1466-
assert_eq!(get_module_hits(empty_checksum), 0);
1484+
assert_eq!(get_module_hits(checksum).hits, 1);
1485+
assert_eq!(get_module_hits(empty_checksum).hits, 0);
14671486
}
14681487

14691488
#[test]

0 commit comments

Comments
 (0)