Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clock, rdtsc and CLOCKS_PER_SEC

Tags:

c

cpu

unistd.h

I am trying to implement my own version of clock() using asm and rdtsc. However I am quite unsure about its return value. Is it cycles? Oder is it micro seconds? I am also confused about CLOCKS_PER_SEC. How can this be constant?

Is there any kind of formula which sets these values into relation?

like image 226
今天春天 Avatar asked Apr 18 '16 14:04

今天春天


1 Answers

You can find a rdtsc reference implementation here:

https://github.com/LITMUS-RT/liblitmus/blob/master/arch/x86/include/asm/cycles.h

TSC counts the number of cycles since reset. If you need a time value unit in seconds, you also need to read the CPU clock frequency and divide TSC value by frequency. However, this may not be accurate if the CPU frequency scaling is enabled. Recent Intel processors include a constant rate TSC (identified by the "constant_tsc" flag in Linux's /proc/cpuinfo). With these processors, the TSC ticks at the processor's nominal frequency, regardless of the actual CPU clock frequency due to turbo or power saving states.

https://en.wikipedia.org/wiki/Time_Stamp_Counter

like image 74
Wei Shen Avatar answered Sep 23 '22 14:09

Wei Shen