Skip to content

Commit 120886b

Browse files
committed
refactor stapsdt handling
1 parent 32e529f commit 120886b

File tree

4 files changed

+293
-690
lines changed

4 files changed

+293
-690
lines changed

tokio/src/util/usdt.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,8 @@ cfg_rt! {
2929
#[path = "usdt/macos.rs"]
3030
pub(crate) mod usdt_impl;
3131

32-
#[cfg(all(target_os = "linux", target_arch = "x86_64"))]
33-
#[path = "usdt/stapsdt_x86_64.rs"]
34-
pub(crate) mod usdt_impl;
35-
36-
#[cfg(all(target_os = "linux", target_arch = "aarch64"))]
37-
#[path = "usdt/stapsdt_aarch64.rs"]
32+
#[cfg(all(target_os = "linux", any(target_arch = "x86_64", target_arch = "aarch64")))]
33+
#[path = "usdt/stapsdt.rs"]
3834
pub(crate) mod usdt_impl;
3935

4036
#[cfg(not(any(

tokio/src/util/usdt/stapsdt.rs

Lines changed: 291 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,291 @@
1+
//! USDT support on linux, and other platforms that use the SystemTap SDT V3 system.
2+
3+
#[cfg(target_arch = "x86_64")]
4+
macro_rules! call_probe {
5+
(
6+
$name:literal,
7+
x86_64: $x86_64:literal,
8+
aarch64: $aarch64:literal,
9+
$($args:tt)*
10+
) => {
11+
std::arch::asm!(
12+
"990: nop
13+
.pushsection .note.stapsdt, \"\", \"note\"
14+
.balign 4
15+
.4byte 992f-991f, 994f-993f, 3
16+
991:
17+
.asciz \"stapsdt\"
18+
992:
19+
.balign 4
20+
993:
21+
.8byte 990b
22+
.8byte _.stapsdt.base
23+
.8byte {semaphore}
24+
.asciz \"tokio\"",
25+
concat!(".asciz \"", $name, "\""),
26+
concat!(".asciz \"", $x86_64, "\""),
27+
"994:
28+
.balign 4
29+
.popsection",
30+
$($args)*
31+
options(att_syntax, readonly, nostack, preserves_flags),
32+
);
33+
};
34+
}
35+
36+
#[cfg(target_arch = "aarch64")]
37+
macro_rules! call_probe {
38+
(
39+
$name:literal,
40+
x86_64: $x86_64:literal,
41+
aarch64: $aarch64:literal,
42+
$($args:tt)*
43+
) => {
44+
std::arch::asm!(
45+
"990: nop
46+
.pushsection .note.stapsdt, \"\", \"note\"
47+
.balign 4
48+
.4byte 992f-991f, 994f-993f, 3
49+
991:
50+
.asciz \"stapsdt\"
51+
992:
52+
.balign 4
53+
993:
54+
.8byte 990b
55+
.8byte _.stapsdt.base
56+
.8byte {semaphore}
57+
.asciz \"tokio\"",
58+
concat!(".asciz \"", $name, "\""),
59+
concat!(".asciz \"", $aarch64, "\""),
60+
"994:
61+
.balign 4
62+
.popsection",
63+
$($args)*
64+
options(readonly, nostack, preserves_flags),
65+
);
66+
};
67+
}
68+
69+
#[inline(always)]
70+
pub(super) fn task_details(task_id: u64, name: &str, file: &str, line: u32, col: u32) {
71+
#[cold]
72+
fn task_details_inner(task_id: u64, name: &str, file: &str, line: u32, col: u32) {
73+
// add nul bytes
74+
let name0 = [name.as_bytes(), b"\0"].concat();
75+
let file0 = [file.as_bytes(), b"\0"].concat();
76+
77+
unsafe {
78+
call_probe!(
79+
"task-details",
80+
x86_64: "8@{task_id:r} 8@{name} 8@{file} 4@{line:e} 4@{col:e}",
81+
aarch64: "8@{task_id} 8@{name} 8@{file} 4@{line:w} 4@{col:w}",
82+
semaphore = sym __usdt_sema_tokio_task__details,
83+
task_id = in(reg) task_id,
84+
name = in(reg) name0.as_ptr(),
85+
file = in(reg) file0.as_ptr(),
86+
line = in(reg) line,
87+
col = in(reg) col,
88+
);
89+
}
90+
}
91+
92+
if unsafe { __usdt_sema_tokio_task__details } != 0 {
93+
task_details_inner(task_id, name, file, line, col);
94+
}
95+
}
96+
97+
#[inline(always)]
98+
pub(super) fn task_start(task_id: u64, kind: u8, size: usize, original_size: usize) {
99+
unsafe {
100+
call_probe!(
101+
"task-start",
102+
x86_64: "8@{task_id:r} 1@{kind:l} 8@{size} 8@{original_size}",
103+
aarch64: "8@{task_id} 1@{kind:w} 8@{size} 8@{original_size}",
104+
semaphore = const 0,
105+
task_id = in(reg) task_id,
106+
kind = in(reg) kind as u32,
107+
size = in(reg) size,
108+
original_size = in(reg) original_size,
109+
);
110+
}
111+
}
112+
113+
#[inline(always)]
114+
pub(super) fn task_poll_start(task_id: u64) {
115+
#[inline(never)]
116+
fn probe_inner(task_id: u64) {
117+
unsafe {
118+
call_probe!(
119+
"task-poll-start",
120+
x86_64: "8@{task_id:r}",
121+
aarch64: "8@{task_id}",
122+
semaphore = sym __usdt_sema_tokio_task__poll__start,
123+
task_id = in(reg) task_id,
124+
);
125+
}
126+
}
127+
128+
if unsafe { __usdt_sema_tokio_task__poll__start } != 0 {
129+
probe_inner(task_id);
130+
}
131+
}
132+
133+
#[inline(always)]
134+
pub(super) fn task_poll_end(task_id: u64) {
135+
#[inline(never)]
136+
fn probe_inner(task_id: u64) {
137+
unsafe {
138+
call_probe!(
139+
"task-poll-end",
140+
x86_64: "8@{task_id:r}",
141+
aarch64: "8@{task_id}",
142+
semaphore = sym __usdt_sema_tokio_task__poll__end,
143+
task_id = in(reg) task_id,
144+
);
145+
}
146+
}
147+
148+
if unsafe { __usdt_sema_tokio_task__poll__end } != 0 {
149+
probe_inner(task_id);
150+
}
151+
}
152+
153+
#[inline(always)]
154+
pub(super) fn task_terminate(task_id: u64, reason: u8) {
155+
#[inline(never)]
156+
fn probe_inner(task_id: u64, reason: u8) {
157+
unsafe {
158+
call_probe!(
159+
"task-terminate",
160+
x86_64: "8@{task_id:r} 1@{reason:l}",
161+
aarch64: "8@{task_id} 1@{reason:w}",
162+
semaphore = sym __usdt_sema_tokio_task__terminate,
163+
task_id = in(reg) task_id,
164+
reason = in(reg) reason as u32,
165+
);
166+
}
167+
}
168+
169+
if unsafe { __usdt_sema_tokio_task__terminate } != 0 {
170+
probe_inner(task_id, reason);
171+
}
172+
}
173+
174+
#[inline(always)]
175+
pub(crate) fn waker_clone(task_id: u64) {
176+
unsafe {
177+
call_probe!(
178+
"task-waker-clone",
179+
x86_64: "8@{task_id:r}",
180+
aarch64: "8@{task_id}",
181+
semaphore = const 0,
182+
task_id = in(reg) task_id,
183+
);
184+
}
185+
}
186+
187+
#[inline(always)]
188+
pub(crate) fn waker_drop(task_id: u64) {
189+
unsafe {
190+
call_probe!(
191+
"task-waker-drop",
192+
x86_64: "8@{task_id:r}",
193+
aarch64: "8@{task_id}",
194+
semaphore = const 0,
195+
task_id = in(reg) task_id,
196+
);
197+
}
198+
}
199+
200+
#[inline(always)]
201+
pub(crate) fn waker_wake(task_id: u64) {
202+
unsafe {
203+
call_probe!(
204+
"task-waker-wake",
205+
x86_64: "8@{task_id:r}",
206+
aarch64: "8@{task_id}",
207+
semaphore = const 0,
208+
task_id = in(reg) task_id,
209+
);
210+
}
211+
}
212+
213+
// if defined, attached probes will increment these counters.
214+
unsafe extern "C" {
215+
static mut __usdt_sema_tokio_task__details: u16;
216+
static mut __usdt_sema_tokio_task__poll__start: u16;
217+
static mut __usdt_sema_tokio_task__poll__end: u16;
218+
static mut __usdt_sema_tokio_task__terminate: u16;
219+
}
220+
221+
std::arch::global_asm!(
222+
".ifndef {semaphore}
223+
.pushsection .probes, \"aw\", \"progbits\"
224+
.weak {semaphore}
225+
.hidden {semaphore}
226+
{semaphore}:
227+
.zero 2
228+
.type {semaphore}, @object
229+
.size {semaphore}, 2
230+
.popsection
231+
.endif",
232+
semaphore = sym __usdt_sema_tokio_task__details
233+
);
234+
235+
std::arch::global_asm!(
236+
".ifndef {semaphore}
237+
.pushsection .probes, \"aw\", \"progbits\"
238+
.weak {semaphore}
239+
.hidden {semaphore}
240+
{semaphore}:
241+
.zero 2
242+
.type {semaphore}, @object
243+
.size {semaphore}, 2
244+
.popsection
245+
.endif",
246+
semaphore = sym __usdt_sema_tokio_task__poll__start
247+
);
248+
249+
std::arch::global_asm!(
250+
".ifndef {semaphore}
251+
.pushsection .probes, \"aw\", \"progbits\"
252+
.weak {semaphore}
253+
.hidden {semaphore}
254+
{semaphore}:
255+
.zero 2
256+
.type {semaphore}, @object
257+
.size {semaphore}, 2
258+
.popsection
259+
.endif",
260+
semaphore = sym __usdt_sema_tokio_task__poll__end
261+
);
262+
263+
std::arch::global_asm!(
264+
".ifndef {semaphore}
265+
.pushsection .probes, \"aw\", \"progbits\"
266+
.weak {semaphore}
267+
.hidden {semaphore}
268+
{semaphore}:
269+
.zero 2
270+
.type {semaphore}, @object
271+
.size {semaphore}, 2
272+
.popsection
273+
.endif",
274+
semaphore = sym __usdt_sema_tokio_task__terminate
275+
);
276+
277+
std::arch::global_asm!(
278+
".ifndef _.stapsdt.base
279+
.pushsection .stapsdt.base, \"aGR\", \"progbits\", .stapsdt.base, comdat
280+
.weak _.stapsdt.base
281+
.hidden _.stapsdt.base
282+
_.stapsdt.base:
283+
.space 1
284+
.size _.stapsdt.base, 1
285+
.popsection
286+
.endif",
287+
);
288+
289+
pub(crate) fn register_probes() -> std::io::Result<()> {
290+
Ok(())
291+
}

0 commit comments

Comments
 (0)