Python 如何让win32pdhutil使用斜杠查询性能计数器?

Python 如何让win32pdhutil使用斜杠查询性能计数器?,python,winapi,pywin32,Python,Winapi,Pywin32,我试图使用Python中的pywin32包中的win32pdhutil查询Windows性能计数器,但每当计数器名称有斜杠时,我就会遇到错误 例如,此代码工作正常: import win32pdhutil query = win32pdhutil.GetPerformanceAttributes("Memory", "Available Bytes") 但该代码失败: import win32pdhutil query = win32pdhutil.GetPerformanceAttrib

我试图使用Python中的pywin32包中的win32pdhutil查询Windows性能计数器,但每当计数器名称有斜杠时,我就会遇到错误

例如,此代码工作正常:

import win32pdhutil

query = win32pdhutil.GetPerformanceAttributes("Memory", "Available Bytes")
但该代码失败:

import win32pdhutil

query = win32pdhutil.GetPerformanceAttributes("Memory", "Cache Faults/sec")
错误消息是:

Traceback (most recent call last):
  File "<path>\Python\Python38-32\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.')
回溯(最近一次呼叫最后一次):
文件“\Python\Python38-32\lib\site packages\win32\lib\win32pdhutil.py”,第61行,在GetPerformanceAttributes中
类型,val=win32pdh.GetFormattedCounterValue(hc,格式)
pywintypes.error:(-1073738810,“GetFormattedCounterValue”,“数据无效”。)
我尝试了许多不同的计数器,只有当计数器名称中有斜杠时才会遇到这个问题

我的原始代码使用win32pdh.AddCounter和win32pdh.CollectQueryData处理相同的问题,但上面的示例在一行中演示了它

我发现了一个类似的线程,没有一个明显的解决方案。

您需要等待“Cache故障/SEC”来收集足够的数据,这里是MSDN上的一个C++示例,用于性能计数器API:

在python中,如果我在win32pdhutil.py中的
GetPerformanceAttributes
函数中硬编码(仅用于测试),如下所示:

def GetPerformanceAttributes(object, counter, instance = None, inum=-1,
                             format = win32pdh.PDH_FMT_LONG, machine=None):
    # NOTE: Many counters require 2 samples to give accurate results,
    # including "% Processor Time" (as by definition, at any instant, a
    # thread's CPU usage is either 0 or 100).  To read counters like this,
    # you should copy this function, but keep the counter open, and call
    # CollectQueryData() each time you need to know.
    # See http://support.microsoft.com/default.aspx?scid=kb;EN-US;q262938
    # and http://msdn.microsoft.com/library/en-us/dnperfmo/html/perfmonpt2.asp
    # My older explanation for this was that the "AddCounter" process forced
    # the CPU to 100%, but the above makes more sense :)
    path = win32pdh.MakeCounterPath( (machine,object,instance, None, inum,counter) )
    hq = win32pdh.OpenQuery()
    try:
        hc = win32pdh.AddCounter(hq, path)
        try:
            win32pdh.CollectQueryData(hq)
            time.sleep(1)
            win32pdh.CollectQueryData(hq)
            type, val = win32pdh.GetFormattedCounterValue(hc, format)
            return val
        finally:
            win32pdh.RemoveCounter(hc)
    finally:
        win32pdh.CloseQuery(hq)
然后
“Cache Faults/sec”
对我有效,您可以尝试自定义自己的
m_GetFormattedCounterValue
函数,然后给采集器足够的时间收集上面示例中的数据。

您需要等待“Cache Faults/sec”来收集足够的数据,下面是MSDN上的性能计数器API:

的C++示例

在python中,如果我在win32pdhutil.py中的
GetPerformanceAttributes
函数中硬编码(仅用于测试),如下所示:

def GetPerformanceAttributes(object, counter, instance = None, inum=-1,
                             format = win32pdh.PDH_FMT_LONG, machine=None):
    # NOTE: Many counters require 2 samples to give accurate results,
    # including "% Processor Time" (as by definition, at any instant, a
    # thread's CPU usage is either 0 or 100).  To read counters like this,
    # you should copy this function, but keep the counter open, and call
    # CollectQueryData() each time you need to know.
    # See http://support.microsoft.com/default.aspx?scid=kb;EN-US;q262938
    # and http://msdn.microsoft.com/library/en-us/dnperfmo/html/perfmonpt2.asp
    # My older explanation for this was that the "AddCounter" process forced
    # the CPU to 100%, but the above makes more sense :)
    path = win32pdh.MakeCounterPath( (machine,object,instance, None, inum,counter) )
    hq = win32pdh.OpenQuery()
    try:
        hc = win32pdh.AddCounter(hq, path)
        try:
            win32pdh.CollectQueryData(hq)
            time.sleep(1)
            win32pdh.CollectQueryData(hq)
            type, val = win32pdh.GetFormattedCounterValue(hc, format)
            return val
        finally:
            win32pdh.RemoveCounter(hc)
    finally:
        win32pdh.CloseQuery(hq)
然后
“Cache Faults/sec”
适用于我,您可以尝试自定义自己的
m_GetFormattedCounterValue
函数,然后给采集器足够的时间收集上述示例中的数据