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 ...
随机推荐
- LAXCUS对数据存储的优化
LAXCUS兼容行存储(NSM)和列存储(DSM)两种数据模型,实现了混合存储.同时在分布环境里,做到将数据的分发和备份自动处理,这样就不再需要人工干预了. 行存储,为了兼容广大用户对 ...
- zabbix从入门到精通
第1章 zabbix监控 1.1 为什么要监控 在需要的时刻,提前提醒我们服务器出问题了 当出问题之后,可以找到问题的根源 网站/服务器 的可用性 1.1.1 网站可用性 在软件系统的高可靠性(也 ...
- 简易cmake多文件多目录工程模板
今天心血来潮,想在服务器上试试写libevent的工程是什么感受,那第一步就是学会怎么用cmake建工程,之前也没接触过cmake,然后一上午,比较懵逼,下午看实验室哥们给的一个教程,然后,慢慢理解C ...
- Pro Git - 笔记1
Getting Started About Version Control Local Version Control Systems Centralized Version Control Syst ...
- 前台如何处理后台返回的json数据
后台返回的json数据格式: { "state": true, "data": { "id": 0, "name": & ...
- Reactor模式是什么(转载)
一.Reactor模式是什么反应器设计模式(Reactor pattern)是一种为处理并发服务请求,并将请求提交到一个或者多个服务处理程序的事件设计模式.当客户端请求抵达后,服务处理程序使用多路分配 ...
- Visual Studio 2012,创建工程Build Driver,基于纯Source Code.
拿到一堆纯代码,怎么去Create Project,设置Include路径,lib路径,要不要Pre-compile技术,配置Project之间的依赖关系. SourcesConverter Bas ...
- C#中多态
我的理解是:通过继承实现的不同对象调用相同的方法,表现出不同的行为,称之为多态. 1: OverRide 实现多态 public class Animal { public virtual void ...
- [洛谷P3743]kotori的设备
题目大意:ことり有$n$个设备,每个设备每秒共减少$a_i$能量(也就是说每一瞬间都在减少,而不是在一个时刻突然减少),开始前有$b_i$能量,ことり还有一个充电宝,无限能量,每秒共可以提供$p$的能 ...
- [Leetcode] 3sum-closest 给定值,最为相近的3数之和
Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...