John Roberts
John Roberts

Reputation: 5966

Measuring Time in Linux Kernel Space With Sub-Microsecond Precision

I am currently using the do_gettimeofday() function to measure time in the kernel, which gives me microsecond precision. Is there anything available that is more precise than this (maybe on the order of nanoseconds)?

Upvotes: 11

Views: 12090

Answers (2)

Wonil
Wonil

Reputation: 6717

As I know, the most precise timer should be the processor specific counter register (such as TSC in x86). Linux kernel provide rdtsc, rdtscl, rdtscll macros from the "./arch/x86/include/asm/msr.h" file to read this register value. For ARM, cycle counter register.

These registers are all different from CPU to CPU. Common interface to access it is "get_cycles" function which is declared in <linux/timex.h> file.

Maybe, this document can be helpful.

Upvotes: 5

caf
caf

Reputation: 239011

The ktime_get() function returns ktime_t, which has nanosecond resolution.

Upvotes: 15

Related Questions