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

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

hrtimer:(high resolution timer):

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

/**
 * struct hrtimer - the basic hrtimer structure
 * @node:	timerqueue node, which also manages node.expires,
 *		the absolute expiry time in the hrtimers internal
 *		representation. The time is related to the clock on
 *		which the timer is based. Is setup by adding
 *		slack to the _softexpires value. For non range timers
 *		identical to _softexpires.
 * @_softexpires: the absolute earliest expiry time of the hrtimer.
 *		The time which was given as expiry time when the timer
 *		was armed.
 * @function:	timer expiry callback function
 * @base:	pointer to the timer base (per cpu and per clock)
 * @state:	state information (See bit values above)
 * @is_rel:	Set if the timer was armed relative
 * @is_soft:	Set if hrtimer will be expired in soft interrupt context.
 *
 * The hrtimer structure must be initialized by hrtimer_init()
 */
struct hrtimer {
	struct timerqueue_node		node;
	ktime_t				_softexpires;
	enum hrtimer_restart		(*function)(struct hrtimer *);
	struct hrtimer_clock_base	*base;
	u8				state;
	u8				is_rel;
	u8				is_soft;
};
  • 字段_softexpires :记录了定时器到期时间
  • 字段function:定时器回调函数,该函数返回一个枚举值,它决定了该hrtimer是否需要被重新激活。
定时器超时后会调用回调函数,回调函数结构类似这样:
enum hrtimer_restart		(*function)(struct hrtimer *);
 
enum hrtimer_restart {
	HRTIMER_NORESTART,	/* 不重启定时器 */
	HRTIMER_RESTART,	/* 重启定时器 */
};
在回调函数返回前要手动设置下一次超时时间。
另外,回调函数执行时间不宜过长,因为是在中断上下文中,如果有什么任务的话,最好使用工作队列等机制。
  • 字段state:用于表示hrtimer当前的状态,有以下几种: 
#define HRTIMER_STATE_INACTIVE	0x00  // 定时器未激活
#define HRTIMER_STATE_ENQUEUED	0x01  // 定时器已经被排入红黑树中
#define HRTIMER_STATE_CALLBACK	0x02  // 定时器的回调函数正在被调用
#define HRTIMER_STATE_MIGRATE	0x04  // 定时器正在CPU之间做迁移

 

相关函数:

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

 参数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, /* 第二种和第三种的结合 */
 

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

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

设置超时时间: 

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

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

使用例子:hrtimer.c

 

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/hrtimer.h>
#include <linux/jiffies.h>
 
//定义一个hrtimer
static struct hrtimer timer;
ktime_t kt;
 
//定时器回调函数
static enum hrtimer_restart hrtimer_hander(struct hrtimer *timer)
{
    printk("I am in hrtimer hander\r\n");
    hrtimer_forward(timer,timer->base->get_time(),kt);//hrtimer_forward(timer, now, tick_period);
    return HRTIMER_RESTART;  //重启定时器
}
 
static int __init test_init(void)
{
    printk("---------%s-----------\r\n",__func__);
 
    kt = ktime_set(0,1000000);// 0s  1000000ns  = 1ms 定时
    hrtimer_init(&timer,CLOCK_MONOTONIC,HRTIMER_MODE_REL);
    hrtimer_start(&timer,kt,HRTIMER_MODE_REL);
    timer.function = hrtimer_hander;
    return 0;
}
 
static void __exit test_exit(void)
{
    hrtimer_cancel(&timer);
    printk("------------test over---------------\r\n");
}
 
module_init(test_init);
module_exit(test_exit);
MODULE_LICENSE("GPL");

Makefile文件:

obj-m:=hrtimer.o
PWD:=$(shell pwd)
KERNELPATH:=/lib/modules/$(shell uname -r)/build
all:
	make -C $(KERNELPATH) M=$(PWD) modules
clean:
	make -C $(KERNELPATH) M=$(PWD) clean

执行过程:

 

运行结果: 

 

测试1ms的误差:hrtimer_test.c 

 

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/hrtimer.h>
#include <linux/jiffies.h>
#include <linux/time.h>
#include <linux/timekeeping.h>
 
static struct hrtimer timer;
ktime_t kt;
struct timespec oldtc;
 
static enum hrtimer_restart hrtimer_hander(struct hrtimer *timer)
{
	struct timespec tc;
    printk("I am in hrtimer hander : %lu... \r\n",jiffies);
	getnstimeofday(&tc); //获取新的当前系统时间
	
	printk("interval: %ld - %ld = %ld us\r\n",tc.tv_nsec/1000,oldtc.tv_nsec/1000,tc.tv_nsec/1000-oldtc.tv_nsec/1000);
	oldtc = tc;
    hrtimer_forward(timer,timer->base->get_time(),kt);
    return HRTIMER_RESTART;
}
 
static int __init test_init(void)
{
    printk("---------test start-----------\r\n");
    
	getnstimeofday(&oldtc);  //获取当前系统时间
    kt = ktime_set(0,1000000);//1ms
    hrtimer_init(&timer,CLOCK_MONOTONIC,HRTIMER_MODE_REL);
    hrtimer_start(&timer,kt,HRTIMER_MODE_REL);
    timer.function = hrtimer_hander;
    return 0;
}
 
static void __exit test_exit(void)
{
    hrtimer_cancel(&timer);
    printk("------------test over---------------\r\n");
}
 
module_init(test_init);
module_exit(test_exit);
MODULE_LICENSE("GPL");
 

 

Makefile文件参考上面。

执行步骤:

运行结果:

 

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

 

 

 

 

 

  • 2
    点赞
  • 33
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
hrtimerLinux内核中的一个重要功能,可提供高精度定时器功能。它可以在纳秒级别的精度下进行定时,主要用于实时任务或需要高精度定时的应用。 使用hrtimer高精度的方法如下: 1. 定义hrtimer使用hrtimer之前,首先需要定义一个hrtimer对象。可以通过声明struct hrtimer类型的变量来完成,例如: struct hrtimer my_hrtimer; 2. 初始化hrtimer: 初始化hrtimer对象可以使用hrtimer_init函数,该函数有三个参数:hrtimer对象,时钟源和是相对还是绝对时间。根据具体需要,可以选择不同的时钟源,如CLOCK_MONOTONIC或CLOCK_REALTIME。 3. 设置并启动hrtimer: 设置hrtimer对象的定时器周期和回调函数,然后使用hrtimer_start函数启动hrtimer。设置定时器周期可以使用hrtimer_set_periodic或hrtimer_set_expires函数。回调函数会在定时器到期时被调用。 4. 处理hrtimer到期: 当hrtimer到期时,会触发之前设置的回调函数,我们可以在回调函数中执行相应的操作。 5. 停止hrtimer: 如果需要停止hrtimer,可以使用hrtimer_cancel函数。 综上所述,通过使用Linux内核hrtimer模块,我们可以实现高精度的定时功能。这对于实时任务或需要高精度定时的应用至关重要。通过定义、初始化、设置和启动hrtimer对象,可以实现定时器的定期触发和相应的操作。同时,可以根据需要选择不同的时钟源来满足特定的应用需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值