DPDK timer 解析

2024-04-23 23:44
文章标签 解析 dpdk timer

本文主要是介绍DPDK timer 解析,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1. 编译 DPDK timer

设置环境变量:

  • export RTE_SDK=/home//dpdk/dpdk-stable-19.08.2/
  • export RTE_TARGET=x86_64-native-linux-gcc

源码路径:./dpdk 源码/examples/timer/

编译方法:在上面路径下执行 make

目标文件路径:./dpdk 源码/examples/timer/build/timer

下面是 timer 部分运行结果:

可以看到的是有两个定时器 timer0 和 timer1, timer0 是绑定到当前 cpu 核心一直是 core 0 触发定时器 0,

而 timer1 则是在不同核心触发定时器 1。

EAL: Detected 8 lcore(s)
EAL: Detected 1 NUMA nodes
EAL: Multi-process socket /var/run/dpdk/rte/mp_socket
EAL: Selected IOVA mode 'PA'
EAL: Probing VFIO support...
EAL: VFIO support initialized
EAL: PCI device 0000:02:01.0 on NUMA socket -1
EAL:   Invalid NUMA socket, default to 0
EAL:   probe driver: 8086:100f net_e1000_em
EAL: PCI device 0000:02:06.0 on NUMA socket -1
EAL:   Invalid NUMA socket, default to 0
EAL:   probe driver: 8086:100f net_e1000_em
EAL: PCI device 0000:03:00.0 on NUMA socket -1
EAL:   Invalid NUMA socket, default to 0
EAL:   probe driver: 15ad:7b0 net_vmxnet3
EAL: PCI device 0000:0b:00.0 on NUMA socket -1
EAL:   Invalid NUMA socket, default to 0
EAL:   probe driver: 15ad:7b0 net_vmxnet3
Starting mainloop on core 1
Starting mainloop on core 2
Starting mainloop on core 3
Starting mainloop on core 4
Starting mainloop on core 5
Starting mainloop on core 6
Starting mainloop on core 7
Starting mainloop on core 0
timer1_cb() on lcore 1
timer1_cb() on lcore 2
timer0_cb() on lcore 0
timer1_cb() on lcore 3
timer1_cb() on lcore 4
timer1_cb() on lcore 5
timer0_cb() on lcore 0
timer1_cb() on lcore 6
timer1_cb() on lcore 7
timer1_cb() on lcore 0

2. DPDK API 学习

2.1. rte_timer_subsystem_init()

#include <rte_timer.h>
int rte_timer_subsystem_init(void);
//作用:确保定时器子系统被正确初始化,从而保证后续的定时器操作能够正常进行。

2.2. rte_timer_init()

#include <rte_timer.h>
void rte_timer_init(struct rte_timer *tim);
// 作用:初始化一个rte_timer对象

2.3. rte_get_timer_hz()

#include <rte_cycles.h>
uint64_t rte_get_timer_hz(void);
// 作用:函数用于获取系统定时器的频率,即每秒钟CPU时钟周期的数量。

2.4. rte_timer_reset()

#include <rte_timer.h>
void rte_timer_reset(struct rte_timer *tim,uint64_t ticks,enum rte_timer_type type,unsigned lcore_id,rte_timer_cb_t *f,void *arg);
//作用:函数用于重新设置定时器的参数,包括定时器的周期、触发模式、回调函数以及回调函数的参数等
//参数:
/*
tim       :指向要重新设置的定时器对象的指针。
ticks     :表示定时器的周期,即定时器触发的时间间隔,单位是CPU时钟周期数。
type      :表示定时器的触发模式,是一个枚举类型。常见的触发模式有:SINGLE    :单次触发,定时器只会在下一个周期触发一次,然后停止。PERIODICAL:周期性触发,定时器会在每个周期都触发一次,直到手动停止。
lcore_id  :表示定时器触发时要在哪个CPU核心上执行回调函数。
f         :是一个函数指针,指向定时器触发时要执行的回调函数。
arg       :是传递给回调函数的参数,可以是任意类型的指针,用于传递额外的数据给回调函数
*/

