按索引获取 PerformanceCounter

Get PerformanceCounter by Index

我想访问在具有不同本地化的系统上运行的应用程序中的 "Processor Time %" 计数器。

为此,我想通过索引访问计数器,保证唯一(参见 https://support.microsoft.com/en-us/kb/287159)。

以下代码有效并为我提供了当前语言环境的正确结果,但要打开性能计数器,我还需要计数器的类别名称(请参阅 PerformanceCounter class 的构造函数)作为以及实例名称:

[DllImport("pdh.dll", SetLastError = true, CharSet = CharSet.Unicode)]
internal static extern UInt32 PdhLookupPerfNameByIndex(string szMachineName, uint dwNameIndex, StringBuilder szNameBuffer, ref uint pcchNameBufferSize); 

void Main()
{
    var buffer = new StringBuilder(1024);
    var bufSize = (uint)buffer.Capacity;
    PdhLookupPerfNameByIndex(null, 6, buffer, ref bufSize);
    Console.WriteLine(buffer.ToString());

    var counter = new PerformanceCounter(/* category??? */, buffer.ToString(), /* instance??? */);
}

如何获取该类别和实例名称?

另请参阅: Retrieve performance counter value in a language-independent way,它描述了同样的问题,但没有提供解决方案。

试试这个:

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

它适用于我在 PC 上的德语本地化。

更新

这是一个示例,您可以用来了解这些类别、实例和计数器的组织方式。不要忘记检查控制面板管理工具下的性能监视器,您可以在其中添加计数器或查找现有计数器。

string counterName = buffer.ToString();
PerformanceCounter counter = null;            
foreach (var category in PerformanceCounterCategory.GetCategories())
{
    // Get all possible instances for the current category
    var instanceNames = category.GetInstanceNames();
    if (instanceNames.Length == 0)
        continue;
    // Get all counters in the category. 
    // We want to find an instance with underscores first, for example, "_Total"             
    var counters = category.GetCounters(
        category.GetInstanceNames().OrderBy(i => i).First());
    foreach (var currentCounter in counters)
    {                                        
        if (currentCounter.CounterName == counterName)
        {
            // Hurray! Here it is!
            counter = currentCounter;
        }
    }
}

您误解了 PdhLookupPerfNameByIndex() 的工作原理。它的工作不是映射性能计数器而是映射 string。它应该用于计数器的类别及其名称。不适用于计数器的实例,如果适用,则未本地化。

查看其功能的最佳方法是使用 Regedit.exe。导航至 HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Perflib。请注意“009”键,其计数器值具有到英文字符串映射的索引。双击计数器并将框的内容复制粘贴到文本编辑器中,以便更好地查看。 “CurrentLanguage”键是相同的映射,但使用本地化名称。

所以PdhLookupPerfNameByIndex()使用了CurrentLanguage键,使用你在上一步得到的列表可以知道字符串的索引号。如知识库文章底部所述(令人困惑)的另一种方法是首先从“009”注册表项中查找索引号。这使您可以将英文字符串翻译成本地化字符串。请注意,知识库文章记录的注册表项位置错误,不知道为什么。

请记住,它并不完美,正如知识库文章中所指出的,这些映射仅针对“基本”计数器存在,并且“009”键不明确,因为某些索引映射到相同的字符串。在本地化 Windows 版本上进行测试非常重要。

一些同时执行这两种操作的代码:

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Win32;
using System.Diagnostics;
using System.Runtime.InteropServices;

public static class PerfMapper {
    private static Dictionary<string, int> English;
    private static Dictionary<int, string> Localized;

    public static PerformanceCounter FromEnglish(string category, string name, string instance = null) {
        return new PerformanceCounter(Map(category), Map(name), instance);
    }

    public static PerformanceCounter FromIndices(int category, int name, string instance = null) {
        return new PerformanceCounter(PdhMap(category), PdhMap(name), instance);
    }

    public static bool HasName(string name) {
        if (English == null) LoadNames();
        if (!English.ContainsKey(name)) return false;
        var index = English[name];
        return Localized.ContainsKey(index);
    }

    public static string Map(string text) {
        if (HasName(text)) return Localized[English[text]];
        else return text;
    }

    private static string PdhMap(int index) {
        int size = 0;
        uint ret = PdhLookupPerfNameByIndex(null, index, null, ref size);
        if (ret == 0x800007D2) {
            var buffer = new StringBuilder(size);
            ret = PdhLookupPerfNameByIndex(null, index, buffer, ref size);
            if (ret == 0) return buffer.ToString();
        }
        throw new System.ComponentModel.Win32Exception((int)ret, "PDH lookup failed");
    }

    private static void LoadNames() {
        string[] english;
        string[] local;
        // Retrieve English and localized strings
        using (var hklm = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64)) {
            using (var key = hklm.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion\Perflib[=10=]9")) {
                english = (string[])key.GetValue("Counter");
            }
            using (var key = hklm.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion\Perflib\CurrentLanguage")) {
                local = (string[])key.GetValue("Counter");
            }
        }
        // Create English lookup table
        English = new Dictionary<string, int>(english.Length / 2, StringComparer.InvariantCultureIgnoreCase);
        for (int ix = 0; ix < english.Length - 1; ix += 2) {
            int index = int.Parse(english[ix]);
            if (!English.ContainsKey(english[ix + 1])) English.Add(english[ix + 1], index);
        }
        // Create localized lookup table
        Localized = new Dictionary<int, string>(local.Length / 2);
        for (int ix = 0; ix < local.Length - 1; ix += 2) {
            int index = int.Parse(local[ix]);
            Localized.Add(index, local[ix + 1]);
        }
    }

    [DllImport("pdh.dll", CharSet = CharSet.Auto)]
    private static extern uint PdhLookupPerfNameByIndex(string machine, int index, StringBuilder buffer, ref int bufsize);
}

示例用法:

class Program {
    static void Main(string[] args) {
        var ctr1 = PerfMapper.FromEnglish("Processor", "% Processor Time");
        var ctr2 = PerfMapper.FromIndices(238, 6);
    }
}

我只能访问 Windows 的英文版本,因此不能保证本地化版本的准确性。请通过编辑此 post.

来更正您遇到的任何错误