Skip to main content
GameDev.net gamedev.net
🔒 Locked

QueryPerformanceCounter bug or chipset bug?

Started by DrEvil Nov 9, 2005 at 1:52 PM 5 replies 5k views
Original Post
DrEvil
DrEvil
So we've been wrestling with a problem that seems to be with QueryPerformanceCounter. Consider the following test code.

#include <iostream>
#include <Windows.h>

int main(const char **_args, int _argn)
{
	LARGE_INTEGER frequency, lastTime, currentTime;
	QueryPerformanceFrequency(&frequency);
	QueryPerformanceCounter(&lastTime);

	std::cout << "Freq: " << frequency.QuadPart << " : counter " << lastTime.QuadPart << std::endl;
	while(1)
	{
		QueryPerformanceCounter(&currentTime);

		LARGE_INTEGER difference;
		difference.QuadPart = currentTime.QuadPart - lastTime.QuadPart;

		double dDeltaTime = (double)difference.QuadPart / (double)frequency.QuadPart;

		if((currentTime.QuadPart < lastTime.QuadPart) || (difference.QuadPart < 0))
		{
			std::cout << "Bad Timer Values: Last Time " << lastTime.QuadPart << 
				" current Time " << currentTime.QuadPart << std::endl <<
				" difference " << currentTime.QuadPart - lastTime.QuadPart << std::endl;
		}
		lastTime = currentTime;
	}


	return 0;
}
Running this on various single cpu machines around the office outputs no errors, however we have a collection of duel core AMD machines that continuously spam out bad values(negative difference). Anyone know anything about this type of thing? We've noticed a post online where apparently this problem can be 'fixed' by setting the processor affinity or something to a specific processor, but I'm concerned that doing this could adversly effect the performance of the application. I just wanted to get some more input. Could anyone out there with a duel core cpu or multi-processor machine run the above code and tell me if you get any output with a negative counter difference. I'm wondering if this is a bug with duel core/multi-proc machines in general, or if its a possible chipset bug. Thanks J
DrEvil
DrEvil
I just noticed the MSDN entry for QueryPerformanceCounter

On a multiprocessor machine, it should not matter which processor is called. However, you can get different results on different processors due to bugs in the basic input/output system (BIOS) or the hardware abstraction layer (HAL). To specify processor affinity for a thread, use the SetThreadAffinityMask function.

So what do people normally to to address these potential issues? Does setting the thread affinity effect performance any?
jollyjeffers
jollyjeffers
There was a really good thread about this on the DIRECTXDEV mailing list a week or two back. The general consensus being that it's completely borked - there are ways of getting timing to work 99.9% of the time, but there are lots of special cases that screw things up [headshake] Another one of them is laptops with variable speed CPU's (e.g. Intel's SpeedStep) - the iirc, frequency for QPC is created at OS startup... thus when the laptop slows down the CPU the timing goes all wonky.

I think it was said that the dual-core one was fixed by a patch recently.

I'll go see if I can dig up the thread for you. There was a good link in there at some point [smile]

hth
Jack
<hr align="left" width="25%" />
Jack Hoxley <small>[</small><small> Forum FAQ | Revised FAQ |
Cocalus
Cocalus
I would try This First.
Note:
Quote:

Makes sure that synchronization of the processor Time Stamp Counter (TSC) registers across processors if the processor TSC uses the ACPI Power Management timer.


I think QueryPerformanceCounter uses TSC on multiproc systems.





You may also want the driver from Here Too

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.