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 ...
随机推荐
- 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 ...
- LeetCode - 70. Climbing Stairs(0ms)
You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...
- java设计模式之门面模式以及在java中作用
门面模式在Tomcat中有多处使用,在Request和Response对象封装,从ApplicationContext到ServletContext封装中都用到了这种设计模式. 一个系统可以有几个门面 ...
- HDU 1698 Just a Hook(线段树区间覆盖)
线段树基本操作练习,防手生 #include <cstdio> #include <cstring> #include <cstdlib> #define lson ...
- URAL 1932 The Secret of Identifier(容斥)
Description Davy Jones: You've been captain of the Black Pearl for 13 years. That was our agreement. ...
- Unity UGUI 图片 轴对称效果 减少资源
制作UI的过程中,为了节省资源,对称的图一般美术切一半给我们 手动拼图 有时会出现拼接处出现裂缝或重叠 调整大小时也不方便 得一块一块调整 所以就用BaseMeshEffect 的ModifyMesh ...
- 【积累】LinqToSql复合查询结果转DataTable数据
最近的项目用到了大量的复合查询结果用于数据源,绑定到数据控件上. 为了方便,我们把它转换成DataTable的数据源形式.请看下面的示例: 1)思考自己需要的数据,然后组合,因此创建一个新的类: // ...
- Linux上删除空行的方法
grep . data.txt grep-v'^$' data.txt grep'[^$]' data.txt sed'/^$/d' data.txt sed'/^\s*$/d' data.txt # ...
- Docker实战系列一:初识Docker for Windows
windows下安装Docker官网教程Install Docker for Windows Docker配置官网教程Get started with Docker for Windows
- post 中文乱码处理 接受的编码--->解码成字节数组(无任何编码形式)----->编码成想要的格式