Linux的定时器应用;
linux 内核 高精度定时器(hrtimer)实现机制

 

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

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

hrtimer:(high resolution timer):

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

数据结构:定义在<Linux/hrtimer.h>中

 

相关函数:

初始化: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);

  1. 参数timer是hrtimer指针
  2.  参数tim是时间,可以使用ktime_set()函数设置时间,
  3.  参数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_tt.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");

 参考案例网址:

Linux驱动开发高精度定时器的精度测量 - 腾讯云开发者社区-腾讯云 (tencent.com)

Linux高精度定时器hrtimer使用实例 - 腾讯云开发者社区-腾讯云 (tencent.com)

 

posted on 2023-04-18 14:54  六翅天蚕  阅读(511)  评论(0)    收藏  举报