本文转载自:http://blog.csdn.net/dean_gdp/article/details/25481225

hrtimer的基本操作

Linux的传统定时器通过时间轮算法实现(timer.c),但hrtimer通过红黑树算法实现。在struct hrtimer里面有一个node域,类型为struct rb_node,这个域代表了hrtimer在红黑树中的位置。

hrtimer_start

hrtimer_start函数将一个hrtimer加入到一个按照到期时间排序的红黑树中,其主要流程为:

int hrtimer_start(struct hrtimer *timer, ktime_t tim, const enum hrtimer_mode mode);

// 根据time和mode参数的值计算hrtimer的超时时间,并设置到timer->expire域。

// expire设置的是绝对时间,所以如果参数mode的值为HRTIMER_MODE_REL(即参数tim的值为相对时间),那么需要将tim的值修正为绝对时间:

// expire = tim + timer->base->get_time()。(注意本文只研究单CPU的情况)

//调用enqueue_hrtimer,将hrtimer加入到红黑树中。

hrtimer的到期

hrtimer在hrtimer_run_queues函数中判断是否到期执行。hrtimer_run_queues的调用链为:linux的系统时钟函数->update_process_times->run_local_timers->hrtimer_run_queues。

void hrtimer_run_queues(void)

// 判断是否是高精度模式,如果是高精度模式,立即返回。本文暂不考虑这种情况。

// 对每一个时钟源(REAL和MONOTONIC)的红黑树,按到期先后顺序检查hrtimer,看它们是否到期(将定时器与时钟源的softirq_time比较)。如果到期,就把这个到期的定时器取出,然后按照定时器的具体模式执行相应的操作:

如果定时器模式为HRTIMER_CB_SOFTIRQ,那么将定时器搬到hrtimer_cpu_base的cb_pending队列

调用__run_hrtimer,在__run_hrtimer中执行定时器的回调函数。

在没有配置高精度模式时,cb_pending队列中的定时器会在T_SOFTIRQ软中断中执行。调用链为

run_timer_softirq-> hrtimer_run_pending-> run_hrtimer_pending-> run_hrtimer_pending

hrtimer_cancel

hrtimer_cancel函数的作用是删除一个正在排队的定时器。这里分三种情况,一种是定时器已到期,并且设置了软中断模式;第二种是没有到期,还在红黑树中;第三种是定时器正在执行。

l 第一种情况,定时器被挂在hrtimer_cpu_base的cb_pending队列中,所以需要把它从pending队列中移出。

l 第二种情况,定时器还在红黑树中,那么把它从红黑树中移出。由于本文暂时只考虑高精度没有打开的情况,所以先不研究定时器正好排在红黑树第一个时的情况(即代码中调用hrtimer_force_reprogram函数的部分)。

l 第三种情况删除失败,hrtimer_cancel函数会循环重试,等到定时器执行完的时候再删除。(这在多CPU系统中可能会发生)

#include <linux/module.h>

#include <linux/kernel.h>
#include <linux/hrtimer.h>
#include <linux/jiffies.h>

static struct hrtimer timer;
ktime_t kt;

static enum hrtimer_restart hrtimer_handler(struct hrtimer *timer)
{
//kt = ktime_set(1, 10);
printk(" ------ I am in hrtimer -----\n");
hrtimer_forward(timer, timer->base->get_time(), kt);
return HRTIMER_RESTART;
}

