Skip to content

Commit f167a66

Browse files
committed
Translate some documents
1 parent 2d39c0a commit f167a66

File tree

3 files changed

+77
-13
lines changed

3 files changed

+77
-13
lines changed

core/docs/en/coroutine.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ The above is excerpted from [corosensei](https://github.com/Amanieu/corosensei).
5454
| switch efficiency | ✅ Higher | ❌ High |
5555
| memory usage | ✅ Bytes/KB/MB | ❌ KB/MB |
5656
| scheduled by OS |||
57-
| stack grow |||
57+
| grow stack |||
5858

5959
## Stackfull VS Stackless
6060

hook/docs/cn/hook.md

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
---
2+
title: Hook总览
3+
date: 2025-01-20 10:00:00
4+
author: loongs-zhang
5+
---
6+
7+
# Hook总览
8+
9+
## 为什么hook?
10+
11+
`Coroutine::resume_with`之后,一个协程可能会长时间占用调度线程(例如,陷入重度计算或系统调用),从而拖慢被该线程调度的其他协程。为了解决陷入系统调用的问题,我们引入hook机制,这样当协程进入系统调用时,它会被自动挂起,从而让其他协程执行。
12+
13+
这带来了一个新问题,`preemptive`特性会`发送大量信号`,而`信号会中断正在执行的系统调用`。此外,由于大多数用户在代码中未处理信号,因此如果他们直接使用`open-routine-core`并且启用`preemptive`特性将导致`灾难性后果`
14+
15+
## 什么是hook?
16+
17+
Hook可以通过在运行时插入自定义代码来修改或扩展现有代码的行为,甚至可以监控、拦截、修改和重定向系统调用。现在,让我们用一个[例子](https://github.com/loongs-zhang/link-example)来直观地体验它。
18+
19+
假设我们有以下测试代码:
20+
21+
```rust
22+
use std::time::{Duration, Instant};
23+
24+
#[test]
25+
fn test_hook() {
26+
let start = Instant::now();
27+
std::thread::sleep(Duration::MAX);
28+
let cost = Instant::now().duration_since(start);
29+
println!("cost: {:?}", cost);
30+
}
31+
```
32+
33+
如果我们不hook,因为`std::thread::sleep(Duration::MAX)`,这个测试几乎永远不会结束,但有了hook,我们可以在`不更改测试代码`的情况下将`nanosleep`系统调用重定向到[我们的自定义代码](https://github.com/loongs-zhang/link-example/blob/master/dep/src/lib.rs),然后测试就会[很快结束](https://github.com/loongs-zhang/link-example/actions/runs/12862762378/job/35858206179)
34+
35+
<div style="text-align: center;">
36+
<img src="/hook/docs/img/result-on-macos.png" width="50%">
37+
</div>
38+
39+
## 工作原理
40+
41+
```mermaid
42+
sequenceDiagram
43+
Actor Your Project
44+
participant open-coroutine
45+
participant open-coroutine-hook
46+
participant open-coroutine-core
47+
48+
Your Project ->> open-coroutine: depends on
49+
open-coroutine ->> open-coroutine-hook: depends on
50+
alt at compile time
51+
open-coroutine ->> open-coroutine: build open-coroutine-hook into dylib
52+
open-coroutine ->> open-coroutine: link open-coroutine-hook's dylib
53+
else runtime
54+
Your Project -->> Operation System: logic execute syscall
55+
alt what actually happened
56+
Your Project ->> open-coroutine-hook: redirect syscall to open-coroutine-hook's syscall mod
57+
open-coroutine-hook ->> open-coroutine-core: call open-coroutine-core's syscall mod
58+
open-coroutine-core ->> Operation System: execute fast syscall actually
59+
Operation System ->> open-coroutine-core: return syscall result and errno
60+
open-coroutine-core -->> Operation System: maybe execute fast syscall many times
61+
open-coroutine-core -->> open-coroutine-core: maybe modify syscall result or errno
62+
open-coroutine-core ->> open-coroutine-hook: return syscall result and errno
63+
open-coroutine-hook ->> Your Project: return syscall result and errno
64+
end
65+
Operation System ->> Your Project: return syscall result and errno
66+
end
67+
```

hook/docs/en/hook.md

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,14 @@ author: loongs-zhang
88

99
## Why hook?
1010

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.
11+
After a `Coroutine::resume_with`, a coroutine may occupy the scheduling thread for a long time (e.g. getting stuck in
12+
heavy computing or syscall), thereby slowing down other coroutines scheduled by that scheduling thread. To solve the
13+
problem of getting stuck in syscall, we introduce hook, which automatically suspends coroutines that enter syscall and
14+
allow other coroutines to execute.
1415

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`.
16+
This brings a new problem, the `preemptive` feature will send a large number of signals `which can interrupt the running
17+
syscall`. In addition, most user code does not handle signals, if they directly use `open-routine-core` and enabling the
18+
preemptive feature will lead to `catastrophic consequences`.
2219

2320
## What is hook?
2421

@@ -40,8 +37,8 @@ fn test_hook() {
4037
}
4138
```
4239

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
40+
If we don't hook, because `std::thread::sleep(Duration::MAX)`, this test almost never ends, but with hook, we redirect
41+
the `nanosleep` syscall
4542
to [our custom code](https://github.com/loongs-zhang/link-example/blob/master/dep/src/lib.rs) `without change the test
4643
code`, and then the test
4744
will [end soon](https://github.com/loongs-zhang/link-example/actions/runs/12862762378/job/35858206179).

0 commit comments

Comments
 (0)