Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is high-resolution performance counter?

In the Win32 API there is a function QueryPerformanceCounter that queries the value of a very high-resolution performance timer.

What is "high-resolution performance timer"? Is it supported by hardware? What systems does not support it?

like image 402
Eugene Maksimov Avatar asked Dec 07 '11 11:12

Eugene Maksimov


2 Answers

Under Windows 7 on present generation processors, this is a reliable high precision (nanosecond) timer inside the CPU (HPET).

Under previous versions and on previous generations of processors, it is "something", which can mean pretty much anything. Most commonly, it is the value returned by the RDTSC instruction (or an equivalent, on non-x86), which may or may not be reliable and clock-independent. Note that RDTSC (originally, by definition, but not any more now) does not measure time, it measures cycles.

On current-and-previous-generation CPUs, RDTSC is usually reliable and clock-independent (i.e. it is now really measuring time), on pre-previous generation, especially on mobile or some multi-cpu rigs it is not. The "timer" may accelerate and decelerate, and even be different on different CPUs, causing "time travel".

Edit: The constant tsc flag in cpuid(0x80000007) can be used to tell whether RDTSC is reliable or not (though this does not really solve the problem, because what to do if it isn't, if there is no alternative...).

On yet older systems (like, 8-10 years old), some other timers may be used for QueryPerformanceCounter. Those may neither have high resolution at all, nor be terribly accurate.

like image 196
Damon Avatar answered Sep 30 '22 13:09

Damon


High resolution performance counters are usually pulled from the rdtsc instruction, which is an x86-specific way to fetch the number of CPU ticks that have occured since boot. The value of it is very precise, usually down to 100ns accuracy.

Compare this to GetTickCount(), which has an accuracy of roughly ~16ms.

On other architectures (which are out of the scope of Win32 APIs, since they only run on x86-based instruction sets) there may be different ways of doing this. For example, on ARM you can use the System Control Coprocessor (CP15) to do something similar.

like image 41
Polynomial Avatar answered Sep 30 '22 14:09

Polynomial