問題描述
我正在研究在我們的系統中使用 QueryPerformanceCounter 的確切含義,并試圖了解它對應用程序的影響.從我的 4 核單 CPU 機器上運行它可以看出,它需要大約 230ns 才能運行.當我在 24 核 4 cpu xeon 上運行它時,它需要大約 1.4 毫秒才能運行.更有趣的是,在我的機器上以多個線程運行時,它們不會相互影響.但是在多 CPU 機器上,線程會引起某種交互,導致它們相互阻塞.我想知道總線上是否有一些他們都查詢的共享資源?當我調用 QueryPerformanceCounter 時到底發生了什么,它真正衡量的是什么?
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_Precision_Event_Timer).
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_Precision_Event_Timer).
這篇關于當 QueryPerformanceCounter 被調用時會發生什么?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!