Skip to content

Commit 6851e69

Browse files
committed
add hook doc
1 parent 87ab737 commit 6851e69

File tree

6 files changed

+87
-5
lines changed

6 files changed

+87
-5
lines changed

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ English | [中文](README_ZH.md)
3232

3333
## 🕊 Roadmap
3434

35-
- [ ] add docs;
3635
- [ ] add
3736
performance [benchmark](https://github.com/TechEmpower/FrameworkBenchmarks/wiki/Project-Information-Framework-Tests-Overview);
3837
- [ ] cancel coroutine/task;
@@ -194,7 +193,7 @@ fn main() {
194193
- [Coroutine Pool Overview](core/docs/en/coroutine-pool.md)
195194
- [Hook Overview](hook/docs/en/hook.md)
196195

197-
[我有故事,你有酒吗?](https://github.com/acl-dev/open-coroutine-docs)
196+
[Old Docs Here](https://github.com/acl-dev/open-coroutine-docs)
198197

199198
## 👍 Credits
200199

README_ZH.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828

2929
## 🕊 未来计划
3030

31-
- [ ] 完善文档;
3231
- [ ]
3332
增加性能[基准测试](https://github.com/TechEmpower/FrameworkBenchmarks/wiki/Project-Information-Framework-Tests-Overview);
3433
- [ ] 取消协程/任务;
@@ -185,7 +184,7 @@ fn main() {
185184
- [诞生之因](docs/cn/background.md)
186185
- [语言选择](docs/cn/why-rust.md)
187186

188-
[我有故事,你有酒吗?](https://github.com/acl-dev/open-coroutine-docs)
187+
[旧版文档在这](https://github.com/acl-dev/open-coroutine-docs)
189188

190189
## 👍 鸣谢
191190

core/docs/en/monitor.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ The `preemptive` feature currently supports the following targets:
2121

2222
✅ Tested and stable; ⚠️ Tested but unstable; ❌ Not supported.
2323

24+
⚠️ If you want to use `preemptive` feature with `open-coroutine-core` not `open-coroutine`, you must learn
25+
[Hook Overview](../../../hook/docs/en/hook.md).
26+
2427
## Usage
2528

2629
```rust

hook/docs/en/hook.md

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,81 @@
1-
todo
1+
---
2+
title: Hook Overview
3+
date: 2025-01-20 10:00:00
4+
author: loongs-zhang
5+
---
6+
7+
# Hook Overview
8+
9+
## Why hook?
10+
11+
After a `Coroutine::resume_with`, a coroutine may occupy the scheduling thread for a long time, thereby slowing down
12+
other coroutines scheduled by that scheduling thread. To solve this problem, we introduce hook, which automatically
13+
suspends coroutines entering syscall and allow other coroutines to execute.
14+
15+
The coroutine occupies scheduling threads for a long time in two scenarios: getting stuck in heavy computing or syscall.
16+
The following only solves the problem of getting stuck in syscall.
17+
18+
Another problem is that `signals can interrupt the running syscall`, and the `preemptive` feature mechanism sends a
19+
large
20+
number of signals. In addition, most user code does not handle signals, if they directly use `open-routine-core` and
21+
enabling preemptive will lead to `catastrophic consequences`.
22+
23+
## What is hook?
24+
25+
Hook can modify or extend the behavior of existing code by inserting custom code at runtime, and even monitor,
26+
intercept, modify, and redirect system calls. Now, let's use an [example](https://github.com/loongs-zhang/link-example)
27+
to visually experience it.
28+
29+
Assuming we have the following test code:
30+
31+
```rust
32+
use std::time::{Duration, Instant};
33+
34+
#[test]
35+
fn test_hook() {
36+
let start = Instant::now();
37+
std::thread::sleep(Duration::MAX);
38+
let cost = Instant::now().duration_since(start);
39+
println!("cost: {:?}", cost);
40+
}
41+
```
42+
43+
If we don't hook, this test would almost never end due to `std::thread::sleep(Duration::MAX)`, but with hook, we
44+
redirect the `nanosleep` syscall
45+
to [our custom code](https://github.com/loongs-zhang/link-example/blob/master/dep/src/lib.rs) `without change the test
46+
code`, and then the test
47+
will [end soon](https://github.com/loongs-zhang/link-example/actions/runs/12862762378/job/35858206179).
48+
49+
<div style="text-align: center;">
50+
<img src="/hook/docs/img/result-on-macos.png" width="50%">
51+
</div>
52+
53+
## How it works
54+
55+
```mermaid
56+
sequenceDiagram
57+
Actor Your Project
58+
participant open-coroutine
59+
participant open-coroutine-hook
60+
participant open-coroutine-core
61+
62+
Your Project ->> open-coroutine: depends on
63+
open-coroutine ->> open-coroutine-hook: depends on
64+
alt at compile time
65+
open-coroutine ->> open-coroutine: build open-coroutine-hook into dylib
66+
open-coroutine ->> open-coroutine: link open-coroutine-hook's dylib
67+
else runtime
68+
Your Project -->> Operation System: logic execute syscall
69+
alt what actually happened
70+
Your Project ->> open-coroutine-hook: redirect syscall to open-coroutine-hook's syscall mod
71+
open-coroutine-hook ->> open-coroutine-core: call open-coroutine-core's syscall mod
72+
open-coroutine-core ->> Operation System: execute fast syscall actually
73+
Operation System ->> open-coroutine-core: return syscall result and errno
74+
open-coroutine-core -->> Operation System: maybe execute fast syscall many times
75+
open-coroutine-core -->> open-coroutine-core: maybe modify syscall result or errno
76+
open-coroutine-core ->> open-coroutine-hook: return syscall result and errno
77+
open-coroutine-hook ->> Your Project: return syscall result and errno
78+
end
79+
Operation System ->> Your Project: return syscall result and errno
80+
end
81+
```

hook/docs/img/result-on-macos.png

296 KB
Loading

hook/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
clippy::indexing_slicing,
4545
clippy::separated_literal_suffix, // conflicts with clippy::unseparated_literal_suffix
4646
clippy::single_char_lifetime_names, // TODO: change lifetime names
47+
clippy::test_attr_in_doctest,
4748
)]
4849
#![doc = include_str!("../docs/en/hook.md")]
4950

0 commit comments

Comments
 (0)