-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdhat_profile.rs
More file actions
226 lines (178 loc) · 5.74 KB
/
dhat_profile.rs
File metadata and controls
226 lines (178 loc) · 5.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
//! DHAT heap profiler for cachekit.
//!
//! Run with: cargo run --example dhat_profile --release
//! View results: Open dhat-heap.json in <https://nnethercote.github.io/dh_view/dh_view.html>
#[global_allocator]
static ALLOC: dhat::Alloc = dhat::Alloc;
use std::sync::Arc;
use cachekit::policy::clock::ClockCache;
use cachekit::policy::fifo::FifoCache;
use cachekit::policy::lfu::LfuCache;
use cachekit::policy::lru::LruCore;
use cachekit::policy::lru_k::LrukCache;
use cachekit::policy::two_q::TwoQCore;
use cachekit::traits::Cache;
/// Simple XorShift64 RNG for deterministic workloads.
struct XorShift64 {
state: u64,
}
impl XorShift64 {
fn new(seed: u64) -> Self {
Self { state: seed.max(1) }
}
fn next_u64(&mut self) -> u64 {
let mut x = self.state;
x ^= x << 13;
x ^= x >> 7;
x ^= x << 17;
self.state = x;
x
}
fn next_f64(&mut self) -> f64 {
const SCALE: f64 = 1.0 / (u64::MAX as f64);
(self.next_u64() as f64) * SCALE
}
}
/// Run a hotset workload: 90% of accesses hit 10% of keys.
fn hotset_workload<C: Cache<u64, Arc<u64>>>(
cache: &mut C,
operations: usize,
universe: u64,
seed: u64,
) {
let mut rng = XorShift64::new(seed);
let hot_size = (universe as f64 * 0.1) as u64;
for _ in 0..operations {
let key = if rng.next_f64() < 0.9 {
// Hot key (10% of universe, 90% of accesses)
rng.next_u64() % hot_size
} else {
// Cold key
hot_size + (rng.next_u64() % (universe - hot_size))
};
if cache.get(&key).is_none() {
let _ = cache.insert(key, Arc::new(key));
}
}
}
/// Run a scan workload: sequential access pattern.
fn scan_workload<C: Cache<u64, Arc<u64>>>(cache: &mut C, operations: usize, universe: u64) {
for i in 0..operations {
let key = (i as u64) % universe;
if cache.get(&key).is_none() {
let _ = cache.insert(key, Arc::new(key));
}
}
}
/// Run eviction churn: insert more items than capacity.
fn eviction_churn<C: Cache<u64, Arc<u64>>>(cache: &mut C, operations: usize) {
for i in 0..operations {
let _ = cache.insert(i as u64, Arc::new(i as u64));
}
}
fn profile_lru() {
println!("=== Profiling LRU ===");
let capacity = 4096;
let operations = 100_000;
let universe = 16_384;
let mut cache = LruCore::new(capacity);
// Warm up
for i in 0..capacity as u64 {
cache.insert(i, Arc::new(i));
}
// Hotset workload
hotset_workload(&mut cache, operations, universe, 42);
// Scan workload
scan_workload(&mut cache, operations / 2, universe);
// Eviction churn
eviction_churn(&mut cache, operations / 4);
println!(" Final size: {}", cache.len());
}
fn profile_lfu() {
println!("=== Profiling LFU ===");
let capacity = 4096;
let operations = 100_000;
let universe = 16_384;
let mut cache = LfuCache::new(capacity);
for i in 0..capacity as u64 {
cache.insert(i, Arc::new(i));
}
hotset_workload(&mut cache, operations, universe, 42);
scan_workload(&mut cache, operations / 2, universe);
eviction_churn(&mut cache, operations / 4);
println!(" Final size: {}", cache.len());
}
fn profile_fifo() {
println!("=== Profiling FIFO ===");
let capacity = 4096;
let operations = 100_000;
let universe = 16_384;
let mut cache = FifoCache::new(capacity);
for i in 0..capacity as u64 {
cache.insert(i, Arc::new(i));
}
hotset_workload(&mut cache, operations, universe, 42);
scan_workload(&mut cache, operations / 2, universe);
eviction_churn(&mut cache, operations / 4);
println!(" Final size: {}", cache.len());
}
fn profile_lru_k() {
println!("=== Profiling LRU-K ===");
let capacity = 4096;
let operations = 100_000;
let universe = 16_384;
let mut cache = LrukCache::new(capacity);
for i in 0..capacity as u64 {
cache.insert(i, Arc::new(i));
}
hotset_workload(&mut cache, operations, universe, 42);
scan_workload(&mut cache, operations / 2, universe);
eviction_churn(&mut cache, operations / 4);
println!(" Final size: {}", cache.len());
}
fn profile_two_q() {
println!("=== Profiling 2Q ===");
let capacity = 4096;
let operations = 100_000;
let universe = 16_384;
// TwoQCore::new takes (protected_cap, a1_frac)
// a1_frac is the fraction of capacity for A1in queue (typically 0.25)
let mut cache = TwoQCore::new(capacity, 0.25);
for i in 0..capacity as u64 {
cache.insert(i, Arc::new(i));
}
hotset_workload(&mut cache, operations, universe, 42);
scan_workload(&mut cache, operations / 2, universe);
eviction_churn(&mut cache, operations / 4);
println!(" Final size: {}", cache.len());
}
fn profile_clock() {
println!("=== Profiling Clock ===");
let capacity = 4096;
let operations = 100_000;
let universe = 16_384;
let mut cache = ClockCache::new(capacity);
for i in 0..capacity as u64 {
cache.insert(i, Arc::new(i));
}
hotset_workload(&mut cache, operations, universe, 42);
scan_workload(&mut cache, operations / 2, universe);
eviction_churn(&mut cache, operations / 4);
println!(" Final size: {}", cache.len());
}
fn main() {
let _profiler = dhat::Profiler::new_heap();
println!("CacheKit DHAT Heap Profiling");
println!("============================\n");
profile_lru();
profile_lfu();
profile_fifo();
profile_lru_k();
profile_two_q();
profile_clock();
println!("\n============================");
println!("Profiling complete!");
println!(
"View results: Open dhat-heap.json in <https://nnethercote.github.io/dh_view/dh_view.html>"
);
}