2

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

5
  • clock_gettime() is defined in time.h. Are you referring to this, or to the actual clock_get_time() (web.mit.edu/darwin/src/modules/xnu/osfmk/man/…) ? Commented Mar 22, 2014 at 14:54
  • Hi @John. In the kernel header <linux/time.h> the functions are not declared, just the structures and IDs etc. Commented Mar 22, 2014 at 15:04
  • I was referring to time.h, not linux/time.h. The former actually defines extern int clock_gettime (clockid_t __clock_id, struct timespec *__tp). Commented Mar 22, 2014 at 16:05
  • Right, I understand that, but time.h is not in the kernel tree. I think I've figured it out now though. There is no such function in the kernel. There is however current_kernel_time() which is a nsec granularity timer. Commented Mar 22, 2014 at 16:14
  • Related: How can I print the current time in the kernel? Commented May 3 at 13:51

3 Answers 3

6

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

Sign up to request clarification or add additional context in comments.

Comments

2

As pointed out in answer by pjenney58, you can use current_kernel_time(), but do keep in mind that it gives you the "wallclock" time (in HZ accuracy), which is not recommended if you want to profile time taken by some kernel code - simply because it'll also consider time spent in suspended mode. To profile timing in kernel code there is a kernel equivalent of userland clock_gettime(CLOCK_MONOTONIC,...) viz. do_posix_clock_monotonic_gettime() which is defined as a macro in timekeeping.h. It takes pointer to struct timespec as an argument in which monotonic clock time is returned in sec and nsec accuracy.

Comments

0

Since 2018, current_kernel_time(), getrawmonotonic() etc. are deprecated, according to ktime accessors docs.

They say to use ktime_t ktime_get(void) as a default choice. There are examples all over the Kernel, let's take arch/mips/kernel/spinlock_test.c:

static int ss_get(void *data, u64 *val) { ktime_t start, finish; [...] start = ktime_get(); /* Loop to measure performance */ [...] finish = ktime_get(); *val = ktime_us_delta(finish, start); return 0; } 

Personally, I prefer u64 ktime_get_raw_ns(void) for doing measurements of nanoseconds, because I think it more readable and CLOCK_MONOTONIC_RAW is unaffected by NTP clock adjustments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.