6

我必须以编程方式创建性能计数器。似乎代码正在运行,但我没有在 perfmon 中看到性能计数器类别。当我的意思是代码正在运行时,我是说它不会引发任何异常。我以管理员身份运行它。

我已经看到关于 SO 的答案说计数器出现可能需要一些时间。但是我在回家之前安装了它们,第二天它们仍然不可见。

这是一个小控制台应用程序来测试它。我安装了计数器,然后使用它们,但在性能监视器中看不到它们。

编辑:我在 3 台机器上对其进行了测试,它可以在其中一台机器上按预期工作(性能计数器在 perfmon 中可见)。是否可以在 Windows 的其他地方看到性能计数器(Powershell,一些 sysinternals 工具?)

using System;
using System.Diagnostics;

namespace PerfCounters { class Program { static void Main() { var program = new Program(); program.Run(); }

    private const string CATEGORY_NAME = "AAAMySpecialCategory";
    private const string CATEGORY_HELP = "AAAMySpecialCategory Help";
    private const string OPS_IN_CURRENT_COUNTER_NAME = "# current calls";

    public void Run()
    {
        while (true)
        {
            PrintUsage();
            Console.Write(":> ");
            var keyInfo = Console.ReadLine();
            if (keyInfo == "q")
            {
                break;
            }

            switch (keyInfo)
            {
                case "i":
                    InstallPerfCountersCategory();
                    break;

                case "c":
                    Console.WriteLine(PerformanceCounterCategoryExists()
                            ? "Perf counter category ({0}) does exist"
                            : "Perf counter categry ({0}) does not exist", CATEGORY_NAME);
                    break;

                case "w":
                    IncrementPerfCounter();
                    break;

                case "u":
                    UninstallPerfCountersCategory();
                    break;
            }
        } 
    }

    private static void PrintUsage()
    {
        Console.WriteLine();
        Console.WriteLine("Usage:");
        Console.WriteLine("i - install performance counters category ({0})", CATEGORY_NAME);
        Console.WriteLine("c - check if category exists ({0})", CATEGORY_NAME);
        Console.WriteLine("w - work with perf counter (increment)");
        Console.WriteLine("u - uninstall performance counters category ({0})", CATEGORY_NAME);
        Console.WriteLine("q - quit");
    }
    private void IncrementPerfCounter()
    {
        if (!PerformanceCounterCategoryExists())
        {
            Console.WriteLine("Perf counter category ({0}) does not exist - install first", CATEGORY_NAME);
            return;
        }

        var currentOps = new PerformanceCounter(CATEGORY_NAME, OPS_IN_CURRENT_COUNTER_NAME, false);
        Console.Write("Incrementing perf counter");
        currentOps.Increment();
        Console.WriteLine(" - incremented");
    }
    private void InstallPerfCountersCategory()
    {
        if (PerformanceCounterCategoryExists())
        {
            Console.WriteLine("Uninstall first");
            return;
        }

        var ccdc = new CounterCreationDataCollection
        {
            new CounterCreationData(OPS_IN_CURRENT_COUNTER_NAME, "", PerformanceCounterType.NumberOfItems32),
        };

        PerformanceCounterCategory.Create(CATEGORY_NAME, CATEGORY_HELP, PerformanceCounterCategoryType.SingleInstance, ccdc);
        Console.WriteLine("Installed");
    }
    private void UninstallPerfCountersCategory()
    {
        if (PerformanceCounterCategoryExists())
        {
            Console.WriteLine("Deleting perf counter category ({0})", CATEGORY_NAME);
            PerformanceCounterCategory.Delete(CATEGORY_NAME);
        }
        else
        {
            Console.WriteLine("Perf counter category ({0}) does not exist - install first", CATEGORY_NAME);
        }
    }
    private bool PerformanceCounterCategoryExists()
    {
        return PerformanceCounterCategory.Exists(CATEGORY_NAME);
    }
}

}

4

1 回答 1

3

我遇到了同样的问题 - .NET 代码报告计数器在那里,但在 perfmon 中没有可见的此类计数器类别。

显然 perfmon 有时会通过在注册表中将其标记为禁用来禁用性能计数器

如果您在注册表中检查,HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services应该能够找到您的性能计数器类别(只需将您的类别名称作为“文件夹”之一查找)。在子项(“文件夹”)下Performance找到注册表值Disable Performance Counters并将其设置为零。重新启动 perfmon,您现在应该在 perfmon 中看到您的类别和计数器。

于 2014-10-09T14:25:52.157 回答