Linux内核定时器:高精度定时器hrtimer的用例

之前介绍了timer_list内核定时器,它的精度在毫秒级别,再高一点它就无能为力了,所幸内核提供了高精度定时器 hrtimer。

源文件在linux/kernel/hrtimer.c中。接口简单。下面介绍一下相关接口

1. 定时器定义与绑定超时回调函数
static struct hrtimer timer;
 
/* 设置回调函数 */
timer.function = hrtimer_hander;
 
2. 定时器初始化
/*
 *  参数timer是hrtimer指针,
 *  参数clock_id有如下常用几种选项:
 *  CLOCK_REALTIME	//实时时间,如果系统时间变了,定时器也会变
 *  CLOCK_MONOTONIC	//递增时间,不受系统影响
 *  参数mode有如下几种选项:
 * 	HRTIMER_MODE_ABS = 0x0,		/* 绝对模式 */
	HRTIMER_MODE_REL = 0x1,		/* 相对模式 */
	HRTIMER_MODE_PINNED = 0x02,	/* 和CPU绑定 */
	HRTIMER_MODE_ABS_PINNED = 0x02, /* 第一种和第三种的结合 */
	HRTIMER_MODE_REL_PINNED = 0x03, /* 第二种和第三种的结合 */
 */
void hrtimer_init(struct hrtimer *timer, clockid_t clock_id, enum hrtimer_mode mode)3. 定时器启动
/*
 * 参数timer是hrtimer指针
 * 参数tim是时间,可以使用ktime_set()函数设置时间,
 * 参数mode和初始化的mode参数一致
 */
hrtimer_start(struct hrtimer *timer, ktime_t tim, const enum hrtimer_mode mode)4. 设置时间
/*
 * 单位为秒和纳秒组合
 */
ktime_t ktime_set(const long secs, const unsigned long nsecs)/* 设置超时时间,当定时器超时后可以用该函数设置下一次超时时间 */
hrtimer_forward_now(struct hrtimer *timer, ktime_t interval)
 
5. 注意事项:
定时器超时后会调用回调函数,回调函数结构类似这样:
enum hrtimer_restart		(*function)(struct hrtimer *);
 
enum hrtimer_restart {
	HRTIMER_NORESTART,	/* 不重启定时器 */
	HRTIMER_RESTART,	/* 重启定时器 */
};
在回调函数返回前要手动设置下一次超时时间。
另外,回调函数执行时间不宜过长,因为是在中断上下文中,如果有什么任务的话,最好使用工作队列等机制。
 
6. 关闭定时器
int hrtimer_cancel(struct hrtimer *timer);
 
简单用例:
/*
 *  Description : 高精度定时器用例
 *  Author : mason
 *  Date   : 201808
 */
 
#include 
#include 
#include 
#include 
 
static struct hrtimer timer;
ktime_t kt;
 
/* 定时器回调函数 */
static enum hrtimer_restart  hrtimer_hander(struct hrtimer *timer)
{
    printk("hrtimer up\r\n");
 
    /* 设置下次过期时间 */
    kt = ktime_set(3,0);    
    hrtimer_forward_now(timer, kt);
 
    /* 该参数将重新启动定时器 */    
    return HRTIMER_RESTART;  
}
 
static int __init hrtimer_demo_init(void)
{
    printk("hello hrtimer \r\n");
 
    kt = ktime_set(1,10);
 
    /* hrtimer初始化 */
    hrtimer_init(&timer,CLOCK_MONOTONIC,HRTIMER_MODE_REL);
 
    /* hrtimer启动 */
    hrtimer_start(&timer,kt,HRTIMER_MODE_REL);
 
    /* 设置回调函数 */
    timer.function = hrtimer_hander;
 
    return 0;
}
 
static void __exit hrtimer_demo_exit(void)
{
    /* hrtimer注销 */
    hrtimer_cancel(&timer);
    printk("bye hrtimer\r\n");
}
 

module_init(hrtimer_demo_init);
module_exit(hrtimer_demo_exit);
MODULE_LICENSE("GPL");

参考文档 :

1.Linux 下定时器的实现方式分析

https://www.ibm.com/developerworks/cn/linux/l-cn-timers/

2.hrtimer高精度定时器的简单使用【学习笔记】

https://www.cnblogs.com/zzb-Dream-90Time/p/7084916.html

版权声明

原文链接:https://blog.csdn.net/fuyuande/article/details/82193600

转载补充

使用高精度定时器的前提是内核打开了CONFIG_HIGH_RES_TIMERS宏,它的位置如下:

General setup  --->
	Timers subsystem  --->
		[*] High Resolution Timer Support

如果没有配置该选项,驱动程序中虽然可以使用相关的函数,但是,精度就可能会达不到预期效果,准确地说,它就只能使用低精度,至于精度达到什么程度,取决于内核CONFIG_HZ配置的值,那么它的精度就是1/CONFIG_HZ,而CONFIG_HZ一般都是100(至于为什么是100,【链接】里有提及),所以这种情况下它的精度只能达到1s/100Hz=10ms。

你可能感兴趣的:(Linux设备驱动知识整理,linux,内核,定时器)