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

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

问题描述

我的目标是使用 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_RESTARTHRTIMER_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
");
    //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
");

    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
");
    printk(KERN_ERR "in %ldms %ld
", 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
");
    else
        printk(KERN_ERR "Timer cancelled
");
}

有人能准确解释一下在回调函数中重置计时器是如何工作的吗?谢谢!

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

推荐答案

如果您查看 kernel/sched.c 中 sched_rt_period_timer 函数中第 170 行附近的内容,您将看到一个示例用法.基本线是

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.

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

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