I've read multiple SO posts about the proper way to use the CPU performance counter; and that the first result will always return 0. The problem is the 2nd value that is returned is completely inaccurate, regardless of the time between the two calls. We have a computer with 8 total processor cores, which idles around 1-2% CPU. Here is a sample of return values once a second:
我已经阅读了多篇关于使用CPU性能计数器的正确方法的帖子;并且第一个结果将始终返回0.问题是返回的第二个值是完全不准确的,无论两次调用之间的时间如何。我们的计算机总共有8个处理器核心,占用了大约1-2%的CPU。以下是每秒返回值的示例:
12.25, 3.47, 0, 23.95, 12.25, 27.85, 6.4, 0, 16.15, 53.2
I watched the total CPU the entire time in Task Manager, and it never went over 5%.
我在任务管理器中看到了整个CPU的总CPU,它从未超过5%。
CPU = GetCounter("Processor", "% Processor Time", "_Total")
Private Function GetCounter(category As String, counter As String, Optional instance As String = Nothing) As Double
Using pc As New PerformanceCounter(category, counter, instance)
pc.NextValue()
System.Threading.Thread.Sleep(100)
Return pc.NextValue()
End Using
End Function
Changing the sleep time to 500ms / 1000ms does not help either. Why the erratic values?
将睡眠时间更改为500毫秒/ 1000毫秒也无济于事。为什么不稳定的价值观?
1 个解决方案
#1
0
The CPU usage performance counter is kind of odd. If you look in task manager you will see this unexpected process called "System Idle Process".
CPU使用性能计数器有点奇怪。如果查看任务管理器,您将看到名为“系统空闲进程”的意外过程。
What does "System Idle Process" do? Why is it always using up my CPU time? Well, the fact is, the CPU is always running at full speed, even if there is no work for it to do. If there is nothing to do, then the "System Idle Process" is the one that is running.
“系统空闲过程”有什么作用?为什么它总是耗尽我的CPU时间?嗯,事实是,CPU总是以全速运行,即使它没有工作要做。如果无事可做,那么“系统空闲进程”就是正在运行的进程。
So, what does the CPU usage performance counter measure? Simply, it measures 100% - (System Idle Process%). That is to say, 100 minus whatever the "System Idle Process" uses. BUT, you can't exactly measure CPU usage for a given process. It always has to be over some period of time. Say you look at it over 1 second. This is pretty normal for when you are looking in Task Manager:
那么,CPU使用性能计量器的衡量标准是什么?简单地说,它测量100% - (系统空闲过程%)。也就是说,100减去“系统空闲过程”使用的任何内容。但是,您无法准确测量给定流程的CPU使用率。它总是要经过一段时间。假设你看1秒钟以上。当你在任务管理器中查找时,这是很正常的:
But you can turn this update speed up or down. So what if you could turn the update speed way up? What would you see? What if the "System Idle Process" % was the average for 10 ms? 1ms? You would obviously see a lot more spikes. In 1ms, you could easily see a spike to 50 or 100% on a machine that appeared to be idling. This is probably what you are seeing.
但您可以上下调整此更新速度。那么如果你可以提高更新速度呢?你会看到什么?如果“系统空闲进程”%是10毫秒的平均值,该怎么办? 1ms的?你显然会看到更多的峰值。在1ms内,您可以轻松地在看似空转的机器上看到50%或100%的峰值。这可能就是你所看到的。
#1
0
The CPU usage performance counter is kind of odd. If you look in task manager you will see this unexpected process called "System Idle Process".
CPU使用性能计数器有点奇怪。如果查看任务管理器,您将看到名为“系统空闲进程”的意外过程。
What does "System Idle Process" do? Why is it always using up my CPU time? Well, the fact is, the CPU is always running at full speed, even if there is no work for it to do. If there is nothing to do, then the "System Idle Process" is the one that is running.
“系统空闲过程”有什么作用?为什么它总是耗尽我的CPU时间?嗯,事实是,CPU总是以全速运行,即使它没有工作要做。如果无事可做,那么“系统空闲进程”就是正在运行的进程。
So, what does the CPU usage performance counter measure? Simply, it measures 100% - (System Idle Process%). That is to say, 100 minus whatever the "System Idle Process" uses. BUT, you can't exactly measure CPU usage for a given process. It always has to be over some period of time. Say you look at it over 1 second. This is pretty normal for when you are looking in Task Manager:
那么,CPU使用性能计量器的衡量标准是什么?简单地说,它测量100% - (系统空闲过程%)。也就是说,100减去“系统空闲过程”使用的任何内容。但是,您无法准确测量给定流程的CPU使用率。它总是要经过一段时间。假设你看1秒钟以上。当你在任务管理器中查找时,这是很正常的:
But you can turn this update speed up or down. So what if you could turn the update speed way up? What would you see? What if the "System Idle Process" % was the average for 10 ms? 1ms? You would obviously see a lot more spikes. In 1ms, you could easily see a spike to 50 or 100% on a machine that appeared to be idling. This is probably what you are seeing.
但您可以上下调整此更新速度。那么如果你可以提高更新速度呢?你会看到什么?如果“系统空闲进程”%是10毫秒的平均值,该怎么办? 1ms的?你显然会看到更多的峰值。在1ms内,您可以轻松地在看似空转的机器上看到50%或100%的峰值。这可能就是你所看到的。