读书人

Linux Hrtimer高精度定时器有关问题

发布时间: 2012-03-16 16:34:56 作者: rapoo

求助Linux Hrtimer高精度定时器问题

C/C++ code
#include <linux/kernel.h>#include <linux/init.h>#include <linux/module.h>#include <linux/fs.h>#include <linux/netdevice.h>#include <linux/pci.h>#include <linux/device.h>#include <linux/time.h>#include <linux/skbuff.h>#include <linux/jiffies.h>#include <linux/interrupt.h>#include <linux/hrtimer.h>#include <linux/ktime.h>#include <asm/irq.h>MODULE_DESCRIPTION("Just for hrtimer test");MODULE_AUTHOR("summon (*********@163.com)");MODULE_LICENSE("GPL");#define DyS_HRTIMER_US_TO_NS(x)   (x)*1000static 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 %d\n", g_hr_times);    }        return HRTIMER_RESTART;}static int hrtimer_init_module(void){    printk( KERN_DEBUG "Module hrtimer_init_module init\n" );      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 exit\n" );    }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 %d\n", 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函数

读书人网 >UNIXLINUX

热点推荐