2.5. rte_timer_manage()

#include <rte_timer.h>
void rte_timer_manage(void);
//作用:管理系统中所有的定时器,检查是否有定时器已经到期(即触发),如果有则执行相应的回调函数。

3. 源代码

3.1. 使用 timer0 和 timer1

#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <errno.h>
#include <sys/queue.h>#include <rte_common.h>
#include <rte_memory.h>
#include <rte_launch.h>
#include <rte_eal.h>
#include <rte_per_lcore.h>
#include <rte_lcore.h>
#include <rte_cycles.h>
#include <rte_timer.h>
#include <rte_debug.h>#define TIMER_RESOLUTION_CYCLES 20000000ULL /* around 10ms at 2 Ghz */static struct rte_timer timer0;
static struct rte_timer timer1;/* timer0 callback */
static void timer0_cb(__attribute__((unused)) struct rte_timer *tim, __attribute__((unused)) void *arg)
{static unsigned counter = 0;unsigned lcore_id = rte_lcore_id();printf("%s() on lcore %u\n", __func__, lcore_id);/* this timer is automatically reloaded until we decide to* stop it, when counter reaches 20. */if ((counter ++) == 20)rte_timer_stop(tim);
}/* timer1 callback */
static void timer1_cb(__attribute__((unused)) struct rte_timer *tim, __attribute__((unused)) void *arg)
{unsigned lcore_id = rte_lcore_id();uint64_t hz;printf("%s() on lcore %u\n", __func__, lcore_id);/* reload it on another lcore */hz = rte_get_timer_hz();lcore_id = rte_get_next_lcore(lcore_id, 0, 1);rte_timer_reset(tim, hz/3, SINGLE, lcore_id, timer1_cb, NULL);
}static __attribute__((noreturn)) int lcore_mainloop(__attribute__((unused)) void *arg)
{uint64_t prev_tsc = 0, cur_tsc, diff_tsc;unsigned lcore_id;lcore_id = rte_lcore_id();printf("Starting mainloop on core %u\n", lcore_id);while (1) {/** Call the timer handler on each core: as we don't* need a very precise timer, so only call* rte_timer_manage() every ~10ms (at 2Ghz). In a real* application, this will enhance performances as* reading the HPET timer is not efficient.*/cur_tsc = rte_rdtsc();diff_tsc = cur_tsc - prev_tsc;if (diff_tsc > TIMER_RESOLUTION_CYCLES) {rte_timer_manage();prev_tsc = cur_tsc;}}
}int main(int argc, char **argv)
{int ret;uint64_t hz;unsigned lcore_id;/* init EAL */ret = rte_eal_init(argc, argv);if (ret < 0)rte_panic("Cannot init EAL\n");/* init RTE timer library */rte_timer_subsystem_init();/* init timer structures */rte_timer_init(&timer0);rte_timer_init(&timer1);/* load timer0, every second, on master lcore, reloaded automatically *//* 每秒加载定时器0,绑定当前核心,自动加载 */hz = rte_get_timer_hz();lcore_id = rte_lcore_id();rte_timer_reset(&timer0, hz, PERIODICAL, lcore_id, timer0_cb, NULL);/* load timer1, every second/3, on next lcore, reloaded manually *//* 每3秒加载定时器0,绑定下一个核心,手动加载 */lcore_id = rte_get_next_lcore(lcore_id, 0, 1);rte_timer_reset(&timer1, hz/3, SINGLE, lcore_id, timer1_cb, NULL);/* call lcore_mainloop() on every slave lcore */RTE_LCORE_FOREACH_SLAVE(lcore_id) {rte_eal_remote_launch(lcore_mainloop, NULL, lcore_id);}/* call it on master lcore too */(void) lcore_mainloop(NULL);return 0;
}

3.2. 只使用 timer0

