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
  • 16
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值