Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
a5cdfc1
add the progressive read idle timout checker
zchuango Nov 25, 2025
ae7bc0f
modify the bug for http c++ example
zchuango Nov 25, 2025
e41c37b
Merge branch 'apache:master' into master
zchuango Nov 28, 2025
f08f266
change the timeout checker bthread to timer bthread
zchuango Nov 28, 2025
f373b45
Merge branch 'apache:master' into master
zchuango Dec 3, 2025
9bd8016
refine ProgressiveReadTimeoutReader class hold SocketId and read_time…
zchuango Dec 3, 2025
8830eab
Prog read timeout dev merge (#4)
zchuango Dec 5, 2025
6f301da
Prog read timeout dev (#5)
zchuango Dec 10, 2025
f46c3b3
Merge branch 'apache:master' into master
zchuango Jan 13, 2026
cbdbb52
Merge branch 'apache:master' into master
zchuango Jan 26, 2026
c3b095f
Merge branch 'apache:master' into master
zchuango Apr 11, 2026
8650f6a
add ubring transport
zchuango Apr 13, 2026
c7a85d4
fix the bug for ub ring transport
zchuango Apr 14, 2026
58a7755
fix the bug for ub ring transport and other
zchuango Apr 14, 2026
bc58224
add the license for ub transport
zchuango Apr 14, 2026
dbb2dad
Modifying the variable naming style
zchuango Apr 14, 2026
5231ae6
optimize the log message and some field name
zchuango Apr 19, 2026
a813d0f
fix some bug for ubring
zchuango Apr 20, 2026
1e9b102
add some log ubring endpoint
zchuango Apr 23, 2026
81051e7
add todo
zchuango May 6, 2026
80dfbec
fix the bug for handshake for ub endpoint
zchuango May 7, 2026
ac09159
fix the bug for client ub endpoint
zchuango May 7, 2026
60cf8cb
optimize the iobuf file code
zchuango May 7, 2026
599d970
modify the log level
zchuango May 8, 2026
b59bcc8
fix the declare_shm_ubs define not found bug
zchuango May 8, 2026
0d17836
add the timer_mgr support for macos and format code style
zchuango May 8, 2026
ca6ccbb
fix the bug for macos epoll
zchuango May 8, 2026
96ad454
fix the timespece bug
zchuango May 8, 2026
1af2c4a
adaptor the itimerspec for macos platform
zchuango May 9, 2026
749a82d
optimize the cmakelist config
zchuango May 9, 2026
6c06fd4
Merge branch 'apache:master' into master
zchuango May 9, 2026
5a7c90f
Merge branch 'master' into ub_ring_transport_dev
zchuango May 9, 2026
423ff7d
add the ubring docs for ubring transport
zchuango May 9, 2026
4bd8328
modify some file name and directory structure
zchuango May 9, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ option(WITH_THRIFT "With thrift framed protocol supported" OFF)
option(WITH_BTHREAD_TRACER "With bthread tracer supported" OFF)
option(WITH_SNAPPY "With snappy" OFF)
option(WITH_RDMA "With RDMA" OFF)
option(WITH_UBRING "With UB" OFF)
option(WITH_DEBUG_BTHREAD_SCHE_SAFETY "With debugging bthread sche safety" OFF)
option(WITH_DEBUG_LOCK "With debugging lock" OFF)
option(WITH_ASAN "With AddressSanitizer" OFF)
Expand Down Expand Up @@ -104,6 +105,11 @@ if(WITH_RDMA)
set(WITH_RDMA_VAL "1")
endif()

set(WITH_UBRING_VAL "0")
if(WITH_UBRING)
set(WITH_UBRING_VAL "1")
endif()

set(WITH_DEBUG_BTHREAD_SCHE_SAFETY_VAL "0")
if(WITH_DEBUG_BTHREAD_SCHE_SAFETY)
set(WITH_DEBUG_BTHREAD_SCHE_SAFETY_VAL "1")
Expand Down Expand Up @@ -136,7 +142,7 @@ if(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
set(CMAKE_CPP_FLAGS "${CMAKE_CPP_FLAGS} -Wno-deprecated-declarations -Wno-inconsistent-missing-override")
endif()

set(CMAKE_CPP_FLAGS "${CMAKE_CPP_FLAGS} ${DEFINE_CLOCK_GETTIME} -DBRPC_WITH_GLOG=${WITH_GLOG_VAL} -DBRPC_WITH_RDMA=${WITH_RDMA_VAL} -DBRPC_DEBUG_BTHREAD_SCHE_SAFETY=${WITH_DEBUG_BTHREAD_SCHE_SAFETY_VAL} -DBRPC_DEBUG_LOCK=${WITH_DEBUG_LOCK_VAL}")
set(CMAKE_CPP_FLAGS "${CMAKE_CPP_FLAGS} ${DEFINE_CLOCK_GETTIME} -DBRPC_WITH_GLOG=${WITH_GLOG_VAL} -DBRPC_WITH_RDMA=${WITH_RDMA_VAL} -DBRPC_WITH_UBRING=${WITH_UBRING_VAL} -DBRPC_DEBUG_BTHREAD_SCHE_SAFETY=${WITH_DEBUG_BTHREAD_SCHE_SAFETY_VAL} -DBRPC_DEBUG_LOCK=${WITH_DEBUG_LOCK_VAL}")
if (WITH_ASAN)
set(CMAKE_CPP_FLAGS "${CMAKE_CPP_FLAGS} -fsanitize=address")
set(CMAKE_C_FLAGS "${CMAKE_CPP_FLAGS} -fsanitize=address")
Expand Down Expand Up @@ -322,6 +328,11 @@ if(WITH_RDMA)
list(APPEND DYNAMIC_LIB ${RDMA_LIB})
endif()

if(WITH_UBRING)
message(STATUS "brpc compile with ubring")
list(APPEND DYNAMIC_LIB ${UB_LIB})
endif()

set(BRPC_PRIVATE_LIBS "-lgflags -lprotobuf -lleveldb -lprotoc -lssl -lcrypto -ldl -lz")

if(WITH_GLOG)
Expand Down Expand Up @@ -564,6 +575,7 @@ set(SOURCES
${MCPACK2PB_SOURCES}
${BRPC_SOURCES}
${THRIFT_SOURCES}
${BRPC_C_SOURCES}
)

add_subdirectory(src)
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ You can use it to:
* [FlatMap](docs/en/flatmap.md)
* [Coroutine](docs/en/coroutine.md)
* [Circuit Breaker](docs/en/circuit_breaker.md)
* [UBRing](docs/en/ubring.md)
* [RDMA](docs/en/rdma.md)
* [Bazel Support](docs/en/bazel_support.md)
* [Wireshark baidu_std dissector plugin](docs/en/wireshark_baidu_std.md)
Expand Down
1 change: 1 addition & 0 deletions README_cn.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
* [FlatMap](docs/cn/flatmap.md)
* [协程](docs/cn/coroutine.md)
* [熔断](docs/cn/circuit_breaker.md)
* [UBRing](docs/cn/ubring.md)
* [RDMA](docs/cn/rdma.md)
* [Bazel构建支持](docs/cn/bazel_support.md)
* [Wireshark baidu_std协议解析插件](docs/cn/wireshark_baidu_std.md)
Expand Down
184 changes: 184 additions & 0 deletions docs/cn/ubring.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
# UBRing: 高性能共享内存 RPC

UBRing 是 brpc 中的高性能 RPC 实现,它利用共享内存进行进程间通信(IPC)。它支持本地共享内存(POSIX IPC)和远端共享内存(ubs-mem)两种模式,提供微秒到纳秒级的进程间通信延迟。

## 技术背景

传统的 RPC 框架通常使用网络套接字进行通信,由于内核参与、上下文切换和数据拷贝等原因,会引入显著的开销。UBRing 通过使用共享内存作为通信介质来解决这个问题,允许进程之间直接内存访问,最小化内核干预。

UBRing 的主要优势:

- **超低延迟**:微秒级 RPC 延迟
- **高吞吐量**:每秒数百万次 RPC 调用
- **减少数据拷贝**:进程间直接内存访问
- **跨平台支持**:支持 Linux 和 macOS

## 支持的共享内存后端

UBRing 支持两种共享内存后端,通过 `ub_shm_type` 参数控制:

### 1. POSIX IPC 共享内存 (ub\_shm\_type = 1)

这是默认模式,使用标准 POSIX 共享内存进行本地 IPC。同一机器上的进程可以通过共享内存区域直接通信。

### 2. UBS-Mem 远端共享内存 (ub\_shm\_type = 2)

此模式使用 ubs-mem(Unified Block Storage Memory),这是来自 openEuler 的开源远端共享内存框架。它支持机架内节点之间的共享内存通信,类似于 RDMA 但部署要求更简单。

**UBS-Mem 开源地址**: <https://atomgit.com/openeuler/ubs-mem>

### 未来扩展

该架构设计支持未来扩展 CXL(Compute Express Link)基于的远端共享内存,实现更灵活的分布式内存共享。

## 构建配置

### 使用 CMake 构建

要构建带有 UBRing 支持的 brpc,请使用以下命令:

```bash
# 构建 brpc 并启用 UBRing 支持
cd /path/to/brpc
cmake -B build -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -DWITH_UBRING:BOOL=ON
cmake --build build -j 8

# 构建 ubring_performance 示例
cd /path/to/brpc/example/ubring_performance
cmake -B build
cmake --build build -j 8
```

### 使用 Bazel 构建

使用 Bazel 构建带有 UBRing 支持的 brpc:

```bash
# 构建 brpc 并启用 UBRing 支持
cd /path/to/brpc
bazel build //... --define=with_ubring=true

# 构建 ubring_performance 示例
bazel build //example/ubring_performance/...
```

### 选择共享内存后端

共享内存后端通过 `--ub_shm_type` 参数控制:

```bash
# 使用 POSIX IPC(默认)
./your_program --ub_shm_type=1

# 使用 UBS-Mem
./your_program --ub_shm_type=2
```

## 性能测试

### 示例: ubring\_performance

brpc 在 `example/ubring_performance/` 目录提供了性能测试示例。

#### 构建示例

```bash
cd example/ubring_performance
mkdir -p build && cd build
cmake ..
make
```

#### 运行服务端

```bash
# 使用 POSIX IPC
./ubring_performance_server --ub_shm_type=1

# 使用 UBS-Mem
./ubring_performance_server --ub_shm_type=2
```

#### 运行客户端

```bash
# 使用 POSIX IPC
./ubring_performance_client --ub_shm_type=1 --server=127.0.0.1:8000

# 使用 UBS-Mem
./ubring_performance_client --ub_shm_type=2 --server=<remote_ip>:8000
```

#### 测试选项

| 选项 | 描述 | 默认值 |
| --------------- | ------------------------- | -------------- |
| `--ub_shm_type` | 共享内存类型 (1=IPC, 2=UBS-Mem) | 1 |
| `--server` | 服务端地址 | 127.0.0.1:8000 |
| `--thread_num` | 客户端线程数 | 1 |
| `--request_num` | 每线程请求总数 | 1000000 |
| `--timeout_ms` | 请求超时时间(毫秒) | 1000 |

## 架构概述

```mermaid
graph TD
subgraph 客户端进程
A[Client]
end

subgraph 服务端进程
B[Server]
end

subgraph 共享内存层
C[SHM Manager]
D[IPC Backend]
E[UBS-Mem Backend]
end

A -->|直接内存访问| C
B -->|直接内存访问| C
C --> D
C --> E

style A fill:#636,color:#fff,stroke:#333,stroke-width:2px
style B fill:#369,color:#fff,stroke:#333,stroke-width:2px
style C fill:#396,color:#fff,stroke:#333,stroke-width:2px
```

### 架构细节

UBRing 架构包含以下组件:

1. **客户端/服务端进程**: 通过共享内存通信的应用进程
2. **SHM Manager**: 共享内存操作的中央管理器 (`shm_mgr.cpp`)
3. **IPC Backend**: 用于本地通信的 POSIX 共享内存实现
4. **UBS-Mem Backend**: 用于跨节点通信的远端共享内存实现

## 实现细节

### 共享内存管理

共享内存管理器 (`shm_mgr.cpp`) 为不同的共享内存后端提供统一接口:

- **初始化**: `ShmMgrInit()` - 初始化共享内存子系统
- **本地分配**: `ShmLocalMalloc()` - 分配本地共享内存
- **远端分配**: `ShmRemoteMalloc()` - 分配远程节点可访问的共享内存
- **释放**: `ShmFree()` - 释放共享内存资源

### 定时器管理

UBRing 使用高精度定时器系统 (`timer_mgr.cpp`) 进行连接管理和超时处理,支持 epoll(Linux)和 kqueue(macOS)。

## 参考资料

- [UBRing 特性提案](https://github.com/apache/brpc/issues/3226)
- [UBRing 技术讨论](https://github.com/apache/brpc/discussions/3217)
- [UBS-Mem 开源项目](https://atomgit.com/openeuler/ubs-mem)

## 相关文档

- [UB Client](ub_client.md) - 访问 UB 服务
- [RDMA 支持](rdma.md) - 远程直接内存访问

Loading
Loading