#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <errno.h>
#include <sys/queue.h>#include <rte_common.h>
#include <rte_memory.h>
#include <rte_launch.h>
#include <rte_eal.h>
#include <rte_per_lcore.h>
#include <rte_lcore.h>
#include <rte_cycles.h>
#include <rte_timer.h>
#include <rte_debug.h>#define TIMER_RESOLUTION_CYCLES 20000000ULL /* around 10ms at 2 Ghz */static struct rte_timer timer0;/* timer0 callback */
static void timer0_cb(__attribute__((unused)) struct rte_timer *tim, __attribute__((unused)) void *arg)
{static unsigned counter = 0;unsigned lcore_id = rte_lcore_id();printf("%s() on lcore %u\n", __func__, lcore_id);/* this timer is automatically reloaded until we decide to* stop it, when counter reaches 20. */if ((counter ++) == 20)rte_timer_stop(tim);
}static __attribute__((noreturn)) int lcore_mainloop(__attribute__((unused)) void *arg)
{uint64_t prev_tsc = 0, cur_tsc, diff_tsc;unsigned lcore_id;lcore_id = rte_lcore_id();printf("Starting mainloop on core %u\n", lcore_id);while (1) {/** Call the timer handler on each core: as we don't* need a very precise timer, so only call* rte_timer_manage() every ~10ms (at 2Ghz). In a real* application, this will enhance performances as* reading the HPET timer is not efficient.*/cur_tsc = rte_rdtsc();diff_tsc = cur_tsc - prev_tsc;if (diff_tsc > TIMER_RESOLUTION_CYCLES) {rte_timer_manage();prev_tsc = cur_tsc;}}
}int main(int argc, char **argv)
{int ret;uint64_t hz;unsigned lcore_id;/* init EAL */ret = rte_eal_init(argc, argv);if (ret < 0)rte_panic("Cannot init EAL\n");/* init RTE timer library */rte_timer_subsystem_init();/* init timer structures */rte_timer_init(&timer0);/* load timer0, every second, on master lcore, reloaded automatically *//* 每秒加载定时器0,绑定当前核心,自动加载 */hz = rte_get_timer_hz();lcore_id = rte_lcore_id();rte_timer_reset(&timer0, hz, PERIODICAL, lcore_id, timer0_cb, NULL);/* call lcore_mainloop() on every slave lcore */RTE_LCORE_FOREACH_SLAVE(lcore_id) {rte_eal_remote_launch(lcore_mainloop, NULL, lcore_id);}/* call it on master lcore too */(void) lcore_mainloop(NULL);return 0;
}
EAL: Detected 8 lcore(s)
EAL: Detected 1 NUMA nodes
EAL: Multi-process socket /var/run/dpdk/rte/mp_socket
EAL: Selected IOVA mode 'PA'
EAL: Probing VFIO support...
EAL: VFIO support initialized
EAL: PCI device 0000:02:01.0 on NUMA socket -1
EAL:   Invalid NUMA socket, default to 0
EAL:   probe driver: 8086:100f net_e1000_em
EAL: PCI device 0000:02:06.0 on NUMA socket -1
EAL:   Invalid NUMA socket, default to 0
EAL:   probe driver: 8086:100f net_e1000_em
EAL: PCI device 0000:03:00.0 on NUMA socket -1
EAL:   Invalid NUMA socket, default to 0
EAL:   probe driver: 15ad:7b0 net_vmxnet3
EAL: PCI device 0000:0b:00.0 on NUMA socket -1
EAL:   Invalid NUMA socket, default to 0
EAL:   probe driver: 15ad:7b0 net_vmxnet3
Starting mainloop on core 1
Starting mainloop on core 2
Starting mainloop on core 3
Starting mainloop on core 4
Starting mainloop on core 7
Starting mainloop on core 0
Starting mainloop on core 5
Starting mainloop on core 6
timer0_cb() on lcore 0
timer0_cb() on lcore 0
timer0_cb() on lcore 0
timer0_cb() on lcore 0
timer0_cb() on lcore 0
timer0_cb() on lcore 0
timer0_cb() on lcore 0

3.3. 只使用 timer1