static int __init test_init(void)
{

pr_info("timer resolution: %lu\n", TICK_NSEC);
kt = ktime_set(1, 10); /* 1 sec, 10 nsec */
hrtimer_init(&timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
//hrtimer_set_expires(&timer, kt);
hrtimer_start(&timer, kt, HRTIMER_MODE_REL);
timer.function = hrtimer_handler;

printk("\n-------- test start ---------\n");
return 0;
}

static void __exit test_exit(void)
{
hrtimer_cancel(&timer);
printk("-------- test over ----------\n");
return;
}

MODULE_LICENSE("GPL");

module_init(test_init);

module_exit(test_exit);

转载地址:http://blog.chinaunix.NET/uid-361890-id-257337.html

http://blog.csdn.net/walkingman321/article/details/6133171

Linux 高精度定时器hrtimer 使用示例【转】的更多相关文章

  1. linux 高精度定时器例子

    //author:DriverMonkey //phone:13410905075 //mail:bookworepeng@Hotmail.com //qq:196568501 #include &l ...

  2. Linux时间子系统之六:高精度定时器(HRTIMER)的原理和实现

    转自:http://blog.csdn.net/droidphone/article/details/8074892 上一篇文章,我介绍了传统的低分辨率定时器的实现原理.而随着内核的不断演进,大牛们已 ...

  3. Linux下的hrtimer高精度定时器【转】

    转自:http://blog.csdn.net/waverider2012/article/details/38305785 hrtimer高精度定时器的interval由ktime_set(cons ...

  4. 使用linux内核hrtimer高精度定时器实现GPIO口模拟PWM,【原创】

    关键词:Android  linux hrtimer 蜂鸣器  等待队列 信号量 字符设备 平台信息:内核:linux3.4.39 系统:android/android5.1平台:S5P4418  作 ...

  5. linux下jiffies定时器和hrtimer高精度定时器【转】

    本文转载自:http://blog.csdn.net/dosculler/article/details/7932315 一.jiffies定时器,HZ=100,精度只能达到10ms. 注:采用jif ...

  6. 芯灵思Sinlinx A64开发板Linux内核定时器编程

    开发平台 芯灵思Sinlinx A64 内存: 1GB 存储: 4GB 开发板详细参数 https://m.tb.cn/h.3wMaSKm 开发板交流群 641395230 Linux 内核定时器是内 ...

  7. 全志A33开发板Linux内核定时器编程

    开发平台 * 芯灵思SinlinxA33开发板 淘宝店铺: https://sinlinx.taobao.com/ 嵌入式linux 开发板交流 QQ:641395230 Linux 内核定时器是内核 ...

  8. 芯灵思SinlinxA33开发板Linux内核定时器编程

    开发平台 * 芯灵思SinlinxA33开发板 淘宝店铺: https://sinlinx.taobao.com/ 嵌入式linux 开发板交流 QQ:641395230 Linux 内核定时器是内核 ...

  9. [转]Linux中find常见用法示例

    Linux中find常见用法示例[转]·find   path   -option   [   -print ]   [ -exec   -ok   command ]   {} \;find命令的参 ...

随机推荐

  1. cf468B Two Sets

    Little X has n distinct integers: p1, p2, ..., pn. He wants to divide all of them into two sets A an ...

  2. phoenixframework集成了所有自动化测试的思想的平台。mark一下。

    phoenixframework http://www.cewan.la/

  3. 如何让Gridview在没有数据的时候显示表头[没有使用SqlDataSource控件时]

    原文发布时间为:2008-08-03 -- 来源于本人的百度文章 [由搬家工具导入] 要看全文请点击http://blog.csdn.net/windok2004/archive/2007/10/28 ...

  4. Android 网络编程之HttpURLConnection运用

    Android 网络编程之HttpURLConnection 利用HttpURLConnection对象,我们可以从网络中获取网页数据. 01 URL url = new URL("http ...

  5. ORA-01940: cannot drop a user that is currently connected 问题解析

    https://www.linuxidc.com/Linux/2012-12/76448.htm

  6. Codeforces Round #512 (Div. 2, based on Technocup 2019 Elimination Round 1) E. Vasya and Good Sequences

    题目链接 官网题解写的好清楚,和昨晚Aguin说的一模一样…… 这题只和每个数1的个数有关,设每个数1的个数的数组为$b$,就是首先一段如果是好的,要满足两个条件: 1.这一段$b$数组和为偶数,因为 ...

  7. 在C#中使用Json.Net进行序列化和反序列化及定制化

    序列化(Serialize)是将对象转换成字节流,并将其用于存储或传输的过程,主要用途是保存对象的状态,以便在需要时重新创建该对象:反序列化(Deserialize)则是将上面的字节流转换为相应对象的 ...

  8. Redhat 5 无法安装elfutils-libelf-devel-0.137问题

    http://whr25.blog.sohu.com/263584338.html 问题: RHEL5.5安装oracle11gR2的时候需要安装elfutils-libelf-devel-0.137 ...

  9. .net core webapi jwt 更为清爽的认证

    原文:.net core webapi jwt 更为清爽的认证 我的方式非主流,控制却可以更加灵活,喜欢的朋友,不妨花一点时间学习一下 jwt认证分为两部分,第一部分是加密解密,第二部分是灵活的应用于 ...

  10. Android:图片中叠加文字,支持拖动改变位置

    之所以做了这么一个Demo,是由于近期项目中有一个奇葩的需求:用户拍摄照片后,分享到微信的同一时候加入备注,想获取用户在微信的弹出框输入的内容.保存在自己的server上.而其实,这个内容程序是无法获 ...