What happens when a QueryPerformanceCounter is called?

I study the exact consequences of using QueryPerformanceCounter on our system and try to understand how this affects the application. I can see how to run it on my 4-core processor with one processor, it takes about 230 ns to work. When I run it on a 24-core 4-processor xeon, it takes about 1.4 ms to run. More interesting on my machine when running in multiple threads they do not affect each other. But on a machine with multiple processors, threads cause some kind of interaction that forces them to block each other. I am wondering if there is any common resource on the bus that they all request? What exactly happens when I call the QueryPerformanceCounter and what does it really measure?

+19
c ++ windows winapi timing
Nov 12 '09 at 16:45
source share
4 answers

Windows QueryPerformanceCounter () has logic to determine the number of processors and, if necessary, calls the synchronization logic. He is trying to use the TSC register, but for multiprocessor systems this register cannot be synchronized between the processors (and, more importantly, it can vary greatly due to the intellectual states of acceleration and sleep).

MSDN says it doesn’t matter which processor it is called on, so you can see the additional synchronization code so that this situation causes overhead. Also remember that it can cause bus transfers, so you can watch for delays in competition.

Try using SetThreadAffinityMask (), if possible, bind it to a specific processor. Otherwise, you just need to live with a delay, or you can try another timer (for example, see http://en.wikipedia.org/wiki/High_Precision_Event_Timer ).

+10
Nov 12 '09 at 17:04
source share
β€” -

I know this thread is a bit outdated, but I would like to add more information. First, I agree that QueryPerformanceCounter may take longer on certain machines, but I'm not sure if this is due to Ron. Although I did some research on this issue, I found various web pages that talk about how the QueryPerformanceCounter is implemented. For example, Accuracy is not the same thing as accuracy tells me that Windows HAL will be more specific, will use a different synchronization device to get the value. This means that if a slower synchronization device, such as a PIT, appears in the windows, it takes longer to get the time value. Obviously, using PIT may require a PCI transaction, so this will be one of the reasons.

I also found another article: How it works: Timer output in SQL Server 2008 R2 - Invariant TSC gives a similar description. In fact, this article describes how SQLServer will best perform the transaction.

Then I found more information about the VMware site because I had to deal with clients who use virtual machines, and I found that there are other problems with measuring time with virtual machines. For those who are interested, refer to the VMware article - "Time Tracking in VMware Virtual Machines" This article also talks about how some versions of windows will synchronize each TSC. Thus, it would be safe to use QueryPerformanceCounter () in certain situations, and I think we should try something like this: the timer outputs in SQL Server 2008 R2 suggested finding what could happen when we call the QueryPerformanceCounter ()

+4
Sep 03 '10 at 20:11
source share

I got the impression that on x86 QueryPerformanceCounter () is just called rdtsc under the covers. I am surprised that it has a slowdown on multi-core machines (I never noticed this on my 4-core processor).

+3
Nov 12 '09 at 16:49
source share

It has been a long time since I used it a lot, but if the memory is serviced, there is no one implementation of this function, since the guts are provided by different equipment manufacturers.

Here is a short article from MSDN: http://msdn.microsoft.com/ja-jp/library/cc399059.aspx

In addition, if you request performance on several processors (as opposed to several cores on one processor), it will have to communicate on the bus, which is slower and may be where you see some kind of lock.

However, as I said, quite a long time has passed.

Mike

+2
Nov 12 '09 at 17:06
source share



All Articles