System.Diagnostics.PerformanceData 名前空間

カウンター データを提供するには、この名前空間のクラスを使用します。 カウンターは、パフォーマンス モニターなどのコンシューマーにパフォーマンス メトリックスを公開するために使われます。 名前空間には、カウンター データを使用するためのクラスは含まれません。 パフォーマンス カウンター アーキテクチャの詳細については、「パフォーマンス カウンター」を参照してください。

クラス

CounterData

カウンターの生データを格納します。

CounterSet

論理カウンターのセットを定義します。

CounterSetInstance

CounterSet クラス内で定義されている論理カウンターのインスタンスを作成します。

CounterSetInstanceCounterDataSet

カウンター値のコレクションを保持します。

列挙型

CounterSetInstanceType

カウンター セットで、プロセスや物理ディスクのような複数インスタンスを許可するか、メモリのような単一インスタンスに制限するかを指定します。

CounterType

使用できるカウンター タイプを定義します。 各カウンターには、カウンター タイプが割り当てられます。 カウンター タイプによって、カウンター データの計算方法、その平均値の求め方、および表示方法が決まります。

単純なマニフェストを次に示します。

<!-- <?xml version="1.0" encoding="UTF-16"?> -->  
<instrumentationManifest xsi:schemaLocation="http://schemas.microsoft.com/win/2004/08/events eventman.xsd"   
     xmlns:win="http://manifests.microsoft.com/win/2004/08/windows/events"   
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
     xmlns:xs="http://www.w3.org/2001/XMLSchema"   
     xmlns:trace=http://schemas.microsoft.com/win/2004/08/events/trace>  

    <instrumentation>  

        <counters xmlns=http://schemas.microsoft.com/win/2005/12/counters>  

            <provider  
              applicationIdentity = "provider1.exe"  
              providerType = "userMode"  
              providerGuid = "{51D1685C-35ED-45be-99FE-17261A4F27F3}">  

               <counterSet guid = "{582803C9-AACD-45e5-8C30-571141A22092}"  
                  uri = "Microsoft.Windows.System.PerfCounters.Typing"  
                  name = "$(string.CounterSet1.Name)"   
                  description = "$(string.CounterSet1.Description)"   
                  instances = "single">  

                    <counter id = "1"  
                      uri = "Microsoft.Windows.System.PerfCounters.Typing.TotalWords"  
                      name = "$(string.CS1.Counter1.Name)"  
                      description = "$(string.CS1.Counter1.Description)"  
                      type = "perf_counter_rawcount"  
                      detailLevel = "standard"/>  

                    <counter id = "2"  
                      uri = "Microsoft.Windows.System.PerfCounters.Typing.WordsInInterval"  
                      name = "$(string.CS1.Counter2.Name)"  
                      description = "$(string.CS1.Counter2.Description)"  
                      type = "perf_counter_delta"  
                      detailLevel = "standard"/>  

                    <counter id = "3"  
                      uri = "Microsoft.Windows.System.PerfCounters.Typing.LetterAPressed"  
                      name = "$(string.CS1.Counter3.Name)"  
                      description = "$(string.CS1.Counter3.Description)"  
                      type = "perf_counter_rawcount"  
                      detailLevel = "standard"/>  

                    <counter id = "4"  
                      uri = "Microsoft.Windows.System.PerfCounters.Typing.WordsContainingLetterA"  
                      name = "$(string.CS1.Counter4.Name)"   
                      description = "$(string.CS1.Counter4.Description)"   
                      type = "perf_counter_rawcount"  
                      detailLevel = "standard"/>  

                    <counter id = "5"  
                      uri = "Microsoft.Windows.System.PerfCounters.Typing.PercentOfWordsContainingLetterA"  
                      name = "$(string.CS1.Counter5.Name)"   
                      description = "$(string.CS1.Counter5.Description)"   
                      type = "perf_sample_fraction"  
                      baseID = "6"  
                      detailLevel = "standard">  
                      <counterAttributes>  
                          <counterAttribute name = "displayAsReal" />  
                      </counterAttributes>  
                    </counter>  

                    <counter id = "6"  
                      uri = "Microsoft.Windows.System.PerfCounters.Typing.PercentBase"  
                      type = "perf_sample_base"  
                      detailLevel = "standard">  
                      <counterAttributes>  
                          <counterAttribute name = "noDisplay" />  
                      </counterAttributes>  
                    </counter>  

                </counterSet>  
            </provider>  
        </counters>  
    </instrumentation>  

    <localization>  
        <resources culture="en-US">  
            <stringTable>  

                <string id="CounterSet1.Name" value="Typing"/>  
                <string id="CounterSet1.Description" value="Captures simple typing metrics."/>  
                <string id="CS1.Counter1.Name" value="Total Words Typed"/>   
                <string id="CS1.Counter1.Description" value="The total number of words typed."/>  
                <string id="CS1.Counter2.Name" value="Words Typed In Interval"/>   
                <string id="CS1.Counter2.Description" value="The total number of words typed in the interval."/>  
                <string id="CS1.Counter3.Name" value="Letter A Pressed"/>   
                <string id="CS1.Counter3.Description" value="The number of times that the letter A is pressed."/>  
                <string id="CS1.Counter4.Name" value="Words Containing A"/>   
                <string id="CS1.Counter4.Description" value="The number of words that contain the letter A."/>  
                <string id="CS1.Counter5.Name" value="Percent of Words Containing A"/>   
                <string id="CS1.Counter5.Description" value="The percent of words that contain the letter A in the last interval."/>  

            </stringTable>  
        </resources>  
    </localization>  
