DPDK所提供的ring本质上是一个FIFO的无锁队列,支持单生产者/单消费者/多生产者/多消费者等多种操作模式,同时也支持burst模式来进行以上操作。物理上它是一个数组,需要在定义时就指定好大小(队列是没有大小限制的),在逻辑上可以看成是一个环形队列。
和队列相比,具备如下优势:
- 更快:仅仅需要一个CAS指令
- 简单:相比标准的linux无锁队列,实现和使用都相对简单
- 支持批量入队/出队操做
逻辑上ring看起来如下图:
rte_ring 的属性
可以看下结构体的定义:
struct rte_ring {
TAILQ_ENTRY(rte_ring) next; /**< Next in list. */
//ring的唯一标示,不可能同时有两个相同name的ring存在
char name[RTE_RING_NAMESIZE]; /**< Name of the ring. */
int flags; /**< Flags supplied at creation. */
/** Ring producer status. */
struct prod {
uint32_t watermark; /**< Maximum items before EDQUOT. */
uint32_t sp_enqueue; /**< True, if single producer. */
uint32_t size; /**< Size of ring. */
uint32_t mask; /**< Mask (size-1) of ring. */
volatile uint32_t head; /**< Producer head. */
volatile uint32_t tail; /**< Producer tail. */
} prod __rte_cache_aligned;
/** Ring consumer status. */
struct cons {
uint32_t sc_dequeue; /**< True, if single consumer. */
uint32_t size; /**< Size of the ring. */
uint32_t mask; /**< Mask (size-1) of ring. */
volatile uint32_t head; /**< Consumer head. */
volatile uint32_t tail; /**< Consumer tail. */
#ifdef RTE_RING_SPLIT_PROD_CONS
/*这个属性就是要求gcc在编译的时候,把cons/prod结构都单独分配到一个cache行,为什么这样做?
因为如果没有这些的话,这两个结构在内存上是连续的&