Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

High precision timing in userspace in Linux

Tags:

c

linux

timing

Right now, I'm trying to determine a method to measure the time that a particular function will take (something like pthread_create). Now, of course, these types of functions are extremely optimized to take as little time as possible; so little, in fact, that my timer that uses gettimeofday in userspace which measures in microseconds is unable to adequately measure anything.

Normally, if I could mess with the kernel, I'd use something like get_cycles to measure the raw number of cycles as a performance metric. However, I haven't found a way to do this in userspace. Is there a way to use get_cycles (or an equivalent) or some other higher precision timer I could use in userspace to measure extremely fast functions?

like image 570
wibarr Avatar asked Dec 10 '22 10:12

wibarr


1 Answers

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

unsigned long long cycleCount() {
  asm ("rdtsc");
}
like image 185
Erik Avatar answered Dec 26 '22 19:12

Erik