다음을 통해 공유


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 기반 매니페스트에 정의됩니다. 카운터는 논리적으로 카운터 집합으로 그룹화됩니다. 카운터 집합 내의 카운터는 카운터 집합 내에서 고유한 숫자 식별자에 의해 식별됩니다. 공급자는 하나 이상의 카운터 집합을 정의할 수 있습니다. 카운터 집합은 공급자에 고유한 Guid로 식별됩니다. 이러한 클래스를 사용하여 공급자를 작성하는 경우 다음을 수행합니다.

    • 공급자 요소의 콜백 특성은 무시됩니다.

    • counterAttribute 요소의 이름 특성에 대한 참조 값은 무시됩니다.

    매니페스트 작성에 대한 자세한 내용은 성능 카운터 스키마를 참조하세요.

  2. 매니페스트를 작성한 후 CTRPP 도구를 사용하여 매니페스트(ctrpp provider.man)를 컴파일합니다. 이 도구는 .h, .c, .rc 및 *_r.h의 네 가지 파일을 생성합니다. .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 sal.h 파일을 복사합니다. Visual C는 /i 스위치에 대해 지정한 디렉터리에 디렉터리를 포함합니다.

    프로젝트의 애플리케이션 속성 페이지에 컴파일된 리소스 파일 (.res)에 대 한 경로 추가 합니다.

  3. 공급자를 작성합니다. 다음 단계에서는 공급자의 호출을 보여줍니다.

    1. CounterSet.CounterSet 생성자를 호출하여 카운터 집합을 정의합니다. 매니페스트에 정의된 각 카운터 집합에 대해 이 메서드를 호출합니다.

    2. 각 카운터 집합에 CounterSet.AddCounter 대해 메서드 중 하나를 호출하여 집합에 카운터를 추가합니다. 카운터 집합에 정의된 각 카운터에 대해 이 메서드를 호출합니다.

    3. 메서드를 CounterSet.CreateCounterSetInstance 호출하여 카운터 집합의 인스턴스를 만듭니다(인스턴스에 카운터 데이터가 포함됨). 단일 인스턴스 카운터 집합의 경우 이 메서드를 한 번 호출합니다. 여러 인스턴스 카운터 집합의 경우 카운터 데이터를 제공해야 하는 각 인스턴스에 대해 이 메서드를 호출합니다(각 인스턴스에 고유한 이름 사용).

    4. CounterSetInstance.Counters 이 속성을 사용하여 카운터에 대한 카운터 데이터에 액세스하고 설정합니다.

  4. 공급자를 완료한 후 LodCtr 도구를 사용하여 컴퓨터에 카운터를 등록합니다. 예를 들면 다음과 같습니다.

    lodctr /m:provider.man  
    

    이 예제에서는 매니페스트 및 실행 파일이 현재 디렉터리에 있다고 가정합니다.

Windows Vista 이상 운영 체제를 실행하는 컴퓨터에서 이 네임스페이스의 클래스를 사용할 수 있습니다.