#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <errno.h>
#include <sys/queue.h>#include <rte_common.h>
#include <rte_memory.h>
#include <rte_launch.h>
#include <rte_eal.h>
#include <rte_per_lcore.h>
#include <rte_lcore.h>
#include <rte_cycles.h>
#include <rte_timer.h>
#include <rte_debug.h>#define TIMER_RESOLUTION_CYCLES 20000000ULL /* around 10ms at 2 Ghz */static struct rte_timer timer1;/* timer1 callback */
static void timer1_cb(__attribute__((unused)) struct rte_timer *tim, __attribute__((unused)) void *arg)
{unsigned lcore_id = rte_lcore_id();uint64_t hz;printf("%s() on lcore %u\n", __func__, lcore_id);/* reload it on another lcore */hz = rte_get_timer_hz();lcore_id = rte_get_next_lcore(lcore_id, 0, 1);rte_timer_reset(tim, hz/3, SINGLE, lcore_id, timer1_cb, NULL);
}static __attribute__((noreturn)) int lcore_mainloop(__attribute__((unused)) void *arg)
{uint64_t prev_tsc = 0, cur_tsc, diff_tsc;unsigned lcore_id;lcore_id = rte_lcore_id();printf("Starting mainloop on core %u\n", lcore_id);while (1) {/** Call the timer handler on each core: as we don't* need a very precise timer, so only call* rte_timer_manage() every ~10ms (at 2Ghz). In a real* application, this will enhance performances as* reading the HPET timer is not efficient.*/cur_tsc = rte_rdtsc();diff_tsc = cur_tsc - prev_tsc;if (diff_tsc > TIMER_RESOLUTION_CYCLES) {rte_timer_manage();prev_tsc = cur_tsc;}}
}int main(int argc, char **argv)
{int ret;uint64_t hz;unsigned lcore_id;/* init EAL */ret = rte_eal_init(argc, argv);if (ret < 0)rte_panic("Cannot init EAL\n");/* init RTE timer library */rte_timer_subsystem_init();/* init timer structures */rte_timer_init(&timer1);hz = rte_get_timer_hz();lcore_id = rte_get_next_lcore(lcore_id, 0, 1);rte_timer_reset(&timer1, hz/3, SINGLE, lcore_id, timer1_cb, NULL);/* call lcore_mainloop() on every slave lcore */RTE_LCORE_FOREACH_SLAVE(lcore_id) {rte_eal_remote_launch(lcore_mainloop, NULL, lcore_id);}/* call it on master lcore too */(void) lcore_mainloop(NULL);return 0;
}
EAL: Detected 8 lcore(s)
EAL: Detected 1 NUMA nodes
EAL: Multi-process socket /var/run/dpdk/rte/mp_socket
EAL: Selected IOVA mode 'PA'
EAL: Probing VFIO support...
EAL: VFIO support initialized
EAL: PCI device 0000:02:01.0 on NUMA socket -1
EAL:   Invalid NUMA socket, default to 0
EAL:   probe driver: 8086:100f net_e1000_em
EAL: PCI device 0000:02:06.0 on NUMA socket -1
EAL:   Invalid NUMA socket, default to 0
EAL:   probe driver: 8086:100f net_e1000_em
EAL: PCI device 0000:03:00.0 on NUMA socket -1
EAL:   Invalid NUMA socket, default to 0
EAL:   probe driver: 15ad:7b0 net_vmxnet3
EAL: PCI device 0000:0b:00.0 on NUMA socket -1
EAL:   Invalid NUMA socket, default to 0
EAL:   probe driver: 15ad:7b0 net_vmxnet3
Starting mainloop on core 1
Starting mainloop on core 3
Starting mainloop on core 4
Starting mainloop on core 5
Starting mainloop on core 2
Starting mainloop on core 6
Starting mainloop on core 7
Starting mainloop on core 0
timer1_cb() on lcore 1
timer1_cb() on lcore 2
timer1_cb() on lcore 3
timer1_cb() on lcore 4
timer1_cb() on lcore 5
timer1_cb() on lcore 6
timer1_cb() on lcore 7
timer1_cb() on lcore 0
timer1_cb() on lcore 1

这篇关于DPDK timer 解析的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/930213

相关文章

深度解析Java DTO(最新推荐)

