Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
3cd5904
add ubring transport
zchuango Apr 13, 2026
0934567
fix the bug for ub ring transport
zchuango Apr 14, 2026
c52fe36
fix the bug for ub ring transport and other
zchuango Apr 14, 2026
37f8747
add the license for ub transport
zchuango Apr 14, 2026
350c305
Modifying the variable naming style
zchuango Apr 14, 2026
aa2a2f9
optimize the log message and some field name
zchuango Apr 19, 2026
fd91351
fix some bug for ubring
zchuango Apr 20, 2026
5998401
add some log ubring endpoint
zchuango Apr 23, 2026
e64f65c
add todo
zchuango May 6, 2026
d8c5e72
fix the bug for handshake for ub endpoint
zchuango May 7, 2026
312dafd
fix the bug for client ub endpoint
zchuango May 7, 2026
9002081
optimize the iobuf file code
zchuango May 7, 2026
e86b9ae
modify the log level
zchuango May 8, 2026
6c9ba54
fix the declare_shm_ubs define not found bug
zchuango May 8, 2026
102058c
add the timer_mgr support for macos and format code style
zchuango May 8, 2026
7050c53
fix the bug for macos epoll
zchuango May 8, 2026
8dacabe
fix the timespece bug
zchuango May 8, 2026
e1459d1
adaptor the itimerspec for macos platform
zchuango May 9, 2026
620b6e5
optimize the cmakelist config
zchuango May 9, 2026
e85ed8b
add the ubring docs for ubring transport
zchuango May 9, 2026
b1cdb99
modify some file name and directory structure
zchuango May 9, 2026
6df15ac
modify some code style and optimize code logical
zchuango May 13, 2026
2340a87
add the cmake ubshm transport ci/testing
zchuango May 23, 2026
72e398d
add the dependency header
zchuango May 23, 2026
d2b00da
bug fix the code
zchuango May 22, 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
2 changes: 1 addition & 1 deletion .github/workflows/ci-linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ jobs:
run: |
export CC=clang && export CXX=clang++
mkdir clang_build_all && cd clang_build_all
cmake -DWITH_MESALINK=OFF -DWITH_GLOG=ON -DWITH_THRIFT=ON -DWITH_RDMA=ON -DWITH_DEBUG_BTHREAD_SCHE_SAFETY=ON -DWITH_DEBUG_LOCK=ON -DWITH_BTHREAD_TRACER=ON -DWITH_ASAN=ON -DCMAKE_POLICY_VERSION_MINIMUM=3.5 ..
cmake -DWITH_MESALINK=OFF -DWITH_GLOG=ON -DWITH_THRIFT=ON -DWITH_RDMA=ON -DWITH_UBRING=ON -DWITH_DEBUG_BTHREAD_SCHE_SAFETY=ON -DWITH_DEBUG_LOCK=ON -DWITH_BTHREAD_TRACER=ON -DWITH_ASAN=ON -DCMAKE_POLICY_VERSION_MINIMUM=3.5 ..
make -j ${{env.proc_num}} && make clean
gcc-compile-with-make-protobuf:
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/ci-macos.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ jobs:
- name: compile with cmake
run: |
echo "CMAKE_PREFIX_PATH=$(brew --prefix protobuf@21)"
mkdir build && cd build && cmake -DCMAKE_POLICY_VERSION_MINIMUM=3.5 -DCMAKE_PREFIX_PATH=$(brew --prefix protobuf@21) ..
mkdir build && cd build && cmake -DCMAKE_POLICY_VERSION_MINIMUM=3.5 -DWITH_UBRING=ON -DCMAKE_PREFIX_PATH=$(brew --prefix protobuf@21) ..
make -j ${{env.proc_num}} && make clean

compile-with-make-cmake-protobuf29:
Expand All @@ -56,7 +56,7 @@ jobs:

- name: compile with cmake
run: |
mkdir build && cd build && cmake -DCMAKE_POLICY_VERSION_MINIMUM=3.5 -DCMAKE_PREFIX_PATH=$(brew --prefix protobuf@29) ..
mkdir build && cd build && cmake -DCMAKE_POLICY_VERSION_MINIMUM=3.5 -DWITH_UBRING=ON -DCMAKE_PREFIX_PATH=$(brew --prefix protobuf@29) ..
make -j ${{env.proc_num}} && make clean

compile-with-bazel:
Expand Down
13 changes: 12 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
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
199 changes: 199 additions & 0 deletions docs/cn/ubring.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
# 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)
Comment thread
chenBright marked this conversation as resolved.

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

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

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you list the libraries that need to be used?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, I'll list the depends libraries later


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

**所需库文件**:
- `libubsm_sdk.so` - UBS-Mem SDK 库(安装路径:`/usr/local/ubs_mem/lib/libubsm_sdk.so`)
- UBS-Mem 通过 `dlopen()` 动态加载该库,并使用 `ubsmem_initialize()`、`ubsmem_create_region()`、`ubsmem_shmem_allocate()`、`ubsmem_shmem_map()` 等函数

**UBS-Mem 关键函数**:
- `ubsmem_init_attributes()` - 初始化 UBS-Mem 属性
- `ubsmem_initialize()` - 初始化 UBS-Mem 库
- `ubsmem_finalize()` - 释放 UBS-Mem 库
- `ubsmem_create_region()` - 创建共享内存区域
- `ubsmem_shmem_allocate()` - 分配共享内存
- `ubsmem_shmem_map()` - 将共享内存映射到本地地址空间
- `ubsmem_shmem_unmap()` - 解除共享内存映射
- `ubsmem_shmem_deallocate()` - 释放共享内存
- `ubsmem_destroy_region()` - 销毁共享内存区域

### 未来扩展

该架构设计支持未来扩展 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