Search code examples
timerkernelfreebsdkernel-modulecallout

Timer in FreeBSD kernel module?


I'd like to let my kernel module periodically do something (a certain time interval, like 10 sec) in FreeBSD kernel. Any example for doing that?

I searched and found that there are functions like callout/timeout(old), but they seem complicated, and I cannot find good examples for them. For callout'', it seems thatcallout_reset'' is similar to the function I want (arguments include a handler and a time interval). But it seems to only execute once. So I'm confused.

Examples are the best, even for function ``timeout''.


Solution

  • You need to use callout(9). As for examples... Hm, for a real-world code you could take a look at this: http://svnweb.freebsd.org/base/head/sys/dev/iscsi/iscsi.c?revision=275925&view=markup; search for is_callout. Basically, you need 'struct timeout', a function that will get called periodically, and then you need to get the timer ticking:

    struct callout callout;
    
    static void
    callout_handler(void *whatever)
    {
            // do your stuff, and make sure to get called again after 'seconds'.
            callout_schedule(&callout, seconds * hz);
    }
    
    static void
    start_ticking(void)
    {
            callout_init(&callout, 1);
            callout_reset(&callout, seconds * hz, callout_handler, whatever);
    }