-
Notifications
You must be signed in to change notification settings - Fork 182
Expand file tree
/
Copy pathheap_live_tracking.rs
More file actions
152 lines (132 loc) · 4.24 KB
/
Copy pathheap_live_tracking.rs
File metadata and controls
152 lines (132 loc) · 4.24 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
use core::cell::UnsafeCell;
use core::time::Duration;
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use std::time::Instant;
// Compile the production tracker without linking the PHP extension executable.
#[allow(dead_code)]
#[path = "../src/profiling/live_heap.rs"]
mod live_heap;
use live_heap::{LiveHeapTracker, LocalLiveHeapTracker};
const BASE_ADDRESS: usize = 0x1000_0000;
const TRACKED_ALLOCATIONS: usize = 2048;
const ADDRESS_STRIDE: usize = 64;
const BATCH_SIZE: u64 = 256;
fn address(index: usize) -> usize {
BASE_ADDRESS + index % TRACKED_ALLOCATIONS * ADDRESS_STRIDE
}
fn untracked_address(index: usize) -> usize {
address(index) + 8
}
struct Tracker {
shared: LiveHeapTracker<[usize; 4]>,
local: UnsafeCell<LocalLiveHeapTracker>,
}
impl Tracker {
fn new() -> Self {
Self {
shared: LiveHeapTracker::new(),
local: UnsafeCell::new(LocalLiveHeapTracker::new()),
}
}
fn track(&self, ptr: usize, sample: [usize; 4]) -> bool {
// SAFETY: Criterion invokes setup and measured routines sequentially.
unsafe { (&mut *self.local.get()).track(&self.shared, ptr, sample) }
}
fn untrack(&self, ptr: usize) -> Option<[usize; 4]> {
// SAFETY: same sequential access as `track`.
unsafe { (&mut *self.local.get()).untrack(&self.shared, ptr) }
}
}
fn populated_tracker() -> Tracker {
let tracker = Tracker::new();
for index in 0..TRACKED_ALLOCATIONS {
assert!(tracker.track(address(index), [0; 4]));
}
tracker
}
fn measure_batched(
iterations: u64,
mut setup: impl FnMut(usize),
mut routine: impl FnMut(usize),
mut cleanup: impl FnMut(usize),
) -> Duration {
let mut elapsed = Duration::ZERO;
let mut completed = 0;
while completed < iterations {
let count = BATCH_SIZE.min(iterations - completed);
for offset in 0..count {
setup((completed + offset) as usize);
}
let start = Instant::now();
for offset in 0..count {
routine((completed + offset) as usize);
}
elapsed += start.elapsed();
for offset in 0..count {
cleanup((completed + offset) as usize);
}
completed += count;
}
elapsed
}
fn benchmark(c: &mut Criterion) {
let mut group = c.benchmark_group("heap_live_tracking");
{
let tracker = populated_tracker();
group.bench_function("allocate_tracked", |b| {
b.iter_custom(|iterations| {
measure_batched(
iterations,
|index| {
let _ = tracker.untrack(untracked_address(index));
},
|index| {
black_box(tracker.track(untracked_address(index), [0; 4]));
},
|index| {
let _ = tracker.untrack(untracked_address(index));
},
)
})
});
}
{
let tracker = populated_tracker();
group.bench_function("free_tracked", |b| {
b.iter_custom(|iterations| {
measure_batched(
iterations,
|index| {
assert!(tracker.track(untracked_address(index), [0; 4]));
},
|index| {
black_box(tracker.untrack(untracked_address(index)));
},
|index| {
let _ = tracker.untrack(untracked_address(index));
},
)
})
});
}
{
let tracker = populated_tracker();
group.bench_function("free_untracked", |b| {
b.iter_custom(|iterations| {
measure_batched(
iterations,
|index| {
let _ = tracker.untrack(untracked_address(index));
},
|index| {
black_box(tracker.untrack(untracked_address(index)));
},
|_| {},
)
})
});
}
group.finish();
}
criterion_group!(benches, benchmark);
criterion_main!(benches);