Agent并发神器Fiber体系详解
Fiber(纤程/用户态线程)是一种用户空间的轻量级线程,相比操作系统线程更轻量、更高效,适合高并发场景。核心特点包括用户态调度、极低切换成本(KB级栈)、协作式调度机制,支持任意函数深度挂起(stackful)。Fiber通过避免内核切换实现高性能(仅需几十纳秒),采用M:N调度模型(如10万纤程运行在16个线程上)。现代应用如数据库(ClickHouse Silk)、AI Agent运行时等广
Fiber(纤程 / 用户态线程)本质上是:
用户空间(user-space)的轻量级线程。
它比 OS thread 更轻、更快、更适合高并发调度。
例如:
“Fast stackful fibers with a NUMA-aware work-stealing scheduler”
Agent 使用 ClickHouse silk
是
现代高性能 AI Runtime / Agent Runtime
方向了。
1. Fiber 是什么
传统线程:
OS Thread
由操作系统调度:
kernel scheduler
Fiber:
User-space scheduled execution context
由程序自己调度。
核心特点:
| 特性 | Thread | Fiber |
|---|---|---|
| 调度者 | OS | 用户态 runtime |
| 切换成本 | 高 | 极低 |
| 栈 | MB级 | KB级 |
| 创建成本 | 高 | 很低 |
| 上下文切换 | syscall | user-space |
| 并发数量 | 千级 | 十万级 |
2. Fiber 的核心思想
线程:
OS 抢占式调度
Fiber:
协作式调度(cooperative)
即:
主动 yield
例如:
fiber_yield();
runtime 才会切换。
3. Fiber vs Coroutine
最容易混淆的。
Coroutine
是:
语言级 suspend/resume 机制
例如:
- C++20 coroutine
- Python asyncio coroutine
- Rust async
本身:
不是 scheduler
Fiber
是:
完整执行上下文
包括:
- stack
- registers
- execution context
可以:
任意函数深度 suspend
因此:
stackful
4. Stackful vs Stackless
asyncio / Rust async
属于:
stackless coroutine
只能:
在 await 点 suspend
Fiber
属于:
stackful coroutine
可以:
任意深度 yield
例如:
foo()
-> bar()
-> baz()
-> yield
整个调用栈都会保存。
5. 为什么 Fiber 很快
因为:
不进入内核
线程切换:
save registers
kernel mode
scheduler
TLB/cache disruption
Fiber:
save few registers
switch stack pointer
continue
通常:
几十 ns ~ 数百 ns
6. Fiber 的运行模型
M:N 调度
经典:
M fibers
mapped onto
N threads
例如:
100000 fibers
running on
16 worker threads
7. Fiber Scheduler
核心:
runtime scheduler
负责:
- ready queue
- sleeping queue
- IO wait
- work stealing
- NUMA affinity
8. Fiber + IO
Fiber 最大价值:
同步写法
异步性能
例如:
auto data = socket.read();
看起来阻塞。
实际上:
fiber suspend
thread 去执行别的 fiber
9. Fiber 与 io_uring
现代 Linux 及相关任务黄金组合:
Fiber + io_uring
流程:
fiber 发起 IO
↓
io_uring async submit
↓
fiber suspend
↓
worker 执行别的 fiber
↓
IO completion
↓
fiber resume
这是:
现代高性能 runtime 核心
10. 为什么数据库喜欢 Fiber
例如:
- ClickHouse
- ScyllaDB
- Meta folly
- Tencent libco
因为:
高 IO + 高并发
Fiber 非常适合。
11. ClickHouse Silk
即silk非常先进。
它的方向:
高性能 stackful fibers
+ work stealing
+ NUMA aware
+ low latency
为什么适合 Agent
Agent runtime 本质:
大量微任务
例如:
- tool call
- HTTP
- DB
- websocket
- streaming
- memory retrieval
特点:
IO-bound
task graph
high concurrency
Fiber 非常适合。
12. Fiber 在 Agent Runtime 中的意义
未来 AI infra 正在从:
thread pool
转向:
fiber runtime
因为:
Agent 任务极碎片化
例如:
Planner
↓
Searcher
↓
Retriever
↓
Coder
↓
Executor
每个:
短生命周期
大量等待 IO
Thread 太重。
Fiber 非常合适。
13. Fiber vs asyncio
| 特性 | asyncio | Fiber |
|---|---|---|
| 语言 | Python | 多为 C++ |
| 类型 | stackless | stackful |
| await required | 是 | 否 |
| 调度 | event loop | scheduler |
| 性能 | 高 | 极高 |
| 调试 | 容易 | 较难 |
| 系统级 runtime | 一般 | 很强 |
14. Fiber vs Goroutine
Go goroutine:
本质也是 fiber runtime
Go runtime:
GMP scheduler
就是:
fiber scheduler
15. Fiber vs Tokio
Rust Tokio:
stackless async runtime
不是 fiber。
Rust fiber 类:
- may
- glommio
- monoio
但 Rust 主流:
async/await
16. Fiber 在现代 AI Runtime 的趋势
越来越重要:
推理系统
- vLLM scheduler
- async batching
- token streaming
Agent
- OpenHands
- OpenManus
- OpenClaw
- browser agent
数据系统
- ClickHouse
- Velox
- DuckDB async
Robotics
研究:
- ROS2
- VLA
- 实时 pipeline
未来也会:
fiberized runtime
因为:
sensor IO extremely concurrent
17. Fiber 最大难点
(1) Debug
stack trace 难。
(2) Blocking syscall
如果 fiber 所在线程:
被 syscall 阻塞
整个 worker 卡住。
所以需要:
non-blocking IO
(3) TLS
thread-local storage 问题。
(4) NUMA
跨 NUMA:
cache miss 非常严重
因此:
NUMA-aware scheduler
非常关键。
18. 现代高性能 Fiber Runtime 代表
C++
Go
- goroutine
Rust
- tokio(stackless)
- monoio
- glommio
19. 最佳理解路径
第一阶段
先彻底理解:
asyncio
↓
event loop
↓
cooperative scheduling
第二阶段
再看:
fiber runtime
↓
work stealing
↓
NUMA scheduling
第三阶段
最后:
io_uring
↓
high-performance agent runtime
20. AI趋势
现代 AI infra 正在逐渐演化成:
Distributed Operating System
而:
Fiber Runtime
正在成为:
AI OS 的核心调度层
这也是:
- ClickHouse Silk
- Tokio
- Go runtime
- Seastar
- Ray
- vLLM scheduler
这些系统越来越重要的原因。
openEuler 是由开放原子开源基金会孵化的全场景开源操作系统项目,面向数字基础设施四大核心场景(服务器、云计算、边缘计算、嵌入式),全面支持 ARM、x86、RISC-V、loongArch、PowerPC、SW-64 等多样性计算架构
更多推荐
所有评论(0)