-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathunwind.rs
More file actions
150 lines (137 loc) · 3.84 KB
/
unwind.rs
File metadata and controls
150 lines (137 loc) · 3.84 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
//! Benchmark for the frame-pointer unwinder.
//!
//! Measures latency of walking a real frame-pointer chain of known depth.
//! Run with: RUSTFLAGS="-C force-frame-pointers=yes" cargo bench --bench unwind --features __internal-bench
//!
//! Requires Linux x86_64 or aarch64.
#[cfg(not(all(
target_os = "linux",
any(target_arch = "x86_64", target_arch = "aarch64")
)))]
fn main() {}
#[cfg(all(
target_os = "linux",
any(target_arch = "x86_64", target_arch = "aarch64")
))]
use std::arch::asm;
#[cfg(all(
target_os = "linux",
any(target_arch = "x86_64", target_arch = "aarch64")
))]
use std::hint::black_box;
#[cfg(all(
target_os = "linux",
any(target_arch = "x86_64", target_arch = "aarch64")
))]
use criterion::Criterion;
#[cfg(all(
target_os = "linux",
any(target_arch = "x86_64", target_arch = "aarch64")
))]
use dial9_perf_self_profile::__bench_internals::{install_handler, unwind};
#[cfg(all(
target_os = "linux",
any(target_arch = "x86_64", target_arch = "aarch64")
))]
fn main() {
let mut criterion = Criterion::default().configure_from_args();
bench_unwind_20(&mut criterion);
bench_unwind_5(&mut criterion);
criterion.final_summary();
}
/// Read the current frame pointer, stack pointer, and instruction pointer.
#[cfg(all(target_os = "linux", target_arch = "x86_64"))]
#[inline(always)]
fn read_registers() -> (usize, usize, usize) {
let fp: usize;
let sp: usize;
let pc: usize;
unsafe {
asm!(
"mov {fp}, rbp",
"mov {sp}, rsp",
"lea {pc}, [rip]",
fp = out(reg) fp,
sp = out(reg) sp,
pc = out(reg) pc,
);
}
(pc, fp, sp)
}
/// Read the current frame pointer, stack pointer, and instruction pointer.
#[cfg(all(target_os = "linux", target_arch = "aarch64"))]
#[inline(always)]
fn read_registers() -> (usize, usize, usize) {
let fp: usize;
let sp: usize;
let pc: usize;
unsafe {
asm!(
"mov {fp}, x29",
"mov {sp}, sp",
"adr {pc}, .",
fp = out(reg) fp,
sp = out(reg) sp,
pc = out(reg) pc,
);
}
(pc, fp, sp)
}
/// Perform the unwind and return the number of frames walked.
#[cfg(all(
target_os = "linux",
any(target_arch = "x86_64", target_arch = "aarch64")
))]
#[inline(never)]
fn do_unwind() -> usize {
let (pc, fp, sp) = read_registers();
let mut out = [0u64; 128];
let (n, _truncated) = unsafe { unwind(pc, fp, sp, &mut out) };
n
}
/// Build a chain of exactly N inline(never) frames via recursion.
#[cfg(all(
target_os = "linux",
any(target_arch = "x86_64", target_arch = "aarch64")
))]
#[inline(never)]
fn recurse(depth: u32) -> usize {
if depth == 0 {
black_box(do_unwind())
} else {
black_box(recurse(depth - 1))
}
}
#[cfg(all(
target_os = "linux",
any(target_arch = "x86_64", target_arch = "aarch64")
))]
fn bench_unwind_20(c: &mut Criterion) {
unsafe { install_handler().expect("failed to install SIGSEGV handler") };
// Verify we actually get enough frames
let frames = recurse(20);
assert!(
frames >= 15,
"expected at least 15 frames from 20-deep recursion, got {frames}"
);
eprintln!("20-frame bench: unwinder walked {frames} frames");
c.bench_function("unwind_20_frames", |b| {
b.iter(|| black_box(recurse(20)));
});
}
#[cfg(all(
target_os = "linux",
any(target_arch = "x86_64", target_arch = "aarch64")
))]
fn bench_unwind_5(c: &mut Criterion) {
// Verify we get frames
let frames = recurse(5);
assert!(
frames >= 4,
"expected at least 4 frames from 5-deep recursion, got {frames}"
);
eprintln!("5-frame bench: unwinder walked {frames} frames");
c.bench_function("unwind_5_frames", |b| {
b.iter(|| black_box(recurse(5)));
});
}