Why are my counters returning negative values?

  • 7010545
  • 01-Aug-2012
  • 06-Jun-2013

Environment

NetIQ AppManager Enterprise 7.0.1

Situation

On occasion some Knowledge Scripts that return data from performance counters can return negative values, even though when viewing these performance counters in PerfMon they appear fine.

Cause

According to Microsoft, PDH_CALC_NEGATIVE_DENOMINATOR is defined as a PDH error code which means "A counter with a negative denominator value was detected".
One point to note is that when we get a return type of PDH_CALC_NEGATIVE_DENOMINATOR then it is to be understood that the present raw counter value is subtracted from the previous raw counter value and a negative result was detected. API’s such as PdhFormatFromRawValue, PdhGetFormattedCounterValue those try to format the raw counter values usually need to calculate the denominator. For example the % processor time performance counter is the percentage of time that the processor is running a non-Idle thread. The calculation is done as follows:

100*(1-(X1-X0)/(Y1-Y0))

When the application calls the PdhCollectQueryData PDH function the first time, the PDH library collects X0 (the total time taken by the processor that is running the thread of the idle process) and Y0 (the time measured in Time base units). The second time the application calls PdhCollectQueryData, the previous sample becomes X0 and Y0, and the current call becomes X1 and Y1.

When the application calls the PdhGetFormattedCounterValue PDH function, the calculation shown above is performed to calculate the % Processor Time. Since there is a requirement to calculate the denominator (Y1 – Y0) so if a negative denominator is detected the API PdhGetFormattedCounterValue will return PDH_CALC_NEGATIVE_DENOMINATOR.
Additional Information

If the function PdhGetFormattedCounterValue() fails, the return value is a system error code or a PDH error code. In case of an error, the following are *possible* values returned by PdhGetFormattedCounterValue():

• PDH_INVALID_HANDLE ERROR
• PDH_INVALID_ARGUMENT ERROR
• PDH_INVALID_DATA ERROR

This means there is a possibility that you might get a return value other than PDH_INVALID_ARGUMENT, PDH_INVALID_DATA or PDH_INVALID_HANDLE, and the return values are stated at PDH error code.

The return values like PDH_CALC_NEGATIVE_DENOMINATOR can occur because of either bug in the provider or overflow of values in data storage. In these situations, you have an option to ignore this return value and retry a bit later like 1 second to get the new values. This is not a fatal error that application can’t continue correctly, like INVALID_HANDLE or INVALID_DATA.