获取虚拟机的总 cpu 百分比始终为零
Getting total cpu percentage of virtual machine is always zero
我试图在我们的虚拟机上每 x 秒获取总 cpu 使用情况。我找到了这段代码:
var cpuCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total", true);
WriteCpuUsageToFile(cpuCounter.NextValue() + "%");
每次计时器触发时,我得到的值为零。
2016-03-15T02:29:26 0%
2016-03-15T02:29:45 0%
2016-03-15T02:30:05 0%
2016-03-15T02:30:25 0%
2016-03-15T02:30:45 0%
服务器平均命中率为 20%。
我正在为虚拟机分配~8 个核心,具体取决于虚拟机。我是否需要使用 C# 以不同方式获取虚拟机的值?
感谢您的协助!
更新
虽然旧答案很有趣,但它没有解决 OP 的问题。
问题(在评论中的一些屏幕截图中发现)是 OP 每次调用 NextValue
时都会实例化 PerformanceCounter
,因此它不会让计数器收集值。
正确的解决方案是实例化PerformanceCounter
一次,并在同一个实例上多次调用NextValue()
。
旧答案保留在下面以提供希望有趣的信息,但它适用于 host,不适用于 guest:
如此处解释:https://msdn.microsoft.com/en-us/library/cc768535(v=bts.10).aspx
Measure guest operating system processor utilization – Traditionally, processor performance can be measured using the “\Processor(*)\% Processor Time” performance monitor counter. This is not an accurate counter for evaluating processor utilization of a guest operating system though because Hyper-V measures and reports this value relative to the number of processors allocated to the virtual machine.
...(关于为什么这不是一个好主意并且实际上可能成为瓶颈的冗长解释,您可以在 link 上阅读)...
To accurately measure the processor utilization of a guest operating system, use the “\Hyper-V Hypervisor Logical Processor(_Total)\% Total Run Time” performance monitor counter on the Hyper-V host operating system. Use the following thresholds to evaluate guest operating system processor utilization using the “\Hyper-V Hypervisor Logical Processor(_Total)\% Total Run Time” performance monitor counter
所以我还没有测试过这个,但是
编辑: 我实际测试过它并且有效,所以:
var cpuCounter = new PerformanceCounter("Hyper-V Hypervisor Logical Processor"
, "% Total Run Time"
, "_Total", true);
Use libvirt api to access host cpu 用法,我做了一个Python代码把所有的数据放到一个csv文件中。 Link Python 代码:https://github.com/leonardo329cf/VMMeter-libvirt
我试图在我们的虚拟机上每 x 秒获取总 cpu 使用情况。我找到了这段代码:
var cpuCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total", true);
WriteCpuUsageToFile(cpuCounter.NextValue() + "%");
每次计时器触发时,我得到的值为零。
2016-03-15T02:29:26 0%
2016-03-15T02:29:45 0%
2016-03-15T02:30:05 0%
2016-03-15T02:30:25 0%
2016-03-15T02:30:45 0%
服务器平均命中率为 20%。
我正在为虚拟机分配~8 个核心,具体取决于虚拟机。我是否需要使用 C# 以不同方式获取虚拟机的值?
感谢您的协助!
更新
虽然旧答案很有趣,但它没有解决 OP 的问题。
问题(在评论中的一些屏幕截图中发现)是 OP 每次调用 NextValue
时都会实例化 PerformanceCounter
,因此它不会让计数器收集值。
正确的解决方案是实例化PerformanceCounter
一次,并在同一个实例上多次调用NextValue()
。
旧答案保留在下面以提供希望有趣的信息,但它适用于 host,不适用于 guest:
如此处解释:https://msdn.microsoft.com/en-us/library/cc768535(v=bts.10).aspx
Measure guest operating system processor utilization – Traditionally, processor performance can be measured using the “\Processor(*)\% Processor Time” performance monitor counter. This is not an accurate counter for evaluating processor utilization of a guest operating system though because Hyper-V measures and reports this value relative to the number of processors allocated to the virtual machine.
...(关于为什么这不是一个好主意并且实际上可能成为瓶颈的冗长解释,您可以在 link 上阅读)...
To accurately measure the processor utilization of a guest operating system, use the “\Hyper-V Hypervisor Logical Processor(_Total)\% Total Run Time” performance monitor counter on the Hyper-V host operating system. Use the following thresholds to evaluate guest operating system processor utilization using the “\Hyper-V Hypervisor Logical Processor(_Total)\% Total Run Time” performance monitor counter
所以我还没有测试过这个,但是
编辑: 我实际测试过它并且有效,所以:
var cpuCounter = new PerformanceCounter("Hyper-V Hypervisor Logical Processor"
, "% Total Run Time"
, "_Total", true);
Use libvirt api to access host cpu 用法,我做了一个Python代码把所有的数据放到一个csv文件中。 Link Python 代码:https://github.com/leonardo329cf/VMMeter-libvirt