win32pdhquery bug - first results for counters containing % and/or / return -1 instead of actual value.
Performance counters containing a % or / will return -1 for their first result. # and other special characters did not show this behaviour. The first line of the output shows collectdatawhile() returning incorrect values for the first result while the subsequent values are as expected. The second line using collectdata() obviously just returns the first set of incorrect values.
import win32pdhquery
from pprint import pprint
from time import sleep
query = win32pdhquery.Query()
query.addcounter('Processor', 'DPC Rate', '_Total')
query.addcounter("Processor", "% Idle Time", "_Total")
query.addcounter('Processor', 'Interrupts/sec', '_Total')
query.open()
query.collectdatawhile(1)
sleep(5)
query.collectdatawhile_stop()
sleep(1)
query.close()
pprint(query.curresults)
pprint(query.collectdata())
Output:
[[13, -1, -1], [20, 99, 44923], [3, 98, 43181], [3, 96, 48018], [8, 99, 38211]]
[2, -1, -1]
Versions: Python 3.7.4 pywin 228
It sounds like this is that the windows pdh library does based on the counter type. Or are you suggesting there's something pywin32 is doing to cause it?
Hi Mark,
I'm a newbie, so I thought I was reporting this in the right place. I just assumed that the query responding with -1 (which seems to be the response when no value is returned by the query) was something going wrong with how the query is being done. Especially since subsequent queries to the counter using collectdatawhile() do get values returned.
I just tried two variants of this simple bit of code using win32pdhutil:
import win32pdhutil
print(win32pdhutil.GetPerformanceAttributes("Processor", "DPC Rate", "_Total"))
print(win32pdhutil.GetPerformanceAttributes("Processor", "Interrupts/sec", "_Total"))
and
import win32pdhutil
print(win32pdhutil.GetPerformanceAttributes("Processor", "DPC Rate", "_Total"))
print(win32pdhutil.GetPerformanceAttributes("Processor", "% Idle Time", "_Total"))
the results respectively were:
Traceback (most recent call last):
File "C:/Users/myuser/PycharmProjects/Metrics/venv/countertest.py", line 22, in <module>
print(win32pdhutil.GetPerformanceAttributes("Processor", "Interrupts/sec", "_Total"))
File "C:\Users\myuser\PycharmProjects\Metrics\venv\lib\site-packages\win32\lib\win32pdhutil.py", line 61, in GetPerformanceAttributes
type, val = win32pdh.GetFormattedCounterValue(hc, format)
pywintypes.error: (-1073738810, 'GetFormattedCounterValue', 'The data is not valid.')
57
11
Traceback (most recent call last):
File "C:/Users/myuser/PycharmProjects/Metrics/venv/countertest.py", line 21, in <module>
print(win32pdhutil.GetPerformanceAttributes("Processor", "% Idle Time", "_Total"))
File "C:\Users\myuser\PycharmProjects\Metrics\venv\lib\site-packages\win32\lib\win32pdhutil.py", line 61, in GetPerformanceAttributes
type, val = win32pdh.GetFormattedCounterValue(hc, format)
pywintypes.error: (-1073738810, 'GetFormattedCounterValue', 'The data is not valid.')
So I guess this might be something in the win32pdh library? Or even in how the windows api returns percentage and per second counters? Do you have any suggestions on where I should go with this information?
Thanks Guy
You could try the python-win32 mailing list (see https://github.com/mhammond/pywin32#support). or try searching for information on, eg, GetPerformanceAttributes and GetFormattedCounterValue when used from any language and not just Python.