mbuf_user_pool_ops和mbuf_platform_pool_ops

本文解析了DPDK中两种内存池mbuf_user_pool_ops和mbuf_platform_pool_ops的工作原理及优先级。用户请求内存时优先从mbuf_user_pool_ops分配,若不足则转向mbuf_platform_pool_ops。深入探讨了rte_mbuf_best_mempool_ops函数实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

dpdk中有两种pool 来有限分配用户内存。分别是mbuf_user_pool_ops和mbuf_platform_pool_ops
当用户调用rte_mbuf_best_mempool_ops 来从pool中分配内存是,优先从mbuf_user_pool_ops 中分配,然后才是从
mbuf_platform_pool_ops 中分配。
具体分析如下:

const char * __rte_experimental
rte_mbuf_best_mempool_ops(void)
{
	/* User defined mempool ops takes the priority */
	#首先查找mbuf_user_pool_ops
	const char *best_ops = rte_mbuf_user_mempool_ops();
	if (best_ops)
		return best_ops;

	/* Next choice is platform configured mempool ops */
	#其次查找mbuf_user_pool_ops
	best_ops = rte_mbuf_platform_mempool_ops();
	if (best_ops)
		return best_ops;

	/* Last choice is to use the compile time config pool */
	最后才是使用 ring buffer #define RTE_MBUF_DEFAULT_MEMPOOL_OPS "ring_mp_mc"

	return RTE_MBUF_DEFAULT_MEMPOOL_OPS;
}
我们以mbuf_user_pool_ops 为例看看
const char * __rte_experimental
rte_mbuf_user_mempool_ops(void)
{
	const struct rte_memzone *mz;
	#查找mbuf_user_pool_ops
	mz = rte_memzone_lookup("mbuf_user_pool_ops");
	if (mz == NULL)
		#如果没有查找到,则直接返回internal_config.user_mbuf_pool_ops_name;
		return rte_eal_mbuf_user_pool_ops();
	#查找到后,返回这个pool的name
	return mz->addr;
}
const struct rte_memzone *
rte_memzone_lookup(const char *name)
{
	struct rte_mem_config *mcfg;
	const struct rte_memzone *memzone = NULL;
	#得到全局变量
	mcfg = rte_eal_get_configuration()->mem_config;
	锁保护
	rte_rwlock_read_lock(&mcfg->mlock);
	#开始查找这个mempool
	memzone = memzone_lookup_thread_unsafe(name);

	rte_rwlock_read_unlock(&mcfg->mlock);

	return memzone;
}

static inline const struct rte_memzone *
memzone_lookup_thread_unsafe(const char *name)
{
	const struct rte_mem_config *mcfg;
	const struct rte_memzone *mz;
	unsigned i = 0;

	/* get pointer to global configuration */
	#得到全局变量
	mcfg = rte_eal_get_configuration()->mem_config;

	/*
	 * the algorithm is not optimal (linear), but there are few
	 * zones and this function should be called at init only
	 */
	#遍历全局变量mcfg中的memzone,以本例中及时查找name为mbuf_user_pool_ops的memzone
	for (i = 0; i < RTE_MAX_MEMZONE; i++) {
		mz = &mcfg->memzone[i];
		if (mz->addr != NULL && !strncmp(name, mz->name, RTE_MEMZONE_NAMESIZE))
			return &mcfg->memzone[i];
	}

	return NULL;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值