从C ++公开的Windows性能计数器总是在perfmon.exe

时间:2015-09-15 17:22:11

标签: c++ windows winapi performancecounter perfmon

我正在尝试使用v2.0 of Windows Performance Counters公开效果数据。我相信我已经正确地遵循了说明,但是perfmon.exe总是为我的计数器设置“无法加载计数器”。

这是我存储在名为PerformanceCounters.xml

的文件中的清单
<?xml version="1.0" encoding="utf-8"?>
<instrumentationManifest xmlns="http://schemas.microsoft.com/win/2004/08/events" xmlns:win="http://manifests.microsoft.com/win/2004/08/windows/events" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <instrumentation>
    <counters xmlns="http://schemas.microsoft.com/win/2005/12/counters" schemaVersion="1.1">
      <provider applicationIdentity="D:\temp\ConsoleApplication7\Debug\ConsoleApplication7.exe" providerGuid="{84C1D6C9-31BD-4B0F-BED2-F7AF3F24BEB9}" symbol="MyPerformanceCounterProvider" providerType="userMode" providerName="MyPerformanceCounterProvider">
        <counterSet uri="MyPerformanceCounterSet" symbol="MyPerformanceCounterSet" guid="{50ADA4E1-AD6B-48F0-A1A4-D87D03B8A281}" name="MyPerformanceCounterSet" description="MyPerformanceCounterSet" instances="multiple">
          <counter id="1" uri="MyPerformanceCounter1" description="MyPerformanceCounter1" type="perf_counter_large_rawcount" detailLevel="standard" />
        </counterSet>
      </provider>
    </counters>
  </instrumentation>
</instrumentationManifest>

我通过运行:

创建.h和.rc文件
ctrpp PerformanceCounters.xml -o PerformanceCounters.h -rc PerformanceCounters.rc

这是我的测试代码:

#include "PerformanceCounters.h"

int _tmain(int argc, _TCHAR* argv[])
{
    auto counterInitializeResult = CounterInitialize();
    if (counterInitializeResult == ERROR_SUCCESS)
    {
        auto counterSet = PerfCreateInstance(MyPerformanceCounterProvider, &MyPerformanceCounterSetGuid, L"FOO", 1ul);
        if (counterSet != nullptr)
        {
            auto setCounterResult = PerfSetULongLongCounterValue(MyPerformanceCounterProvider, counterSet, 1ul, 23);
            if (setCounterResult == ERROR_SUCCESS)
            {
                MSG msg;
                while (GetMessage(&msg, NULL, 0, 0)) // message pump is unnecessary but it keeps the process alive
                {
                    TranslateMessage(&msg);
                    DispatchMessage(&msg);
                }
            }
            PerfDeleteInstance(MyPerformanceCounterProvider, counterSet);
            counterSet = nullptr;
        }
        CounterCleanup();
    }
    return 0;
}

我从管理员命令提示符运行以下命令将字符串加载到注册表中:

D:\temp\ConsoleApplication7>lodctr /m:PerformanceCounters.xml

Info: Successfully installed performance counters in D:\temp\ConsoleApplication7\PerformanceCounters.xml

这会将以下内容添加到注册表中:

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Perflib\_V2Providers\{84c1d6c9-31bd-4b0f-bed2-f7af3f24beb9}]
"ProviderType"=dword:00000000
"ProviderName"="MyPerformanceCounterProvider"
"ApplicationIdentity"=hex(2):44,00,3a,00,5c,00,74,00,65,00,6d,00,70,00,5c,00,\
  43,00,6f,00,6e,00,73,00,6f,00,6c,00,65,00,41,00,70,00,70,00,6c,00,69,00,63,\
  00,61,00,74,00,69,00,6f,00,6e,00,37,00,5c,00,44,00,65,00,62,00,75,00,67,00,\
  5c,00,43,00,6f,00,6e,00,73,00,6f,00,6c,00,65,00,41,00,70,00,70,00,6c,00,69,\
  00,63,00,61,00,74,00,69,00,6f,00,6e,00,37,00,2e,00,65,00,78,00,65,00,00,00
  ^^^^^^^^ This is actually a REG_EXPAND_SZ value that equals "D:\temp\ConsoleApplication7\Debug\ConsoleApplication7.exe"

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Perflib\_V2Providers\{84c1d6c9-31bd-4b0f-bed2-f7af3f24beb9}\{50ada4e1-ad6b-48f0-a1a4-d87d03b8a281}]
"NameResource"=dword:00000001
"ExplainResource"=dword:00000003
"InstanceType"=dword:00000002
"First Counter"=dword:0000302e
"NeutralName"="MyPerformanceCounterSet"
"Last Counter"=dword:00003030
"CounterBlock"=hex:01,00,00,00,00,01,01,00,00,00,00,00,00,00,00,00,64,00,00,00,\
  00,00,00,00,ff,ff,ff,ff,05,00,00,00,00,00,00,00,ff,ff,ff,ff,ff,ff,ff,ff,ff,\
  ff,ff,ff,ff,ff,ff,ff,00,00,00,00
"CounterCount"=dword:00000001

我已经包含了上面的C ++和我的C ++项目中生成的.rc文件。当我编译它时,我看到字符串资源确实嵌入在.exe中。程序运行正常 - 没有错误返回,我进入我的(不必要的)消息循环。

当我运行perfmon时,我看到我的计数器在列表中设置了“MyPerformanceCounterSet”。当我的程序运行时,当我选择计数器集时,我会看到我的实例“FOO”。但如果我扩展计数器集,我会看到“无法加载计数器”。

我是这台机器的管理员。我也是当地小组Performance Log UsersPerformance Monitor Users的成员。我已经对清单中的路径进行了三次检查。如果我启用回调(通过清单中的callback="custom"),我接收到回调方法的回调,因为perfmon正在收集信息。但它似乎无法枚举计数器。

更新:我在Windows 7 SDK(C:\ Program Files \ Microsoft SDKs \ Windows \ v7.0 \ Samples \ winbase \ PerfCounters \ Basic \ CPP)中找到了一个示例。此示例产生相同的结果 - “无法加载计数器”

1 个答案:

答案 0 :(得分:-1)

我参加聚会很晚,但是想回答,以防其他任何人遇到。当为应用程序生成新的性能计数器时遇到相同的问题时,99.9%的时间是因为当我从一个清单懒惰地复制到另一个清单而导致冲突时,我忘记生成新的guid。若要更正此问题,请为您的提供程序(providerGuid属性)和counterSet(guid属性)生成新的guid。这样,当它加载计数器时就可以正确地做到这一点。