linuxtimerkernelposix

Kernel mode clock_gettime()


I'm trying to use the POSIX clock functions in the kernel but the compiler keeps giving me the error: error: implicit declaration of function ‘clock_gettime’

long __timer_end(struct timespec start_time)
{
    struct timespec end_time;
    clock_gettime(CLOCK_REALTIME_COARSE, &end_time);
    return(end_time.tv_nsec - start_time.tv_nsec);

}

struct timespec __timer_start(void)
{
    struct timespec start_time;
    clock_gettime(CLOCK_REALTIME_COARSE, &start_time);
    return start_time;
}

The functions are defined in <linux/posix_clock.h> as part of structure called posix_clock_operations and there are a pair of functions, posix_clock_register() and posix_clock_unregister(). The comments lead one to believe that these functions will populate the posix_clock_operations structure. I've implemented both in my init and exit functions hoping that their presence would magically make the forward declarations for clock_gettime() appear, but it doesn't.

Does anyone know what I need to do to make this one function work? Do I really need to define all my own functions an populate posix clock_operations?

Thanks in advance,

Pete


Solution

  • It seems there is no clock_gettime() in the kernel however there is a nsec resolution clock called current_kernel_time(). So rewriting my timer looks like this:

    long timer_end(struct timespec start_time)
    {
        struct timespec end_time = current_kernel_time();
        return(end_time.tv_nsec - start_time.tv_nsec);
    }
    
    struct timespec timer_start(void)
    {
        return current_kernel_time();
    }
    

    It seems to work fine, but a higher performance version of the same suitable for ns granular performance testing looks like this:

    long timer_end(struct timespec start_time)
    {
        struct timespec end_time;
        getrawmonotonic(&end_time);
        return(end_time.tv_nsec - start_time.tv_nsec);
    }
    
    struct timespec timer_start(void)
    {
        struct timespec start_time;
        getrawmonotonic(&start_time);
        return start_time;
    }
    

    Thanks for the comments and pointers.

    Pete