简体   繁体   中英

Created performance counters not visible

I have to create performance counters programmaticaly. Seems that code is working but I don't see performance counters category in perfmon. When I mean that code is working I'm saying that it doesn't throw any exceptions. I am running it as administrator.

I've seen answers on SO saying that it may take some time for counters to appear. But I installed them before going home and next day they are still not visible.

Here is a little console app to test it. I install counters, then work with them but can't see them in performance monitor.

Edit: I tested it on 3 machines, and it works on one of them as expected (perf counters visible in perfmon). Is it possible to see performance counters somewhere else in windows (Powershell, some sysinternals tool?)


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);
    }
}

}

I had the same issue - .NET code reported that the counters were there, but there were no such counter categories visible in perfmon.

Apparently perfmon will sometimes disable performance counters by flagging it as disabled in the registry .

If you check in the registry under HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Services you should be able to find your performance counter category (just look for the your category name as one of the "folders"). Under the subkey ("folder") Performance find the registry value Disable Performance Counters and set it to zero. Restart perfmon and you should now see your categories and counters in perfmon.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM