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

相关文章

python常见环境管理工具超全解析

《python常见环境管理工具超全解析》在Python开发中,管理多个项目及其依赖项通常是一个挑战,下面:本文主要介绍python常见环境管理工具的相关资料,文中通过代码介绍的非常详细,需要的朋友... 目录1. conda2. pip3. uvuv 工具自动创建和管理环境的特点4. setup.py5.

全面解析HTML5中Checkbox标签

《全面解析HTML5中Checkbox标签》Checkbox是HTML5中非常重要的表单元素之一,通过合理使用其属性和样式自定义方法,可以为用户提供丰富多样的交互体验,这篇文章给大家介绍HTML5中C... 在html5中,Checkbox(复选框)是一种常用的表单元素,允许用户在一组选项中选择多个项目。本

Python包管理工具核心指令uvx举例详细解析

《Python包管理工具核心指令uvx举例详细解析》:本文主要介绍Python包管理工具核心指令uvx的相关资料,uvx是uv工具链中用于临时运行Python命令行工具的高效执行器,依托Rust实... 目录一、uvx 的定位与核心功能二、uvx 的典型应用场景三、uvx 与传统工具对比四、uvx 的技术实

SpringBoot排查和解决JSON解析错误(400 Bad Request)的方法

《SpringBoot排查和解决JSON解析错误(400BadRequest)的方法》在开发SpringBootRESTfulAPI时,客户端与服务端的数据交互通常使用JSON格式,然而,JSON... 目录问题背景1. 问题描述2. 错误分析解决方案1. 手动重新输入jsON2. 使用工具清理JSON3.

Redis过期删除机制与内存淘汰策略的解析指南

《Redis过期删除机制与内存淘汰策略的解析指南》在使用Redis构建缓存系统时,很多开发者只设置了EXPIRE但却忽略了背后Redis的过期删除机制与内存淘汰策略,下面小编就来和大家详细介绍一下... 目录1、简述2、Redis http://www.chinasem.cn的过期删除策略(Key Expir

Go学习记录之runtime包深入解析

《Go学习记录之runtime包深入解析》Go语言runtime包管理运行时环境,涵盖goroutine调度、内存分配、垃圾回收、类型信息等核心功能,:本文主要介绍Go学习记录之runtime包的... 目录前言:一、runtime包内容学习1、作用:① Goroutine和并发控制:② 垃圾回收:③ 栈和

Spring组件实例化扩展点之InstantiationAwareBeanPostProcessor使用场景解析

《Spring组件实例化扩展点之InstantiationAwareBeanPostProcessor使用场景解析》InstantiationAwareBeanPostProcessor是Spring... 目录一、什么是InstantiationAwareBeanPostProcessor?二、核心方法解

深入解析 Java Future 类及代码示例

《深入解析JavaFuture类及代码示例》JavaFuture是java.util.concurrent包中用于表示异步计算结果的核心接口,下面给大家介绍JavaFuture类及实例代码,感兴... 目录一、Future 类概述二、核心工作机制代码示例执行流程2. 状态机模型3. 核心方法解析行为总结:三

springboot项目中使用JOSN解析库的方法

《springboot项目中使用JOSN解析库的方法》JSON,全程是JavaScriptObjectNotation,是一种轻量级的数据交换格式,本文给大家介绍springboot项目中使用JOSN... 目录一、jsON解析简介二、Spring Boot项目中使用JSON解析1、pom.XML文件引入依

Python中文件读取操作漏洞深度解析与防护指南

《Python中文件读取操作漏洞深度解析与防护指南》在Web应用开发中,文件操作是最基础也最危险的功能之一,这篇文章将全面剖析Python环境中常见的文件读取漏洞类型,成因及防护方案,感兴趣的小伙伴可... 目录引言一、静态资源处理中的路径穿越漏洞1.1 典型漏洞场景1.2 os.path.join()的陷