Mita
Mita

Reputation: 263

Updating performance counters from unmanaged code

I am having problem with previewing custom performance counters with PerflibV2.

Performance Monitor shows my custom performance counter group by GUID, and when I want to expand it "Can't load counters" is shown.

I tried adding myself to "Performance Monitor Users" and "Performance Log Users" groups with no success.

I googled it, and read a lot of MSDN articles, but no success.

Is someone familiar with this problem?

Following is detailed procedure how I created and added custom performance counter:

I need to create a performance counter that will be updated from my unamanged application.

There are two approaches that I found:

As a test application, I created following schema.xml schema describing custom performance counter:

<!-- <?xml version="1.0" encoding="UTF-16"?> -->
<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">
        <provider callback = "custom"
          applicationIdentity = "PerfCounters.exe"
          providerType = "userMode"
          providerGuid = "{ab8e1320-965a-4cf9-9c07-fe25378c2a23}">
            <counterSet
              guid = "{ad36a036-c923-4794-b696-70577630b5cf}"
              uri = "Microsoft.Windows.System.PerfCounters.MyCounterSet1"
              name = "My LogicalDisk" 
              description = "This is a sample counter set with multiple instances." 
              instances = "multiple">
                <counter id = "1"
                  uri = "Microsoft.Windows.System.PerfCounters.MyCounterSet1.MyCounter1"
                  name = "My Free Megabytes"
                  description = "First sample counter."
                  type = "perf_counter_rawcount"
                  detailLevel = "standard"
                  defaultScale = "1"/>
            </counterSet>
        </provider>
    </counters>
    </instrumentation>
</instrumentationManifest>

And executed:

ctrpp schema.xml

I added created files to my test application, and in my test app, roughly:

PerfAutoInitialize();

ULONG instanceId = 0;
wchar_t instanceName[] = {'t', 'e', 's', 't', 0};
PPERF_COUNTERSET_INSTANCE b = PerfCreateInstance(hDataSource_schema_1, &CtrSetGuid_schema_1_1, instanceName, instanceId);

I installed performance counters with:

lodctr /m:schema.xml

My PerfCounters application is up and running while trying to read counters from Performance Monitor.

Upvotes: 2

Views: 3203

Answers (3)

slaphappy
slaphappy

Reputation: 6999

There are a few reasons why registering your PerfCounter provider would fail:

  • Check that the schema of your manifest file is valid. You can validate the file against a XSD definition file provided by Microsoft.

  • If you'd like to check, register your manifest using the lodctr tool. Make sure your run the lodctr tool as Administrator. If your rights are insufficient, it will fail silently. Once your manifest is registered, you should be able to see the GUID of your CounterSet in the PerfCounter Dialog. (See Browsing Performance Counters for a tool that can list providers.)

GUID Displayed in the PerfCounter browser dialog

  • You have to generate both the header file and the resource file (use the -rc and -o options of ctrpp). The resource file has to be added to your solution.

  • Build your application, then re-run the lodctr tool while having both the manifest file and your .exe in the current directory. Ensure that the manifest file points to the filename of your binary in the applicationIdentity property of the provider:

    <provider symbol="MyProvider" applicationIdentity="PerfCounterTest.exe" providerName="PerfCounterTest"

  • Run the app. While the app is running, you should be able to see your provider's name in the browser dialog:

enter image description here

Upvotes: 4

nothrow
nothrow

Reputation: 16168

I know this question is already answered, but I hit the same issue, and it was caused by me forgetting to include the .RC file generated into executable. When I recompiled the executable including the .RC file with stringtable, unlodctred and lodctred the schema file, it started to work.

Upvotes: 1

Mita
Mita

Reputation: 263

What was exact problem with my sample I am not completely sure but, there is an Microsoft example with Microsoft Windows SDK for Windows 7 and .NET Framework 3.5 SP1:

http://www.microsoft.com/en-us/download/confirmation.aspx?id=3138

After istalling MS SDK sample is located at:

C:\Program Files\Microsoft SDKs\Windows\v7.0\Samples\winbase\PerfCounters\Basic\CPP

It should be adapted for Windows 7 (ctrcpp has fewer arguments, and PerfAutoInitialize() and PerfAutoCleanup() are used instead of CounterInitialize() and CounterCleanup()).

It app crashes on adding counter from perfmon, see: Perflib 2 crashes when adding a counter (from Perfmon)

Upvotes: 1

Related Questions