High precision synchronization in user space on Linux

I'm currently trying to define a method for measuring the time during which a certain function will be executed (for example, pthread_create). Now, of course, these types of functions are extremely optimized to minimize time; so small that my timer, which uses gettimeofday in user space, which measures in microseconds, cannot adequately measure anything.

Usually, if I can mess around with the kernel, I would use something like get_cycles to measure the number of cycles as an indicator of performance. However, I did not find a way to do this in user space. Is there a way to use get_cycles (or the equivalent) or some other high precision timer that I could use in user space to measure extremely fast functions?

+4
source share
4 answers

clock_gettime allows you to get the exact time from a nanosecond from the beginning of the stream, the beginning of the process or era.

+4
source

Use RDTSC (if you're on x86) or clock_gettime

 unsigned long long cycleCount() { asm ("rdtsc"); } 
+6
source

Have you tried to get the time needed to complete your function, say, 10,000 times and taking the average? This saves you from looking for more accurate synchronization features.

Having said that, this answer: Is gettimeofday () a guarantee of microsecond resolution? seems to indicate better functions to use than gettimeofday() .

+2
source

My linux man page tells me

CONFORMITY

SVr4, 4.3BSD. POSIX.1-2001 describes gettimeofday (), but not settimeofday (). POSIX.1-2008 signs gettimeofday () as deprecated, recommending using clock_gettime (2).

+1
source

Source: https://habr.com/ru/post/1341943/


All Articles