Environment
Situation
Cause
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 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.
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.