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

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

hrtimer:(high resolution timer):

高精度定时器,为我们提供了纳秒级别的定时精度,以满足对精确时间有迫切需求的应用程序或内核驱动。因原有定时器已经相对完善,避免大幅度改动,内核为高精度定时器重新设计了一台软件架构。

1/** 2 * struct hrtimer - the basic hrtimer structure 3 * @node: timerqueue node, which also manages node.expires, 4 * the absolute expiry time in the hrtimers internal 5 * representation. The time is related to the clock on 6 * which the timer is based. Is setup by adding 7 * slack to the _softexpires value. For non range timers 8 * identical to _softexpires. 9 * @_softexpires: the absolute earliest expiry time of the hrtimer. 10 * The time which was given as expiry time when the timer 11 * was armed. 12 * @function: timer expiry callback function 13 * @base: pointer to the timer base (per cpu and per clock) 14 * @state: state information (See bit values above) 15 * @is_rel: Set if the timer was armed relative 16 * @is_soft: Set if hrtimer will be expired in soft interrupt context. 17 * 18 * The hrtimer structure must be initialized by hrtimer_init() 19 */ 20struct hrtimer { 21 struct timerqueue_node node; 22 ktime_t _softexpires; 23 enum hrtimer_restart (*function)(struct hrtimer *); 24 struct hrtimer_clock_base *base; 25 u8 state; 26 u8 is_rel; 27 u8 is_soft; 28}; 29
  • 字段_softexpires :记录了定时器到期时间

  • 字段function:定时器回调函数,该函数返回一个枚举值,它决定了该hrtimer是否需要被重新激活。

1定时器超时后会调用回调函数,回调函数结构类似这样: 2enum hrtimer_restart (*function)(struct hrtimer *); 3 4enum hrtimer_restart { 5 HRTIMER_NORESTART, /* 不重启定时器 */ 6 HRTIMER_RESTART, /* 重启定时器 */ 7}; 8在回调函数返回前要手动设置下一次超时时间。 9另外,回调函数执行时间不宜过长,因为是在中断上下文中,如果有什么任务的话,最好使用工作队列等机制。 10
  • 字段state:用于表示hrtimer当前的状态,有以下几种: 

1#define HRTIMER_STATE_INACTIVE 0x00 // 定时器未激活 2#define HRTIMER_STATE_ENQUEUED 0x01 // 定时器已经被排入红黑树中 3#define HRTIMER_STATE_CALLBACK 0x02 // 定时器的回调函数正在被调用 4#define HRTIMER_STATE_MIGRATE 0x04 // 定时器正在CPU之间做迁移 5

 

相关函数:

**初始化:**void hrtimer_init(struct hrtimer *timer, clockid_t clock_id, enum hrtimer_mode mode);

1 参数timer是hrtimer指针, 2 参数clock_id有如下常用几种选项: 3 CLOCK_REALTIME //实时时间,如果系统时间变了,定时器也会变 4 CLOCK_MONOTONIC //递增时间,不受系统影响 5 参数mode有如下几种选项: 6 HRTIMER_MODE_ABS = 0x0, /* 绝对模式 */ 7 HRTIMER_MODE_REL = 0x1, /* 相对模式 */ 8 HRTIMER_MODE_PINNED = 0x02, /* 和CPU绑定 */ 9 HRTIMER_MODE_ABS_PINNED = 0x02, /* 第一种和第三种的结合 */ 10 HRTIMER_MODE_REL_PINNED = 0x03, /* 第二种和第三种的结合 */ 11 12

**启动定时器:**hrtimer_start(struct hrtimer *timer, ktime_t tim, const enum hrtimer_mode mode); 

1参数timer是hrtimer指针 2参数tim是时间,可以使用ktime_set()函数设置时间, 3参数mode和初始化的mode参数一致 4

设置超时时间: 

