Find the answer to your Linux question:
Results 1 to 4 of 4
Hello ! Right now I'm testing and learning kernel hrtimers functionality. I wrote this piece of module code: Code: ... 15 static struct hrtimer hr_timer; 16 static int loop_cnt = ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Jun 2010
    Posts
    7

    hrtimer callback function - restart timer


    Hello !

    Right now I'm testing and learning kernel hrtimers functionality. I wrote this piece of module code:

    Code:
     ...
     15 static struct hrtimer hr_timer;
     16 static int loop_cnt = 0;
     17 
     18 enum hrtimer_restart hrtimer_handler(struct hrtimer *timer)
     19 {
     20     if(loop_cnt < 10)
     21     {
     22         loop_cnt ++;
     23         printk("HR timer function %d s \n", loop_cnt);
     24         return HRTIMER_RESTART;
     25     }
     26     else
     27     {
     28         printk("HR timer function loop end \n");
     29         return HRTIMER_NORESTART;
     30     }
     31 }
     32 
     33 static int init_mod(void)
     34 {
     35     int ret = 0;
     36     ktime_t ktime;
     37     ktime = ktime_set(3, 0);
     38     hrtimer_init(&hr_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
     39     hr_timer.function = &hrtimer_handler;
     40     printk("Start HR timer \n");
     41     ret = hrtimer_start(&hr_timer, ktime, HRTIMER_MODE_REL);
     42     printk("start status %d \n", ret);
     43     return 0;
     44 } 
    ...
    Module is compiling with no errors. It should display ten times "HR timer function ..." every 3 seconds. But when I use insmod module waits 3 seconds and then displays "HR timer function ..." 10 times. There are no further delays after first 3 seconds.
    What am I doing wrong ? Should I set ktime again in hrtimer_handler or use hrtimer_restart() ?

    Please move to "The Linux Kernel".

  2. #2
    Just Joined!
    Join Date
    Nov 2010
    Posts
    5
    Having the same problem here.

    It appears the RESTART ignores the timeout.
    It just interrupts back almost immediately.

    I think we need a way of re priming the timer
    but all my experiments so far have failed in this.
    Last edited by robin48gx; 11-11-2010 at 04:39 PM.

  3. #3
    Just Joined!
    Join Date
    Nov 2010
    Posts
    5
    OK found out how to re-trigger it


    /************************************************** ******************************
    Function called by high resolution timer
    ************************************************** ******************************/

    enum hrtimer_restart hr_timer_function(struct hrtimer *timer_for_restart)
    {
    int i,ret;
    char *cp;
    ktime_t time, mi_timer_now = ktime_set(0,0);
    unsigned long misses;

    // event_handler is a char buffer that is mapped to /proc/sys/kernel/mainsinputs
    i = sprintf(event_handler + 30, " NNN %lx NNN ", int_counter++);

    cp = event_handler + 30 + 10;
    *cp++ = ' '; *cp++ = ' ';

    //
    mi_timer = ktime_set(10, 2000); // 10S+2mS


    // FROM: CENSORED
    //In the restart case, the callback must set a new expiration time before returning.
    //Usually, restarting timers are used by kernel subsystems which need a callback at
    //a regular interval. The hrtimer code provides a function for advancing
    //the expiration time to the next such interval:
    //
    // unsigned long hrtimer_forward(struct hrtimer *timer, ktime_t interval);
    // 12NOV2010: IN THE MEANTIME the api has changed and needs another `now'
    // timer object.
    //


    // found this on CENSORED
    mi_timer_now = ktime_get();

    misses = hrtimer_forward(timer_for_restart, mi_timer_now, mi_timer);

    sprintf(event_handler+40,"misses %ld",misses);

    if (int_counter > 6)
    return HRTIMER_NORESTART; // one shot
    else
    return HRTIMER_RESTART;
    }




  4. $spacer_open
    $spacer_close
  5. #4
    Just Joined!
    Join Date
    Aug 2011
    Posts
    1

    Thumbs up periodic HRtimer restart

    Hi robin48gx,

    I appreciate your response on how to re trigger the hrtimer, it gave me an idea to get control on it.

    but this code works only the for the seconds value in

    mi_timer = ktime_set(10, 2000); // 10S+2mS

    The above line suppossed to set the timer to trigger after every 10 sec and 2 mses, but the trigger is happening after every 10 secs only, the ms is not considered.

    But my requirement is to restart the timer for every 20ms. when I gave

    mi_timer = ktime_set(0, 20000); // 0S+20mS

    it didn't work,

    for this the solution is

    #define MS_TO_NS(x) (x * 1E6L)

    unsigned long delay_in_ms = 20L; // 20 ms

    mi_timer = ktime_set(0, MS_TO_NS(delay_in_ms)); // 20mS

    the it works..

    Regards
    Srinivasjr04

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •