Performance counter categories come in one of two modes: single instance and multi-instance. If you want your category to be visible through WMI, it’s important to know the difference.
A single instance category has only one machine wide value for each counter. A good example of a category supplied by Windows is the “System” category. It has counters like “Threads” and “System up time”, and clearly there’s only one value for each of those counters. Writing to a single instance category using System.Diagnostics.PerformanceCounter looks something like this:
PerformanceCounter pc = new PerformanceCounter(myCategory, myCounter, false);
pc.RawValue = 10;
Other categories are multi-instance, meaning they can have unlimited number of different values for each counter. The example here is “Process”, which has one instance (and oneset of values for each counter) for each process running . So currently on my machine, there’s an instance for “Outlook”, another for “Iexplore”, several for “cmd”, and quite a few more. Using PerformanceCounter you can write to these by specifying an instance name in the constructor:
PerformanceCounter pc = new PerformanceCounter(myCategory, myCounter, myInstance, false);
pc.RawValue = 10;
Why is it important to know the difference here? After all, PerformanceCounter automatically does the right thing depending on how you write to your counter. However, with Whidbey, we’ve done a lot of work to allow counters written with PerformanceCounter to be visible through WMI. One of the problems we faced was that WMI didn’t like the flexibility of switching between category types. You could treat your category as single instance one minute and multi-instance the next. That’s an error as far as WMI is concerned. And in fact even if you were careful to always write as a multi-instance category, when your app wasn’t running we’d treat it as a single instance category.
To solve this in Whidbey we’ve added some new apis which let you specify single or multi-instance up front. When you install your category, you need to specify your PerformanceCounterCategoryType, either on the installer or to the call to PerformanceCounterCategory.Create.
PerformanceCounterInstaller pcInstaller = new PerformanceCounterInstaller();
pcInstaller.CategoryType = PerformanceCounterCategoryType.MultiInstance;
PerformanceCounterCategory.Create(myCategoryname, myCategoryHelp, PerformanceCounterCategoryType.MultiInstance, myCounters);
By specifying the PerformanceCounterCategoryType, your counters will be visible through WMI. As an added benefit, we can do some error checking on your behalf, ensuring that your category is only used in the way you specified.