PerformanceCounter 详解,使用方法 c performancecounter

这两天写代码控制CPU的占用率,显示为一条直线或者曲线,显示为一条直线的时候用到了PerformanceCounter 这个类。







由于对这个类还是很感兴趣的,在网上找了一些资料也不是很清楚。就自己研究了一下。







首先,PerformanceCounter 分为各个category, 每个category 对应不同类型的资源,一般有"Processor", "IPv6",什么的。。。下面有一个所有的类型的列表:

[html] view plaincopyprint?

Thread

RASTotal

TCPv6

TCPv4

PagingFile

SQLServer:Latches

IPsecAuthIPIPv4

MSDTCBridge4.0.0.0

IPsecAuthIPIPv6

.NETCLRData

WF(System.Workflow)4.0.0.0

Synchronization

Processor

SecurityPer-ProcessStatistics

MSDTCBridge3.0.0.0

GenericIKEv1,AuthIP,andIKEv2

Database==>TableClasses

EventTracingforWindowsSession

ASP.NETv4.0.30319

.NETCLRNetworking

Objects

TerminalServices

BITSNetUtilization

SQLServer:UserSettable

.NETCLRExceptions

IPsecIKEv2IPv6

Process

IPsecIKEv2IPv4

SQLServer:BrokerActivation

WFPv6

SQLServer:DatabaseMirroring

SearchGatherer

TeredoRelay

IPv4

SQLServer:CursorManagerTotal

IPv6

JobObjectDetails

WFPv4

IPHTTPSGlobal

ICMP

OfflineFiles

SQLServer:CursorManagerbyType

SQLAgent:JobSteps

PhysicalDisk

SearchIndexer

WindowsWorkflowFoundation

TeredoServer

ServiceModelService3.0.0.0

ProcessorInformation

SQLServer:ReplicationLogreader

SQLServer:Transactions

ASP.NETAppsv4.0.30319

SQLServer:BrokerTOStatistics

SQLServer:DeprecatedFeatures

PerProcessorNetworkInterfaceCardActivity

Database

Browser

.NETCLRRemoting

PacerPipe

EventTracingforWindows

.NETCLRLocksAndThreads

JobObject

ClientSideCaching

.NETDataProviderforSqlServer

ASP.NETApplications

TerminalServicesSession

SQLServer:Broker/DBMTransport

NetworkInterface

.NETMemoryCache4.0

SQLServer:GeneralStatistics

SQLServer:ResourcePoolStats

SQLServer:ReplicationMerge

SecuritySystem-WideStatistics

SQLServer:BackupDevice

UDPv6

.NETCLRSecurity

Outlook

.NETCLRJit

SQLServer:WorkloadGroupStats

WFP

ASP.NETStateService

SMSvcHost4.0.0.0

SQLServer:Databases

SQLServer:Locks

ServerWorkQueues

ICMPv6

NBTConnection

.NETCLRLoading

Server

SQLAgent:Jobs

RASPort

PerProcessorNetworkActivityCycles

SMSvcHost3.0.0.0

ServiceModelEndpoint3.0.0.0

.NETCLRNetworking4.0.0.0

ASP.NET

PerformanceCounter 详解,使用方法 c performancecounter
USB

SQLServer:MemoryManager

Cache

SQLServer:ReplicationSnapshot

System

Redirector

SQLServer:WaitStatistics

SQLAgent:Statistics

LogicalDisk

.NETDataProviderforOracle

.NETCLRInterop

分布式事务处理协调器

WMIObjects

.NETCLRMemory

SQLServer:AccessMethods

SQLServer:BufferPartition

SQLServer:BufferManager

Database==>Instances

ReadyBoostCache

SQLServer:PlanCache

SQLServer:ReplicationAgents

SQLServer:ExecStatistics

TeredoClient

Telephony

IPsecIKEv1IPv6

Memory

SQLServer:SQLErrors

SQLServer:ReplicationDist.

ServiceModelOperation3.0.0.0

SQLServer:BufferNode

IPsecIKEv1IPv4

SearchGathererProjects

IPsecDriver

SQLServer:BrokerStatistics

SQLServer:SQLStatistics

SQLServer:CLR

SQLAgent:Alerts

UDPv4

SQLServer:CatalogMetadata

