hrtimer和usleep说明

内核高精度定时器hrtimer

这里只讲使用,不讲原理,只要芯片支持,打开内核配置,按测试代码就可以正常使用us级的定时器

1、内核打开配置CONFIG_HIGH_RES_TIMERS

示例代码

#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>

#include <linux/ktime.h>
#include <linux/hrtimer.h>

static struct hrtimer timer;
volatile static int t1 = 0, t2 = 0;
static int sys_get_time(void)
{
    struct timespec64 tv;

    ktime_get_real_ts64(&tv);

    return tv.tv_sec*1000000+tv.tv_nsec/1000;
}

static enum hrtimer_restart timer_handler(struct hrtimer *timer )
{
    dump_stack();
    t2 = sys_get_time();
    printk("hrtimer handler,delay %d us\n", t2 - t1);
    hrtimer_forward_now(timer, ktime_set(0,10*1000*1000));
    t1=t2;
    return HRTIMER_NORESTART;
    // return HRTIMER_RESTART;
}

static int __init my_init(void) 
{
    ktime_t tim;
    
	hrtimer_init(&timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
    timer.function = timer_handler;
    tim = ktime_set(0,10000); //10000ns
    t1 = sys_get_time();
    hrtimer_start(&timer,tim,HRTIMER_MODE_REL);

    return 0;
}

static void __exit my_exit(void)
{
	printk("%s enter\n", __func__);
	hrtimer_cancel(&timer);
}

module_init(my_init);
module_exit(my_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("lzf <snail_studying@163.com>");
MODULE_DESCRIPTION("hrtimer example");

高精度定时会重新配置event_handler这个回调函数
在这里插入图片描述

1.1高精度定时器中断触发的调用流程

timer_handler+0x34/0xcc [hrtimer]
__hrtimer_run_queues+0x124/0x244
hrtimer_interrupt+0x11c/0x360
arch_timer_handler_phys+0x34/0x44//这里调timer_handler->event_handler->hrtimer_interrupt
handle_percpu_devid_irq+0x84/0x150
__handle_domain_irq+0x7c/0xe0
gic_handle_irq+0x88/0x150
el1_irq+0xbc/0x140
add_device_randomness+0xc8/0x104
do_one_initcall+0xd4/0x1c0
do_init_module+0x44/0x1d0
load_module+0x1dfc/0x2444
__se_sys_finit_module+0xb8/0x100
__arm64_sys_finit_module+0x1c/0x30
do_el0_svc+0xec/0x210
el0_svc+0x14/0x20
el0_sync_handler+0xb0/0xd0
el0_sync+0x170/0x180

1.2以下是普通定时器调用流程

timer_handler+0x34/0xcc [hrtimer]
__hrtimer_run_queues+0x124/0x244
hrtimer_run_queues+0xdc/0x110
run_local_timers+0x20/0x50
update_process_times+0x58/0xa0
tick_periodic+0x3c/0xe0
tick_handle_periodic+0x34/0x8c
arch_timer_handler_phys+0x34/0x44
handle_percpu_devid_irq+0x84/0x150
__handle_domain_irq+0x7c/0xe0
gic_handle_irq+0x88/0x150
el1_irq+0xbc/0x140
arch_cpu_idle+0x18/0x30
default_idle_call+0x3c/0x8c
do_idle+0xd4/0x160
cpu_startup_entry+0x24/0x50
secondary_start_kernel+0x130/0x160

1.3 差异

event_handler这个回调函数不一样
普通定时器的回调是这个 tick_handle_periodic
高精度定时器的回调是这个hrtimer_interrupt

2、usleep

这个函数正常情况下和内核配置的HZ有关,也就是cpu产生的tick周期相关,不开高精度定时器的时候cpu配置100HZ就是最短usleep都是10ms,1000HZ就是1ms
开了高精度定时器,usleep就可以做到us级别

注:如果开了高精度定时器usleep和hrtimer都无法做到us级别,那就是内核本身就不支持

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值