本文介绍了CPU周期与总CPU时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Windows上,可以使用GetProcessTimes()和QueryProcessCycleTime()来获取应用程序所有线程的总数。我期望(显然是天真的)在总周期数和总处理器时间(用户+内核)之间找到比例关系。当转换为相同的单位(秒)并以应用程序运行时间的百分比表示时,它们甚至没有关闭;并且它们之间的比率差异很大。

On Windows, GetProcessTimes() and QueryProcessCycleTime() can be used to get totals for all threads of an app. I expected (apparently naively) to find a proportional relationship between the total number of cycles and the total processor time (user + kernel). When converted to the same units (seconds) and expressed at a percent of the app's running time, they're not even close; and the ratio between them varies greatly.

在应用启动后,它们就相当接近了。

Right after an app starts, they're fairly close.

3.6353% CPU cycles
5.2000% CPU time
0.79    Ratio

但是随着应用程序保持空闲状态(在11小时后,大部分时间处于空闲状态),该比率会增加。

But this ratio increases as an app remains idle (below, after 11 hours, mostly idle).

0.0474% CPU cycles
0.0039% CPU time
12.16   Ratio

显然,周期被认为对用户或内核时间没有贡献。我很好奇它的工作原理。

Apparently, cycles are counted that don't contribute to user or kernel time. I'm curious about how it works. Please enlighten me.

谢谢。


  • Vince

推荐答案

GetProcessTimes和QueryProcessCycleTime值的计算方法不同。 GetProcesTimes / GetThreadTimes响应计时器中断而更新,而QueryProcessCycleTime值基于对实际线程执行时间的跟踪。当同时使用和比较两个API结果时,这些不同的测量方式可能会导致时序结果大不相同。特别是因为GetThreadTimes仅包含其线程计数器的完整时隙值(请参见),通常会导致错误的计时。

The GetProcessTimes and the QueryProcessCycleTime values are calculated in different ways. GetProcesTimes/GetThreadTimes are updated in response to timer interrupts, while QueryProcessCycleTime values are based on the tracking of actual thread execution times. These different ways of measuring may cause vastly different timing results when both API results are used and compared. Especially since the GetThreadTimes includes only fully completed time-slot values for its thread counters (see http://blog.kalmbachnet.de/?postid=28), which usually results in incorrect timings.

由于GetProcessTimes通常报告的时间少于实际花费的时间(由于并非总是完成时间分段),与循环测量百分比相比,其CPU时间百分比将随着时间的推移而减少,这是有道理的

Since GetProcessTimes will in general report less time than actually spent (due to not always completing its time-slice) it makes sensethat its CPU time percentage will decrease over time compared to the cycle measurement percentage.

这篇关于CPU周期与总CPU时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-27 07:11
查看更多