user584018
user584018

Reputation: 11354

Is there any uniform method to calculate any counters within any category for PerformanceCounter?

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();

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?

Upvotes: 2

Views: 216

Answers (2)

James
James

Reputation: 3928

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).

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.

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).

Upvotes: 2

HouseCat
HouseCat

Reputation: 1623

By my experience and mostly MSDN documentation, is that it varies per Performance Counter Category, then again by specific attribute Property such as Available Bytes or % Committed in your case.

What might be what you are looking for is NextSample().

Perf Counter

Property: RawValue

Gets or sets the raw, or uncalculated, value of this counter.

^ meaning that it's not necessarily up to the developer who created it.

Method: NextValue()

Obtains a counter sample and returns the calculated value for it.

^ meaning that it's up to the developer who created it.

Method: NextSample()

Obtains a counter sample, and returns the raw, or uncalculated, value for it.

Also something that was explained to me long ago, so take it with a grain of salt, the concept of RawValue is not always valid.

RawValues are used to create samples. NextSample(), or samples, is/are averages - much more realistic - of RawValues over time. NextValue() is cleaned up samples converted into either %, or from bytes to Kilobytes (based on the context of the value and the implementation by developer).

So, in my humble opinion, even if the info is over 10 years old, is to abandon usage of RawValue and utilize NextSample() in its place - should you need a realistic/accurate value.

Upvotes: 2

Related Questions