hrtimer重复任务在Linux内核中 [英] hrtimer repeating task in the Linux kernel

查看:988
本文介绍了hrtimer重复任务在Linux内核中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是使用 hrtimer 结构在linux内核中创建一个重复的任务。我希望它每500毫秒重现一次。

My goal is to create a recurring task in the linux kernel using the hrtimer struct. I would like it to recur every 500 ms.

然而,我对于 hrtimer 在linux内核中(参见 linux / hrtimer.h )。我知道指定的时间,并且回调应该返回 HRTIMER_RESTART HRTIMER_NORESTART 。我在网上找到一些资源,说明定时器需要在回调中使用 hrtimer_forward 方法重置。然而,我看到的来源有点不清楚如何添加时间的工作原理。这是我到目前为止的代码:

However, I'm a little confused about how hrtimer works in the linux kernel (see linux/hrtimer.h). I know that the time is specified, and the callback should return either HRTIMER_RESTART or HRTIMER_NORESTART. I've found some sources online that state that the timer needs to be reset in the callback using the hrtimer_forward method. However, the sources I've seen are a little unclear on how adding the time works. Here's the code I have so far:

static struct hrtimer timer;

static enum hrtimer_restart timer_callback(struct hrtimer *timer)
{
    printk(KERN_ERR "Callback\n");
    //I know something needs to go here to reset the timer
    return HRTIMER_RESTART;
}

static int init_timer(void)
{   
    ktime_t ktime;
    unsigned long delay_in_ms = 500L;
    printk(KERN_ERR "Timer being set up\n");

    ktime = ktime_set(0,delay_in_ms*1E6L);
    hrtimer_init(&timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);

    timer.function = &timer_callback;
    printk(KERN_ERR "Timer starting to fire\n");
    printk(KERN_ERR "in %ldms %ld\n", delay_in_ms, jiffies);

    hrtimer_start(&timer, ktime, HRTIMER_MODE_REL);
    return 0;
}

static void clean_load_balancing_timer(void)
{
    int cancelled = hrtimer_cancel(&timer);

    if (cancelled)
        printk(KERN_ERR "Timer still running\n");
    else
        printk(KERN_ERR "Timer cancelled\n");
}



谢谢!

Can someone explain exactly how resetting the timer would work in the callback function? Thanks!

推荐答案

如果你在函数sched_rt_period_timer中的第170行查看kernel / sched.c, 。基本行是

If you look in kernel/sched.c around line 170 in the function sched_rt_period_timer, you will see an example usage. The essential lines are

now = hrtimer_cb_get_time(timer);                               
overrun = hrtimer_forward(timer, now, rt_b->rt_period);

现在获取定时器的当前时间为ktime_t和rt_b-> rt_period是另一个ktime_t,其中提前定时器。 hrtimer的到期时间将连续增加该周期,直到它大于当前时间。如果花费了多于一个时间段的添加以获得大于当前时间的到期时间,则返回值将大于1(表示更多的超时)。

Now get's the timer's current time as a ktime_t and rt_b->rt_period is another ktime_t specifying the period at which to advance timer. The expiration time of the hrtimer will be continuously incremented by the period until it is greater than the current time. If it took more than one addition of the period to get the expiration time greater than the current time, the return value will greater than 1 (indicating more overrruns). It can be zero, if the timer expire didn't get advanced at all.

参考: http://lwn.net/Articles/167897/

它使用的API来自不同版本的内核,所以一些参数已经改变。基本的想法是一样的。

The API it uses is from a different version of the kernel so some of the arguments have changed. The basic idea is still the same.

这篇关于hrtimer重复任务在Linux内核中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