Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

}

like image 912
Piotr Perak Avatar asked Oct 17 '13 10:10

Piotr Perak


People also ask

How do I turn on my performance counter?

In the navigation pane, expand Monitoring Tools, and then choose Performance Monitor. In the console pane toolbar, choose the Add button. In the Add Counters window, in the Select counters from computer drop-down list, choose the computer that is running Business Central Server.

How do you save counters in Performance Monitor?

Then go to File → Add/Remove Snap-in… Select Performance Monitor from the Available snap-ins list and click the Add button to add it the right panel. Now, start adding the counters you need to track. When you finished, go to File → Save as to save a copy of MMC file that has all your performance counters included.


1 Answers

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.

like image 107
SharpC Avatar answered Oct 10 '22 09:10

SharpC