這兩天寫代碼控制CPU的占用率,顯示為一條直線或者曲線,顯示為一條直線的時候用到了PerformanceCounter 這個類。
由於對這個類還是很感興趣的,在網上找了一些資料也不是很清楚。就自己研究了一下。
首先,PerformanceCounter 分為各個category, 每個category 對應不同類型的資源,一般有"Processor", "IPv6",什麼的。。。下面有一個所有的類型的列表:
view plain
Thread
RAS Total
TCPv6
TCPv4
Paging File
SQLServer:Latches
IPsec AuthIP IPv4
MSDTC Bridge 4.0.0.0
IPsec AuthIP IPv6
.NET CLR Data
WF (System.Workflow) 4.0.0.0
Synchronization
Processor
Security Per-Process Statistics
MSDTC Bridge 3.0.0.0
Generic IKEv1, AuthIP, and IKEv2
Database ==> TableClasses
Event Tracing for Windows Session
ASP.NET v4.0.30319
.NET CLR Networking
Objects
Terminal Services
BITS Net Utilization
SQLServer:User Settable
.NET CLR Exceptions
IPsec IKEv2 IPv6
Process
IPsec IKEv2 IPv4
SQLServer:Broker Activation
WFPv6
SQLServer:Database Mirroring
Search Gatherer
Teredo Relay
IPv4
SQLServer:Cursor Manager Total
IPv6
Job Object Details
WFPv4
IPHTTPS Global
ICMP
Offline Files
SQLServer:Cursor Manager by Type
SQLAgent:JobSteps
PhysicalDisk
Search Indexer
Windows Workflow Foundation
Teredo Server
ServiceModelService 3.0.0.0
Processor Information
SQLServer:Replication Logreader
SQLServer:Transactions
ASP.NET Apps v4.0.30319
SQLServer:Broker TO Statistics
SQLServer:Deprecated Features
Per Processor Network Interface Card Activity
Database
Browser
.NET CLR Remoting
Pacer Pipe
Event Tracing for Windows
.NET CLR LocksAndThreads
Job Object
Client Side Caching
.NET Data Provider for SqlServer
ASP.NET Applications
Terminal Services Session
SQLServer:Broker/DBM Transport
Network Interface
.NET Memory Cache 4.0
SQLServer:General Statistics
SQLServer:Resource Pool Stats
SQLServer:Replication Merge
Security System-Wide Statistics
SQLServer:Backup Device
UDPv6
.NET CLR Security
Outlook
.NET CLR Jit
SQLServer:Workload Group Stats
WFP
ASP.NET State Service
SMSvcHost 4.0.0.0
SQLServer:Databases
SQLServer:Locks
Server Work Queues
ICMPv6
NBT Connection
.NET CLR Loading
Server
SQLAgent:Jobs
RAS Port
Per Processor Network Activity Cycles
SMSvcHost 3.0.0.0
ServiceModelEndpoint 3.0.0.0
.NET CLR Networking 4.0.0.0
ASP.NET
USB
SQLServer:Memory Manager
Cache
SQLServer:Replication Snapshot
System
Redirector
SQLServer:Wait Statistics
SQLAgent:Statistics
LogicalDisk
.NET Data Provider for Oracle
.NET CLR Interop
分布式事務處理協調器
WMI Objects
.NET CLR Memory
SQLServer:Access Methods
SQLServer:Buffer Partition
SQLServer:Buffer Manager
Database ==> Instances
ReadyBoost Cache
SQLServer:Plan Cache
SQLServer:Replication Agents
SQLServer:Exec Statistics
Teredo Client
Telephony
IPsec IKEv1 IPv6
Memory
SQLServer:SQL Errors
SQLServer:Replication Dist.
ServiceModelOperation 3.0.0.0
SQLServer:Buffer Node
IPsec IKEv1 IPv4
Search Gatherer Projects
IPsec Driver
SQLServer:Broker Statistics
SQLServer:SQL Statistics
SQLServer:CLR
SQLAgent:Alerts
UDPv4
SQLServer:Catalog Metadata
然後,每個類型下面對應多個實例,例如通過如下代碼獲得Processor的實例:
view plain
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);
//}
}
}
}
}
}
}
輸出結果為:
view plain
-----------------------------------
Processor
-----------------------------------
**** Instance Name **********
_Total
**** Instance Name **********
0
**** Instance Name **********
1
請按任意鍵繼續. . .
在每個實例中,又對應多個計數器,這樣就需要選擇你需要的計數器了。
獲得的Processor類別的_Total 實例,看看這個實例都包含什麼計數器,上面代碼注釋的部門包括計數器的輸出,結果為:
view plain
-----------------------------------
Processor
-----------------------------------
**** Instance Name **********
_Total
**************************************************
* counter name *
% Processor Time
% User Time
% Privileged Time
Interrupts/sec
% DPC Time
% Interrupt Time
DPCs Queued/sec
DPC Rate
% Idle Time
% C1 Time
% C2 Time
% C3 Time
C1 Transitions/sec
C2 Transitions/sec
C3 Transitions/sec
**** Instance Name **********
0
**************************************************
* counter name *
% Processor Time
% User Time
% Privileged Time
Interrupts/sec
% DPC Time
% Interrupt Time
DPCs Queued/sec
DPC Rate
% Idle Time
% C1 Time
% C2 Time
% C3 Time
C1 Transitions/sec
C2 Transitions/sec
C3 Transitions/sec
**** Instance Name **********
1
**************************************************
* counter name *
% Processor Time
% User Time
% Privileged Time
Interrupts/sec
% DPC Time
% Interrupt Time
DPCs Queued/sec
DPC Rate
% Idle Time
% C1 Time
% C2 Time
% C3 Time
C1 Transitions/sec
C2 Transitions/sec
C3 Transitions/sec
請按任意鍵繼續. . .
這樣,在使用PerformanceCounter 的時候需要指定類別,實例名,計數器名。
view plain
PerformanceCounter pc1 = new PerformanceCounter("Processor","% Processor Time","_Total");
然後通過pc1.NextValue() 獲得相關計數器的值就可以了
作者:Watkins.Song chanllege yourself