ThreadRAS TotalTCPv6TCPv4Paging FileSQLServer:LatchesIPsec AuthIP IPv4MSDTC Bridge 4.0.0.0IPsec AuthIP IPv6.NET CLR DataWF (System.Workflow) 4.0.0.0SynchronizationProcessorSecurity Per-Process StatisticsMSDTC Bridge 3.0.0.0Generic IKEv1, AuthIP, and IKEv2Database ==> TableClassesEvent Tracing for Windows SessionASP.NET v4.0.30319.NET CLR NetworkingObjectsTerminal ServicesBITS Net UtilizationSQLServer:User Settable.NET CLR ExceptionsIPsec IKEv2 IPv6ProcessIPsec IKEv2 IPv4SQLServer:Broker ActivationWFPv6SQLServer:Database MirroringSearch GathererTeredo RelayIPv4SQLServer:Cursor Manager TotalIPv6Job Object DetailsWFPv4IPHTTPS GlobalICMPOffline FilesSQLServer:Cursor Manager by TypeSQLAgent:JobStepsPhysicalDiskSearch IndexerWindows Workflow FoundationTeredo ServerServiceModelService 3.0.0.0Processor InformationSQLServer:Replication LogreaderSQLServer:TransactionsASP.NET Apps v4.0.30319SQLServer:Broker TO StatisticsSQLServer:Deprecated FeaturesPer Processor Network Interface Card ActivityDatabaseBrowser.NET CLR RemotingPacer PipeEvent Tracing for Windows.NET CLR LocksAndThreadsJob ObjectClient Side Caching.NET Data Provider for SqlServerASP.NET ApplicationsTerminal Services SessionSQLServer:Broker/DBM TransportNetwork Interface.NET Memory Cache 4.0SQLServer:General StatisticsSQLServer:Resource Pool StatsSQLServer:Replication MergeSecurity System-Wide StatisticsSQLServer:Backup DeviceUDPv6.NET CLR SecurityOutlook.NET CLR JitSQLServer:Workload Group StatsWFPASP.NET State ServiceSMSvcHost 4.0.0.0SQLServer:DatabasesSQLServer:LocksServer Work QueuesICMPv6NBT Connection.NET CLR LoadingServerSQLAgent:JobsRAS PortPer Processor Network Activity CyclesSMSvcHost 3.0.0.0ServiceModelEndpoint 3.0.0.0.NET CLR Networking 4.0.0.0ASP.NETUSBSQLServer:Memory ManagerCacheSQLServer:Replication SnapshotSystemRedirectorSQLServer:Wait StatisticsSQLAgent:StatisticsLogicalDisk.NET Data Provider for Oracle.NET CLR Interop分布式事务处理协调器WMI Objects.NET CLR MemorySQLServer:Access MethodsSQLServer:Buffer PartitionSQLServer:Buffer ManagerDatabase ==> InstancesReadyBoost CacheSQLServer:Plan CacheSQLServer:Replication AgentsSQLServer:Exec StatisticsTeredo ClientTelephonyIPsec IKEv1 IPv6MemorySQLServer:SQL ErrorsSQLServer:Replication Dist.ServiceModelOperation 3.0.0.0SQLServer:Buffer NodeIPsec IKEv1 IPv4Search Gatherer ProjectsIPsec DriverSQLServer:Broker StatisticsSQLServer:SQL StatisticsSQLServer:CLRSQLAgent:AlertsUDPv4SQLServer:Catalog Metadata





然后,每个类型下面对应多个实例,例如通过如下代码获得Processor的实例:





[html] view plaincopyprint?

usingSystem;

usingSystem.Collections.Generic;

usingSystem.Linq;

usingSystem.Text;

usingSystem.Diagnostics;



namespacePerformanceCounterTest

