好吧,所以我基本上試圖創建一個安裝的性能計數器類別的列表,就像你在PerfMon中得到的那樣。對於這個我使用PerformanceCounterCategory.GetCategories與Perfmon不一致
System.Diagnostics.PerformanceCounterCategory.GetCategories()
這似乎是它的工作原理,直到你檢查清單,並找出一些缺失。我發現第一個失蹤的是ReadyBoost Cache。這是因爲該項目設置爲在「x86」上編譯。將其更改爲「任何CPU」可解決該問題。例如,其中一臺測試機器有一個「授權管理器應用程序」(Authorization Manager Applications)類別(我的不知道,沒有人知道爲什麼,或者它來自哪裏)。但是,在該機器上,性能計數器類別在PerfMon中顯示,但在從C#調用GetCategories()
方法時不顯示。
有誰知道爲什麼?有沒有更可靠的方法來獲得PerformanceCounterCategories
?這是因爲我使用.Net嗎?是否有一些我可以使用的原生API?
編輯
我很抱歉,我還是不明白這一點。我寫了這段代碼或許更好地說明它:
using System;
using System.Diagnostics;
using System.Linq;
using System.Text.RegularExpressions;
using Microsoft.Win32;
namespace PccHack
{
class Program
{
private static readonly Regex Numeric = new Regex(@"^\d+$");
static void Main(string[] args)
{
var pcc1 = PerformanceCounterCategory.GetCategories();
Console.Out.WriteLine("Getting automatically from the microsoft framework gave {0} results.", pcc1.Count());
string[] counters;
using (var regKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion\Perflib\009"))
{
counters = regKey.GetValue("Counter") as string[];
}
var pcc2 = counters.Where(counter => !Numeric.IsMatch(counter)).ToList();
pcc2.Sort();
Console.Out.WriteLine("Getting manually from the registry gave {0} results.", pcc2.Count());
Console.In.ReadLine();
}
}
}
這現在給我3236個結果。因爲它獲得了系統中的所有性能計數器。所以我想我需要做的就是過濾那些實際上是性能計數器的東西,只留下類別。然而,似乎沒有一個PerformanceCounter的構造函數,它只接受名稱(因爲這不是唯一的),似乎也沒有采用索引值的構造函數。我發現了一個名爲Performance Data Helper的Win32 API,但這似乎並沒有我想要的功能。所以。如果我有一個性能計數器索引,那麼在C#中如何獲取該索引的PerformanceCounterCategory? PerfMon做到了,所以它一定是可能的。有什麼方法可以解析索引「幻數」來找出哪個是哪個?
編輯2
好。因此,這是做我的頭在最新版本中使用三種不同的方法(.NET /註冊表/ PowerShell中)的代碼提示的:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using Microsoft.Win32;
using System.Management.Automation;
namespace PccHack
{
internal class Program
{
private static void Main()
{
var counterMap = new Dictionary<string, string>();
using (var regKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion\Perflib\009"))
{
var counter = regKey.GetValue("Counter") as string[];
for (var i = 0; i < counter.Count() - 1; i += 2)
{
counterMap.Add(counter[i], counter[i + 1]);
}
}
var pcc1 = PerformanceCounterCategory.GetCategories().Select(o => o.CategoryName).ToList();
var pcc2 = new List<string>();
// Get v1 providers
using (var regKey = Registry.LocalMachine.OpenSubKey(@"SYSTEM\CurrentControlSet\services"))
{
foreach (var subKeyName in regKey.GetSubKeyNames())
{
using (var subKey = regKey.OpenSubKey(subKeyName))
{
if (!subKey.GetSubKeyNames().Contains("Performance")) continue;
using (var perfKey = subKey.OpenSubKey("Performance"))
{
var blah = (string) perfKey.GetValue("Object List");
if (blah != null)
{
pcc2.AddRange(blah.Split(' ').Select(b => counterMap[b]));
}
}
}
}
}
// Get v2 providers
using (var regKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion\Perflib\_V2Providers"))
{
foreach (var subKeyName in regKey.GetSubKeyNames())
{
using (var subKey = regKey.OpenSubKey(subKeyName))
{
foreach (var perfKeyName in subKey.GetSubKeyNames())
{
using (var perfKey = subKey.OpenSubKey(perfKeyName))
{
var blah = (string) perfKey.GetValue("NeutralName");
if (blah != null)
{
pcc2.Add(blah);
}
}
}
}
}
}
var ps = PowerShell.Create();
ps.AddCommand("Get-Counter").AddParameter("listSet", "*");
var pcc3 = ps.Invoke().Select(result => result.Members["CounterSetName"].Value.ToString()).ToList();
pcc1.Sort();
pcc2.Sort();
pcc3.Sort();
Console.Out.WriteLine("Getting automatically from the microsoft framework gave {0} results.", pcc1.Count());
Console.Out.WriteLine("Getting manually from the registry gave {0} results.", pcc2.Count());
Console.Out.WriteLine("Getting from PowerShell gave {0} results.", pcc3.Count());
Console.In.ReadLine();
}
}
}
在我的機器我通過使用.NET Framework,117弄138解析註冊表,以及使用PowerShell(這是正確的答案)。
但是,取決於安裝PowerShell/Windows SDK的用戶不是真正的選擇。
任何人有任何想法呢?是否有一些絕密的版本3性能計數器類別,隱藏在註冊表中的其他位置,需要追蹤?我不僅沒有想法嘗試,我已經用盡了不好的想法去嘗試。有什麼祕密的命令行開關可以在perfmon上使用,讓它列出所有的類別?
我發現這個PowerShell片段'Get-Counter -listSet * | Select-Object -ExpandProperty Paths'(來源:http://blogs.msdn.com/b/powershell/archive/2009/04/21/v2-quick-tip-monitoring-performance-counters-with-powershell.aspx) 。 – 2013-04-10 11:57:54
謝謝。但我需要通過編程來完成。更重要的是,我不能依賴安裝了PowerShell的最終用戶。我沒有安裝PowerShell。 :) – 2013-04-10 12:01:41