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
|
#![cfg(all(
tokio_unstable,
tokio_taskdump,
target_os = "linux",
any(target_arch = "aarch64", target_arch = "x86", target_arch = "x86_64")
))]
use std::hint::black_box;
use tokio::runtime::{self, Handle};
#[inline(never)]
async fn a() {
black_box(b()).await
}
#[inline(never)]
async fn b() {
black_box(c()).await
}
#[inline(never)]
async fn c() {
loop {
black_box(tokio::task::yield_now()).await
}
}
#[test]
fn current_thread() {
let rt = runtime::Builder::new_current_thread()
.enable_all()
.build()
.unwrap();
async fn dump() {
let handle = Handle::current();
let dump = handle.dump().await;
let tasks: Vec<_> = dump.tasks().iter().collect();
assert_eq!(tasks.len(), 3);
for task in tasks {
let trace = task.trace().to_string();
eprintln!("\n\n{trace}\n\n");
assert!(trace.contains("dump::a"));
assert!(trace.contains("dump::b"));
assert!(trace.contains("dump::c"));
assert!(trace.contains("tokio::task::yield_now"));
}
}
rt.block_on(async {
tokio::select!(
biased;
_ = tokio::spawn(a()) => {},
_ = tokio::spawn(a()) => {},
_ = tokio::spawn(a()) => {},
_ = dump() => {},
);
});
}
#[test]
fn multi_thread() {
let rt = runtime::Builder::new_multi_thread()
.enable_all()
.worker_threads(3)
.build()
.unwrap();
async fn dump() {
let handle = Handle::current();
let dump = handle.dump().await;
let tasks: Vec<_> = dump.tasks().iter().collect();
assert_eq!(tasks.len(), 3);
for task in tasks {
let trace = task.trace().to_string();
eprintln!("\n\n{trace}\n\n");
assert!(trace.contains("dump::a"));
assert!(trace.contains("dump::b"));
assert!(trace.contains("dump::c"));
assert!(trace.contains("tokio::task::yield_now"));
}
}
rt.block_on(async {
tokio::select!(
biased;
_ = tokio::spawn(a()) => {},
_ = tokio::spawn(a()) => {},
_ = tokio::spawn(a()) => {},
_ = dump() => {},
);
});
}
|