DPDK 分支预测性能优化

82 阅读1分钟

简介

在一些分支判断的业务场景中,有些时候我们是比较明确条件的发生概率,这个时候可以人为的利用likely或者unlikely来进行分支预测,即该分支条件被满足的概率比较大或比较小。

编译器利用这一信息优化其机器指令,从而最大限度减少CPU分支预测失败带来的惩罚,达到性能优化的效果。

源码


/**
 * @file
 * Branch Prediction Helpers in RTE
 */

#ifndef _RTE_BRANCH_PREDICTION_H_
#define _RTE_BRANCH_PREDICTION_H_

#ifdef __cplusplus
extern "C" {
#endif

/**
 * Check if a branch is likely to be taken.
 *
 * This compiler builtin allows the developer to indicate if a branch is
 * likely to be taken. Example:
 *
 *   if (likely(x > 1))
 *      do_stuff();
 */
#ifndef likely
#ifdef RTE_TOOLCHAIN_MSVC
#define likely(x)	(!!(x))
#else
#define likely(x)	__builtin_expect(!!(x), 1)
#endif
#endif /* likely */

/**
 * Check if a branch is unlikely to be taken.
 *
 * This compiler builtin allows the developer to indicate if a branch is
 * unlikely to be taken. Example:
 *
 *   if (unlikely(x < 1))
 *      do_stuff();
 */
#ifndef unlikely
#ifdef RTE_TOOLCHAIN_MSVC
#define unlikely(x)	(!!(x))
#else
#define unlikely(x)	__builtin_expect(!!(x), 0)
#endif
#endif /* unlikely */

#ifdef __cplusplus
}
#endif

#endif /* _RTE_BRANCH_PREDICTION_H_ */

示例

  • if (likely(x > 1)) //分支大概率执行
  • if (unlikely(x < 1)) //分支小概率执行
static inline void
rte_rwlock_read_lock_tm(rte_rwlock_t *rwl)
	__rte_no_thread_safety_analysis
{
	// 条件大概率会满足
	if (likely(rte_try_tm(&rwl->cnt)))
		return;
	rte_rwlock_read_lock(rwl);
}

static inline void
rte_rwlock_read_unlock_tm(rte_rwlock_t *rwl)
	__rte_no_thread_safety_analysis
{
	// 条件低概率会满足
	if (unlikely(rwl->cnt))
		rte_rwlock_read_unlock(rwl);
	else
		rte_xend();
}