</instrumentationManifest>  

マニフェストの単純なプロバイダー実装を次に示します。

using System.Diagnostics.PerformanceData;  

        private static Guid providerId = new Guid("{51D1685C-35ED-45be-99FE-17261A4F27F3}");  
        private static Guid typingCounterSetId = new Guid("{582803C9-AACD-45e5-8C30-571141A22092}");  

        private static CounterSet typingCounterSet;         // Defines the counter set  
        private static CounterSetInstance typingCsInstance; // Instance of the counter set  

        private static int numberOfLetterAInWord = 0;  

        . . .  

            // Create the 'Typing' counter set.  
            typingCounterSet = new CounterSet(providerId, typingCounterSetId, CounterSetInstanceType.Single);  

            // Add the counters to the counter set definition.  
            typingCounterSet.AddCounter(1, CounterType.RawData32, "Total Word Count");  
            typingCounterSet.AddCounter(2, CounterType.Delta32, "Words Typed In Interval");  
            typingCounterSet.AddCounter(3, CounterType.RawData32, "A Key Pressed");  
            typingCounterSet.AddCounter(4, CounterType.RawData32, "Words Containing A");  
            typingCounterSet.AddCounter(5, CounterType.SampleFraction, "Percent of Words Containing A");  
            typingCounterSet.AddCounter(6, CounterType.SampleBase, "Percent Base");  

            // Create an instance of the counter set (contains the counter data).  
            typingCsInstance = typingCounterSet.CreateCounterSetInstance("Typing Instance");  
            typingCsInstance.Counters[1].Value = 0;  
            typingCsInstance.Counters[2].Value = 0;  
            typingCsInstance.Counters[3].Value = 0;  
            typingCsInstance.Counters[4].Value = 0;  
            typingCsInstance.Counters[5].Value = 0;  
            typingCsInstance.Counters[6].Value = 0;  

        . . .  

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)  
        {  
            typingCounterSet.Dispose();  
        }  

        // Simple effort to capture letter A key press and words typed.  
        private void textInput_KeyDown(object sender, KeyEventArgs e)  
        {  
            Keys keyData = e.KeyData;  

            switch (e.KeyData)  
            {  
                case Keys.A :  
                    // In the .NET 3.5 Framework, you had to use the
                    // Value property to set and increment the counter   
                    // value. Beginning with the .NET 4.0 Framework,   
                    // the Value property is safe to use in a multi-  
                    // threaded application.  
                    typingCsInstance.Counters["A Key Pressed"].Value++;  
                    numberOfLetterAInWord++;  

                    break;  

                case Keys.Enter:  
                case Keys.Space:  
                case Keys.Tab:  

                    if (numberOfLetterAInWord > 0)  
                    {  
                        // Beginning with the .NET 4.0 Framework, you   
                        // can use the Increment method to increment   
                        // the counter value by 1. The Increment method   
                        // is safe to use in a multi-threaded   
                        // application.  
                        typingCsInstance.Counters["Words Containing A"].Increment();  
                        typingCsInstance.Counters["Percent of Words Containing A"].Increment();  
                        numberOfLetterAInWord = 0;  
                    }  

                    typingCsInstance.Counters["Percent Base"].Increment();  
                    typingCsInstance.Counters["Total Word Count"].Increment();  
                    typingCsInstance.Counters["Words Typed In Interval"].Increment();  

                    break;  
            }  
        }  