{

classProgram

{

staticvoidMain(string[]args)

{

PerformanceCounterCategory[]pcc=PerformanceCounterCategory.GetCategories();

for(inti=0;i<pcc.Length;i++)

{

if(pcc[i].CategoryName=="Processor")

{

Console.WriteLine("-----------------------------------");

Console.WriteLine(pcc[i].CategoryName);

Console.WriteLine("-----------------------------------");

//Console.WriteLine(pcc[i].MachineName);

string[]instanceNames=pcc[i].GetInstanceNames();

for(intj=0;j<instanceNames.Length;j++)

{

Console.WriteLine("****InstanceName**********");

Console.WriteLine(instanceNames[j]);

//PerformanceCounter[]counters=pcc[i].GetCounters(instanceNames[j]);



//Console.WriteLine("**************************************************");

//Console.WriteLine("*countername*");

//for(intk=0;k<counters.Length;k++)

//{

//Console.WriteLine(counters[k].CounterName);

//}

}

}

}

}

}

}

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Diagnostics;namespace PerformanceCounterTest{ class Program { static void Main(string[] args) { PerformanceCounterCategory[] pcc = PerformanceCounterCategory.GetCategories(); for (int i = 0; i < pcc.Length; i++) { if (pcc[i].CategoryName=="Processor") { Console.WriteLine("-----------------------------------"); Console.WriteLine(pcc[i].CategoryName); Console.WriteLine("-----------------------------------"); //Console.WriteLine(pcc[i].MachineName); string[] instanceNames = pcc[i].GetInstanceNames(); for (int j = 0; j < instanceNames.Length; j++) { Console.WriteLine("**** Instance Name **********"); Console.WriteLine(instanceNames[j]); //PerformanceCounter[] counters = pcc[i].GetCounters(instanceNames[j]); //Console.WriteLine("**************************************************"); //Console.WriteLine("* counter name * "); //for (int k = 0; k < counters.Length; k++) //{ // Console.WriteLine(counters[k].CounterName); //} } } } } }}

输出结果为:

[html] view plaincopyprint?

-----------------------------------

Processor

-----------------------------------

****InstanceName**********

_Total

****InstanceName**********

0

****InstanceName**********

1

请按任意键继续...

-----------------------------------Processor-----------------------------------**** Instance Name **********_Total**** Instance Name **********0**** Instance Name **********1请按任意键继续. . .





在每个实例中,又对应多个计数器,这样就需要选择你需要的计数器了。



获得的Processor类别的_Total 实例,看看这个实例都包含什么计数器,上面代码注释的部门包括计数器的输出,结果为:

[html] view plaincopyprint?

-----------------------------------

Processor

-----------------------------------

****InstanceName**********

_Total

**************************************************

*countername*

%ProcessorTime

%UserTime

%PrivilegedTime

Interrupts/sec

%DPCTime

%InterruptTime

DPCsQueued/sec

DPCRate

%IdleTime

%C1Time

%C2Time

%C3Time

C1Transitions/sec

C2Transitions/sec

C3Transitions/sec

****InstanceName**********

0

**************************************************

*countername*

%ProcessorTime

%UserTime

%PrivilegedTime

Interrupts/sec

%DPCTime

%InterruptTime

DPCsQueued/sec

DPCRate

%IdleTime

%C1Time

%C2Time

%C3Time

C1Transitions/sec

C2Transitions/sec

C3Transitions/sec

****InstanceName**********

1

**************************************************

*countername*

%ProcessorTime

%UserTime

%PrivilegedTime

Interrupts/sec

%DPCTime

%InterruptTime

DPCsQueued/sec

DPCRate

%IdleTime

%C1Time

%C2Time

%C3Time

C1Transitions/sec

C2Transitions/sec

C3Transitions/sec

请按任意键继续...

-----------------------------------Processor-----------------------------------**** Instance Name **********_Total*************************************************** counter name *% Processor Time% User Time% Privileged TimeInterrupts/sec% DPC Time% Interrupt TimeDPCs Queued/secDPC Rate% Idle Time% C1 Time% C2 Time% C3 TimeC1 Transitions/secC2 Transitions/secC3 Transitions/sec**** Instance Name **********0*************************************************** counter name *% Processor Time% User Time% Privileged TimeInterrupts/sec% DPC Time% Interrupt TimeDPCs Queued/secDPC Rate% Idle Time% C1 Time% C2 Time% C3 TimeC1 Transitions/secC2 Transitions/secC3 Transitions/sec**** Instance Name **********1*************************************************** counter name *% Processor Time% User Time% Privileged TimeInterrupts/sec% DPC Time% Interrupt TimeDPCs Queued/secDPC Rate% Idle Time% C1 Time% C2 Time% C3 TimeC1 Transitions/secC2 Transitions/secC3 Transitions/sec请按任意键继续. . .





这样,在使用 PerformanceCounter 的时候需要指定类别,实例名,计数器名。

[html] view plaincopyprint?

PerformanceCounterpc1=newPerformanceCounter("Processor","%ProcessorTime","_Total");

PerformanceCounter pc1 = new PerformanceCounter("Processor","% Processor Time","_Total");

  

爱华网本文地址 » http://www.aihuau.com/a/25101012/141028.html

更多阅读

100%成功详解,谷歌Google adwords api申请攻略 googleapiclient 详解

100%成功详解,谷歌Google adwords api申请攻略——简介在过一段时间,有可能闲下来一台机器,那时会拿出来供大家拓词用,谷歌api、百度api都可以,看大家需求,价格上可能有些差异,到时再说吧。100%成功详解,谷歌Google adwords api申请攻略—

csol中破碎炙炎双管 生化详解 精 csol破碎炙炎ex

以上为仓库属性图csol中破碎炙炎(双管)生化详解 精——步骤/方法csol中破碎炙炎(双管)生化详解 精 1、首先,这个枪一次上堂两发子弹,上弹速度比较快,没有过多动作,两发是一起上的,左

什么是金表?金表详解,购买金表须知 儿童购买机票须知

关于镀金词义:  附金于[金属表面],其法是先用金汞薄薄地镀上一层,随后加热使汞挥发。用电解或其他化学方法,使金子附着到金属或别的物体表面上,形成一层薄金什么是金表?金表详解,购买金表须知——镀金表工艺类别什么是金表?金表详解,购买金

12593和17951的区别图文详解 git使用教程图文详解

12593和17951的区别(图文详解)——简介在使用手机通信时经常会听到12593和17951,可能大家在使用的时候都知道在电话号码前加上17951或12593在通话时可以享受通话优惠,但是可能很大一部分人都不知道什么时候该使用17951,什么时候该使用125

女用避孕套使用方法 杜蕾斯女用避孕套

?避孕套可分为男用和女用两种,相对于女用避孕套来说,男用避孕套使用率高,使用方法已普遍被大众掌握。但是女用避孕套使用率较低,使用方法尚未被大众熟习。?  展开时的女用避孕套避孕套两端各有一个易弯曲的环,套底完全封闭,使用时将

声明:《PerformanceCounter 详解,使用方法 c performancecounter》为网友腹肌男分享!如侵犯到您的合法权益请联系我们删除