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.
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!