从计算过程Process.TotalProcessorTime CPU使用率 [英] Calculating process cpu usage from Process.TotalProcessorTime
问题描述
我一直在试图从使用的PerformanceCounter 类移开用于监视一个进程的CPU占用率,因为它似乎它需要CPU的一个体面的数额时,进程计数得到体面高(监控了许多工序时)。
我的第一种方法是采取使用 Win32_PerfFormattedData_PerfOS_Processor 类和PercentProcessorTime的WMI路线属性,但似乎有尝试计算在多个内核的CPU使用率(它返回被100%使得不可能根据CPU核心数量除以它,导致不准确的结果的最大值)时的一个问题。对>
所以最后我决定使用过程的类的 TotalProcessorTime 财产。可悲的是,我不能确定如何计算基于价值的过程中所使用的CPU使用总量的百分比。我知道我应该从以前的PercentProcessorTime值中减去当前PercentProcessorTime值来获得处理器的过程中花了多少时间在一定时限内,但我不确定如何从那里继续。
先谢谢了。
类GetCPUUsage
{
静态时间跨度启动;
公共静态双CPUUsageTotal
{
获得;
私人集;
}
公共静态双CPUUsageLastMinute
{
获得;
私人集;
}
静态时间跨度oldCPUTime =新的时间跨度(0);
静态的DateTime lastMonitorTime = DateTime.UtcNow;
公共静态的DateTime开始时间= DateTime.UtcNow;
//调用一次一切准备就绪
静态无效OnStartup()
{
开始= Process.GetCurrentProcess()TotalProcessorTime。
}
//调用这个每30秒
静态无效CallCPU()
{
时间跨度newCPUTime = Process.GetCurrentProcess()TotalProcessorTime - 启动;
CPUUsageLastMinute =(newCPUTime - oldCPUTime).TotalSeconds /(Environment.ProcessorCount * DateTime.UtcNow.Subtract(lastMonitorTime).TotalSeconds);
lastMonitorTime = DateTime.UtcNow;
CPUUsageTotal = newCPUTime.TotalSeconds /(Environment.ProcessorCount * DateTime.UtcNow.Subtract(开始时间).TotalSeconds);
oldCPUTime = newCPUTime;
}
}
类GetCPUInfo
{
公共静态字符串GetInfoMinute()
{
返回的String.Format( {0:0.0},GetCPUUsage.CPUUsageLastMinute * 100);
}
公共静态字符串GetInfoTotal()
{
返回的String.Format({0:0.0},GetCPUUsage.CPUUsageTotal * 100);
}
}
I've been trying to move away from using the PerformanceCounter class for monitoring a process's CPU usage since it seems it requires a decent amount of CPU when the process count gets decently high (when monitoring a lot of processes).
My first approach was to take the WMI route using Win32_PerfFormattedData_PerfOS_Processor class and the PercentProcessorTime property, but there seems to be an issue when trying to calculate the CPU usage over multiple cores (the max value it's returning is 100% making it impossible to divide it based on the CPU core count, resulting in inaccurate results).
So finally I decided to use the Process's class TotalProcessorTime property. Sadly I am unsure how to calculate the percentage of the total CPU usage used by the Process based the value. I know that I should subtract the current PercentProcessorTime value from a previous PercentProcessorTime value to get how much time the processor spent on the process within a certain time limit but I am unsure how to continue from there.
Thanks in advance.
class GetCPUUsage
{
static TimeSpan start;
public static double CPUUsageTotal
{
get;
private set;
}
public static double CPUUsageLastMinute
{
get;
private set;
}
static TimeSpan oldCPUTime = new TimeSpan(0);
static DateTime lastMonitorTime = DateTime.UtcNow;
public static DateTime StartTime = DateTime.UtcNow;
// Call it once everything is ready
static void OnStartup()
{
start = Process.GetCurrentProcess().TotalProcessorTime;
}
// Call this every 30 seconds
static void CallCPU()
{
TimeSpan newCPUTime = Process.GetCurrentProcess().TotalProcessorTime - start;
CPUUsageLastMinute = (newCPUTime - oldCPUTime).TotalSeconds / (Environment.ProcessorCount * DateTime.UtcNow.Subtract(lastMonitorTime).TotalSeconds);
lastMonitorTime = DateTime.UtcNow;
CPUUsageTotal = newCPUTime.TotalSeconds / (Environment.ProcessorCount * DateTime.UtcNow.Subtract(StartTime).TotalSeconds);
oldCPUTime = newCPUTime;
}
}
class GetCPUInfo
{
public static string GetInfoMinute()
{
return String.Format("{0:0.0}", GetCPUUsage.CPUUsageLastMinute * 100);
}
public static string GetInfoTotal()
{
return String.Format("{0:0.0}", GetCPUUsage.CPUUsageTotal * 100);
}
}
这篇关于从计算过程Process.TotalProcessorTime CPU使用率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!