注釈

この名前空間のクラスは、Windows Vista で導入されたパフォーマンス カウンターの新しいアーキテクチャ (バージョン 2.0) をサポートしています。 新しいアーキテクチャでは、プロバイダーはコンシューマー要求に直接応答しなくなりましたが、代わりにカウンター データを維持するだけです。 システムは、プロバイダーがカウンター セットのインスタンスを作成するときに、プロバイダーのプロセスにスレッドを挿入します。スレッドは、コンシューマー要求の処理を担当します。

次の手順は、カウンター プロバイダーを記述するプロセスを示しています。

  1. プロバイダーが提供するカウンターは、XML ベースのマニフェストで定義されます。 カウンターは論理的にカウンター セットにグループ化されます。 カウンター セット内のカウンターは、カウンター セット内で一意の数値識別子によって識別されます。 プロバイダーは、1 つ以上のカウンター セットを定義できます。 カウンター セットは、プロバイダーに固有の Guid によって識別されます。 これらのクラスを使用してプロバイダーを記述する場合は、次の点に注意してください。

    • プロバイダー要素の callback 属性は無視されます。

    • counterAttribute 要素の name 属性の参照値は無視されます。

    マニフェストの記述の詳細については、「 パフォーマンス カウンター スキーマ」を参照してください。

  2. マニフェストを作成した後、 CTRPP ツールを使用してマニフェストをコンパイルします (ctrpp provider.man)。 このツールでは、.h、.c、.rc、*_r.h の 4 つのファイルが生成されます。 .h ファイルと .c ファイルは無視できます。 .rc ファイルには、マニフェストで定義されているローカライズされた文字列が含まれています。 .rc ファイルと *_r.h ファイルを使用して、プロジェクトに含めるコンパイル済みリソース ファイル (.res) を作成します。 次の呼び出しは、リソース ファイルをコンパイルする方法を示しています。

    rc /r /i "c:\Program Files\Microsoft SDKs\Windows\v6.0\Include" provider.rc  
    

    sal.h を参照するエラーが発生した場合は、Microsoft Visual Studio、Visual C インクルード ディレクトリから /i スイッチに指定したディレクトリに sal.h ファイルをコピーします。

    コンパイルされたリソース ファイル (.res) へのパスをプロジェクトの [アプリケーション] プロパティ ページに追加します。

  3. プロバイダーを記述します。 次の手順は、プロバイダーによって行われた呼び出しを示しています。

    1. コンストラクターを CounterSet.CounterSet 呼び出してカウンター セットを定義します。 マニフェストで定義されているカウンター セットごとに、このメソッドを呼び出します。

    2. カウンター セットごとに、いずれかのメソッドを CounterSet.AddCounter 呼び出して、カウンターをセットに追加します。 カウンター セットで定義されているカウンターごとに、このメソッドを呼び出します。

    3. メソッドを CounterSet.CreateCounterSetInstance 呼び出してカウンター セットのインスタンスを作成します (インスタンスにはカウンター データが含まれています)。 単一インスタンス カウンター セットの場合は、このメソッドを 1 回呼び出します。 複数のインスタンス カウンター セットの場合は、カウンター データを指定する必要があるインスタンスごとにこのメソッドを呼び出します (インスタンスごとに一意の名前を使用します)。

    4. プロパティを CounterSetInstance.Counters 使用して、カウンターのカウンター データにアクセスして設定します。

  4. プロバイダーが完了したら、 LodCtr ツールを使用してコンピューターにカウンターを登録します。 たとえば、次のように入力します。

    lodctr /m:provider.man  
    

    この例では、マニフェストファイルと実行可能ファイルが現在のディレクトリに存在することを前提としています。

この名前空間のクラスは、Windows Vista 以降のオペレーティング システムを実行するコンピューターで使用できます。