上一章讲完「进程怎么起来」。这一章讲热路径上跑的东西:
一个包要占一块内存,这块内存从哪来、怎么分配、怎么还回去。这是 DPDK 最核心的两块数据结构,也是 26.11 改动最大的地方。
读完之后你应该能回答:
rte_mempool_get()到底做了什么?为什么它比malloc快得多?- per-lcore cache 是怎么避免核间竞争的?
- ring 的头尾指针为什么要用「单调递增 + mask」而不是循环下标?
- 老文章里的
lib/mempool/rte_mempool_ring.c去哪了?
本文所有行号基于 /Users/growduduan/cwork/dpdk(27.03.0-rc0)。
4.1 先说一个变化:mempool 变成驱动了
这是 26.11 最重要的一次重构,也是最容易踩空的地方。
| 18.11 ~ 22.11 | 26.11 / 27.03 | |
|---|---|---|
| ring 版 mempool 的位置 | lib/mempool/rte_mempool_ring.c |
drivers/mempool/ring/rte_mempool_ring.c |
| mempool 实现方式 | 编译期写死在 lib/ 里 |
可插拔的 driver,像网卡驱动一样注册 |
| 可选实现 | 只有 ring / stack | 7 种:ring stack bucket cnxk dpaa dpaa2 octeontx |
drivers/mempool/
├── ring/ rte_mempool_ring.c ← 经典的 ring 版
├── stack/ rte_mempool_stack.c ← LIFO 版
├── bucket/ rte_mempool_bucket.c
├── cnxk/ Marvell CNX 硬件 mempool
├── dpaa/ NXP dpaa 硬件 mempool
├── dpaa2/ NXP dpaa2 硬件 mempool ← DPAA2 平台专属,见 10 章
└── octeontx/
这一条对读者的直接影响:你在 18.11 的文章里看到
lib/mempool/rte_mempool_ring.c:79,现在去树里找会扑空 ——
文件搬家了,去drivers/mempool/ring/。
4.1.1 驱动是怎么注册的
mempool 的 ops 表在 lib/mempool/rte_mempool_ops.c:19:
struct rte_mempool_ops_table rte_mempool_ops_table = {
注册一个 ops 就是往这张表里塞一个 struct rte_mempool_ops,
里面是 alloc / free / enqueue / dequeue / get_count 这些回调。
ring 驱动注册了 6 个 ops(drivers/mempool/ring/rte_mempool_ring.c):
| ops 名称 | 行号 | 含义 |
|---|---|---|
ring_mp_mc |
:146 |
多生产者 / 多消费者(默认) |
ring_sp_sc |
:155 |
单生产者 / 单消费者 |
ring_mp_sc |
:164 |
多生产者 / 单消费者 |
ring_sp_mc |
:173 |
单生产者 / 多消费者 |
ring_mt_rts |
:183 |
RTS 内存序变体 |
ring_mt_hts |
:193 |
HTS 内存序变体 |
用户可以在建池时指定用哪个:
rte_mempool_set_ops_byname(mp, "ring_sp_sc");
默认是 ring_mp_mc。
4.1.2 驱动化带来了什么
以前 mempool 的实现是「编译进 lib/ 的代码」,你想换个实现就得改 DPDK 源码。
现在它和网卡驱动一样是运行时注册的:
- 硬件 mempool 成为可能:
dpaa2那个 driver 把 buffer 的分配权交给网卡硬件,
包 buffer 直接由 DPA 分配,应用根本不用管内存 - 不用 DPDK 源码也能加新实现
- 实现和框架解耦:
lib/mempool/只剩通用逻辑
4.2 mempool:对象池与 per-lcore cache
4.2.1 结构
lib/mempool/rte_mempool.h:239:
struct __rte_cache_aligned rte_mempool {
char name[RTE_MEMPOOL_NAMESIZE];
union {
void *pool_data; /* Ring or pool to store objects. */
uint64_t pool_id; /* External mempool identifier. */
};
void *pool_config; /* optional args for ops alloc. */
const struct rte_memzone *mz; /* Memzone where pool is alloc'd. */
unsigned int flags;
int socket_id; /* Socket id passed at create. */
uint32_t size; /* Max size of the mempool. */
uint32_t cache_size; /* Size of per-lcore default local cache. */
uint32_t elt_size; /* Size of an element. */
uint32_t header_size; /* Size of header (before elt). */
uint32_t trailer_size; /* Size of trailer (after elt). */
...
int32_t ops_index; /* Index into rte_mempool_ops_table */
struct rte_mempool_cache *local_cache; /* Per-lcore local cache */
uint32_t sizeof_cache_per_lcore;
...
};
三个字段先记住:
| 字段 | 说明 |
|---|---|
ops_index |
用下标而不是函数指针 —— 注释里写了原因:副进程要能共享这张表 |
local_cache |
per-lcore 本地缓存,这是性能的关键 |
socket_id |
池属于哪个 NUMA 节点(02 章那条传递线) |
4.2.2 per-lcore cache
lib/mempool/rte_mempool.h:90:
struct __rte_cache_aligned rte_mempool_cache {
uint32_t size; /**< Size of the cache */
uint32_t len; /**< Current cache count */
...
/** Cache objects */
alignas(RTE_CACHE_LINE_SIZE) void *objs[];
};
每个 lcore 有一份独立的缓存。分配对象时:
这才是 mempool 比 malloc 快的真正原因:
malloc |
rte_mempool_get |
|
|---|---|---|
| 热路径开销 | 走 glibc 的 tcache/arena,涉及原子操作 | 从 per-lcore 数组取一个指针 |
| 核间竞争 | 有(arena 锁 / 原子) | 无(每个核一份) |
| 内存碎片 | 会产生 | 不会(固定大小对象) |
| NUMA | 由 glibc 决定 | 建池时指定 |
实践:热路径上永远不要用
rte_malloc。mempool 就是为这件事设计的。
4.2.3 cache 是怎么算出来的
local_cache 是一块连续内存,所有 lcore 的 cache 挤在一起,
用 sizeof_cache_per_lcore 做步长索引:
struct rte_mempool_cache *local_cache; /**< Per-lcore local cache */
uint32_t sizeof_cache_per_lcore; /**< Multiplier for indexing into the local cache. */
26.11 对这块做过调整(doc/guides/rel_notes/release_26_11.rst:491-497):rte_mempool 新增 sizeof_cache_per_lcore 字段用于索引,rte_mempool_cache 删掉了废弃的 flushthresh 和 unused 字段,objs[] 从固定大小改成变长。
4.2.4 建池的调用链
rte_mempool_create() lib/mempool/rte_mempool.c:990
└─ rte_mempool_create_empty() :806
├─ rte_memzone_reserve() ← 池本体落在 memzone 上(02 章)
├─ rte_mempool_set_ops_byname() ← 选驱动
└─ rte_mempool_populate_default() :489 ← 真正填满对象
(由 :1007 调用)
common_ring_alloc()(drivers/mempool/ring/rte_mempool_ring.c:109)把
mempool 的 SP/SC 标志翻译成 ring 的标志:
static int
common_ring_alloc(struct rte_mempool *mp)
{
uint32_t rg_flags = 0;
if (mp->flags & RTE_MEMPOOL_F_SP_PUT)
rg_flags |= RING_F_SP_ENQ;
if (mp->flags & RTE_MEMPOOL_F_SC_GET)
rg_flags |= RING_F_SC_DEQ;
return ring_alloc(mp, rg_flags);
}
而 RTS / HTS 变体是另外两个 ops(:124、:132),直接带上新的 flag:
static int
rts_ring_alloc(struct rte_mempool *mp)
{
return ring_alloc(mp, RING_F_MP_RTS_ENQ | RING_F_MC_RTS_DEQ);
}
4.3 ring:头尾指针与内存序
4.3.1 结构:三个变体,一个 union
lib/ring/rte_ring_core.h:116:
struct rte_ring {
alignas(RTE_CACHE_LINE_SIZE) char name[RTE_RING_NAMESIZE];
int flags;
const struct rte_memzone *memzone;
uint32_t size; /* Size of ring. */
uint32_t mask; /* Mask (size-1) of ring. */
uint32_t capacity; /* Usable size of ring */
RTE_CACHE_GUARD;
/** Ring producer status. */
union __rte_cache_aligned {
struct rte_ring_headtail prod;
struct rte_ring_hts_headtail hts_prod;
struct rte_ring_rts_headtail rts_prod;
};
RTE_CACHE_GUARD;
/** Ring consumer status. */
union __rte_cache_aligned {
struct rte_ring_headtail cons;
struct rte_ring_hts_headtail hts_cons;
struct rte_ring_rts_headtail rts_cons;
};
RTE_CACHE_GUARD;
};
三个要点:
- 生产者和消费者状态各占一条 cache 行(
__rte_cache_aligned+RTE_CACHE_GUARD)——
两个核写同一个 ring 时,最怕的就是伪共享 mask是size-1—— ring 大小必须是 2 的幂,索引靠位与而不是取模- 三个 headtail 变体共用一个 union —— 这就是 26.11 的新东西
4.3.2 为什么头尾是「单调递增 + mask」
经典的 headtail(lib/ring/rte_ring_core.h:65)里:
struct rte_ring_headtail {
union {
volatile uint32_t head; /**< Ring head pointer. */
uint32_t head_cached;
};
union {
volatile uint32_t tail; /**< Ring tail pointer. */
uint32_t tail_cached;
};
};
head 和 tail 是32 位单调递增计数器,不是循环下标。
访问第 i 个元素靠 head & mask。
为什么不用循环下标:如果 head 存的是 0..size-1 的循环值,
生产者要判断「tail == head」就得先比较再处理回绕,多一次分支;
而且回绕时读到的 head 到底是「刚好满」还是「刚绕回来」分不清 ——
单调计数把这个问题彻底消掉了:空 = head == tail,满 = head - tail == size。
4.3.3 三种内存序变体
| 变体 | headtail 类型 | 什么时候用 |
|---|---|---|
| 经典 | rte_ring_headtail(:65) |
默认 |
| RTS | rte_ring_rts_headtail(:85) |
Relaxed Store / Sequential consistency 组合 |
| HTS | rte_ring_hts_headtail(:101) |
高度线程安全变体 |
对应的 flag(lib/ring/rte_ring_core.h:160-164):
#define RING_F_MP_RTS_ENQ 0x0008 /**< The default enqueue is "MP RTS". */
#define RING_F_MC_RTS_DEQ 0x0010 /**< The default enqueue is "MC RTS". */
#define RING_F_MP_HTS_ENQ 0x0020 /**< The default enqueue is "MP HTS". */
#define RING_F_MC_HTS_DEQ 0x0040 /**< The default enqueue is "MC HTS". */
26.11 的 release notes 提到了一串 ring 相关的内存序改动
(”establish a safe partial order in default / RTS / HTS mode” 系列提交)——
这是重写过一遍的实现,不是小修补。
实用建议:先别急着上 RTS / HTS。默认的经典模式在
「一个核收、一个核发」这种典型场景下已经足够。
只有当你的场景是多个核同时往同一个 ring 塞包,
且实测发现有序性问题时,才需要换。
4.4 stack:LIFO 那一族
lib/stack/rte_stack.c:30 是 rte_stack 的实现。与 ring 的区别:
rte_ring |
rte_stack |
|
|---|---|---|
| 顺序 | FIFO | LIFO |
| 用途 | 收发包队列 | 「后进先出」的栈语义 |
drivers/mempool/stack/ 里也有对应的 mempool 驱动。
注意:
rte_stack不是「栈式分配器」的意思,它是队列语义的一种变体。
4.5 一个已经不存在的库:rte_rlibring
老文章里偶尔会提到 rte_rlibring / rlib/ring:
$ grep -rn "rte_rlibring" ~/cwork/dpdk
(零命中)
这个库在当前这棵树上不存在。 它是很早期的实验性环形缓冲区,
在正式版 DPDK 里从来没有过。
4.6 本章要点回顾
- mempool 在 26.11 变成驱动:
lib/mempool/rte_mempool_ring.c→drivers/mempool/ring/rte_mempool_ring.c,共 7 种实现、6 个 ring ops - per-lcore cache 是性能的关键(
rte_mempool.h:90):
热路径上无锁、无原子操作 - ring 的头尾是单调递增计数器,靠
mask取下标;生产/消费各占一条 cache 行 - 26.11 新增 RTS / HTS 两种内存序变体,整个 ring 的内存序被重写过
rte_rlibring不存在,别照着老文章找
4.7 接下来
- 05 章:mbuf —— DPDK 里最核心的数据结构,26.11/27.03 把它扁平化了,
老的rte_mbuf_core/rte_mbuf_seg两个结构体已经合并成一个。 - 06 章:ethdev 与 PMD —— 一个包从网卡描述符变成 mbuf 的完整路径。