interplane.Get Omega
Read in your language

synapse

A component of the AienOS ecosystem.

Brought by drakestapleton

branch main Branches Tags History

synapse / crates / synapse-api / tests / c1_tree_digest.rs

109 lines · 4133 bytes Raw Blame History
1//! C1 (ADR 0003 section 9 step 4): the tree store's byte accounting never falls below the
2//! heap it really holds, so "accounted bytes <= bound" implies "heap <= bound".
3//!
4//! A counting global allocator tracks live heap bytes (requested sizes). This binary holds a
5//! single test so no other test allocates concurrently. The store is driven past its bound
6//! (evicting) with many changeset-like edits of a base tree; after every edit the live heap
7//! attributable to the store is compared with its accounted `total_bytes` and the bound.
8
9use std::alloc::{GlobalAlloc, Layout, System};
10use std::sync::atomic::{AtomicIsize, Ordering};
11use synapse_api::{TreeCacheConfig, TreeEdit, TreeFile, TreeStore};
12use synapse_core::{BlobDigest, FileMode, GitOid, TreeDigest};
13
14struct Counting;
15
16static LIVE: AtomicIsize = AtomicIsize::new(0);
17
18// SAFETY: every call forwards to the system allocator unchanged; only a counter is updated.
19unsafe impl GlobalAlloc for Counting {
20 unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
21 let p = unsafe { System.alloc(layout) };
22 if !p.is_null() {
23 LIVE.fetch_add(layout.size() as isize, Ordering::Relaxed);
24 }
25 p
26 }
27 unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
28 unsafe { System.dealloc(ptr, layout) };
29 LIVE.fetch_sub(layout.size() as isize, Ordering::Relaxed);
30 }
31 unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 {
32 let p = unsafe { System.alloc_zeroed(layout) };
33 if !p.is_null() {
34 LIVE.fetch_add(layout.size() as isize, Ordering::Relaxed);
35 }
36 p
37 }
38 unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
39 let p = unsafe { System.realloc(ptr, layout, new_size) };
40 if !p.is_null() {
41 LIVE.fetch_add(
42 new_size as isize - layout.size() as isize,
43 Ordering::Relaxed,
44 );
45 }
46 p
47 }
48}
49
50#[global_allocator]
51static ALLOC: Counting = Counting;
52
53fn file(i: u64) -> TreeFile {
54 let content = format!("content {i}\n");
55 TreeFile {
56 mode: FileMode::Regular644,
57 blob_digest: BlobDigest::compute(content.as_bytes()),
58 size_bytes: content.len() as u64,
59 git_blob_oid: GitOid::compute_blob(content.as_bytes()),
60 }
61}
62
63fn path(dir: u64, f: u64) -> String {
64 format!(
65 "crates/c{:02}/src/m{:02}/file_{f:04}.rs",
66 dir % 40,
67 dir / 40
68 )
69}
70
71#[test]
72fn c1_tree_store_accounting_covers_the_real_heap_under_eviction() {
73 const MAX: usize = 2 * 1024 * 1024;
74 let live_before = LIVE.load(Ordering::SeqCst);
75 let mut store = TreeStore::new(TreeCacheConfig { max_bytes: MAX }).unwrap();
76 // Base: 4,000 files in 80 directories, pinned like a registered base tree.
77 let mut edit = TreeEdit::new(&mut store, TreeDigest::empty()).unwrap();
78 for i in 0..4_000u64 {
79 edit.upsert(&mut store, &path(i % 80, i), file(i)).unwrap();
80 }
81 let base = edit.finish(&mut store).unwrap();
82 store.pin(base).unwrap();
83 let mut worst_ratio = 0.0f64;
84 for k in 0..2_000u64 {
85 let mut edit = TreeEdit::new(&mut store, base).unwrap();
86 for j in 0..3u64 {
87 let i = (k * 7919 + j * 104_729) % 4_000;
88 edit.upsert(&mut store, &path(i % 80, i), file(1_000_000 + k * 3 + j))
89 .unwrap();
90 }
91 let _root = edit.finish(&mut store).unwrap();
92 let live = (LIVE.load(Ordering::SeqCst) - live_before) as usize;
93 let s = store.stats();
94 assert!(
95 live <= s.total_bytes,
96 "edit {k}: live heap {live} bytes exceeds the accounted {} bytes",
97 s.total_bytes
98 );
99 assert!(
100 s.total_bytes <= MAX,
101 "edit {k}: accounted {} > bound {MAX}",
102 s.total_bytes
103 );
104 worst_ratio = worst_ratio.max(live as f64 / s.total_bytes as f64);
105 }
106 let s = store.stats();
107 assert!(s.nodes_evicted > 0, "the load must exceed the bound: {s:?}");
108 println!("c1 heap accounting: worst live/accounted = {worst_ratio:.3}; final stats {s:?}");
109}