Win7下添加性能计数器

一、写性能计数器manifest文件

具体的语法可以参考:http://msdn.microsoft.com/en-US/library/windows/desktop/aa373092(v=vs.85).aspx   这个上面给的例子有问题。

win7 sdk里面给了一个例子:ucs.man

<?xml version='1.0' encoding='utf-8' standalone='yes'?>
<instrumentationManifest
    xmlns="http://schemas.microsoft.com/win/2004/08/events"
    xmlns:trace="http://schemas.microsoft.com/win/2004/08/events/trace"
    xmlns:win="http://manifests.microsoft.com/win/2004/08/windows/events"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://schemas.microsoft.com/win/2004/08/events eventman.xsd"
    >
  <instrumentation>
    <counters
        xmlns="http://schemas.microsoft.com/win/2005/12/counters"
        xmlns:auto-ns1="http://schemas.microsoft.com/win/2004/08/events"
        schemaVersion="1.1"
        >
            <provider callback            = "custom"
                      applicationIdentity = "ucs.exe"
                      providerName        = "UsermodeCountersSample"
                      providerType        = "userMode"
                      symbol              = "UserModeCountersSample"
                      providerGuid        = "{ffeeaadd-965a-4cf9-9c07-fe25378c2a23}">
                <counterSet    guid        = "{ffeeaadd-c923-4794-b696-70577630b5cf}"
                               uri         = "Microsoft.Sdk.Samples.Ucs.GeometricWave"
                               name        = "Geometric Waves"
                               description = "This counter set displays a Triangle and a Square wave"
                               symbol      = "GeometricWave"
                               instances   = "multipleAggregate">
                  <counter id           = "1"
                           uri          = "Microsoft.Sdk.Samples.Ucs.GeometricWave.Triangle"
                           name         = "Triangle Wave"
                           description  = "This counter displays triangle wave"
                           type         = "perf_counter_rawcount"
                           aggregate    = "sum"
                           detailLevel  = "standard">
                    </counter>
                    <counter id           = "2"
                             uri          = "Microsoft.Sdk.Samples.Ucs.GeometricWave.Square"
                             name         = "Square Wave"
                             description  = "This counter displays Square Wave"
                             type         = "perf_counter_rawcount"
                             aggregate    = "avg"
                             detailLevel  = "standard">
                    </counter>
                </counterSet>
		            <counterSet    guid        = "{ffeeaadd-eaa6-45ba-bf6d-4c7cb0d6ef73}"
                               uri         = "Microsoft.Sdk.Samples.Ucs.TrignometricWave"
                               name        = "Trignometric Waves"
                               description = "This counter set displays a sine, cosine and a constant wave"
                               symbol      = "TrignometricWave"
                               instances   = "single">
                  <counter id           = "1"
                           uri          = "Microsoft.Sdk.Samples.Ucs.TrignometricWave.Sine"
                           name         = "Sine Wave"
                           description  = "This counter displays Sine Wave"
                           type         = "perf_counter_rawcount"
                           detailLevel  = "standard">
                        <counterAttributes>
                            <counterAttribute name = "reference" />
                        </counterAttributes>
                    </counter>
                    <counter id           = "2"
                             uri          = "Microsoft.Sdk.Samples.Ucs.TrignometricWave.Cosine"
                             name         = "Cosine Wave"
                             description  = "This counter displays Cosine Wave"
                             type         = "perf_counter_rawcount"
                             detailLevel  = "standard">
                        <counterAttributes>
                            <counterAttribute name = "reference" />
                        </counterAttributes>
                    </counter>
                    <counter id           = "3"
                             uri          = "Microsoft.Sdk.Samples.Ucs.TrignometricWave.Constant"
                             name         = "Constant Value"
                             description  = "This counter displays Constant Value"
                             type         = "perf_counter_rawcount"
                             detailLevel  = "standard"
                             defaultScale = "1">
                        <counterAttributes>
                            <counterAttribute name = "reference" />
                        </counterAttributes>
                    </counter>
                    <counter id           = "4"
                             uri          = "Microsoft.Sdk.Samples.Ucs.TrignometricWave.ConstantNoDisplay"
                             name         = "Constant Number"
                             description  = "This counter stores Constant Value but do not display"
                             type         = "perf_raw_base"
                             detailLevel  = "standard">
                        <counterAttributes>
                            <counterAttribute name="noDisplay" />
                        </counterAttributes>
                    </counter>
                    <counter id           = "5"
                             uri          = "Microsoft.Sdk.Samples.Ucs.TrignometricWave.RawFraction"
                             name         = "Raw Fraction"
                             description  = "RawFraction ; Takes counter with ID = 4 as base counter"
                             type         = "perf_raw_fraction"
                             baseID       = "4"
                             detailLevel  = "standard">
                        <counterAttributes>
                            <counterAttribute name="reference" />
                        </counterAttributes>
                    </counter>
                </counterSet>
            </provider>
        </counters>
    </instrumentation>
</instrumentationManifest>

二、ctrpp

ctrpp可以根据第一步编写出来的manifest文件生成对应的.h和.rc文件

ctrpp.exe -o ucsCounters.h -rc ucsCounters.rc ucs.man

三、代码

win7 sdk  Windows\v7.1\Samples\winbase\PerfCounters\Basic\CPP  下面有现成的代码。编译后生成ucs.exe

四、加载性能计数器

这里有一点很关键:将ucs.man 和 ucs.exe放到同一个目录,不然在性能计数器里面看到的只是一个GUID。

然后,cd到manifest所在目录,执行下面lodctr命名加载性能计数器

lodctr /m:ucs.man
执行unlodctr可以卸载性能计数器

unlodctr /m:ucs.man


使用perfmon命令打开性能计数器面板,在添加对话框里面可以找到新添加的计数器。

运行ucs.exe就会有性能数据上报上来了。


你可能感兴趣的:(performance,Counter,perfmon)