《深度解析JavaDTO(最新推荐)》DTO(DataTransferObject)是一种用于在不同层(如Controller层、Service层)之间传输数据的对象设计模式,其核心目的是封装数据,... 目录一、什么是DTO?DTO的核心特点:二、为什么需要DTO?(对比Entity)三、实际应用场景解析

深度解析Java项目中包和包之间的联系

《深度解析Java项目中包和包之间的联系》文章浏览阅读850次,点赞13次,收藏8次。本文详细介绍了Java分层架构中的几个关键包:DTO、Controller、Service和Mapper。_jav... 目录前言一、各大包1.DTO1.1、DTO的核心用途1.2. DTO与实体类(Entity)的区别1

Java中的雪花算法Snowflake解析与实践技巧

《Java中的雪花算法Snowflake解析与实践技巧》本文解析了雪花算法的原理、Java实现及生产实践,涵盖ID结构、位运算技巧、时钟回拨处理、WorkerId分配等关键点,并探讨了百度UidGen... 目录一、雪花算法核心原理1.1 算法起源1.2 ID结构详解1.3 核心特性二、Java实现解析2.

使用Python绘制3D堆叠条形图全解析

《使用Python绘制3D堆叠条形图全解析》在数据可视化的工具箱里,3D图表总能带来眼前一亮的效果,本文就来和大家聊聊如何使用Python实现绘制3D堆叠条形图,感兴趣的小伙伴可以了解下... 目录为什么选择 3D 堆叠条形图代码实现:从数据到 3D 世界的搭建核心代码逐行解析细节优化应用场景:3D 堆叠图

深度解析Python装饰器常见用法与进阶技巧

《深度解析Python装饰器常见用法与进阶技巧》Python装饰器(Decorator)是提升代码可读性与复用性的强大工具,本文将深入解析Python装饰器的原理,常见用法,进阶技巧与最佳实践,希望可... 目录装饰器的基本原理函数装饰器的常见用法带参数的装饰器类装饰器与方法装饰器装饰器的嵌套与组合进阶技巧

解析C++11 static_assert及与Boost库的关联从入门到精通

《解析C++11static_assert及与Boost库的关联从入门到精通》static_assert是C++中强大的编译时验证工具,它能够在编译阶段拦截不符合预期的类型或值,增强代码的健壮性,通... 目录一、背景知识:传统断言方法的局限性1.1 assert宏1.2 #error指令1.3 第三方解决

全面解析MySQL索引长度限制问题与解决方案

《全面解析MySQL索引长度限制问题与解决方案》MySQL对索引长度设限是为了保持高效的数据检索性能,这个限制不是MySQL的缺陷,而是数据库设计中的权衡结果,下面我们就来看看如何解决这一问题吧... 目录引言:为什么会有索引键长度问题?一、问题根源深度解析mysql索引长度限制原理实际场景示例二、五大解决

深度解析Spring Boot拦截器Interceptor与过滤器Filter的区别与实战指南

《深度解析SpringBoot拦截器Interceptor与过滤器Filter的区别与实战指南》本文深度解析SpringBoot中拦截器与过滤器的区别,涵盖执行顺序、依赖关系、异常处理等核心差异,并... 目录Spring Boot拦截器(Interceptor)与过滤器(Filter)深度解析:区别、实现

深度解析Spring AOP @Aspect 原理、实战与最佳实践教程

《深度解析SpringAOP@Aspect原理、实战与最佳实践教程》文章系统讲解了SpringAOP核心概念、实现方式及原理,涵盖横切关注点分离、代理机制(JDK/CGLIB)、切入点类型、性能... 目录1. @ASPect 核心概念1.1 AOP 编程范式1.2 @Aspect 关键特性2. 完整代码实

解决未解析的依赖项:‘net.sf.json-lib:json-lib:jar:2.4‘问题

《解决未解析的依赖项:‘net.sf.json-lib:json-lib:jar:2.4‘问题》:本文主要介绍解决未解析的依赖项:‘net.sf.json-lib:json-lib:jar:2.4... 目录未解析的依赖项:‘net.sf.json-lib:json-lib:jar:2.4‘打开pom.XM