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

lzy0001sl 2010-08-29 02:25:42

#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)*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 %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过了,不好使
...全文
771 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
ddcien 2011-11-18
  • 打赏
  • 举报
回复
如需要阅读该回复,请登录或注册CSDN!
jiaguang3010717 2010-09-01
  • 打赏
  • 举报
回复
顶!.............
yufeng1213 2010-09-01
  • 打赏
  • 举报
回复
嘿嘿,不好意思我少打了一个括号。
hrtimer_for_test.expires=ktime_add_ns(hrtimer_for_test.expires,(u64)(1*1E9+250*1000));
yufeng1213 2010-09-01
  • 打赏
  • 举报
回复
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函数
lzy0001sl 2010-08-30
  • 打赏
  • 举报
回复
看看有没有人会的,再加分也行

4,436

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 内核源代码研究区
社区管理员
  • 内核源代码研究区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