Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is QueryPerformanceFrequency accurate when using HPET?

I'm playing around with QueryPerformanceFrequency. It used to return 3.6 Mhz, but it was not enough for what I was trying to do.

I've enabled HPET using this command bcdedit /set useplatformclock true. Now it returns 14.3 Mhz. It's great it's more precise... excepted it's not. I quickly realized that I did not get the granularity I expected.

If I try to poll QueryPerformanceCounter until it ticks, the smallest increment I can get is 11, which means 1.27Mhz. If I try to count the number of different values I can get from QueryPerformanceCounter in one second, I get 1.26Mhz.

So I was wondering is there was a way to really use the 14.3 Mhz to their full extent ?

I'm using windows 7, 64 bit system, visual studio 2008.

like image 736
0x26res Avatar asked Mar 20 '23 23:03

0x26res


1 Answers

Using the HPET hardware as a source for QueryPerformanceCounter (QPC) is known to be assosiated with large overheads.

QPC is an expensive call when configured with HPET.

It provides 14.3 MHz which suggests high accuracy but as you found, it can't be called fast enough to actually resolve that frequency.

Therefore Microsoft has turned into the CPUs time stamp counter (TSC) as a source for QPC whenever the hardware is capable doing so. TSC queries have much lower overhead. The associated frequency used for QPC is typically the CPU frequency divided by 1024; also typically a few MHz.

The call of QPC in TSC mode is so fast that a lot of consecutive calls may show the same result (typically approx. 20-30 calls or 15 - 20 ns/call). This way you may obtain typical resolutions of approx. 0.3 us (on a 3.4 GHz CPU).

You observed 3.6 MHz before you switched to HPET. That's likely the signiture of the systems ACPI PM timer (3579545 Hz), which indicates that you were not operating on TSC based QPC before switching to HPET.

So either way, running HPET or ACPI PM timer results in a usable resoluion in the range of a few MHz. Both cannot expose the full resolution given by the performance counter frequency (PCF) because the call to QPC is too expensive. Only The TSC based QPC is fast enough and capable to actually oversample the QPC.

Microsoft has just recently released more detailed information about this matter:

See Acquiring high-resolution time stamps (MSDN 2014) for the details.

This is a comprehensive article with lots of examples and detailed description. A must read for users of QPC.

...a way to really use the 14.3 Mhz to their full extent ?

Unfortunately not.

You can run Coreinfo.exe utility from Windows Sysinternals. Sysinternals has moved to Microsoft technet. Here is the link: Sysinternals System Information Utilities. This will give you an answer to the question: How can I check if my system has a non-invariant TSC?

Summary: The best resolution/accuracy/granularty is obtained by QPC based on TSC.

BTW: The proper choice of hardware as resource for QPC also influences the call expense of the new GetSystemTimePreciseAsFileTime function (Windows 8 desktop and upwards) because it internally uses QPC.

like image 113
Arno Avatar answered Apr 02 '23 13:04

Arno