1/* 2 * 单位为秒和纳秒组合 3 */ 4ktime_t ktime_set(const long secs, const unsigned long nsecs)5 6/* 设置超时时间,当定时器超时后可以用该函数设置下一次超时时间 */ 7hrtimer_forward_now(struct hrtimer *timer, ktime_t interval) 8

关闭定时器: int hrtimer_cancel(struct hrtimer *timer);

使用例子:hrtimer.c

 

1#include <linux/module.h> 2#include <linux/kernel.h> 3#include <linux/hrtimer.h> 4#include <linux/jiffies.h> 5 6//定义一个hrtimer 7static struct hrtimer timer; 8ktime_t kt; 9 10//定时器回调函数 11static enum hrtimer_restart hrtimer_hander(struct hrtimer *timer) 12{ 13 printk("I am in hrtimer hander\r\n"); 14 hrtimer_forward(timer,timer->base->get_time(),kt);//hrtimer_forward(timer, now, tick_period); 15 return HRTIMER_RESTART; //重启定时器 16} 17 18static int __init test_init(void) 19{ 20 printk("---------%s-----------\r\n",__func__); 21 22 kt = ktime_set(0,1000000);// 0s 1000000ns = 1ms 定时 23 hrtimer_init(&timer,CLOCK_MONOTONIC,HRTIMER_MODE_REL); 24 hrtimer_start(&timer,kt,HRTIMER_MODE_REL); 25 timer.function = hrtimer_hander; 26 return 0; 27} 28 29static void __exit test_exit(void) 30{ 31 hrtimer_cancel(&timer); 32 printk("------------test over---------------\r\n"); 33} 34 35module_init(test_init); 36module_exit(test_exit); 37MODULE_LICENSE("GPL"); 38

Makefile文件:

1obj-m:=hrtimer.o 2PWD:=$(shell pwd) 3KERNELPATH:=/lib/modules/$(shell uname -r)/build 4all: 5 make -C $(KERNELPATH) M=$(PWD) modules 6clean: 7 make -C $(KERNELPATH) M=$(PWD) clean 8

执行过程:

 

运行结果: 

 

**测试1ms的误差:**hrtimer_test.c 

 

1#include <linux/module.h> 2#include <linux/kernel.h> 3#include <linux/hrtimer.h> 4#include <linux/jiffies.h> 5#include <linux/time.h> 6#include <linux/timekeeping.h> 7 8static struct hrtimer timer; 9ktime_t kt; 10struct timespec oldtc; 11 12static enum hrtimer_restart hrtimer_hander(struct hrtimer *timer) 13{ 14 struct timespec tc; 15 printk("I am in hrtimer hander : %lu... \r\n",jiffies); 16 getnstimeofday(&tc); //获取新的当前系统时间 17 18 printk("interval: %ld - %ld = %ld us\r\n",tc.tv_nsec/1000,oldtc.tv_nsec/1000,tc.tv_nsec/1000-oldtc.tv_nsec/1000); 19 oldtc = tc; 20 hrtimer_forward(timer,timer->base->get_time(),kt); 21 return HRTIMER_RESTART; 22} 23 24static int __init test_init(void) 25{ 26 printk("---------test start-----------\r\n"); 27 28 getnstimeofday(&oldtc); //获取当前系统时间 29 kt = ktime_set(0,1000000);//1ms 30 hrtimer_init(&timer,CLOCK_MONOTONIC,HRTIMER_MODE_REL); 31 hrtimer_start(&timer,kt,HRTIMER_MODE_REL); 32 timer.function = hrtimer_hander; 33 return 0; 34} 35 36static void __exit test_exit(void) 37{ 38 hrtimer_cancel(&timer); 39 printk("------------test over---------------\r\n"); 40} 41 42module_init(test_init); 43module_exit(test_exit); 44MODULE_LICENSE("GPL"); 45 46

 

Makefile文件参考上面。

执行步骤:

运行结果:

 

 可见出现一个340微秒和1284微秒,仍存在一定误差。

 

 

 

 

 

代码交流 2021