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中递归下降解析器的原理与实现》在编译器设计、配置文件处理和数据转换领域,递归下降解析器是最常用且最直观的解析技术,本文将详细介绍递归下降解析器的原理与实现,感兴趣的小伙伴可以跟随... 目录引言:解析器的核心价值一、递归下降解析器基础1.1 核心概念解析1.2 基本架构二、简单算术表达

深度解析Java @Serial 注解及常见错误案例

《深度解析Java@Serial注解及常见错误案例》Java14引入@Serial注解,用于编译时校验序列化成员,替代传统方式解决运行时错误,适用于Serializable类的方法/字段,需注意签... 目录Java @Serial 注解深度解析1. 注解本质2. 核心作用(1) 主要用途(2) 适用位置3

Java MCP 的鉴权深度解析

《JavaMCP的鉴权深度解析》文章介绍JavaMCP鉴权的实现方式,指出客户端可通过queryString、header或env传递鉴权信息,服务器端支持工具单独鉴权、过滤器集中鉴权及启动时鉴权... 目录一、MCP Client 侧(负责传递,比较简单)(1)常见的 mcpServers json 配置

从原理到实战解析Java Stream 的并行流性能优化

《从原理到实战解析JavaStream的并行流性能优化》本文给大家介绍JavaStream的并行流性能优化:从原理到实战的全攻略,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的... 目录一、并行流的核心原理与适用场景二、性能优化的核心策略1. 合理设置并行度:打破默认阈值2. 避免装箱

Maven中生命周期深度解析与实战指南

《Maven中生命周期深度解析与实战指南》这篇文章主要为大家详细介绍了Maven生命周期实战指南,包含核心概念、阶段详解、SpringBoot特化场景及企业级实践建议,希望对大家有一定的帮助... 目录一、Maven 生命周期哲学二、default生命周期核心阶段详解(高频使用)三、clean生命周期核心阶

深入解析C++ 中std::map内存管理

《深入解析C++中std::map内存管理》文章详解C++std::map内存管理,指出clear()仅删除元素可能不释放底层内存,建议用swap()与空map交换以彻底释放,针对指针类型需手动de... 目录1️、基本清空std::map2️、使用 swap 彻底释放内存3️、map 中存储指针类型的对象

Java Scanner类解析与实战教程

《JavaScanner类解析与实战教程》JavaScanner类(java.util包)是文本输入解析工具,支持基本类型和字符串读取,基于Readable接口与正则分隔符实现,适用于控制台、文件输... 目录一、核心设计与工作原理1.底层依赖2.解析机制A.核心逻辑基于分隔符(delimiter)和模式匹

Java+AI驱动实现PDF文件数据提取与解析

《Java+AI驱动实现PDF文件数据提取与解析》本文将和大家分享一套基于AI的体检报告智能评估方案,详细介绍从PDF上传、内容提取到AI分析、数据存储的全流程自动化实现方法,感兴趣的可以了解下... 目录一、核心流程:从上传到评估的完整链路二、第一步:解析 PDF,提取体检报告内容1. 引入依赖2. 封装

深度解析Python yfinance的核心功能和高级用法

《深度解析Pythonyfinance的核心功能和高级用法》yfinance是一个功能强大且易于使用的Python库,用于从YahooFinance获取金融数据,本教程将深入探讨yfinance的核... 目录yfinance 深度解析教程 (python)1. 简介与安装1.1 什么是 yfinance?

99%的人都选错了! 路由器WiFi双频合一还是分开好的专业解析与适用场景探讨

《99%的人都选错了!路由器WiFi双频合一还是分开好的专业解析与适用场景探讨》关于双频路由器的“双频合一”与“分开使用”两种模式,用户往往存在诸多疑问,本文将从多个维度深入探讨这两种模式的优缺点,... 在如今“没有WiFi就等于与世隔绝”的时代,越来越多家庭、办公室都开始配置双频无线路由器。但你有没有注