是否有任何统一的方法来计算 PerformanceCounter 的任何类别中的任何计数器? [英] Is there any uniform method to calculate any counters within any category for PerformanceCounter?

查看:66
本文介绍了是否有任何统一的方法来计算 PerformanceCounter 的任何类别中的任何计数器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 C# 的PerformanceCounter"类来计算内存"类别下的可用字节数"和使用中的已提交字节数百分比"以下 2 个计数器.

I am using "PerformanceCounter" class of C# to calculate below 2 counters "Available Bytes" and "% Committed Bytes In Use" under "Memory" category.

            PerformanceCounter pc = new PerformanceCounter("Memory", "Available Bytes", true);
        PerformanceCounter pc1 = new PerformanceCounter("Memory", "% Committed Bytes In Use", true);

        var a = pc.RawValue;
        var b = pc1.NextValue();

我在这里看到的问题是RawValue"用于可用字节"计数器,而NextValue()"用于使用中的已提交字节百分比"计数器.

The issue I'm seeing here is that "RawValue" is used for "Available Bytes" counter whereas "NextValue()" is used for "% Committed Bytes In Use" counter.

是否有统一的方法来计算两个或所有计数器?

Is there any uniform method to calculate both or all counters?

推荐答案

它只因类别而异,因为不同的类别包含不同的计数器类型.PerformanceCounter.CounterType 属性定义了计数器保存的数据类型,以及数据的计算方式.对于随时间测量差异的计数器而言,原始值存在差异是没有意义的,因为对于想要进行测量的不同客户而言,差异可能存在于不同的时间段内.请参阅 性能计数器类型枚举 了解有关不同类型的更多信息.如果您真的想深入了解每种类型的工作原理,则必须求助于所有这些都基于的 Win32 文档.曾经有一个页面包含所有这些内容,但我目前无法找到它.我能找到的最接近的是:https://technet.microsoft.com/en-us/library/cc960029.aspx.某些性能计数器类型使用主计数器和基本"计数器,然后使用基于每个计数器的当前和先前原始值(也可能是系统时间)的公式来计算 NextValue().RawValue 可能看起来对于某些计数器类型无效,因为以与计算值相同的方式解释它是没有意义的.例如,IIRC for % CPU used for the process,原始值是自程序启动以来使用的 CPU 滴答数,如果解释为百分比是无意义的.只有与之前的值和经过的时间(您还可以从中推断出最大可能的变化)相比,它才有意义.

It only varies per category because different categories contain different counter types. The PerformanceCounter.CounterType property defines what type of data the counter is holding, and therefore how the data is calculated. It doesn't make sense for a counter that's measuring the difference over time to have the difference in the raw value because the difference could be over different time periods for different clients wanting to do the measurement. See the Performance Counter Type Enumeration for more info on the different types. If you really want to get into the details of how each type works, you have to resort to the Win32 documentation on which all of this is based. There used to be a single page with all of this, but I'm having trouble finding that at the moment. The closest I can find is here: https://technet.microsoft.com/en-us/library/cc960029.aspx. Some performance counter types use a main counter and a "base" counter and then use a formula based on the current and previous raw values for each of those (and possibly system time as well) to compute NextValue(). RawValue may appear to be invalid for certain counter types because it just doesn't make sense to interpret it in the same way as the computed value. For example, IIRC for % CPU used for the process, the raw value is the number of CPU ticks used since the program started, which, if interpreted as a percentage is nonsense. It's only meaningful when compared to previous values and the elapsed time (from which you can also infer out the maximum possible change).

使用 RawValue 对某些计数器有意义,而对其他计数器则不然.但是,NextValue() 通常无法在您第一次调用它时返回有意义的值,因为当它被计算为样本之间的差异时,您没有前一个样本可以与之进行比较.您可以忽略它,或者您可以将代码设置为在启动期间调用一次,以便后续调用获得实际值.请记住,NextValue() 预计会在计时器上调用.例如,如果您在 Network Bytes Sent 计数器上调用它,它将返回上一次调用和本次调用之间发送的字节数.因此,例如,如果您在初始调用 2 秒后在 Network Bytes Sent 计数器上调用 NextValue() ,然后在 2 分钟后再次调用,您将获得非常不同的值,即使网络传输是稳定的,因为2秒后的调用返回的是2秒内传输的字节数,2分钟后的调用返回的是2分钟内传输的字节数.

Using RawValue makes sense for some counters, not for others. However, NextValue() often can't return a meaningful value the first time you call it because when it's computed as a difference between samples, you don't have a previous sample to compare it to. You can just ignore that, or you can set up your code to call it once during startup so that subsequent calls get real values. Keep in mind that NextValue() is expected to be called on a timer. For example, if you're calling it on the Network Bytes Sent counter, it will return the number of bytes sent between the previous call and this one. So for example if you call NextValue() on the Network Bytes Sent counter 2 seconds after the initial call and then again after 2 minutes, you're going to get very different values, even if the network transfer is steady because the call after 2 seconds is return the number of bytes transferred in 2 seconds, and the call after 2 minutes is going to return the number of bytes transferred in 2 minutes.

所以,简而言之,您可以将 NextValue() 用于所有计数器类型,但是您必须丢弃或忽略返回的第一个值,并且您必须调用 NextValue()code> 在一个固定的时间间隔内使结果有意义(就像交互式 Windows 性能监视器程序所做的那样).

So, in short, you can use NextValue() for all counter types, but you must throw away or ignore the first value returned, and you must call NextValue() on a fixed interval for the results to make sense (just like the interactive Windows Performance Monitor program does).

这篇关于是否有任何统一的方法来计算 PerformanceCounter 的任何类别中的任何计数器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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