Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)?

like image 561
John Roberts Avatar asked Apr 14 '13 02:04

John Roberts


People also ask

How is time measured in kernel space?

It measures time taken for each recv with something like: clock_gettime(before); recv() clock_gettime(after); global_time += after - before. In the end, I print the average time for a single recv() with "global_time/N". Lets call this time as "user_space_avg_recv" time.

Which of the following delayed execution methods allows sleeping inside the scheduled task?

schedule_timeout() A more optimal method of delaying execution is to use schedule_timeout(). This call puts your task to sleep until at least the specified time has elapsed. There is no guarantee that the sleep duration will be exactly the specified timeonly that the duration is at least as long as specified.


2 Answers

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

like image 145
caf Avatar answered Nov 02 '22 02:11

caf


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.

like image 24
Wonil Avatar answered Nov 02 '22 02:11

Wonil