当QueryPerformanceCounter被调用时会发生什么? [英] What happens when QueryPerformanceCounter is called?

查看:417
本文介绍了当QueryPerformanceCounter被调用时会发生什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究在我们的系统中使用QueryPerformanceCounter的确切含义,我试图理解它对应用程序的影响。我可以看到从我的4核单CPU的机器上运行它需要大约230ns运行。当我在一个24核4的CPU上运行它需要大约1.4ms运行。更有趣的是在我的机器上运行它在多个线程,他们不会互相影响。但是在多CPU机器上,线程引起某种交互,导致它们彼此阻塞。 我想知道总线上是否有一些共享资源,他们都查询?

I'm looking into the exact implications of using QueryPerformanceCounter in our system and am trying to understand it's impact on the application. I can see from running it on my 4-core single cpu machine that it takes around 230ns to run. When I run it on a 24-core 4 cpu xeon it takes around 1.4ms to run. More interestingly on my machine when running it in multiple threads they don't impact each other. But on the multi-cpu machine the threads cause some sort of interaction that causes them to block each other. I'm wondering if there is some shared resource on the bus that they all query? What exactly happens when I call QueryPerformanceCounter and what does it really measure?

推荐答案

Windows QueryPerformanceCounter确定处理器的数量并在必要时调用同步逻辑。它试图使用TSC寄存器,但是对于多处理器系统,该寄存器不能保证在处理器之间同步(并且更重要的是,由于智能降频和睡眠状态,该寄存器可能变化很大)。

Windows QueryPerformanceCounter() has logic to determine the number of processors and invoke syncronization logic if necessary. It attempts to use the TSC register but for multiprocessor systems this register is not guaranteed to be syncronized between processors (and more importantly can vary greatly due to intelligent downclocking and sleep states).

MSDN说,这是被调用的处理器无关紧要,所以你可能会看到额外的同步代码,这种情况导致开销。还要记住,它可以调用总线传输,因此您可能会看到总线争用延迟。

MSDN says that it doesn't matter which processor this is called on so you may be seeing extra syncronization code for such a situation cause overhead. Also remember that it can invoke a bus transfer so you may be seeing bus contention delays.

尝试使用SetThreadAffinityMask()如果可能将其绑定到特定的处理器。否则,您可能只需要忍受延迟,或者您可以尝试不同的计时器(例如,查看 http://en.wikipedia.org/wiki/High%5FPrecision%5FEvent%5FTimer )。

Try using SetThreadAffinityMask() if possible to bind it to a specific processor. Otherwise you might just have to live with the delay or you could try a different timer (for example take a look at http://en.wikipedia.org/wiki/High%5FPrecision%5FEvent%5FTimer).

这篇关于当QueryPerformanceCounter被调用时会发生什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