当前位置: 技术问答>linux和unix
求助Linux Hrtimer高精度定时器问题
来源: 互联网 发布时间:2017-01-17
本文导语: #include #include #include #include #include #include #include #include #include #include #include #include #include #include MODULE_DESCRIPTION("Just for hrtimer test"); MODULE_AUTHOR("summon (*********@163.com)"); MODULE_LICENSE("GPL"); #define DyS_HRTIMER_US_...
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
MODULE_DESCRIPTION("Just for hrtimer test");
MODULE_AUTHOR("summon (*********@163.com)");
MODULE_LICENSE("GPL");
#define DyS_HRTIMER_US_TO_NS(x) (x)*1000
static struct hrtimer hrtimer_for_test;
unsigned long g_hr_times = 0;
enum hrtimer_restart hrtimer_callback_func(struct hrtimer *p_timer)
{
g_hr_times++;
if (net_ratelimit())
{
printk(KERN_DEBUG "The function hrtimer_callback_func, times is %dn", g_hr_times);
}
return HRTIMER_RESTART;
}
static int hrtimer_init_module(void)
{
printk( KERN_DEBUG "Module hrtimer_init_module initn" );
ktime_t ktime;
unsigned long delay_in_ns = DyS_HRTIMER_US_TO_NS(250);
ktime = ktime_set(1,delay_in_ns);
hrtimer_init(&hrtimer_for_test,CLOCK_MONOTONIC,HRTIMER_MODE_REL);
hrtimer_for_test.function = hrtimer_callback_func;
hrtimer_start(&hrtimer_for_test,ktime,HRTIMER_MODE_REL);
return 0;
}
static void hrtimer_exit_module(void)
{
printk( KERN_DEBUG "Module hrtimer_exit_module exitn" );
}
module_init(hrtimer_init_module);
module_exit(hrtimer_exit_module);
一个测试程序,最后结果是每秒钟打印1千万次左右,总之不是我想要的1+250us的。
哪位用过的同学告诉一声,万分感谢哈,google过了,不好使
|
enum hrtimer_restart hrtimer_callback_func(struct hrtimer *p_timer)
{
g_hr_times++;
if (net_ratelimit())
{
printk(KERN_DEBUG "The function hrtimer_callback_func, times is %dn", g_hr_times);
}
hrtimer_for_test.expires=ktime_add_ns(hrtimer_for_test.expires,(u64)(1*1E9+250*1000);
return HRTIMER_RESTART;
}
在定时器重启前楼主没有更新定时器,所以直接返回HRTIMER_RESTART的后果就是又立即直接调用hrtimer_callback_func函数
{
g_hr_times++;
if (net_ratelimit())
{
printk(KERN_DEBUG "The function hrtimer_callback_func, times is %dn", g_hr_times);
}
hrtimer_for_test.expires=ktime_add_ns(hrtimer_for_test.expires,(u64)(1*1E9+250*1000);
return HRTIMER_RESTART;
}
在定时器重启前楼主没有更新定时器,所以直接返回HRTIMER_RESTART的后果就是又立即直接调用hrtimer_callback_func函数