C#PerformanceMonitor仅报告0/100%的随机峰值,不存在峰值 [英] C# PerformanceMonitor only reports 0/100% with random, nonexistent spikes

查看:58
本文介绍了C#PerformanceMonitor仅报告0/100%的随机峰值,不存在峰值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 PerformanceMonitor 和图表制作某种任务管理器风格的程序.将CPU使用率值扔到标签上,然后图表显然将其全部绘制出来.

I'm trying to make a sort of task manager -esque program using PerformanceMonitor and a chart. The CPU usage value is thrown onto a label and the chart, obviously, graphs it all out.

问题是 PerformanceMonitor 仅报告CPU闲置或已满,但是该图显示了两者之间的许多小峰值,通常与Windows Task Manager图输出不匹配.

Problem is PerformanceMonitor only reports the CPU being at nothing or full, but the graph shows lots of little spikes in between, usually not matching the Windows Task Manager graph output.

我需要知道如何获得 PerformanceMonitor 或类似的C#产品,以将可行,一致且准确的信息输出到图形图表中.

I need to know how I can get PerformanceMonitor, or a similar C# product, to output viable, consistent, and accurate information into the graphing chart.

下面的代码.目前,出于测试目的,计时器间隔设置为25.

Code below. Timer interval is set to 25 for testing purposes currently.

public partial class formMain : Form
    {
        int usage;
        int x = 1;
        protected PerformanceCounter countCpu;

        public formMain()
        {
            InitializeComponent();
            myInit();
        }

        private void myInit()
        {
            countCpu = new PerformanceCounter("Processor", "% Processor Time", "_Total");

            timerMain.Tick += new EventHandler(timerMain_Tick);
            timerMain.Start();
        }

        private void timerMain_Tick(object sender, EventArgs e)
        {
            usage = Convert.ToInt32(countCpu.NextValue());
            chartCpu.Series["CPU"].Points.AddXY(x, usage);
            lblCpu.Text = Convert.ToString(usage.ToString()) + "%";
            x++;

            if(chartCpu.ChartAreas[0].AxisX.Maximum > chartCpu.ChartAreas[0].AxisX.ScaleView.Size)
            {
                chartCpu.ChartAreas[0].AxisX.ScaleView.Scroll(chartCpu.ChartAreas[0].AxisX.Maximum);
            }

        }
    }

这是一个截图,详细说明了我的意思.

Here's a screenshot detailing what I mean.

推荐答案

当您使用PerformanceCounter时,尖峰图是一个标准的事故.处理器只有两种状态:尽其所能全速运行,或者在不需要进行任何工作时完全关闭.性能计数器会告诉您自上次调用NextValue()方法以来,它运行的频率与关闭的频率.

Your spiky graph is a standard mishap when you use that PerformanceCounter. A processor only has two states, running full bore as fast as it can or turned off completely when no work needs to be done. The perf counter tells you how often it was running vs how often it was turned off since you last called the NextValue() method.

如果执行速度太快,那么分辨率会受到影响,您将获得太多的0、33、50、67或100样本.将其做得很短,您将永远只能获得0或100.因此,主要诊断这是您的Timer.Interval太短.您可能将其保留为默认值100毫秒.您需要将其设置为1000,才能模拟任务管理器或性能监视器的行为.

If you do that too fast then the resolution suffers, you get too many samples that are 0, 33, 50, 67 or 100. Make it really short and you'll only ever get 0 or 100. So the primary diagnostic here is that your Timer.Interval is too short. You probably left it at the default, 100 msec. You need to make it 1000 to emulate the behavior of Task Manager or Performance Monitor.

此外,在现代版本的Windows上,您需要使用其他PerformanceCounter来模拟从Windows实用程序获得的内容.该计数器位于处理器信息"类别中,该类别仍称为%Processor Time".它是经过调整的计数器,可以更好地表示现代处理器的实际工作量.令人费解的是,现代处理器会根据芯片温度动态调整其时钟频率,因此,他们所做的工作量会受到之前所做工作的影响.并且它们使用超线程来模拟额外的内核.但是,该额外的逻辑核心无法完成真正的核心工作.调整后的计数器试图补偿这些影响.

Furthermore, on modern version of Windows you need to use a different PerformanceCounter to emulate what you get from the Windows utilities. That counter lives in the "Processor Information" category, still called "% Processor Time". It is a tweaked counter that better represents the amount of work a modern processor really does. Which is convoluted, modern processors dynamically adjust their clock frequency based on the chip temperature, so the amount of work they do is affected by what they've done before. And they emulate an extra core with hyper-threading. That extra logical core however cannot do as much work as a real core. The tweaked counter tries to compensate for these effects.

这篇关于C#PerformanceMonitor仅报告0/100%的随机峰值,不存在峰值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