Linux下的hrtimer高精度定时器【转】
转自:http://blog.csdn.net/waverider2012/article/details/38305785
hrtimer高精度定时器的interval由ktime_set(const long secs, const unsigned long nsecs)决定,可做到ns级。此处的例子为5ms interval:
- #include <linux/kernel.h>
- #include <linux/module.h>
- #include <linux/hrtimer.h>
- #include <linux/ktime.h>
- MODULE_LICENSE("GPL");
- static struct hrtimer hr_timer;
- static struct work_struct wq_hrtimer;
- static ktime_t ktime;
- static unsigned int interval=5000; /* unit: us */
- struct timespec uptimeLast;
- static unsigned int count=0;
- #define COUNT_INTERVAL 4000
- unsigned long long diff_tv(struct timespec start, struct timespec end) {
- return (end.tv_sec-start.tv_sec)*1000000000+(end.tv_nsec-start.tv_nsec);
- }
- enum hrtimer_restart my_hrtimer_callback( struct hrtimer *timer )
- {
- schedule_work(&wq_hrtimer);
- return HRTIMER_NORESTART;
- }
- static void wq_func_hrtimer(struct work_struct *work)
- {
- struct timespec uptime;
- hr_timer.function = my_hrtimer_callback;
- ktime = ktime_set( interval/1000000, (interval%1000000)*1000 );
- hrtimer_start(&hr_timer, ktime, HRTIMER_MODE_REL );
- /* print time every COUNT_INTERVAL*interval second*/
- if(count%COUNT_INTERVAL==0)
- {
- do_posix_clock_monotonic_gettime(&uptime);
- printk(KERN_INFO"hrtimer:%9lu sec, %9lu ns, interval_delay=%lu ns\n",
- (unsigned long) uptime.tv_sec, uptime.tv_nsec,
- (unsigned long)(diff_tv(uptimeLast, uptime)-interval*1000*COUNT_INTERVAL) \
- /COUNT_INTERVAL);
- uptimeLast=uptime;
- }
- count++;
- }
- static int __init module_hrtimer_init( void )
- {
- struct timespec uptime;
- printk(KERN_INFO"HR Timer module installing\n");
- hrtimer_init( &hr_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL );
- ktime = ktime_set( interval/1000000, (interval%1000000)*1000 );
- hr_timer.function = my_hrtimer_callback;
- hrtimer_start( &hr_timer, ktime, HRTIMER_MODE_REL );
- do_posix_clock_monotonic_gettime(&uptime);
- uptimeLast = uptime;
- printk(KERN_INFO"hrtimer:%9lu sec, %9lu ns\n", (unsigned long) uptime.tv_sec,
- uptime.tv_nsec );
- INIT_WORK(&wq_hrtimer, wq_func_hrtimer);
- return 0;
- }
- static void __exit module_hrtimer_exit( void )
- {
- int ret;
- ret = hrtimer_cancel( &hr_timer );
- if (ret)
- printk("The timer was still in use...\n");
- printk("HR Timer module uninstalling\n");
- return;
- }
- module_init(module_hrtimer_init);
- module_exit(module_hrtimer_exit);
如果在my_hrtimer_callback()里面直接返回HRTIMER_RESTART会导致立即重新进入my_hrtimer_callback()。这时shell对输入没有任何响应。
所以为了解决这个问题,创建了一个work queue,由my_hrtimer_callback() enqueue这个工作队列。在work queue的处理函数里面重启hrtimer。
但是这样带来的负面影响是进入hrtimer_callback和wq_func被调用之间有Linux系统调度引入的延迟,导致interval出现误差。经过实测,在ZC706缺省配置下,这个延迟大约是17.5us (hrtimer interval为5ms,每20秒计算一次interval误差)。
- root@zynq:~/nfs/hrtimer# insmod hrtimer.ko
- HR Timer module installing
- hrtimer: 2900 sec, 993366078 ns
- hrtimer: 2900 sec, 998395278 ns, interval_delay=369966 ns
- hrtimer: 2921 sec, 69525447 ns, interval_delay=17782 ns
- hrtimer: 2941 sec, 139764655 ns, interval_delay=17559 ns
- hrtimer: 2961 sec, 210029519 ns, interval_delay=17566 ns
- hrtimer: 2981 sec, 280465631 ns, interval_delay=17609 ns
- hrtimer: 3001 sec, 350677038 ns, interval_delay=17552 ns
- hrtimer: 3021 sec, 420625114 ns, interval_delay=17487 ns
- hrtimer: 3041 sec, 490744847 ns, interval_delay=17529 ns
Linux下的hrtimer高精度定时器【转】的更多相关文章
- linux下jiffies定时器和hrtimer高精度定时器【转】
本文转载自:http://blog.csdn.net/dosculler/article/details/7932315 一.jiffies定时器,HZ=100,精度只能达到10ms. 注:采用jif ...
- 使用linux内核hrtimer高精度定时器实现GPIO口模拟PWM,【原创】
关键词:Android linux hrtimer 蜂鸣器 等待队列 信号量 字符设备 平台信息:内核:linux3.4.39 系统:android/android5.1平台:S5P4418 作 ...
- Linux下的微秒级定时器: usleep, nanosleep, select, pselect
Linux下的微秒级定时器: usleep, nanosleep, select, pselect 标签: linuxnulldelaystructdate 2012-02-07 23:29 4979 ...
- hrtimer高精度定时器的简单使用【学习笔记】
#include <linux/module.h> #include <linux/kernel.h> #include <linux/hrtimer.h> #in ...
- 高精度定时器实现 z
1背景Permalink .NET Framework 提供了四种定时器,然而其精度都不高(一般情况下 15ms 左右),难以满足一些场景下的需求. 在进行媒体播放.绘制动画.性能分析以及和硬件交互时 ...
- Linux时间子系统之六:高精度定时器(HRTIMER)的原理和实现
转自:http://blog.csdn.net/droidphone/article/details/8074892 上一篇文章,我介绍了传统的低分辨率定时器的实现原理.而随着内核的不断演进,大牛们已 ...
- Linux下的定时器:alarm()与setitimer()
Linux下的定时器有两种,以下分别介绍: 1.alarm 如果不要求很精确的话,用alarm()和signal()就够了 unsigned int alarm(unsigned int second ...
- Linux下的定时器
以下摘自linux下的man文件:(man getitimer) #include <sys/time.h> int getitimer(int which, struct iti ...
- Linux下定时器
http://unix8.net/linux%E4%B8%8B%E5%AE%9A%E6%97%B6%E5%99%A8.html 一. 基础知识 1.时间类型.Linux下常用的时间类型有4个:time ...
随机推荐
- 深挖 NGUI 基础 之UICamera (二)
一.UI Camera作用 UICamera需要挂载在摄像机上才能发挥作用 UICamera仅负责 发送NGUI 事件 到 脚本所附加的摄像机中看得到的对象,比如我自定义了NGUI层(在Inspect ...
- JavaScript调试中Console命令
JS调试中,用console.log 感觉比 alert 好用,不用弹出窗口,还要关闭.除了console.log()其他命令没怎么用过,先在这里记一下,用到时在看看 一.显示信息的命令 consol ...
- Spark性能超过Hadoop百倍
Spark在偷换概念,Hadoop跑硬盘,Spark跑内存,地球人都知道,内存的速度可是远超硬盘一个量级,超过100倍又有什么奇怪的.如果要比,咱们都拿硬盘来跑跑看!
- Leetcode 54. Spiral Matrix & 59. Spiral Matrix II
54. Spiral Matrix [Medium] Description Given a matrix of m x n elements (m rows, n columns), return ...
- BZOJ 4031 HEOI2015 小Z的房间 基尔霍夫矩阵+行列式+高斯消元 (附带行列式小结)
原题链接:http://www.lydsy.com/JudgeOnline/problem.php?id=4031 Description 你突然有了一个大房子,房子里面有一些房间.事实上,你的房子可 ...
- 在Android Studio中创建(或添加)第一个Hello World应用程序
下面我们将使用Android Studio创建第一个简单的Hello World应用程序. 1.打开Android Studio,加载画面如下图所示: 2.选择”Start a new Andro ...
- Week7 Teamework from Z.XML-NABC
NABC 引言:我们团队计划做一个手机端的类RPG2d游戏.之所以我们定义为类RPG,是因为我们希望弱化RPG在游戏中的概念--减少或者排除人物对话等较为无趣的内容,而将重点放在玩家的娱乐享受中.为了 ...
- HDU 1798 Tell me the area
http://acm.hdu.edu.cn/showproblem.php?pid=1798 Problem Description There are two circles in the ...
- HTML精确定位之位置参数乱炖一锅
一.前言 公司项目,需要在一个图片的右上角放置一个类似“X”的东西(其实是需要显示一个数字,就像微信一样,在右上角显示几个消息),然后需要用到html的定位,看了几个网上的例子,恍惚间看到了一个off ...
- Hibernate配置文件说明
<property name="hbm2ddl.auto">create</property> create - 启动Hibernate前先删除表,重新创建 ...