Sign in to follow this  
beebs1

High-Res Counter support

Recommended Posts

Hiya, Can anyone tell me how widespread CPU support for the high-resolution performance counter is? I've been using it with the Windows API functions QueryPerformanceCounter() and QueryPerformanceFrequency(), but I've noticed that MSDN says it might not be supported. Thanks for any help :) James.

Share this post


Link to post
Share on other sites
I posted a topic on this a while back, here are the important quotes from that thread:

Quote:
Original post by Yann L
Quote:
Original post by fpsgamer
You mentioned that the "Query performance counter" doesnt work properly on multicore systems. For example if the system is multicore will Windows list it as an unavailable device or will it just let me use it?

It will just let you use it. There are mainly two problems with rdtsc based functions (QPC is partially based on rdtsc, but is not guaranteed to use it either - although in practice it will use it most of the time): F

First, multicore. Each core has its own time stamp counter. If both are not synchronised properly, and your process is switched from one core to the next by the OS task scheduler, then you'll suddendly read two completely unrelated counter values. AFAIK, Intel CPUs use synchronized time stamp counters, so you won't run into the problem with them. AMD multi core CPUs however, are asynchronous last time I checked. It may or may not be fixed in the future, but I wouldn't rely on it. There are some obscure hacks (SetAffinityMask) which have other negative side effects, and there is a fix available from AMD (although it doesn't seem to work all of the time).

The second problem comes from CPUs that dynamically change their frequency during operation. SpeedStep, for example. This feature is often used in notebooks to save energy when the CPU is not heavily used. In practice, this means that your "precise" timer will fluctuate in random ways, making it completely unusable.

GetTickCounter is the best choice. Not as precise as QPC, but much more reliable.


Quote:
Original post by Yann L
Quote:
Original post by Anonymous Poster
There is nothing wrong with multicore systems and QueryPerformanceTimer. There WAS a problem but it was fixed with update from Microsoft (if we are talking about that OS) and AMD/Intel. So don't worry about QPT on Dual Core systems.

Well, uhm, no. Microsoft did in fact release a fix (and so did AMD), but it is far from perfect. A lot of people still report sporadic weird behaviour and timing anomalies when using QPC on AMD multicore system, especially when cool'n'quiet is enabled. The only thing that seems to work 100% is manually setting the affinity of the process to a single core. Which is a bad hack with bad side effects. Reportedly Windows Vista resolved the issue.

Oh, and it's QueryPerformanceCounter...


May I suggest designing a series of classes that can implement several different timers depending on the situation? This is very good for portability.

Share this post


Link to post
Share on other sites
Thanks for the info.

The timing classes are probably the best idea, or perhaps something like this:

// free function pointer
extern double (*GetTimeMsPtr)()

// different time functions
double GetTimeMsQPC(); // uses the QPF/QPC functions
double GetTimeMsTickCount(); // uses GetTickCount()

// on initialization
{
if( single_threaded && single_core && QPC_supported )
GetTimeMsPtr = GetTimeMsQPC;
else if( multi_threaded || multi_core || QPC_not_supported )
GetTimeMsPtr = GetTimeMsTickCount;
// or even:
#ifdef MACOS
GetTimeMsPtr = SomeMacOSTimeFunction;
#endif
}


Anyway, I'm rambling a bit, but thanks for the help. rating++

Share this post


Link to post
Share on other sites
If you're using C++ may I suggest using inheritance instead.

However if you're strictly using C, you're going do have to use some combination of preprocessor macros like you demonstrated above.

Share this post


Link to post
Share on other sites
Just an additional note to my quotes above: QueryPerformanceCounter() seems fully reliable on Vista. AFAIH, MS dropped rdtsc completely in favour of HPET as potential timer methods for QPC.

Someone here on the forum published a nice research paper about timer precision and reliability on Windows not too long ago, but I don't remember who it was.

Share this post


Link to post
Share on other sites
Quote:
Original post by Yann L
Just an additional note to my quotes above: QueryPerformanceCounter() seems fully reliable on Vista. AFAIH, MS dropped rdtsc completely in favour of HPET as potential timer methods for QPC.


In addition to problems with rdtsc wrt dual-core and variable frequencies, there are also bugs with some motherboards that QPC doesn't account for.

Quote:

Someone here on the forum published a nice research paper about timer precision and reliability on Windows not too long ago, but I don't remember who it was.


Jan Wassenberg in this thread, recommended.


Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this